prysm-pulse/beacon-chain/db/state_test.go

102 lines
2.6 KiB
Go
Raw Normal View History

2018-10-05 17:14:50 +00:00
package db
import (
2018-10-17 06:11:24 +00:00
"bytes"
"reflect"
2018-10-05 17:14:50 +00:00
"testing"
"github.com/prysmaticlabs/prysm/beacon-chain/types"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
2018-10-05 17:14:50 +00:00
)
2018-10-17 06:11:24 +00:00
func TestInitializeState(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
if err := db.InitializeState(nil); err != nil {
t.Fatalf("Failed to initialize state: %v", err)
}
b, err := db.GetChainHead()
if err != nil {
t.Fatalf("Failed to get chain head: %v", err)
}
if b.SlotNumber() != 0 {
t.Fatalf("Expected block height to equal 1. Got %d", b.SlotNumber())
}
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
aState, err := db.GetActiveState()
if err != nil {
t.Fatalf("Failed to get active state: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
cState, err := db.GetCrystallizedState()
if err != nil {
t.Fatalf("Failed to get crystallized state: %v", err)
}
if aState == nil || cState == nil {
t.Fatalf("Failed to retrieve state: %v, %v", aState, cState)
}
aStateEnc, err := aState.Marshal()
if err != nil {
t.Fatalf("Failed to encode active state: %v", err)
}
cStateEnc, err := cState.Marshal()
if err != nil {
t.Fatalf("Failed t oencode crystallized state: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
aStatePrime, err := db.GetActiveState()
if err != nil {
t.Fatalf("Failed to get active state: %v", err)
}
aStatePrimeEnc, err := aStatePrime.Marshal()
if err != nil {
t.Fatalf("Failed to encode active state: %v", err)
}
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
cStatePrime, err := db.GetCrystallizedState()
if err != nil {
t.Fatalf("Failed to get crystallized state: %v", err)
}
cStatePrimeEnc, err := cStatePrime.Marshal()
if err != nil {
t.Fatalf("Failed to encode crystallized state: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
if !bytes.Equal(aStateEnc, aStatePrimeEnc) {
t.Fatalf("Expected %#x and %#x to be equal", aStateEnc, aStatePrimeEnc)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
if !bytes.Equal(cStateEnc, cStatePrimeEnc) {
t.Fatalf("Expected %#x and %#x to be equal", cStateEnc, cStatePrimeEnc)
2018-10-05 17:14:50 +00:00
}
}
func TestGetUnfinalizedBlockState(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
aState := types.NewActiveState(&pb.ActiveState{})
cState := types.NewCrystallizedState(&pb.CrystallizedState{})
if err := db.SaveUnfinalizedBlockState(aState, cState); err != nil {
t.Fatalf("Could not save unfinalized block state: %v", err)
}
aStateHash, err := aState.Hash()
if err != nil {
t.Fatal(err)
}
cStateHash, err := cState.Hash()
if err != nil {
t.Fatal(err)
}
got1, got2, err := db.GetUnfinalizedBlockState(aStateHash, cStateHash)
if err != nil {
t.Errorf("Unexpected error: wanted nil, received %v", err)
return
}
if !reflect.DeepEqual(got1, aState) {
t.Errorf("ActiveState not equal: got = %v, want %v", got1, aState)
}
if !reflect.DeepEqual(got2, cState) {
t.Errorf("CrystallizedState not equal: got = %v, want %v", got2, cState)
}
}