Skip to content

Commit

Permalink
fix duty calculation for pectra spec alpha.10
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Dec 14, 2024
1 parent 5df6232 commit 41b1b65
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions indexer/beacon/duties/duties.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,29 +160,49 @@ func GetProposerIndex(spec *consensus.ChainSpec, state *BeaconState, slot phase0

seed := Hash(seedData)

maxEffectiveBalance := spec.MaxEffectiveBalance
if spec.ElectraForkEpoch != nil && phase0.Epoch(slot/phase0.Slot(spec.SlotsPerEpoch)) >= phase0.Epoch(*spec.ElectraForkEpoch) {
maxEffectiveBalance = spec.MaxEffectiveBalanceElectra
}

activeIndicesCount := state.GetActiveCount()
if activeIndicesCount == 0 {
return 0, fmt.Errorf("empty active indices list")
}
maxRandomByte := uint64(1<<8 - 1)

for i := uint64(0); ; i++ {
candidateIndex, err := ComputeShuffledIndex(spec, i%activeIndicesCount, activeIndicesCount, seed, true)
if err != nil {
return 0, err
if spec.ElectraForkEpoch != nil && epoch >= phase0.Epoch(*spec.ElectraForkEpoch) {
// Electra fork
maxRandomValue := uint64(1<<16 - 1)

for i := uint64(0); ; i++ {
candidateIndex, err := ComputeShuffledIndex(spec, i%activeIndicesCount, activeIndicesCount, seed, true)
if err != nil {
return 0, err
}
b := append(seed[:], UintToBytes(i/16)...)
offset := (i % 16) * 2
hash := Hash(b)
randomValue := BytesToUint(hash[offset : offset+2])

effectiveBal := uint64(state.GetEffectiveBalance(ActiveIndiceIndex(candidateIndex)))

if effectiveBal*maxRandomValue >= spec.MaxEffectiveBalanceElectra*uint64(randomValue) {
return ActiveIndiceIndex(candidateIndex), nil
}
}
b := append(seed[:], UintToBytes(i/32)...)
randomByte := Hash(b)[i%32]

effectiveBal := uint64(state.GetEffectiveBalance(ActiveIndiceIndex(candidateIndex)))
} else {
// pre-Electra fork
maxRandomByte := uint64(1<<8 - 1)

for i := uint64(0); ; i++ {
candidateIndex, err := ComputeShuffledIndex(spec, i%activeIndicesCount, activeIndicesCount, seed, true)
if err != nil {
return 0, err
}
b := append(seed[:], UintToBytes(i/32)...)
randomByte := Hash(b)[i%32]

effectiveBal := uint64(state.GetEffectiveBalance(ActiveIndiceIndex(candidateIndex)))

if effectiveBal*maxRandomByte >= maxEffectiveBalance*uint64(randomByte) {
return ActiveIndiceIndex(candidateIndex), nil
if effectiveBal*maxRandomByte >= spec.MaxEffectiveBalance*uint64(randomByte) {
return ActiveIndiceIndex(candidateIndex), nil
}
}
}
}
Expand Down

0 comments on commit 41b1b65

Please sign in to comment.