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

92 lines
2.4 KiB
Go
Raw Normal View History

package rpc
import (
"context"
"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/hashutil"
"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'},
},
params.BeaconConfig().MaxDeposit,
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,
}
req := &pb.PublicKey{
PublicKey: []byte{'A'},
}
if _, err := validatorServer.ValidatorIndex(context.Background(), req); err != nil {
t.Errorf("Could not get validator index: %v", err)
}
}
func TestValidatorShard(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)
}
unixTime := uint64(time.Now().Unix())
if err := db.InitializeState(unixTime); err != nil {
t.Fatalf("Can't initialze genesis state: %v", err)
}
beaconState, err := db.State()
if err != nil {
t.Fatalf("Can't get 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,
}
pubkey := hashutil.Hash([]byte{byte(0)})
req := &pb.PublicKey{
PublicKey: pubkey[:],
}
if _, err := validatorServer.ValidatorShard(context.Background(), req); err != nil {
t.Errorf("Could not get validator shard ID: %v", err)
}
}