prysm-pulse/beacon-chain/rpc/validator_server_test.go

183 lines
5.5 KiB
Go
Raw Normal View History

package rpc
import (
"context"
"encoding/binary"
"fmt"
"strconv"
"strings"
"sync"
"testing"
"time"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/rpc/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)
func TestValidatorIndex_OK(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
pubKey := []byte{'A'}
if err := db.SaveValidatorIndex(pubKey, 0); err != nil {
t.Fatalf("Could not save validator index: %v", err)
}
validatorServer := &ValidatorServer{
beaconDB: db,
}
req := &pb.ValidatorIndexRequest{
PublicKey: pubKey,
}
if _, err := validatorServer.ValidatorIndex(context.Background(), req); err != nil {
t.Errorf("Could not get validator index: %v", err)
}
}
2019-03-03 22:55:12 +00:00
func TestNextEpochCommitteeAssignment_WrongPubkeyLength(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
validatorServer := &ValidatorServer{
beaconDB: db,
}
req := &pb.ValidatorEpochAssignmentsRequest{
PublicKey: []byte{},
2019-03-03 22:55:12 +00:00
EpochStart: params.BeaconConfig().GenesisEpoch,
}
want := fmt.Sprintf("expected public key to have length %d", params.BeaconConfig().BLSPubkeyLength)
2019-03-03 22:55:12 +00:00
if _, err := validatorServer.CommitteeAssignment(context.Background(), req); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %v, received %v", want, err)
}
}
func TestNextEpochCommitteeAssignment_CantFindValidatorIdx(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
if err := db.SaveState(&pbp2p.BeaconState{ValidatorRegistry: []*pbp2p.Validator{}}); err != nil {
t.Fatalf("could not save state: %v", err)
}
vs := &ValidatorServer{
beaconDB: db,
}
2019-03-03 22:55:12 +00:00
pubKey := make([]byte, 96)
req := &pb.ValidatorEpochAssignmentsRequest{
2019-03-03 22:55:12 +00:00
PublicKey: pubKey,
EpochStart: params.BeaconConfig().GenesisEpoch,
}
want := fmt.Sprintf("validator %#x does not exist", req.PublicKey)
if _, err := vs.CommitteeAssignment(context.Background(), req); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %v, received %v", want, err)
}
}
func TestCommitteeAssignment_OK(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
genesis := b.NewGenesisBlock([]byte{})
if err := db.SaveBlock(genesis); err != nil {
t.Fatalf("Could not save genesis block: %v", err)
}
state, err := genesisState(params.BeaconConfig().DepositsForChainStart)
if err != nil {
t.Fatalf("Could not setup genesis state: %v", err)
}
if err := db.UpdateChainHead(genesis, state); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}
var wg sync.WaitGroup
numOfValidators := int(params.BeaconConfig().DepositsForChainStart)
errs := make(chan error, numOfValidators)
for i := 0; i < numOfValidators; i++ {
2019-03-03 22:55:12 +00:00
pubKeyBuf := make([]byte, params.BeaconConfig().BLSPubkeyLength)
binary.PutUvarint(pubKeyBuf, uint64(i))
wg.Add(1)
go func(index int) {
2019-03-03 22:55:12 +00:00
errs <- db.SaveValidatorIndexBatch(pubKeyBuf, index)
wg.Done()
}(i)
}
wg.Wait()
close(errs)
for err := range errs {
if err != nil {
t.Fatalf("Could not save validator index: %v", err)
}
}
vs := &ValidatorServer{
beaconDB: db,
}
2019-03-03 22:55:12 +00:00
pubKeyBuf := make([]byte, params.BeaconConfig().BLSPubkeyLength)
binary.PutUvarint(pubKeyBuf, 0)
// Test the first validator in registry.
req := &pb.ValidatorEpochAssignmentsRequest{
2019-03-03 22:55:12 +00:00
PublicKey: pubKeyBuf,
EpochStart: params.BeaconConfig().GenesisSlot,
}
res, err := vs.CommitteeAssignment(context.Background(), req)
if err != nil {
2019-03-03 22:55:12 +00:00
t.Fatalf("Could not call epoch committee assignment %v", err)
}
if res.Shard >= params.BeaconConfig().ShardCount {
t.Errorf("Assigned shard %d can't be higher than %d",
res.Shard, params.BeaconConfig().ShardCount)
}
if res.Slot > state.Slot+params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Assigned slot %d can't be higher than %d",
res.Slot, state.Slot+params.BeaconConfig().SlotsPerEpoch)
}
// Test the last validator in registry.
lastValidatorIndex := params.BeaconConfig().DepositsForChainStart - 1
2019-03-03 22:55:12 +00:00
pubKeyBuf = make([]byte, params.BeaconConfig().BLSPubkeyLength)
binary.PutUvarint(pubKeyBuf, lastValidatorIndex)
req = &pb.ValidatorEpochAssignmentsRequest{
2019-03-03 22:55:12 +00:00
PublicKey: pubKeyBuf,
EpochStart: params.BeaconConfig().GenesisSlot,
}
res, err = vs.CommitteeAssignment(context.Background(), req)
if err != nil {
2019-03-03 22:55:12 +00:00
t.Fatalf("Could not call epoch committee assignment %v", err)
}
if res.Shard >= params.BeaconConfig().ShardCount {
t.Errorf("Assigned shard %d can't be higher than %d",
res.Shard, params.BeaconConfig().ShardCount)
}
if res.Slot > state.Slot+params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Assigned slot %d can't be higher than %d",
res.Slot, state.Slot+params.BeaconConfig().SlotsPerEpoch)
}
}
func genesisState(validators uint64) (*pbp2p.BeaconState, error) {
genesisTime := time.Unix(0, 0).Unix()
deposits := make([]*pbp2p.Deposit, validators)
for i := 0; i < len(deposits); i++ {
var pubKey [96]byte
copy(pubKey[:], []byte(strconv.Itoa(i)))
depositInput := &pbp2p.DepositInput{
Pubkey: pubKey[:],
}
depositData, err := helpers.EncodeDepositData(
depositInput,
params.BeaconConfig().MaxDepositAmount,
genesisTime,
)
if err != nil {
return nil, err
}
deposits[i] = &pbp2p.Deposit{DepositData: depositData}
}
return state.GenesisBeaconState(deposits, uint64(genesisTime), nil)
}