Avoid active validator count cache for genesis (#6292)

* Add helper to prevent zero hashes

* Test

* Don't use cache if it's genesis

* Regression test

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
terence tsao 2020-06-17 17:48:10 -07:00 committed by GitHub
parent 6781bd643b
commit 640bba8a6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 1 deletions

View File

@ -63,6 +63,7 @@ go_test(
embed = [":go_default_library"],
shard_count = 2,
deps = [
"//beacon-chain/cache:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/stateutil:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",

View File

@ -110,7 +110,7 @@ func ActiveValidatorCount(state *stateTrie.BeaconState, epoch uint64) (uint64, e
if err != nil {
return 0, errors.Wrap(err, "could not interface with committee cache")
}
if activeCount != 0 {
if activeCount != 0 && state.Slot() != 0 {
return uint64(activeCount), nil
}

View File

@ -5,6 +5,7 @@ import (
"reflect"
"testing"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/shared/hashutil"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
@ -369,6 +370,40 @@ func TestDelayedActivationExitEpoch_OK(t *testing.T) {
}
}
func TestActiveValidatorCount_Genesis(t *testing.T) {
c := 1000
validators := make([]*ethpb.Validator, c)
for i := 0; i < len(validators); i++ {
validators[i] = &ethpb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
}
beaconState, err := beaconstate.InitializeFromProto(&pb.BeaconState{
Slot: 0,
Validators: validators,
RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector),
})
if err != nil {
t.Fatal(err)
}
// Preset cache to a bad count.
seed, err := Seed(beaconState, 0, params.BeaconConfig().DomainBeaconAttester)
if err != nil {
t.Fatal(err)
}
if err := committeeCache.AddCommitteeShuffledList(&cache.Committees{Seed: seed, ShuffledIndices: []uint64{1, 2, 3}}); err != nil {
t.Fatal(err)
}
validatorCount, err := ActiveValidatorCount(beaconState, CurrentEpoch(beaconState))
if err != nil {
t.Fatal(err)
}
if validatorCount != uint64(c) {
t.Error("Did not get the correct validator count")
}
}
func TestChurnLimit_OK(t *testing.T) {
tests := []struct {
validatorCount int