Skip to content

Commit

Permalink
fix critical proposer selection bug #4259
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon committed Dec 11, 2019
1 parent e72ff1b commit cc822b5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions beacon-chain/core/helpers/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func ComputeProposerIndex(state *pb.BeaconState, indices []uint64, seed [32]byte
if err != nil {
return 0, err
}
candidateIndex = indices[candidateIndex]
b := append(seed[:], bytesutil.Bytes8(i/32)...)
randomByte := hashutil.Hash(b)[i%32]
effectiveBal := state.Validators[candidateIndex].EffectiveBalance
Expand Down
60 changes: 60 additions & 0 deletions beacon-chain/core/helpers/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,63 @@ func TestActiveValidatorIndices(t *testing.T) {
})
}
}

func TestComputeProposerIndex(t *testing.T) {
seed := bytesutil.ToBytes32([]byte("seed"))
type args struct {
state *pb.BeaconState
indices []uint64
seed [32]byte
}
tests := []struct {
name string
args args
want uint64
wantErr bool
}{
{
name: "all_active_indices",
args: args{
state: &pb.BeaconState{
Validators: []*ethpb.Validator{
&ethpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
&ethpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
&ethpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
&ethpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
},
},
indices: []uint64{0,1,2,3,4},
seed: seed,
},
want: 2,
},
{ // Regression test for https://github.com/prysmaticlabs/prysm/issues/4259.
name: "1_active_index",
args: args{
state: &pb.BeaconState{
Validators: []*ethpb.Validator{
&ethpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
&ethpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
&ethpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
&ethpb.Validator{EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance},
},
},
indices: []uint64{3},
seed: seed,
},
want: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ComputeProposerIndex(tt.args.state, tt.args.indices, tt.args.seed)
if (err != nil) != tt.wantErr {
t.Errorf("ComputeProposerIndex() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ComputeProposerIndex() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit cc822b5

Please sign in to comment.