Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electra: Properly Calculate Proposer Probabilities for MaxEB #14010

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions beacon-chain/core/helpers/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/prysmaticlabs/prysm/v5/crypto/hash"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
"github.com/prysmaticlabs/prysm/v5/time/slots"
log "github.com/sirupsen/logrus"
"go.opencensus.io/trace"
Expand Down Expand Up @@ -357,10 +358,10 @@ func BeaconProposerIndexAtSlot(ctx context.Context, state state.ReadOnlyBeaconSt
// candidate_index = indices[compute_shuffled_index(i % total, total, seed)]
// random_byte = hash(seed + uint_to_bytes(uint64(i // 32)))[i % 32]
// effective_balance = state.validators[candidate_index].effective_balance
// if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE * random_byte:
// if effective_balance * MAX_RANDOM_BYTE >= MAX_EFFECTIVE_BALANCE_ELECTRA * random_byte: #[Modified in Electra:EIP7251]
// return candidate_index
// i += 1
func ComputeProposerIndex(bState state.ReadOnlyValidators, activeIndices []primitives.ValidatorIndex, seed [32]byte) (primitives.ValidatorIndex, error) {
func ComputeProposerIndex(bState state.ReadOnlyBeaconState, activeIndices []primitives.ValidatorIndex, seed [32]byte) (primitives.ValidatorIndex, error) {
length := uint64(len(activeIndices))
if length == 0 {
return 0, errors.New("empty active indices list")
Expand All @@ -385,7 +386,12 @@ func ComputeProposerIndex(bState state.ReadOnlyValidators, activeIndices []primi
}
effectiveBal := v.EffectiveBalance()

if effectiveBal*maxRandomByte >= params.BeaconConfig().MaxEffectiveBalance*uint64(randomByte) {
maxEB := params.BeaconConfig().MaxEffectiveBalance
if bState.Version() >= version.Electra {
maxEB = params.BeaconConfig().MaxEffectiveBalanceElectra
}

if effectiveBal*maxRandomByte >= maxEB*uint64(randomByte) {
return candidateIndex, nil
}
}
Expand Down
62 changes: 58 additions & 4 deletions beacon-chain/core/helpers/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,11 @@ func TestComputeProposerIndex(t *testing.T) {
seed [32]byte
}
tests := []struct {
name string
args args
want primitives.ValidatorIndex
wantedErr string
name string
isElectraOrAbove bool
args args
want primitives.ValidatorIndex
wantedErr string
}{
{
name: "all_active_indices",
Expand Down Expand Up @@ -682,6 +683,54 @@ func TestComputeProposerIndex(t *testing.T) {
},
want: 7,
},
{
name: "electra_probability_changes",
isElectraOrAbove: true,
args: args{
validators: []*ethpb.Validator{
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
},
indices: []primitives.ValidatorIndex{3},
seed: seed,
},
want: 3,
},
{
name: "electra_probability_changes_all_active",
isElectraOrAbove: true,
args: args{
validators: []*ethpb.Validator{
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance}, // skip this one
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra},
},
indices: []primitives.ValidatorIndex{0, 1, 2, 3, 4},
seed: seed,
},
want: 4,
},
{
name: "electra_probability_returns_first_validator_with_criteria",
isElectraOrAbove: true,
args: args{
validators: []*ethpb.Validator{
{EffectiveBalance: 1},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra},
{EffectiveBalance: 1},
{EffectiveBalance: 1},
{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalanceElectra},
},
indices: []primitives.ValidatorIndex{1},
seed: seed,
},
want: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -690,6 +739,11 @@ func TestComputeProposerIndex(t *testing.T) {
bState := &ethpb.BeaconState{Validators: tt.args.validators}
stTrie, err := state_native.InitializeFromProtoUnsafePhase0(bState)
require.NoError(t, err)
if tt.isElectraOrAbove {
stTrie, err = state_native.InitializeFromProtoUnsafeElectra(&ethpb.BeaconStateElectra{Validators: tt.args.validators})
require.NoError(t, err)
}

got, err := helpers.ComputeProposerIndex(stTrie, tt.args.indices, tt.args.seed)
if tt.wantedErr != "" {
assert.ErrorContains(t, tt.wantedErr, err)
Expand Down
Loading