prysm-pulse/beacon-chain/db/state_test.go

102 lines
2.6 KiB
Go
Raw Normal View History

2018-10-05 17:14:50 +00:00
package db
import (
2018-10-17 06:11:24 +00:00
"bytes"
2018-10-05 17:14:50 +00:00
"testing"
"time"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/validators"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
2018-10-05 17:14:50 +00:00
)
func setupInitialDeposits(t *testing.T) []*pb.Deposit {
genesisValidatorRegistry := validators.InitialValidatorRegistry()
deposits := make([]*pb.Deposit, len(genesisValidatorRegistry))
for i := 0; i < len(deposits); i++ {
depositInput := &pb.DepositInput{
Pubkey: genesisValidatorRegistry[i].Pubkey,
}
2019-02-09 13:09:09 +00:00
balance := params.BeaconConfig().MaxDepositAmount
depositData, err := blocks.EncodeDepositData(depositInput, balance, time.Now().Unix())
if err != nil {
t.Fatalf("Cannot encode data: %v", err)
}
deposits[i] = &pb.Deposit{DepositData: depositData}
}
return deposits
}
2018-10-17 06:11:24 +00:00
func TestInitializeState(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
2018-10-05 17:14:50 +00:00
genesisTime := uint64(time.Now().Unix())
deposits := setupInitialDeposits(t)
if err := db.InitializeState(genesisTime, deposits); err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("Failed to initialize state: %v", err)
}
b, err := db.ChainHead()
2018-10-17 06:11:24 +00:00
if err != nil {
t.Fatalf("Failed to get chain head: %v", err)
}
if b.GetSlot() != 0 {
t.Fatalf("Expected block height to equal 1. Got %d", b.GetSlot())
2018-10-17 06:11:24 +00:00
}
2018-10-05 17:14:50 +00:00
beaconState, err := db.State()
2018-10-17 06:11:24 +00:00
if err != nil {
2018-12-01 22:09:12 +00:00
t.Fatalf("Failed to get state: %v", err)
2018-10-17 06:11:24 +00:00
}
2018-12-01 22:09:12 +00:00
if beaconState == nil {
t.Fatalf("Failed to retrieve state: %v", beaconState)
2018-10-17 06:11:24 +00:00
}
beaconStateEnc, err := proto.Marshal(beaconState)
2018-10-17 06:11:24 +00:00
if err != nil {
2018-12-01 22:09:12 +00:00
t.Fatalf("Failed to encode state: %v", err)
2018-10-05 17:14:50 +00:00
}
statePrime, err := db.State()
2018-10-17 06:11:24 +00:00
if err != nil {
2018-12-01 22:09:12 +00:00
t.Fatalf("Failed to get state: %v", err)
2018-10-17 06:11:24 +00:00
}
statePrimeEnc, err := proto.Marshal(statePrime)
2018-10-17 06:11:24 +00:00
if err != nil {
2018-12-01 22:09:12 +00:00
t.Fatalf("Failed to encode state: %v", err)
2018-10-17 06:11:24 +00:00
}
2018-10-05 17:14:50 +00:00
2018-12-01 22:09:12 +00:00
if !bytes.Equal(beaconStateEnc, statePrimeEnc) {
t.Fatalf("Expected %#x and %#x to be equal", beaconStateEnc, statePrimeEnc)
}
}
func TestGenesisTime(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
genesisTime, err := db.GenesisTime()
if err == nil {
t.Fatal("expected GenesisTime to fail")
}
deposits := setupInitialDeposits(t)
if err := db.InitializeState(uint64(genesisTime.Unix()), deposits); err != nil {
t.Fatalf("failed to initialize state: %v", err)
}
time1, err := db.GenesisTime()
if err != nil {
t.Fatalf("GenesisTime failed on second attempt: %v", err)
}
time2, err := db.GenesisTime()
if err != nil {
t.Fatalf("GenesisTime failed on second attempt: %v", err)
}
if time1 != time2 {
t.Fatalf("Expected %v and %v to be equal", time1, time2)
}
}