mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-15 06:28:20 +00:00
78 lines
1.4 KiB
Go
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")
|
||
|
}
|
||
|
}
|