mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 07:28:06 -05:00
* Remove gogoproto compiler * Remove more gogoproto * Improvements * Fix gengo * More scripts * Gazelle, fix deps * Fix version and errors * Fix gocast for arrays * Fix ethapis * Fixes * Fix compile errors * fix go.mod * //proto/... builds * Update for protov2 * temp fix compilation to move on * Change everything to emptypb.empty * Add grpc to proto/slashings * Fix almost all build failures * Oher build problems * FIX THIS FUCKING THING * gaz literally every .bazel * Final touches * Final final touches * Fix proto * Begin moving proto.Marshal to native * Fix site_data * Fixes * Fix duplicate gateway * Fix gateway target * Fix ethapis * Fixes from review * Update * Fix * Fix status test * Fix fuzz * Add isprotoslice to fun * Change DeepEqual to DeepSSZEqual for proto arrays * Fix build * Fix gaz * Update go * Fixes * Fixes * Add case for nil validators after copy * Fix cast * Fix test * Fix imports * Go mod * Only use extension where needed * Fixes * Split gateway from gengo * gaz * go mod * Add back hydrated state * fix hydrate * Fix proto.clone * Fies * Revert "Split gateway from gengo" This reverts commit7298bb2054. * Revert "gaz" This reverts commitca95256570. * Merge all gateway into one target * go mod * Gaz * Add generate v1_gateway files * run pb again * goimports * gaz * Fix comments * Fix protos * Fix PR * Fix protos * Update grpc-gateway and ethapis * Update ethapis and gen-go-cast * Go tidy * Reorder * Fix ethapis * fix spec tests * Fix script * Remove unused import * Fix fuzz * Fix gomod * Update version * Error if the cloned result is nil * Handle optional slots * ADd more empty checks to clone * Undo fuzz changes * Fix build.bazel * Gaz * Redo fuzz changes * Undo some eth1data changes * Update go.mod Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> * Undo clone beacon state * Remove gogo proto more and unused v1_gateway * Add manual fix for nil vals * Fix gaz * tidy * Tidy again * Add detailed error * Revert "Add detailed error" This reverts commit59bc053dcd. * Undo varint changes * Fix nil validators in deposit test * Commit * Undo Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com> Co-authored-by: Radosław Kapka <rkapka@wp.pl> Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
222 lines
6.9 KiB
Go
222 lines
6.9 KiB
Go
package beacon
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"testing"
|
|
"time"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
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"
|
|
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
|
|
"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"
|
|
"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: timeutils.Now().Add(time.Duration(-1*offset) * time.Second),
|
|
}
|
|
bs := &Server{
|
|
HeadFetcher: m,
|
|
GenesisTimeFetcher: m,
|
|
StateGen: stategen.New(db),
|
|
}
|
|
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 := ð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 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))
|
|
|
|
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))
|
|
|
|
offset := int64(headState.Slot().Mul(params.BeaconConfig().SecondsPerSlot))
|
|
m := &mock.ChainService{
|
|
State: headState,
|
|
Genesis: timeutils.Now().Add(time.Duration(-1*offset) * time.Second),
|
|
}
|
|
bs := &Server{
|
|
HeadFetcher: m,
|
|
GenesisTimeFetcher: m,
|
|
StateGen: stategen.New(db),
|
|
}
|
|
|
|
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: ð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: timeutils.Now().Add(time.Duration(-1*offset) * time.Second),
|
|
}
|
|
bs := &Server{
|
|
HeadFetcher: m,
|
|
GenesisTimeFetcher: m,
|
|
StateGen: stategen.New(db),
|
|
}
|
|
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 := ð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) iface.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 := testutil.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
|
|
}
|