mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-27 05:38:55 +00:00
b8785ba1d8
* adding fields to initalize state * fix references * change naming * change to eth1data * lint
112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
package db
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/rand"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
"github.com/gogo/protobuf/proto"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
)
|
|
|
|
func setupInitialDeposits(t *testing.T, numDeposits int) ([]*pb.Deposit, []*bls.SecretKey) {
|
|
privKeys := make([]*bls.SecretKey, numDeposits)
|
|
deposits := make([]*pb.Deposit, numDeposits)
|
|
for i := 0; i < len(deposits); i++ {
|
|
priv, err := bls.RandKey(rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
depositInput := &pb.DepositInput{
|
|
Pubkey: priv.PublicKey().Marshal(),
|
|
}
|
|
balance := params.BeaconConfig().MaxDepositAmount
|
|
depositData, err := helpers.EncodeDepositData(depositInput, balance, time.Now().Unix())
|
|
if err != nil {
|
|
t.Fatalf("Cannot encode data: %v", err)
|
|
}
|
|
deposits[i] = &pb.Deposit{DepositData: depositData}
|
|
privKeys[i] = priv
|
|
}
|
|
return deposits, privKeys
|
|
}
|
|
|
|
func TestInitializeState_OK(t *testing.T) {
|
|
db := setupDB(t)
|
|
defer teardownDB(t, db)
|
|
ctx := context.Background()
|
|
|
|
genesisTime := uint64(time.Now().Unix())
|
|
deposits, _ := setupInitialDeposits(t, 10)
|
|
if err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}); err != nil {
|
|
t.Fatalf("Failed to initialize state: %v", err)
|
|
}
|
|
b, err := db.ChainHead()
|
|
if err != nil {
|
|
t.Fatalf("Failed to get chain head: %v", err)
|
|
}
|
|
if b.GetSlot() != params.BeaconConfig().GenesisSlot {
|
|
t.Fatalf("Expected block height to equal 1. Got %d", b.GetSlot())
|
|
}
|
|
|
|
beaconState, err := db.State(ctx)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get state: %v", err)
|
|
}
|
|
if beaconState == nil {
|
|
t.Fatalf("Failed to retrieve state: %v", beaconState)
|
|
}
|
|
beaconStateEnc, err := proto.Marshal(beaconState)
|
|
if err != nil {
|
|
t.Fatalf("Failed to encode state: %v", err)
|
|
}
|
|
|
|
statePrime, err := db.State(ctx)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get state: %v", err)
|
|
}
|
|
statePrimeEnc, err := proto.Marshal(statePrime)
|
|
if err != nil {
|
|
t.Fatalf("Failed to encode state: %v", err)
|
|
}
|
|
|
|
if !bytes.Equal(beaconStateEnc, statePrimeEnc) {
|
|
t.Fatalf("Expected %#x and %#x to be equal", beaconStateEnc, statePrimeEnc)
|
|
}
|
|
}
|
|
|
|
func TestGenesisTime_OK(t *testing.T) {
|
|
db := setupDB(t)
|
|
defer teardownDB(t, db)
|
|
ctx := context.Background()
|
|
|
|
genesisTime, err := db.GenesisTime(ctx)
|
|
if err == nil {
|
|
t.Fatal("expected GenesisTime to fail")
|
|
}
|
|
|
|
deposits, _ := setupInitialDeposits(t, 10)
|
|
if err := db.InitializeState(uint64(genesisTime.Unix()), deposits, &pb.Eth1Data{}); err != nil {
|
|
t.Fatalf("failed to initialize state: %v", err)
|
|
}
|
|
|
|
time1, err := db.GenesisTime(ctx)
|
|
if err != nil {
|
|
t.Fatalf("GenesisTime failed on second attempt: %v", err)
|
|
}
|
|
time2, err := db.GenesisTime(ctx)
|
|
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)
|
|
}
|
|
}
|