2019-11-13 05:32:42 +00:00
|
|
|
package beacon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-09 22:45:05 +00:00
|
|
|
"encoding/binary"
|
2019-11-13 05:32:42 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-11-13 05:32:42 +00:00
|
|
|
mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
|
|
|
dbTest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
2020-01-31 20:57:01 +00:00
|
|
|
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
|
2019-11-13 05:32:42 +00:00
|
|
|
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-01-31 20:57:01 +00:00
|
|
|
"gopkg.in/d4l3k/messagediff.v1"
|
2019-11-13 05:32:42 +00:00
|
|
|
)
|
|
|
|
|
2020-01-25 01:54:18 +00:00
|
|
|
func TestServer_ListBeaconCommittees_CurrentEpoch(t *testing.T) {
|
2019-11-13 05:32:42 +00:00
|
|
|
db := dbTest.SetupDB(t)
|
|
|
|
defer dbTest.TeardownDB(t, db)
|
2020-01-31 20:57:01 +00:00
|
|
|
helpers.ClearCache()
|
2019-11-13 05:32:42 +00:00
|
|
|
|
|
|
|
numValidators := 128
|
|
|
|
headState := setupActiveValidators(t, db, numValidators)
|
|
|
|
|
2020-01-31 20:57:01 +00:00
|
|
|
randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
|
|
|
|
for i := 0; i < len(randaoMixes); i++ {
|
|
|
|
randaoMixes[i] = make([]byte, 32)
|
|
|
|
}
|
|
|
|
if err := headState.SetRandaoMixes(randaoMixes); err != nil {
|
|
|
|
t.Fatal(err)
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bs := &Server{
|
|
|
|
HeadFetcher: &mock.ChainService{
|
|
|
|
State: headState,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-02-17 21:57:13 +00:00
|
|
|
committees, err := computeCommittees(0, activeIndices, attesterSeed)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 21:57:13 +00:00
|
|
|
wanted := ðpb.BeaconCommittees{
|
|
|
|
Epoch: 0,
|
|
|
|
Committees: committees,
|
|
|
|
ActiveValidatorCount: uint64(numValidators),
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|
2020-02-17 21:57:13 +00:00
|
|
|
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)
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-25 01:54:18 +00:00
|
|
|
func TestServer_ListBeaconCommittees_PreviousEpoch(t *testing.T) {
|
|
|
|
db := dbTest.SetupDB(t)
|
|
|
|
defer dbTest.TeardownDB(t, db)
|
2020-01-31 20:57:01 +00:00
|
|
|
helpers.ClearCache()
|
2020-01-25 01:54:18 +00:00
|
|
|
|
|
|
|
numValidators := 128
|
|
|
|
headState := setupActiveValidators(t, db, numValidators)
|
|
|
|
|
2020-01-31 20:57:01 +00:00
|
|
|
mixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
|
|
|
|
for i := 0; i < len(mixes); i++ {
|
|
|
|
mixes[i] = make([]byte, 32)
|
2020-01-25 01:54:18 +00:00
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
headState.SetRandaoMixes(mixes)
|
|
|
|
headState.SetSlot(params.BeaconConfig().SlotsPerEpoch * 2)
|
2020-01-25 01:54:18 +00:00
|
|
|
|
|
|
|
bs := &Server{
|
|
|
|
HeadFetcher: &mock.ChainService{
|
|
|
|
State: headState,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
activeIndices, err := helpers.ActiveValidatorIndices(headState, 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
attesterSeed, err := helpers.Seed(headState, 1, params.BeaconConfig().DomainBeaconAttester)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
startSlot := helpers.StartSlot(1)
|
2020-02-17 21:57:13 +00:00
|
|
|
wanted, err := computeCommittees(startSlot, activeIndices, attesterSeed)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2020-01-25 01:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
req *ethpb.ListCommitteesRequest
|
|
|
|
res *ethpb.BeaconCommittees
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
req: ðpb.ListCommitteesRequest{
|
|
|
|
QueryFilter: ðpb.ListCommitteesRequest_Epoch{Epoch: 1},
|
|
|
|
},
|
|
|
|
res: ðpb.BeaconCommittees{
|
|
|
|
Epoch: 1,
|
|
|
|
Committees: wanted,
|
|
|
|
ActiveValidatorCount: uint64(numValidators),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
helpers.ClearCache()
|
|
|
|
for i, test := range tests {
|
2020-01-25 01:54:18 +00:00
|
|
|
res, err := bs.ListBeaconCommittees(context.Background(), test.req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !proto.Equal(res, test.res) {
|
2020-01-31 20:57:01 +00:00
|
|
|
diff, _ := messagediff.PrettyDiff(res, test.res)
|
|
|
|
t.Errorf("%d/ Diff between responses %s", i, diff)
|
2020-01-25 01:54:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 05:32:42 +00:00
|
|
|
func TestServer_ListBeaconCommittees_FromArchive(t *testing.T) {
|
|
|
|
db := dbTest.SetupDB(t)
|
|
|
|
defer dbTest.TeardownDB(t, db)
|
2020-01-31 20:57:01 +00:00
|
|
|
helpers.ClearCache()
|
2019-11-13 05:32:42 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
numValidators := 128
|
2020-01-31 20:57:01 +00:00
|
|
|
balances := make([]uint64, numValidators)
|
|
|
|
validators := make([]*ethpb.Validator, 0, numValidators)
|
|
|
|
for i := 0; i < numValidators; i++ {
|
|
|
|
pubKey := make([]byte, params.BeaconConfig().BLSPubkeyLength)
|
|
|
|
binary.LittleEndian.PutUint64(pubKey, uint64(i))
|
|
|
|
if err := db.SaveValidatorIndex(ctx, pubKey, uint64(i)); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
balances[i] = uint64(i)
|
|
|
|
validators = append(validators, ðpb.Validator{
|
|
|
|
PublicKey: pubKey,
|
|
|
|
ActivationEpoch: 0,
|
|
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
WithdrawalCredentials: make([]byte, 32),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
headState, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{Validators: validators, Balances: balances})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-13 05:32:42 +00:00
|
|
|
|
2020-01-31 20:57:01 +00:00
|
|
|
randaoMixes := make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector)
|
|
|
|
for i := 0; i < len(randaoMixes); i++ {
|
|
|
|
randaoMixes[i] = make([]byte, 32)
|
|
|
|
}
|
|
|
|
if err := headState.SetRandaoMixes(randaoMixes); err != nil {
|
|
|
|
t.Fatal(err)
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|
|
|
|
|
2020-01-31 20:57:01 +00:00
|
|
|
if err := headState.SetSlot(params.BeaconConfig().SlotsPerEpoch * 10); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-13 05:32:42 +00:00
|
|
|
|
|
|
|
// Store the genesis seed.
|
|
|
|
seed, err := helpers.Seed(headState, 0, params.BeaconConfig().DomainBeaconAttester)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-27 05:08:18 +00:00
|
|
|
if err := db.SaveArchivedCommitteeInfo(ctx, 0, &pbp2p.ArchivedCommitteeInfo{
|
2019-11-13 05:32:42 +00:00
|
|
|
AttesterSeed: seed[:],
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bs := &Server{
|
|
|
|
BeaconDB: db,
|
|
|
|
HeadFetcher: &mock.ChainService{
|
|
|
|
State: headState,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
activeIndices, err := helpers.ActiveValidatorIndices(headState, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
|
2020-02-17 21:57:13 +00:00
|
|
|
wanted, err := computeCommittees(0, activeIndices, seed)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|
|
|
|
res1, err := bs.ListBeaconCommittees(context.Background(), ðpb.ListCommitteesRequest{
|
|
|
|
QueryFilter: ðpb.ListCommitteesRequest_Genesis{
|
|
|
|
Genesis: true,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
res2, err := bs.ListBeaconCommittees(context.Background(), ðpb.ListCommitteesRequest{
|
|
|
|
QueryFilter: ðpb.ListCommitteesRequest_Epoch{
|
|
|
|
Epoch: 0,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(res1, res2) {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
wantedRes := ðpb.BeaconCommittees{
|
|
|
|
Epoch: 0,
|
|
|
|
Committees: wanted,
|
|
|
|
ActiveValidatorCount: uint64(numValidators),
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(wantedRes, res1) {
|
2020-01-31 20:57:01 +00:00
|
|
|
t.Errorf("Wanted %v", wantedRes)
|
|
|
|
t.Errorf("Received %v", res1)
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 20:57:01 +00:00
|
|
|
func setupActiveValidators(t *testing.T, db db.Database, count int) *stateTrie.BeaconState {
|
2019-11-13 05:32:42 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
balances := make([]uint64, count)
|
|
|
|
validators := make([]*ethpb.Validator, 0, count)
|
|
|
|
for i := 0; i < count; i++ {
|
2020-01-09 22:45:05 +00:00
|
|
|
pubKey := make([]byte, params.BeaconConfig().BLSPubkeyLength)
|
|
|
|
binary.LittleEndian.PutUint64(pubKey, uint64(i))
|
|
|
|
if err := db.SaveValidatorIndex(ctx, pubKey, uint64(i)); err != nil {
|
2019-11-13 05:32:42 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
balances[i] = uint64(i)
|
|
|
|
validators = append(validators, ðpb.Validator{
|
2020-01-31 20:57:01 +00:00
|
|
|
PublicKey: pubKey,
|
|
|
|
ActivationEpoch: 0,
|
|
|
|
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
|
|
|
|
WithdrawalCredentials: make([]byte, 32),
|
2019-11-13 05:32:42 +00:00
|
|
|
})
|
|
|
|
}
|
2020-01-31 20:57:01 +00:00
|
|
|
st, err := stateTrie.InitializeFromProto(&pbp2p.BeaconState{Validators: validators, Balances: balances})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return st
|
2019-11-13 05:32:42 +00:00
|
|
|
}
|