package beacon import ( "context" "encoding/binary" "testing" "time" "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/db" dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/roughtime" "github.com/prysmaticlabs/prysm/shared/testutil" ) func TestServer_ListBeaconCommittees_CurrentEpoch(t *testing.T) { db := dbTest.SetupDB(t) defer dbTest.TeardownDB(t, db) helpers.ClearCache() numValidators := 128 ctx := context.Background() headState := setupActiveValidators(t, db, numValidators) m := &mock.ChainService{ Genesis: roughtime.Now().Add(time.Duration(-1*int64((headState.Slot()*params.BeaconConfig().SecondsPerSlot))) * time.Second), } bs := &Server{ HeadFetcher: m, GenesisTimeFetcher: m, StateGen: stategen.New(db, cache.NewStateSummaryCache()), } b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} if err := db.SaveBlock(ctx, b); err != nil { t.Fatal(err) } gRoot, err := ssz.HashTreeRoot(b.Block) if err != nil { t.Fatal(err) } if err := db.SaveGenesisBlockRoot(ctx, gRoot); err != nil { t.Fatal(err) } if err := db.SaveState(ctx, headState, gRoot); err != nil { t.Fatal(err) } activeIndices, err := helpers.ActiveValidatorIndices(headState, 0) if err != nil { t.Fatal(err) } attesterSeed, err := helpers.Seed(headState, 0, params.BeaconConfig().DomainBeaconAttester) if err != nil { t.Fatal(err) } committees, err := computeCommittees(0, activeIndices, attesterSeed) if err != nil { t.Fatal(err) } wanted := ðpb.BeaconCommittees{ Epoch: 0, Committees: committees, ActiveValidatorCount: uint64(numValidators), } res, err := bs.ListBeaconCommittees(context.Background(), ðpb.ListCommitteesRequest{ QueryFilter: ðpb.ListCommitteesRequest_Genesis{Genesis: true}, }) if err != nil { t.Fatal(err) } if !proto.Equal(res, wanted) { t.Errorf("Expected %v, received %v", wanted, res) } } func setupActiveValidators(t *testing.T, db db.Database, count int) *stateTrie.BeaconState { balances := make([]uint64, count) validators := make([]*ethpb.Validator, 0, count) for i := 0; i < count; i++ { pubKey := make([]byte, params.BeaconConfig().BLSPubkeyLength) binary.LittleEndian.PutUint64(pubKey, uint64(i)) balances[i] = uint64(i) validators = append(validators, ðpb.Validator{ PublicKey: pubKey, ActivationEpoch: 0, ExitEpoch: params.BeaconConfig().FarFutureEpoch, WithdrawalCredentials: make([]byte, 32), }) } s := testutil.NewBeaconState() if err := s.SetValidators(validators); err != nil { return nil } if err := s.SetBalances(balances); err != nil { return nil } return s }