mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
879e310332
* panic in SizeSSZ * moving slowly * adapt old code to new interfaces * return interfaces from factory functions * replace the rest of WrappedSignedBeaconBlock * WrappedBeaconBlock * WrappedBeaconBlockBody * miscellaneous * Test_BeaconBlockIsNil * replace usages of BeaconBlockIsNil * replace usages of mutator * fix all build errors * fix some more issues * mutator changes * relax assertions when initializing * revert changes in object_mapping.go * allow calling Proto on nil * Revert "allow calling Proto on nil" This reverts commit ecc84e455381b03d24aec2fa0fa17bddbec71705. * modify Copy and Proto methods * remove unused var * fix block batch tests * correct BUILD file * Error when initializing nil objects * one more error fix * add missing comma * rename alias to blocktest * add logging * error when SignedBeaconBlock is nil * fix last test * import fix * broken * working * test fixes * reduce complexity of processPendingBlocks * simplified
239 lines
7.7 KiB
Go
239 lines
7.7 KiB
Go
package beacon
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"math"
|
|
"testing"
|
|
"time"
|
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stategen"
|
|
mockstategen "github.com/prysmaticlabs/prysm/beacon-chain/state/stategen/mock"
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
|
"github.com/prysmaticlabs/prysm/consensus-types/blocks"
|
|
blocktest "github.com/prysmaticlabs/prysm/consensus-types/blocks/testing"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
"github.com/prysmaticlabs/prysm/testing/util"
|
|
prysmTime "github.com/prysmaticlabs/prysm/time"
|
|
"github.com/prysmaticlabs/prysm/time/slots"
|
|
"google.golang.org/protobuf/proto"
|
|
"gopkg.in/d4l3k/messagediff.v1"
|
|
)
|
|
|
|
func TestServer_ListBeaconCommittees_CurrentEpoch(t *testing.T) {
|
|
db := dbTest.SetupDB(t)
|
|
helpers.ClearCache()
|
|
|
|
numValidators := 128
|
|
ctx := context.Background()
|
|
headState := setupActiveValidators(t, numValidators)
|
|
|
|
offset := int64(headState.Slot().Mul(params.BeaconConfig().SecondsPerSlot))
|
|
m := &mock.ChainService{
|
|
Genesis: prysmTime.Now().Add(time.Duration(-1*offset) * time.Second),
|
|
}
|
|
bs := &Server{
|
|
HeadFetcher: m,
|
|
GenesisTimeFetcher: m,
|
|
StateGen: stategen.New(db),
|
|
}
|
|
b := util.NewBeaconBlock()
|
|
util.SaveBlock(t, ctx, db, 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))
|
|
|
|
bs.ReplayerBuilder = mockstategen.NewMockReplayerBuilder(mockstategen.WithMockState(headState))
|
|
|
|
activeIndices, err := helpers.ActiveValidatorIndices(ctx, headState, 0)
|
|
require.NoError(t, err)
|
|
attesterSeed, err := helpers.Seed(headState, 0, params.BeaconConfig().DomainBeaconAttester)
|
|
require.NoError(t, err)
|
|
committees, err := computeCommittees(context.Background(), 0, activeIndices, attesterSeed)
|
|
require.NoError(t, err)
|
|
|
|
wanted := ðpb.BeaconCommittees{
|
|
Epoch: 0,
|
|
Committees: committees.SlotToUint64(),
|
|
ActiveValidatorCount: uint64(numValidators),
|
|
}
|
|
res, err := bs.ListBeaconCommittees(context.Background(), ðpb.ListCommitteesRequest{
|
|
QueryFilter: ðpb.ListCommitteesRequest_Genesis{Genesis: true},
|
|
})
|
|
require.NoError(t, err)
|
|
if !proto.Equal(res, wanted) {
|
|
t.Errorf("Expected %v, received %v", wanted, res)
|
|
}
|
|
}
|
|
|
|
func addDefaultReplayerBuilder(s *Server, h stategen.HistoryAccessor) {
|
|
cc := &mockstategen.MockCanonicalChecker{Is: true, Err: nil}
|
|
cs := &mockstategen.MockCurrentSlotter{Slot: math.MaxUint64 - 1}
|
|
s.ReplayerBuilder = stategen.NewCanonicalHistory(h, cc, cs)
|
|
}
|
|
|
|
func TestServer_ListBeaconCommittees_PreviousEpoch(t *testing.T) {
|
|
params.SetupTestConfigCleanup(t)
|
|
params.OverrideBeaconConfig(params.MainnetConfig())
|
|
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, fieldparams.RootLength)
|
|
}
|
|
require.NoError(t, headState.SetRandaoMixes(mixes))
|
|
require.NoError(t, headState.SetSlot(params.BeaconConfig().SlotsPerEpoch))
|
|
|
|
b, err := blocks.NewSignedBeaconBlock(util.NewBeaconBlock())
|
|
require.NoError(t, err)
|
|
b, err = blocktest.SetBlockSlot(b, headState.Slot())
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.SaveBlock(ctx, b))
|
|
gRoot, err := b.Block().HashTreeRoot()
|
|
require.NoError(t, err)
|
|
require.NoError(t, db.SaveState(ctx, headState, gRoot))
|
|
|
|
offset := int64(headState.Slot().Mul(params.BeaconConfig().SecondsPerSlot))
|
|
m := &mock.ChainService{
|
|
State: headState,
|
|
Genesis: prysmTime.Now().Add(time.Duration(-1*offset) * time.Second),
|
|
}
|
|
bs := &Server{
|
|
HeadFetcher: m,
|
|
GenesisTimeFetcher: m,
|
|
StateGen: stategen.New(db),
|
|
}
|
|
addDefaultReplayerBuilder(bs, db)
|
|
|
|
activeIndices, err := helpers.ActiveValidatorIndices(ctx, headState, 1)
|
|
require.NoError(t, err)
|
|
attesterSeed, err := helpers.Seed(headState, 1, params.BeaconConfig().DomainBeaconAttester)
|
|
require.NoError(t, err)
|
|
startSlot, err := slots.EpochStart(1)
|
|
require.NoError(t, err)
|
|
wanted, err := computeCommittees(context.Background(), startSlot, activeIndices, attesterSeed)
|
|
require.NoError(t, err)
|
|
|
|
tests := []struct {
|
|
req *ethpb.ListCommitteesRequest
|
|
res *ethpb.BeaconCommittees
|
|
}{
|
|
{
|
|
req: ðpb.ListCommitteesRequest{
|
|
QueryFilter: ðpb.ListCommitteesRequest_Epoch{Epoch: 1},
|
|
},
|
|
res: ðpb.BeaconCommittees{
|
|
Epoch: 1,
|
|
Committees: wanted.SlotToUint64(),
|
|
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 := dbTest.SetupDB(t)
|
|
helpers.ClearCache()
|
|
ctx := context.Background()
|
|
|
|
numValidators := 128
|
|
headState := setupActiveValidators(t, numValidators)
|
|
|
|
offset := int64(headState.Slot().Mul(params.BeaconConfig().SecondsPerSlot))
|
|
m := &mock.ChainService{
|
|
Genesis: prysmTime.Now().Add(time.Duration(-1*offset) * time.Second),
|
|
}
|
|
bs := &Server{
|
|
HeadFetcher: m,
|
|
GenesisTimeFetcher: m,
|
|
StateGen: stategen.New(db),
|
|
}
|
|
b := util.NewBeaconBlock()
|
|
util.SaveBlock(t, ctx, db, 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 := ðpb.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(ctx, headState, 0)
|
|
require.NoError(t, err)
|
|
|
|
wanted, err := computeCommittees(context.Background(), 0, activeIndices, seed)
|
|
require.NoError(t, err)
|
|
committees, activeIndices, err := bs.retrieveCommitteesForRoot(context.Background(), gRoot[:])
|
|
require.NoError(t, err)
|
|
|
|
wantedRes := ðpb.BeaconCommittees{
|
|
Epoch: 0,
|
|
Committees: wanted.SlotToUint64(),
|
|
ActiveValidatorCount: uint64(numValidators),
|
|
}
|
|
receivedRes := ðpb.BeaconCommittees{
|
|
Epoch: 0,
|
|
Committees: committees.SlotToUint64(),
|
|
ActiveValidatorCount: uint64(len(activeIndices)),
|
|
}
|
|
assert.DeepEqual(t, wantedRes, receivedRes)
|
|
}
|
|
|
|
func setupActiveValidators(t *testing.T, count int) state.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, err := util.NewBeaconState()
|
|
require.NoError(t, err)
|
|
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
|
|
}
|