prysm-pulse/beacon-chain/rpc/beacon/committees_test.go
Preston Van Loon 7cc32c4dda
Various code inspection resolutions (#7438)
* remove unused code

* remove defer use in loop

* Remove unused methods and constants

* gofmt and gaz

* nilness check

* remove unused args

* Add TODO for refactoring subscribeWithBase to remove unused arg. It seems too involved to include in this sweeping PR. https://github.com/prysmaticlabs/prysm/issues/7437

* replace empty slice declaration

* Remove unnecessary type conversions

* remove redundant type declaration

* rename receivers to be consistent

* Remove bootnode query tool. It is now obsolete by discv5

* Remove relay node. It is no longer used or supported

* Revert "Remove relay node. It is no longer used or supported"

This reverts commit 4bd7717334dad85ef4766ed9bc4da711fb5fa810.

* Delete unused test directory

* Delete unsupported gcp startup script

* Delete old k8s script

* build fixes

* fix build

* go mod tidy

* revert slasher/db/kv/block_header.go

* fix build

* remove redundant nil check

* combine func args

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
2020-10-12 08:11:05 +00:00

219 lines
6.8 KiB
Go

package beacon
import (
"context"
"encoding/binary"
"testing"
"time"
"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
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"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/shared/timeutils"
"gopkg.in/d4l3k/messagediff.v1"
)
func TestServer_ListBeaconCommittees_CurrentEpoch(t *testing.T) {
db, sc := dbTest.SetupDB(t)
helpers.ClearCache()
numValidators := 128
ctx := context.Background()
headState := setupActiveValidators(t, numValidators)
m := &mock.ChainService{
Genesis: timeutils.Now().Add(time.Duration(-1*int64(headState.Slot()*params.BeaconConfig().SecondsPerSlot)) * time.Second),
}
bs := &Server{
HeadFetcher: m,
GenesisTimeFetcher: m,
StateGen: stategen.New(db, sc),
}
b := testutil.NewBeaconBlock()
require.NoError(t, db.SaveBlock(ctx, b))
gRoot, err := b.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot))
require.NoError(t, db.SaveState(ctx, headState, gRoot))
activeIndices, err := helpers.ActiveValidatorIndices(headState, 0)
require.NoError(t, err)
attesterSeed, err := helpers.Seed(headState, 0, params.BeaconConfig().DomainBeaconAttester)
require.NoError(t, err)
committees, err := computeCommittees(0, activeIndices, attesterSeed)
require.NoError(t, err)
wanted := &ethpb.BeaconCommittees{
Epoch: 0,
Committees: committees,
ActiveValidatorCount: uint64(numValidators),
}
res, err := bs.ListBeaconCommittees(context.Background(), &ethpb.ListCommitteesRequest{
QueryFilter: &ethpb.ListCommitteesRequest_Genesis{Genesis: true},
})
require.NoError(t, err)
if !proto.Equal(res, wanted) {
t.Errorf("Expected %v, received %v", wanted, res)
}
}
func TestServer_ListBeaconCommittees_PreviousEpoch(t *testing.T) {
params.UseMainnetConfig()
ctx := context.Background()
db, _ := dbTest.SetupDB(t)
helpers.ClearCache()
numValidators := 128
headState := setupActiveValidators(t, numValidators)
mixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
for i := 0; i < len(mixes); i++ {
mixes[i] = make([]byte, 32)
}
require.NoError(t, headState.SetRandaoMixes(mixes))
require.NoError(t, headState.SetSlot(params.BeaconConfig().SlotsPerEpoch*2))
b := testutil.NewBeaconBlock()
require.NoError(t, db.SaveBlock(ctx, b))
gRoot, err := b.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, db.SaveState(ctx, headState, gRoot))
require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot))
m := &mock.ChainService{
State: headState,
Genesis: timeutils.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()),
}
activeIndices, err := helpers.ActiveValidatorIndices(headState, 1)
require.NoError(t, err)
attesterSeed, err := helpers.Seed(headState, 1, params.BeaconConfig().DomainBeaconAttester)
require.NoError(t, err)
startSlot, err := helpers.StartSlot(1)
require.NoError(t, err)
wanted, err := computeCommittees(startSlot, activeIndices, attesterSeed)
require.NoError(t, err)
tests := []struct {
req *ethpb.ListCommitteesRequest
res *ethpb.BeaconCommittees
}{
{
req: &ethpb.ListCommitteesRequest{
QueryFilter: &ethpb.ListCommitteesRequest_Epoch{Epoch: 1},
},
res: &ethpb.BeaconCommittees{
Epoch: 1,
Committees: wanted,
ActiveValidatorCount: uint64(numValidators),
},
},
}
helpers.ClearCache()
for i, test := range tests {
res, err := bs.ListBeaconCommittees(context.Background(), test.req)
require.NoError(t, err)
if !proto.Equal(res, test.res) {
diff, _ := messagediff.PrettyDiff(res, test.res)
t.Errorf("%d/ Diff between responses %s", i, diff)
}
}
}
func TestRetrieveCommitteesForRoot(t *testing.T) {
db, sc := dbTest.SetupDB(t)
helpers.ClearCache()
ctx := context.Background()
numValidators := 128
headState := setupActiveValidators(t, numValidators)
m := &mock.ChainService{
Genesis: timeutils.Now().Add(time.Duration(-1*int64(headState.Slot()*params.BeaconConfig().SecondsPerSlot)) * time.Second),
}
bs := &Server{
HeadFetcher: m,
GenesisTimeFetcher: m,
StateGen: stategen.New(db, sc),
}
b := testutil.NewBeaconBlock()
require.NoError(t, db.SaveBlock(ctx, b))
gRoot, err := b.Block.HashTreeRoot()
require.NoError(t, err)
require.NoError(t, db.SaveGenesisBlockRoot(ctx, gRoot))
require.NoError(t, db.SaveState(ctx, headState, gRoot))
stateSummary := &pbp2p.StateSummary{
Slot: 0,
Root: gRoot[:],
}
require.NoError(t, db.SaveStateSummary(ctx, stateSummary))
// Store the genesis seed.
seed, err := helpers.Seed(headState, 0, params.BeaconConfig().DomainBeaconAttester)
require.NoError(t, err)
require.NoError(t, headState.SetSlot(params.BeaconConfig().SlotsPerEpoch*10))
activeIndices, err := helpers.ActiveValidatorIndices(headState, 0)
require.NoError(t, err)
wanted, err := computeCommittees(0, activeIndices, seed)
require.NoError(t, err)
committees, activeIndices, err := bs.retrieveCommitteesForRoot(context.Background(), gRoot[:])
require.NoError(t, err)
wantedRes := &ethpb.BeaconCommittees{
Epoch: 0,
Committees: wanted,
ActiveValidatorCount: uint64(numValidators),
}
receivedRes := &ethpb.BeaconCommittees{
Epoch: 0,
Committees: committees,
ActiveValidatorCount: uint64(len(activeIndices)),
}
assert.DeepEqual(t, wantedRes, receivedRes)
}
func setupActiveValidators(t *testing.T, 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, &ethpb.Validator{
PublicKey: pubKey,
ActivationEpoch: 0,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawalCredentials: make([]byte, 32),
})
}
s := testutil.NewBeaconState()
if err := s.SetValidators(validators); err != nil {
t.Error(err)
return nil
}
if err := s.SetBalances(balances); err != nil {
t.Error(err)
return nil
}
return s
}