prysm-pulse/beacon-chain/db/kv/state_test.go
terence tsao 27319a8990 Implement State DB Methods (#3193)
* Added state implementation

* Gaze

* Fixed test

* Fixed build file

* Fixed all tests

* Merged with master

* Added comments to save and get from roots

* Make it explicit signing root

* s/./,

* s/marshalled/marshaled
2019-08-13 18:33:31 -04:00

78 lines
1.4 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")
}
}