2019-01-28 15:40:40 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-29 12:56:14 +00:00
|
|
|
"strconv"
|
2019-01-28 15:40:40 +00:00
|
|
|
"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(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'},
|
|
|
|
},
|
2019-02-09 13:09:09 +00:00
|
|
|
params.BeaconConfig().MaxDepositAmount,
|
2019-01-28 15:40:40 +00:00
|
|
|
time.Now().Unix(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not encode deposit input: %v", err)
|
|
|
|
}
|
|
|
|
deposits := []*pbp2p.Deposit{
|
|
|
|
{DepositData: depositData},
|
|
|
|
}
|
|
|
|
beaconState, err := state.InitialBeaconState(deposits, 0, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not instantiate initial state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.UpdateChainHead(genesis, beaconState); err != nil {
|
|
|
|
t.Fatalf("Could not save genesis state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
validatorServer := &ValidatorServer{
|
|
|
|
beaconDB: db,
|
|
|
|
}
|
2019-01-29 12:56:14 +00:00
|
|
|
req := &pb.ValidatorIndexRequest{
|
2019-01-28 15:40:40 +00:00
|
|
|
PublicKey: []byte{'A'},
|
|
|
|
}
|
|
|
|
if _, err := validatorServer.ValidatorIndex(context.Background(), req); err != nil {
|
|
|
|
t.Errorf("Could not get validator index: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-29 12:56:14 +00:00
|
|
|
func TestValidatorEpochAssignments(t *testing.T) {
|
2019-01-28 15:40:40 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-01-29 12:56:14 +00:00
|
|
|
genesisTime := params.BeaconConfig().GenesisTime.Unix()
|
|
|
|
deposits := make([]*pbp2p.Deposit, params.BeaconConfig().DepositsForChainStart)
|
|
|
|
for i := 0; i < len(deposits); i++ {
|
|
|
|
var pubKey [48]byte
|
|
|
|
copy(pubKey[:], []byte(strconv.Itoa(i)))
|
|
|
|
depositInput := &pbp2p.DepositInput{
|
2019-02-09 20:35:51 +00:00
|
|
|
Pubkey: pubKey[:],
|
2019-01-29 12:56:14 +00:00
|
|
|
}
|
|
|
|
depositData, err := b.EncodeDepositData(
|
|
|
|
depositInput,
|
2019-02-09 13:09:09 +00:00
|
|
|
params.BeaconConfig().MaxDepositAmount,
|
2019-01-29 12:56:14 +00:00
|
|
|
genesisTime,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not encode initial block deposits: %v", err)
|
|
|
|
}
|
|
|
|
deposits[i] = &pbp2p.Deposit{DepositData: depositData}
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
2019-01-29 12:56:14 +00:00
|
|
|
beaconState, err := state.InitialBeaconState(deposits, uint64(genesisTime), nil)
|
2019-01-28 15:40:40 +00:00
|
|
|
if err != nil {
|
2019-01-29 12:56:14 +00:00
|
|
|
t.Fatalf("Could not instantiate initial state: %v", err)
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.UpdateChainHead(genesis, beaconState); err != nil {
|
|
|
|
t.Fatalf("Could not save genesis state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
validatorServer := &ValidatorServer{
|
|
|
|
beaconDB: db,
|
|
|
|
}
|
2019-01-29 12:56:14 +00:00
|
|
|
var pubKey [48]byte
|
|
|
|
copy(pubKey[:], []byte("0"))
|
|
|
|
req := &pb.ValidatorEpochAssignmentsRequest{
|
2019-02-10 22:09:35 +00:00
|
|
|
EpochStart: params.BeaconConfig().GenesisSlot,
|
2019-01-29 12:56:14 +00:00
|
|
|
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
|
2019-02-10 22:09:35 +00:00
|
|
|
// public key 0 from genesis slot should correspond to an attester slot of 9223372036854775808 at shard 0.
|
|
|
|
if res.Assignment.Shard != 0 {
|
2019-01-29 12:56:14 +00:00
|
|
|
t.Errorf(
|
2019-02-10 22:09:35 +00:00
|
|
|
"Expected validator with pubkey %#x to be assigned to shard 0, received %d",
|
2019-01-29 12:56:14 +00:00
|
|
|
req.PublicKey,
|
|
|
|
res.Assignment.Shard,
|
|
|
|
)
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
2019-02-10 22:09:35 +00:00
|
|
|
if res.Assignment.AttesterSlot != 9223372036854775808 {
|
2019-01-29 12:56:14 +00:00
|
|
|
t.Errorf(
|
2019-02-10 22:09:35 +00:00
|
|
|
"Expected validator with pubkey %#x to be assigned as attester of slot 9223372036854775808, "+
|
|
|
|
"received %d",
|
2019-01-29 12:56:14 +00:00
|
|
|
req.PublicKey,
|
|
|
|
res.Assignment.AttesterSlot,
|
|
|
|
)
|
2019-01-28 15:40:40 +00:00
|
|
|
}
|
|
|
|
}
|