prysm-pulse/beacon-chain/rpc/validator_server_test.go
terence tsao 8c04ced1a6 Name Changes to Align with V0.3 (#1621)
* fixed epoch_processing

* penalize->slash

* exit -> voluntary_exit

*  SEED_LOOKAHEAD -> MIN_SEED_LOOKAHED

* ENTRY_EXIT_DELAY -> ACTIVATION_EXIT_DELAY

* `INCLUDER_REWARD_QUOTIENT` -> `ATTESTATION_INCLUSION_REWARD_QUOTIEN`

* LatestIndexRoots -> LatestActiveIndexRoots

* `MIN_VALIDATOR_WITHDRAWAL_EPOCHS` -> `MIN_VALIDATOR_WITHDRAWAL_DELAY`

* MAX_WITHDRAWALS_PER_EPOCH -> MAX_EXIT_DEQUEUES_PER_EPOCH

* ETH1_DATA_VOTING_PERIOD -> EPOCHS_PER_ETH1_VOTING_PERIOD

* SLOT_DURATION -> SECONDS_PER_SLOT

* EPOCH_LENGTH -> SLOTS_PER_EPOCH

* SLOT_DURATION -> SECONDS_PER_SLOT take 2

* rest of the misc fixes for config name changes

* remove tools/bootnode/.!74296!bootnode.go

* `current_epoch_start_shard` -> `current_shuffling_start_shard`, `current_shuffling_epoch`, `current_shuffling_see`

* go fmt

* fixed comment

* updated pseudocode comments

* merged master
2019-02-18 10:52:16 -06:00

241 lines
7.0 KiB
Go

package rpc
import (
"context"
"fmt"
"strconv"
"strings"
"testing"
"time"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"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)
genesis := b.NewGenesisBlock([]byte{})
if err := db.SaveBlock(genesis); err != nil {
t.Fatalf("Could not save genesis block: %v", err)
}
depositData, err := b.EncodeDepositData(
&pbp2p.DepositInput{
Pubkey: []byte{'A'},
},
params.BeaconConfig().MaxDepositAmount,
time.Now().Unix(),
)
if err != nil {
t.Fatalf("Could not encode deposit input: %v", err)
}
deposits := []*pbp2p.Deposit{
{DepositData: depositData},
}
beaconState, err := state.GenesisBeaconState(deposits, 0, nil)
if err != nil {
t.Fatalf("Could not instantiate genesis state: %v", err)
}
if err := db.UpdateChainHead(genesis, beaconState); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}
validatorServer := &ValidatorServer{
beaconDB: db,
}
req := &pb.ValidatorIndexRequest{
PublicKey: []byte{'A'},
}
if _, err := validatorServer.ValidatorIndex(context.Background(), req); err != nil {
t.Errorf("Could not get validator index: %v", err)
}
}
func TestValidatorEpochAssignments_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)
}
genesisTime := time.Unix(0, 0).Unix()
deposits := make([]*pbp2p.Deposit, params.BeaconConfig().DepositsForChainStart)
for i := 0; i < len(deposits); i++ {
var pubKey [96]byte
copy(pubKey[:], []byte(strconv.Itoa(i)))
depositInput := &pbp2p.DepositInput{
Pubkey: pubKey[:],
}
depositData, err := b.EncodeDepositData(
depositInput,
params.BeaconConfig().MaxDepositAmount,
genesisTime,
)
if err != nil {
t.Fatalf("Could not encode genesis block deposits: %v", err)
}
deposits[i] = &pbp2p.Deposit{DepositData: depositData}
}
beaconState, err := state.GenesisBeaconState(deposits, uint64(genesisTime), nil)
if err != nil {
t.Fatalf("Could not instantiate genesis state: %v", err)
}
if err := db.UpdateChainHead(genesis, beaconState); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}
validatorServer := &ValidatorServer{
beaconDB: db,
}
var pubKey [96]byte
copy(pubKey[:], []byte("0"))
req := &pb.ValidatorEpochAssignmentsRequest{
EpochStart: params.BeaconConfig().GenesisSlot,
PublicKey: pubKey[:],
}
res, err := validatorServer.ValidatorEpochAssignments(context.Background(), req)
if err != nil {
t.Errorf("Could not get validator index: %v", err)
}
// With initial shuffling of default 16384 validators, the validator corresponding to
// public key 0 from genesis slot should correspond to an attester slot of 9223372036854775808 at shard 0.
if res.Assignment.Shard != 1 {
t.Errorf(
"Expected validator with pubkey %#x to be assigned to shard 0, received %d",
req.PublicKey,
res.Assignment.Shard,
)
}
if res.Assignment.AttesterSlot != 9223372036854775808 {
t.Errorf(
"Expected validator with pubkey %#x to be assigned as attester of slot 9223372036854775808, "+
"received %d",
req.PublicKey,
res.Assignment.AttesterSlot,
)
}
}
func TestValidatorEpochAssignments_WrongPubkeyLength(t *testing.T) {
db := internal.SetupDB(t)
defer internal.TeardownDB(t, db)
validatorServer := &ValidatorServer{
beaconDB: db,
}
req := &pb.ValidatorEpochAssignmentsRequest{
EpochStart: params.BeaconConfig().GenesisSlot,
PublicKey: []byte{},
}
want := fmt.Sprintf("expected public key to have length %d", params.BeaconConfig().BLSPubkeyLength)
if _, err := validatorServer.ValidatorEpochAssignments(context.Background(), req); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %v, received %v", want, err)
}
}
func TestValidatorCommitteeAtSlot_CrosslinkCommitteesFailure(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)
}
genesisTime := time.Unix(0, 0).Unix()
deposits := make([]*pbp2p.Deposit, params.BeaconConfig().DepositsForChainStart)
for i := 0; i < len(deposits); i++ {
var pubKey [96]byte
copy(pubKey[:], []byte(strconv.Itoa(i)))
depositInput := &pbp2p.DepositInput{
Pubkey: pubKey[:],
}
depositData, err := b.EncodeDepositData(
depositInput,
params.BeaconConfig().MaxDepositAmount,
genesisTime,
)
if err != nil {
t.Fatalf("Could not encode genesis block deposits: %v", err)
}
deposits[i] = &pbp2p.Deposit{DepositData: depositData}
}
beaconState, err := state.GenesisBeaconState(deposits, uint64(genesisTime), nil)
if err != nil {
t.Fatalf("Could not instantiate genesis state: %v", err)
}
if err := db.UpdateChainHead(genesis, beaconState); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}
validatorServer := &ValidatorServer{
beaconDB: db,
}
req := &pb.CommitteeRequest{
Slot: params.BeaconConfig().SlotsPerEpoch * 10,
}
want := "could not get crosslink committees at slot"
if _, err := validatorServer.ValidatorCommitteeAtSlot(context.Background(), req); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %v, received %v", want, err)
}
}
func TestValidatorCommitteeAtSlot_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)
}
genesisTime := time.Unix(0, 0).Unix()
deposits := make([]*pbp2p.Deposit, params.BeaconConfig().DepositsForChainStart)
for i := 0; i < len(deposits); i++ {
var pubKey [96]byte
copy(pubKey[:], []byte(strconv.Itoa(i)))
depositInput := &pbp2p.DepositInput{
Pubkey: pubKey[:],
}
depositData, err := b.EncodeDepositData(
depositInput,
params.BeaconConfig().MaxDepositAmount,
genesisTime,
)
if err != nil {
t.Fatalf("Could not encode genesis block deposits: %v", err)
}
deposits[i] = &pbp2p.Deposit{DepositData: depositData}
}
beaconState, err := state.GenesisBeaconState(deposits, uint64(genesisTime), nil)
if err != nil {
t.Fatalf("Could not instantiate genesis state: %v", err)
}
if err := db.UpdateChainHead(genesis, beaconState); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}
validatorServer := &ValidatorServer{
beaconDB: db,
}
req := &pb.CommitteeRequest{
Slot: params.BeaconConfig().GenesisSlot + 1,
ValidatorIndex: 31,
}
res, err := validatorServer.ValidatorCommitteeAtSlot(context.Background(), req)
if err != nil {
t.Fatalf("Unable to fetch committee at slot: %v", err)
}
if res.Shard != 0 {
t.Errorf("Shard for validator at index 31 should be 2, received %d", res.Shard)
}
}