prysm-pulse/beacon-chain/db/kv/state_test.go
terence tsao f0332e1131 Save genesis state in DB (#3359)
* Done

* Test

* Fixed test

* emit state initialized
2019-08-29 15:32:35 -07:00

116 lines
2.2 KiB
Go

package kv
import (
"context"
"reflect"
"testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
)
func TestState_CanSaveRetrieve(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
s := &pb.BeaconState{Slot: 100}
r := [32]byte{'A'}
if err := db.SaveState(context.Background(), s, r); err != nil {
t.Fatal(err)
}
savedS, err := db.State(context.Background(), r)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(s, savedS) {
t.Error("did not retrieve saved state")
}
savedS, err = db.State(context.Background(), [32]byte{'B'})
if err != nil {
t.Fatal(err)
}
if savedS != nil {
t.Error("unsaved state should've been nil")
}
}
func TestHeadState_CanSaveRetrieve(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
s := &pb.BeaconState{Slot: 100}
headRoot := [32]byte{'A'}
if err := db.SaveHeadBlockRoot(context.Background(), headRoot); err != nil {
t.Fatal(err)
}
if err := db.SaveState(context.Background(), s, headRoot); err != nil {
t.Fatal(err)
}
savedHeadS, err := db.HeadState(context.Background())
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(s, savedHeadS) {
t.Error("did not retrieve saved state")
}
if err := db.SaveHeadBlockRoot(context.Background(), [32]byte{'B'}); err != nil {
t.Fatal(err)
}
savedHeadS, err = db.HeadState(context.Background())
if err != nil {
t.Fatal(err)
}
if savedHeadS != nil {
t.Error("unsaved head state should've been nil")
}
}
func TestGenesisState_CanSaveRetrieve(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
s := &pb.BeaconState{Slot: 1}
headRoot := [32]byte{'B'}
if err := db.SaveGenesisBlockRoot(context.Background(), headRoot); err != nil {
t.Fatal(err)
}
if err := db.SaveState(context.Background(), s, headRoot); err != nil {
t.Fatal(err)
}
savedGenesisS, err := db.GenesisState(context.Background())
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(s, savedGenesisS) {
t.Error("did not retrieve saved state")
}
if err := db.SaveGenesisBlockRoot(context.Background(), [32]byte{'C'}); err != nil {
t.Fatal(err)
}
savedGenesisS, err = db.HeadState(context.Background())
if err != nil {
t.Fatal(err)
}
if savedGenesisS != nil {
t.Error("unsaved genesis state should've been nil")
}
}