mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-27 05:38:55 +00:00
7a46cc0681
* Enforce stronger head state operations * fix genesis state generation * one test left to fix * all tests passing now * gofmt * Update beacon-chain/db/kv/state_test.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * Update beacon-chain/db/kv/state.go Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com> * fix tests
172 lines
4.9 KiB
Go
172 lines
4.9 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"reflect"
|
|
"testing"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
)
|
|
|
|
func TestReceiveBlock_ProcessCorrectly(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
ctx := context.Background()
|
|
|
|
chainService := setupBeaconChain(t, db)
|
|
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
|
|
genesis, _ := testutil.GenerateFullBlock(beaconState, privKeys, nil, beaconState.Slot+1)
|
|
beaconState, err := state.ExecuteStateTransition(ctx, beaconState, genesis)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
genesisBlkRoot, err := ssz.SigningRoot(genesis)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cp := ðpb.Checkpoint{Root: genesisBlkRoot[:]}
|
|
if err := chainService.forkChoiceStore.GenesisStore(ctx, cp, cp); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := chainService.beaconDB.SaveBlock(ctx, genesis); err != nil {
|
|
t.Fatalf("Could not save block to db: %v", err)
|
|
}
|
|
|
|
if err := db.SaveState(ctx, beaconState, genesisBlkRoot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
slot := beaconState.Slot + 1
|
|
block, err := testutil.GenerateFullBlock(beaconState, privKeys, nil, slot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := chainService.beaconDB.SaveBlock(ctx, block); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := chainService.ReceiveBlock(context.Background(), block); err != nil {
|
|
t.Errorf("Block failed processing: %v", err)
|
|
}
|
|
testutil.AssertLogsContain(t, hook, "Finished applying state transition")
|
|
}
|
|
|
|
func TestReceiveReceiveBlockNoPubsub_CanSaveHeadInfo(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
ctx := context.Background()
|
|
|
|
chainService := setupBeaconChain(t, db)
|
|
|
|
headBlk := ðpb.BeaconBlock{Slot: 100}
|
|
if err := db.SaveBlock(ctx, headBlk); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r, err := ssz.SigningRoot(headBlk)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
head := &pb.BeaconState{Slot: 100, FinalizedCheckpoint: ðpb.Checkpoint{Root: r[:]}}
|
|
if err := db.SaveState(ctx, head, r); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
chainService.forkChoiceStore = &store{headRoot: r[:]}
|
|
|
|
if err := chainService.ReceiveBlockNoPubsub(ctx, ðpb.BeaconBlock{
|
|
Slot: 1,
|
|
Body: ðpb.BeaconBlockBody{}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !bytes.Equal(r[:], chainService.HeadRoot()) {
|
|
t.Error("Incorrect head root saved")
|
|
}
|
|
|
|
if !reflect.DeepEqual(headBlk, chainService.HeadBlock()) {
|
|
t.Error("Incorrect head block saved")
|
|
}
|
|
|
|
testutil.AssertLogsContain(t, hook, "Saved new head info")
|
|
}
|
|
|
|
func TestReceiveReceiveBlockNoPubsub_SameHead(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
ctx := context.Background()
|
|
|
|
chainService := setupBeaconChain(t, db)
|
|
|
|
headBlk := ðpb.BeaconBlock{}
|
|
if err := db.SaveBlock(ctx, headBlk); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
newBlk := ðpb.BeaconBlock{
|
|
Slot: 1,
|
|
Body: ðpb.BeaconBlockBody{}}
|
|
newRoot, _ := ssz.SigningRoot(newBlk)
|
|
if err := db.SaveBlock(ctx, newBlk); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
chainService.forkChoiceStore = &store{headRoot: newRoot[:]}
|
|
chainService.canonicalRoots[0] = newRoot[:]
|
|
|
|
if err := chainService.ReceiveBlockNoPubsub(ctx, newBlk); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
testutil.AssertLogsDoNotContain(t, hook, "Saved new head info")
|
|
}
|
|
|
|
func TestReceiveBlockNoPubsubForkchoice_ProcessCorrectly(t *testing.T) {
|
|
hook := logTest.NewGlobal()
|
|
db := testDB.SetupDB(t)
|
|
defer testDB.TeardownDB(t, db)
|
|
ctx := context.Background()
|
|
|
|
chainService := setupBeaconChain(t, db)
|
|
beaconState, privKeys := testutil.DeterministicGenesisState(t, 100)
|
|
|
|
block, err := testutil.GenerateFullBlock(beaconState, privKeys, nil, beaconState.Slot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := chainService.forkChoiceStore.GenesisStore(ctx, ðpb.Checkpoint{}, ðpb.Checkpoint{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := chainService.beaconDB.SaveBlock(ctx, block); err != nil {
|
|
t.Fatalf("Could not save block to db: %v", err)
|
|
}
|
|
|
|
block, err = testutil.GenerateFullBlock(beaconState, privKeys, nil, beaconState.Slot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.SaveState(ctx, beaconState, bytesutil.ToBytes32(block.ParentRoot)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := chainService.beaconDB.SaveBlock(ctx, block); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := chainService.ReceiveBlockNoPubsubForkchoice(context.Background(), block); err != nil {
|
|
t.Errorf("Block failed processing: %v", err)
|
|
}
|
|
testutil.AssertLogsContain(t, hook, "Finished applying state transition")
|
|
testutil.AssertLogsDoNotContain(t, hook, "Finished fork choice")
|
|
}
|