mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-17 23:38:46 +00:00
9e5cc81340
* new ssz hash tree root * Merge branch 'master' into new-ssz-state * better comments on func * add errors instead of panic in state * utilize errors wrap everywhere * include bench * added bench info * equality test * dup * gaz * use new hash tree root in state transition * fix build * separate test package * three targets failign * single target fails * please test targets...pass for me * revert * Merge branch 'master' into new-ssz-state * rev * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state * broken build * Merge branch 'master' into new-ssz-state * gaz * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state * ssz workspace * master ssz * Merge branch 'master' into new-ssz-state * resolve conf * resolve some conflicts and fix up broken file * fix up build file issues and sync * eth1 data votes included * further abstractions, simplifications * Merge branch 'master' into new-ssz-state * gaz * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state * feature flag gating * add field count test * Merge branch 'master' into new-ssz-state * resolving ivan feedback * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state * gaz * Merge branch 'master' into new-ssz-state * addressed * Merge branch 'new-ssz-state' of github.com:prysmaticlabs/prysm into new-ssz-state
285 lines
7.5 KiB
Go
285 lines
7.5 KiB
Go
package blockchain
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"reflect"
|
|
"testing"
|
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
|
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
|
|
"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)
|
|
deposits, _, privKeys := testutil.SetupInitialDeposits(t, 100)
|
|
beaconState, err := state.GenesisBeaconState(deposits, 0, ðpb.Eth1Data{BlockHash: make([]byte, 32)})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
beaconState.Eth1Data.BlockHash = nil
|
|
beaconState.Eth1DepositIndex = 100
|
|
stateRoot, err := ssz.HashTreeRoot(beaconState)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
genesis := b.NewGenesisBlock(stateRoot[:])
|
|
bodyRoot, err := ssz.HashTreeRoot(genesis.Body)
|
|
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)
|
|
}
|
|
|
|
beaconState.LatestBlockHeader = ðpb.BeaconBlockHeader{
|
|
Slot: genesis.Slot,
|
|
ParentRoot: genesis.ParentRoot,
|
|
BodyRoot: bodyRoot[:],
|
|
StateRoot: genesis.StateRoot,
|
|
}
|
|
if err := chainService.beaconDB.SaveBlock(ctx, genesis); err != nil {
|
|
t.Fatalf("Could not save block to db: %v", err)
|
|
}
|
|
parentRoot, err := ssz.SigningRoot(genesis)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := db.SaveState(ctx, beaconState, parentRoot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
slot := beaconState.Slot + 1
|
|
epoch := helpers.SlotToEpoch(slot)
|
|
beaconState.Slot++
|
|
randaoReveal, err := testutil.CreateRandaoReveal(beaconState, epoch, privKeys)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
beaconState.Slot--
|
|
|
|
depRoot := []byte("a")
|
|
depRoot32 := bytesutil.ToBytes32(depRoot)
|
|
blockHash := []byte("b")
|
|
blockHash32 := bytesutil.ToBytes32(blockHash)
|
|
block := ðpb.BeaconBlock{
|
|
Slot: slot,
|
|
ParentRoot: parentRoot[:],
|
|
Body: ðpb.BeaconBlockBody{
|
|
Eth1Data: ðpb.Eth1Data{
|
|
DepositCount: uint64(len(deposits)),
|
|
DepositRoot: depRoot32[:],
|
|
BlockHash: blockHash32[:],
|
|
},
|
|
RandaoReveal: randaoReveal[:],
|
|
Attestations: nil,
|
|
},
|
|
}
|
|
|
|
stateRootCandidate, err := state.ExecuteStateTransitionNoVerify(context.Background(), beaconState, block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
stateRoot, err = ssz.HashTreeRoot(stateRootCandidate)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
block.StateRoot = stateRoot[:]
|
|
|
|
block, err = testutil.SignBlock(beaconState, block, privKeys)
|
|
if err != nil {
|
|
t.Error(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)
|
|
}
|
|
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)
|
|
deposits, _, privKeys := testutil.SetupInitialDeposits(t, 100)
|
|
beaconState, err := state.GenesisBeaconState(deposits, 0, ðpb.Eth1Data{BlockHash: make([]byte, 32)})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
beaconState.Eth1DepositIndex = 100
|
|
stateRoot, err := ssz.HashTreeRoot(beaconState)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
genesis := b.NewGenesisBlock(stateRoot[:])
|
|
bodyRoot, err := ssz.HashTreeRoot(genesis.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := chainService.forkChoiceStore.GenesisStore(ctx, ðpb.Checkpoint{}, ðpb.Checkpoint{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
beaconState.LatestBlockHeader = ðpb.BeaconBlockHeader{
|
|
Slot: genesis.Slot,
|
|
ParentRoot: genesis.ParentRoot,
|
|
BodyRoot: bodyRoot[:],
|
|
StateRoot: genesis.StateRoot,
|
|
}
|
|
if err := chainService.beaconDB.SaveBlock(ctx, genesis); err != nil {
|
|
t.Fatalf("Could not save block to db: %v", err)
|
|
}
|
|
parentRoot, err := ssz.SigningRoot(genesis)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if err := db.SaveState(ctx, beaconState, parentRoot); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
slot := beaconState.Slot + 1
|
|
epoch := helpers.SlotToEpoch(slot)
|
|
beaconState.Slot++
|
|
randaoReveal, err := testutil.CreateRandaoReveal(beaconState, epoch, privKeys)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
beaconState.Slot--
|
|
|
|
depRoot := []byte("a")
|
|
depRoot32 := bytesutil.ToBytes32(depRoot)
|
|
blockHash := []byte("b")
|
|
blockHash32 := bytesutil.ToBytes32(blockHash)
|
|
block := ðpb.BeaconBlock{
|
|
Slot: slot,
|
|
ParentRoot: parentRoot[:],
|
|
Body: ðpb.BeaconBlockBody{
|
|
Eth1Data: ðpb.Eth1Data{
|
|
DepositCount: uint64(len(deposits)),
|
|
DepositRoot: depRoot32[:],
|
|
BlockHash: blockHash32[:],
|
|
},
|
|
RandaoReveal: randaoReveal[:],
|
|
Attestations: nil,
|
|
},
|
|
}
|
|
|
|
stateRootCandidate, err := state.ExecuteStateTransitionNoVerify(context.Background(), beaconState, block)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
stateRoot, err = ssz.HashTreeRoot(stateRootCandidate)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
block.StateRoot = stateRoot[:]
|
|
|
|
block, err = testutil.SignBlock(beaconState, block, privKeys)
|
|
if err != nil {
|
|
t.Error(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")
|
|
}
|