2018-10-05 17:14:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-10-17 06:11:24 +00:00
|
|
|
"bytes"
|
2018-10-05 17:14:50 +00:00
|
|
|
"testing"
|
2018-12-23 22:51:04 +00:00
|
|
|
|
|
|
|
"github.com/gogo/protobuf/proto"
|
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
|
|
|
|
2019-01-09 03:03:57 +00:00
|
|
|
if err := db.InitializeState(); err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatalf("Failed to initialize state: %v", err)
|
|
|
|
}
|
|
|
|
b, err := db.GetChainHead()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get chain head: %v", err)
|
|
|
|
}
|
2018-12-20 22:00:38 +00:00
|
|
|
if b.GetSlot() != 0 {
|
|
|
|
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
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
beaconState, err := db.GetState()
|
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
|
|
|
}
|
|
|
|
|
2018-12-01 22:09:12 +00:00
|
|
|
statePrime, err := db.GetState()
|
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
|
|
|
|
|
|
|
func TestGenesisTime(t *testing.T) {
|
|
|
|
db := setupDB(t)
|
|
|
|
defer teardownDB(t, db)
|
|
|
|
|
|
|
|
time, err := db.GenesisTime()
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected GenesisTime to fail")
|
|
|
|
}
|
|
|
|
|
2019-01-09 03:03:57 +00:00
|
|
|
err = db.InitializeState()
|
2018-12-25 06:47:07 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to initialize state: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
time, err = db.GenesisTime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("GenesisTime failed on second attempt: %v", err)
|
|
|
|
}
|
|
|
|
time2, err := db.GenesisTime()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("GenesisTime failed on second attempt: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if time != time2 {
|
|
|
|
t.Fatalf("Expected %v and %v to be equal", time, time2)
|
|
|
|
}
|
|
|
|
}
|