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

258 lines
6.0 KiB
Go
Raw Normal View History

2018-10-05 17:14:50 +00:00
package db
import (
"testing"
2018-12-01 22:09:12 +00:00
"github.com/prysmaticlabs/prysm/beacon-chain/core/types"
2018-10-05 17:14:50 +00:00
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
2018-10-05 17:14:50 +00:00
)
2018-10-17 06:11:24 +00:00
func TestNilDB(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
b := types.NewBlock(nil)
h, _ := b.Hash()
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
hasBlock := db.HasBlock(h)
if hasBlock {
t.Fatal("HashBlock should return false")
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
bPrime, err := db.GetBlock(h)
if err != nil {
t.Fatalf("failed to get block: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
if bPrime != nil {
t.Fatalf("get should return nil for a non existent key")
}
}
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
func TestSave(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
b1 := types.NewBlock(nil)
h1, _ := b1.Hash()
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
err := db.SaveBlock(b1)
if err != nil {
t.Fatalf("save block failed: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b1Prime, err := db.GetBlock(h1)
2018-10-05 17:14:50 +00:00
if err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to get block: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
h1Prime, _ := b1Prime.Hash()
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
if b1Prime == nil || h1 != h1Prime {
t.Fatalf("get should return b1: %x", h1)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b2 := types.NewBlock(&pb.BeaconBlock{
Slot: 0,
})
h2, _ := b2.Hash()
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
err = db.SaveBlock(b2)
if err != nil {
t.Fatalf("save block failed: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b2Prime, err := db.GetBlock(h2)
if err != nil {
t.Fatalf("failed to get block: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
h2Prime, _ := b2Prime.Hash()
if b2Prime == nil || h2 != h2Prime {
t.Fatalf("get should return b2: %x", h2)
2018-10-05 17:14:50 +00:00
}
}
2018-10-17 06:11:24 +00:00
func TestGetBlockBySlotEmptyChain(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
b, err := db.GetBlockBySlot(0)
2018-10-05 17:14:50 +00:00
if err != nil {
2018-10-17 06:11:24 +00:00
t.Errorf("failure when fetching block by slot: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
if b != nil {
t.Error("GetBlockBySlot should return nil for an empty chain")
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
}
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
func TestUpdateChainHeadNoBlock(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
err := db.InitializeState(nil)
if err != nil {
t.Fatalf("failed to initialize state: %v", err)
}
2018-12-01 22:09:12 +00:00
beaconState, err := db.GetState()
2018-10-05 17:14:50 +00:00
if err != nil {
2018-12-01 22:09:12 +00:00
t.Fatalf("failed to get beacon state: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b := types.NewBlock(&pb.BeaconBlock{Slot: 1})
2018-12-01 22:09:12 +00:00
if err := db.UpdateChainHead(b, beaconState); err == nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("expected UpdateChainHead to fail if the block does not exist: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
}
2018-10-05 17:14:50 +00:00
2018-10-17 06:11:24 +00:00
func TestUpdateChainHead(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
err := db.InitializeState(nil)
2018-10-05 17:14:50 +00:00
if err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to initialize state: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b, err := db.GetBlockBySlot(0)
if err != nil {
t.Fatalf("failed to get genesis block: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
bHash, err := b.Hash()
2018-10-05 17:14:50 +00:00
if err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to get hash of b: %v", err)
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 beacon state: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b2 := types.NewBlock(&pb.BeaconBlock{
Slot: 1,
AncestorHash32S: [][]byte{bHash[:]},
2018-10-05 17:14:50 +00:00
})
2018-10-17 06:11:24 +00:00
b2Hash, err := b2.Hash()
2018-10-05 17:14:50 +00:00
if err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to hash b2: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
if err := db.SaveBlock(b2); err != nil {
t.Fatalf("failed to save block: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-12-01 22:09:12 +00:00
if err := db.UpdateChainHead(b2, beaconState); err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to record the new head of the main chain: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b2Prime, err := db.GetBlockBySlot(1)
2018-10-05 17:14:50 +00:00
if err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to retrieve slot 1: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b2Sigma, err := db.GetChainHead()
if err != nil {
t.Fatalf("failed to retrieve head: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b2PrimeHash, err := b2Prime.Hash()
2018-10-05 17:14:50 +00:00
if err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to hash b2Prime: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
b2SigmaHash, err := b2Sigma.Hash()
if err != nil {
t.Fatalf("failed to hash b2Sigma: %v", err)
2018-10-05 17:14:50 +00:00
}
2018-10-17 06:11:24 +00:00
if b2Hash != b2PrimeHash {
t.Fatalf("expected %x and %x to be equal", b2Hash, b2PrimeHash)
}
if b2Hash != b2SigmaHash {
t.Fatalf("expected %x and %x to be equal", b2Hash, b2SigmaHash)
2018-10-05 17:14:50 +00:00
}
}
2018-10-17 06:11:24 +00:00
func TestChainProgress(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
2018-10-17 06:11:24 +00:00
err := db.InitializeState(nil)
if err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to initialize state: %v", err)
}
2018-12-01 22:09:12 +00:00
beaconState, err := db.GetState()
if err != nil {
2018-12-01 22:09:12 +00:00
t.Fatalf("Failed to get beacon state: %v", err)
}
cycleLength := params.BeaconConfig().CycleLength
2018-10-17 06:11:24 +00:00
b1 := types.NewBlock(&pb.BeaconBlock{Slot: 1})
if err := db.SaveBlock(b1); err != nil {
t.Fatalf("failed to save block: %v", err)
}
2018-12-01 22:09:12 +00:00
if err := db.UpdateChainHead(b1, beaconState); err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to record the new head: %v", err)
}
heighestBlock, err := db.GetChainHead()
if err != nil {
t.Fatalf("failed to get chain head: %v", err)
}
if heighestBlock.SlotNumber() != b1.SlotNumber() {
t.Fatalf("expected height to equal %d, got %d", b1.SlotNumber(), heighestBlock.SlotNumber())
}
2018-10-17 06:11:24 +00:00
b2 := types.NewBlock(&pb.BeaconBlock{Slot: cycleLength})
if err := db.SaveBlock(b2); err != nil {
t.Fatalf("failed to save block: %v", err)
}
2018-12-01 22:09:12 +00:00
if err := db.UpdateChainHead(b2, beaconState); err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to record the new head: %v", err)
}
heighestBlock, err = db.GetChainHead()
if err != nil {
t.Fatalf("failed to get block: %v", err)
}
if heighestBlock.SlotNumber() != b2.SlotNumber() {
t.Fatalf("expected height to equal %d, got %d", b2.SlotNumber(), heighestBlock.SlotNumber())
}
b3 := types.NewBlock(&pb.BeaconBlock{Slot: 3})
if err := db.SaveBlock(b3); err != nil {
t.Fatalf("failed to save block: %v", err)
}
2018-12-01 22:09:12 +00:00
if err := db.UpdateChainHead(b3, beaconState); err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to update head: %v", err)
}
heighestBlock, err = db.GetChainHead()
if err != nil {
t.Fatalf("failed to get chain head: %v", err)
}
if heighestBlock.SlotNumber() != b3.SlotNumber() {
t.Fatalf("expected height to equal %d, got %d", b3.SlotNumber(), heighestBlock.SlotNumber())
}
}
2018-10-17 06:11:24 +00:00
func TestGetGenesisTime(t *testing.T) {
db := setupDB(t)
defer teardownDB(t, db)
time, err := db.GetGenesisTime()
if err == nil {
t.Fatal("expected GetGenesisTime to fail")
}
2018-10-17 06:11:24 +00:00
err = db.InitializeState(nil)
if err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("failed to initialize state: %v", err)
}
2018-10-17 06:11:24 +00:00
time, err = db.GetGenesisTime()
if err != nil {
t.Fatalf("GetGenesisTime failed on second attempt: %v", err)
}
time2, err := db.GetGenesisTime()
if err != nil {
2018-10-17 06:11:24 +00:00
t.Fatalf("GetGenesisTime failed on second attempt: %v", err)
}
2018-10-17 06:11:24 +00:00
if time != time2 {
t.Fatalf("Expected %v and %v to be equal", time, time2)
}
}