2018-10-05 17:14:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-10-17 06:11:24 +00:00
|
|
|
"bytes"
|
2019-02-28 03:55:47 +00:00
|
|
|
"context"
|
2019-02-20 05:07:28 +00:00
|
|
|
"crypto/rand"
|
2019-03-13 15:12:42 +00:00
|
|
|
"strings"
|
2018-10-05 17:14:50 +00:00
|
|
|
"testing"
|
2019-01-28 11:59:37 +00:00
|
|
|
"time"
|
2018-12-23 22:51:04 +00:00
|
|
|
|
2019-02-05 18:52:14 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
2019-02-03 22:44:48 +00:00
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
2019-02-20 05:07:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2019-02-05 18:52:14 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2018-10-05 17:14:50 +00:00
|
|
|
)
|
|
|
|
|
2019-03-12 19:30:10 +00:00
|
|
|
func setupInitialDeposits(t testing.TB, numDeposits int) ([]*pb.Deposit, []*bls.SecretKey) {
|
2019-02-20 05:07:28 +00:00
|
|
|
privKeys := make([]*bls.SecretKey, numDeposits)
|
|
|
|
deposits := make([]*pb.Deposit, numDeposits)
|
2019-02-03 22:44:48 +00:00
|
|
|
for i := 0; i < len(deposits); i++ {
|
2019-02-20 05:07:28 +00:00
|
|
|
priv, err := bls.RandKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-03 22:44:48 +00:00
|
|
|
depositInput := &pb.DepositInput{
|
2019-02-20 05:07:28 +00:00
|
|
|
Pubkey: priv.PublicKey().Marshal(),
|
2019-02-03 22:44:48 +00:00
|
|
|
}
|
2019-02-09 13:09:09 +00:00
|
|
|
balance := params.BeaconConfig().MaxDepositAmount
|
2019-02-19 18:05:34 +00:00
|
|
|
depositData, err := helpers.EncodeDepositData(depositInput, balance, time.Now().Unix())
|
2019-02-03 22:44:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot encode data: %v", err)
|
|
|
|
}
|
|
|
|
deposits[i] = &pb.Deposit{DepositData: depositData}
|
2019-02-20 05:07:28 +00:00
|
|
|
privKeys[i] = priv
|
2019-02-03 22:44:48 +00:00
|
|
|
}
|
2019-02-20 05:07:28 +00:00
|
|
|
return deposits, privKeys
|
2019-02-03 22:44:48 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestInitializeState_OK(t *testing.T) {
|
2018-10-17 06:11:24 +00:00
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
2019-02-28 03:55:47 +00:00
|
|
|
ctx := context.Background()
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2019-01-28 11:59:37 +00:00
|
|
|
genesisTime := uint64(time.Now().Unix())
|
2019-02-20 05:07:28 +00:00
|
|
|
deposits, _ := setupInitialDeposits(t, 10)
|
2019-03-02 23:38:22 +00:00
|
|
|
if err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}); err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatalf("Failed to initialize state: %v", err)
|
|
|
|
}
|
2019-01-21 09:34:11 +00:00
|
|
|
b, err := db.ChainHead()
|
2018-10-17 06:11:24 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get chain head: %v", err)
|
|
|
|
}
|
2019-02-10 22:09:35 +00:00
|
|
|
if b.GetSlot() != params.BeaconConfig().GenesisSlot {
|
2018-12-20 22:00:38 +00:00
|
|
|
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
|
|
|
|
2019-02-28 03:55:47 +00:00
|
|
|
beaconState, err := db.State(ctx)
|
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
|
|
|
}
|
2018-12-23 22:51:04 +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
|
|
|
}
|
|
|
|
|
2019-02-28 03:55:47 +00:00
|
|
|
statePrime, err := db.State(ctx)
|
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-23 22:51:04 +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)
|
2018-10-18 04:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-25 06:47:07 +00:00
|
|
|
|
2019-02-22 15:11:26 +00:00
|
|
|
func TestGenesisTime_OK(t *testing.T) {
|
2018-12-25 06:47:07 +00:00
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
2019-02-28 03:55:47 +00:00
|
|
|
ctx := context.Background()
|
2018-12-25 06:47:07 +00:00
|
|
|
|
2019-02-28 03:55:47 +00:00
|
|
|
genesisTime, err := db.GenesisTime(ctx)
|
2018-12-25 06:47:07 +00:00
|
|
|
if err == nil {
|
2019-03-07 03:02:47 +00:00
|
|
|
t.Fatal("Expected GenesisTime to fail")
|
2018-12-25 06:47:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 05:07:28 +00:00
|
|
|
deposits, _ := setupInitialDeposits(t, 10)
|
2019-03-02 23:38:22 +00:00
|
|
|
if err := db.InitializeState(uint64(genesisTime.Unix()), deposits, &pb.Eth1Data{}); err != nil {
|
2019-03-07 03:02:47 +00:00
|
|
|
t.Fatalf("Failed to initialize state: %v", err)
|
2018-12-25 06:47:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 03:55:47 +00:00
|
|
|
time1, err := db.GenesisTime(ctx)
|
2018-12-25 06:47:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("GenesisTime failed on second attempt: %v", err)
|
|
|
|
}
|
2019-02-28 03:55:47 +00:00
|
|
|
time2, err := db.GenesisTime(ctx)
|
2018-12-25 06:47:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("GenesisTime failed on second attempt: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-28 11:59:37 +00:00
|
|
|
if time1 != time2 {
|
|
|
|
t.Fatalf("Expected %v and %v to be equal", time1, time2)
|
2018-12-25 06:47:07 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-07 03:02:47 +00:00
|
|
|
|
|
|
|
func TestFinalizeState_OK(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := db.State(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to retrieve state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.SaveFinalizedState(state); err != nil {
|
|
|
|
t.Fatalf("Unable to save finalized state")
|
|
|
|
}
|
|
|
|
|
|
|
|
fState, err := db.FinalizedState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to retrieve finalized state")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proto.Equal(fState, state) {
|
|
|
|
t.Error("Retrieved and saved finalized are unequal")
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 19:30:10 +00:00
|
|
|
|
2019-03-12 20:44:21 +00:00
|
|
|
func TestCurrentAndFinalizeState_OK(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := db.State(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to retrieve state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state.FinalizedEpoch = 10000
|
|
|
|
state.BatchedBlockRootHash32S = [][]byte{[]byte("testing-prysm")}
|
|
|
|
|
|
|
|
if err := db.SaveCurrentAndFinalizedState(state); err != nil {
|
|
|
|
t.Fatalf("Unable to save state")
|
|
|
|
}
|
|
|
|
|
|
|
|
fState, err := db.FinalizedState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to retrieve finalized state")
|
|
|
|
}
|
|
|
|
|
|
|
|
cState, err := db.State(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to retrieve state")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proto.Equal(fState, state) {
|
|
|
|
t.Error("Retrieved and saved finalized are unequal")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !proto.Equal(cState, state) {
|
|
|
|
t.Error("Retrieved and saved current are unequal")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 19:30:10 +00:00
|
|
|
func BenchmarkState_ReadingFromCache(b *testing.B) {
|
|
|
|
db := setupDB(b)
|
|
|
|
defer teardownDB(b, db)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
genesisTime := uint64(time.Now().Unix())
|
|
|
|
deposits, _ := setupInitialDeposits(b, 10)
|
|
|
|
if err := db.InitializeState(genesisTime, deposits, &pb.Eth1Data{}); err != nil {
|
|
|
|
b.Fatalf("Failed to initialize state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err := db.State(ctx)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Could not read DV beacon state from DB: %v", err)
|
|
|
|
}
|
|
|
|
state.Slot++
|
|
|
|
err = db.SaveState(state)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Could not save beacon state to cache from DB: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if db.currentState.Slot != params.BeaconConfig().GenesisSlot+1 {
|
|
|
|
b.Fatal("cache should be prepared on state after saving to DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
_, err := db.State(ctx)
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Could not read beacon state from cache: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-13 15:12:42 +00:00
|
|
|
|
|
|
|
func TestFinalizedState_NoneExists(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
|
|
|
wanted := "no finalized state saved"
|
|
|
|
_, err := db.FinalizedState()
|
|
|
|
if !strings.Contains(err.Error(), wanted) {
|
|
|
|
t.Errorf("Expected: %s, received: %s", wanted, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJustifiedState_CanSaveRetrieve(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
|
|
|
|
|
|
|
stateSlot := uint64(10)
|
|
|
|
state := &pb.BeaconState{
|
|
|
|
Slot: stateSlot,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.SaveJustifiedState(state); err != nil {
|
|
|
|
t.Fatalf("could not save justified state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
justifiedState, err := db.JustifiedState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not get justified state: %v", err)
|
|
|
|
}
|
|
|
|
if justifiedState.Slot != stateSlot {
|
|
|
|
t.Errorf("Saved state does not have the slot from which it was requested, wanted: %d, got: %d",
|
|
|
|
stateSlot, justifiedState.Slot)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJustifiedState_NoneExists(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
|
|
|
wanted := "no justified state saved"
|
|
|
|
_, err := db.JustifiedState()
|
|
|
|
if !strings.Contains(err.Error(), wanted) {
|
|
|
|
t.Errorf("Expected: %s, received: %s", wanted, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFinalizedState_CanSaveRetrieve(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
|
|
|
|
|
|
|
stateSlot := uint64(10)
|
|
|
|
state := &pb.BeaconState{
|
|
|
|
Slot: stateSlot,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.SaveFinalizedState(state); err != nil {
|
|
|
|
t.Fatalf("could not save finalized state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
finalizedState, err := db.FinalizedState()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not get finalized state: %v", err)
|
|
|
|
}
|
|
|
|
if finalizedState.Slot != stateSlot {
|
|
|
|
t.Errorf("Saved state does not have the slot from which it was requested, wanted: %d, got: %d",
|
|
|
|
stateSlot, finalizedState.Slot)
|
|
|
|
}
|
|
|
|
}
|