diff --git a/beacon-chain/forkchoice/doubly-linked-tree/BUILD.bazel b/beacon-chain/forkchoice/doubly-linked-tree/BUILD.bazel index 405223f2547..c56262852b0 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/BUILD.bazel +++ b/beacon-chain/forkchoice/doubly-linked-tree/BUILD.bazel @@ -49,6 +49,7 @@ go_library( go_test( name = "go_default_test", srcs = [ + "epbs_test.go", "ffg_update_test.go", "forkchoice_test.go", "last_root_test.go", diff --git a/beacon-chain/forkchoice/doubly-linked-tree/epbs_test.go b/beacon-chain/forkchoice/doubly-linked-tree/epbs_test.go new file mode 100644 index 00000000000..8358a1611fd --- /dev/null +++ b/beacon-chain/forkchoice/doubly-linked-tree/epbs_test.go @@ -0,0 +1,27 @@ +package doublylinkedtree + +import ( + "context" + "testing" + + "github.com/prysmaticlabs/prysm/v5/config/params" + "github.com/prysmaticlabs/prysm/v5/testing/require" +) + +func TestFullInsertionPreEPBS(t *testing.T) { + ctx := context.Background() + f := setup(1, 1) + require.Equal(t, 1, f.NodeCount()) + + payloadHash := [32]byte{'A'} + state, blk, err := prepareForkchoiceState(ctx, f, 1, [32]byte{'a'}, params.BeaconConfig().ZeroHash, payloadHash, 1, 1) + require.NoError(t, err) + require.NoError(t, f.InsertNode(ctx, state, blk)) + node, ok := f.store.emptyNodeByRoot[blk.Root()] + require.Equal(t, true, ok) + require.Equal(t, blk.Root(), node.block.root) + + fullNode, ok := f.store.fullNodeByPayload[payloadHash] + require.Equal(t, true, ok) + require.Equal(t, payloadHash, fullNode.block.payloadHash) +} diff --git a/beacon-chain/forkchoice/doubly-linked-tree/ffg_update_test.go b/beacon-chain/forkchoice/doubly-linked-tree/ffg_update_test.go index bbed0d5b23f..830e19e58b8 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/ffg_update_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/ffg_update_test.go @@ -29,13 +29,13 @@ func TestFFGUpdates_OneBranch(t *testing.T) { // 2 <- justified: 1, finalized: 0 // | // 3 <- justified: 2, finalized: 1 - state, blkRoot, err := prepareForkchoiceState(context.Background(), 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(context.Background(), f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 2, 1) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 2, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -101,35 +101,35 @@ func TestFFGUpdates_TwoBranches(t *testing.T) { // | | // justified: 2, finalized: 0 -> 9 10 <- justified: 2, finalized: 0 // Left branch. - state, blkRoot, err := prepareForkchoiceState(context.Background(), 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(context.Background(), f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 2, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 2, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 3, indexToHash(5), indexToHash(3), params.BeaconConfig().ZeroHash, 1, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 3, indexToHash(5), indexToHash(3), params.BeaconConfig().ZeroHash, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 4, indexToHash(7), indexToHash(5), params.BeaconConfig().ZeroHash, 1, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 4, indexToHash(7), indexToHash(5), params.BeaconConfig().ZeroHash, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 4, indexToHash(9), indexToHash(7), params.BeaconConfig().ZeroHash, 2, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 4, indexToHash(9), indexToHash(7), params.BeaconConfig().ZeroHash, 2, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // Right branch. - state, blkRoot, err = prepareForkchoiceState(context.Background(), 1, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 1, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 2, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 2, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 3, indexToHash(6), indexToHash(4), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 3, indexToHash(6), indexToHash(4), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 4, indexToHash(8), indexToHash(6), params.BeaconConfig().ZeroHash, 1, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 4, indexToHash(8), indexToHash(6), params.BeaconConfig().ZeroHash, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 4, indexToHash(10), indexToHash(8), params.BeaconConfig().ZeroHash, 2, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 4, indexToHash(10), indexToHash(8), params.BeaconConfig().ZeroHash, 2, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -220,7 +220,7 @@ func setup(justifiedEpoch, finalizedEpoch primitives.Epoch) *ForkChoice { f := New() f.store.justifiedCheckpoint = &forkchoicetypes.Checkpoint{Epoch: justifiedEpoch, Root: params.BeaconConfig().ZeroHash} f.store.finalizedCheckpoint = &forkchoicetypes.Checkpoint{Epoch: finalizedEpoch, Root: params.BeaconConfig().ZeroHash} - state, blkRoot, err := prepareForkchoiceState(ctx, 0, params.BeaconConfig().ZeroHash, [32]byte{}, params.BeaconConfig().ZeroHash, justifiedEpoch, finalizedEpoch) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 0, params.BeaconConfig().ZeroHash, [32]byte{}, params.BeaconConfig().ZeroHash, justifiedEpoch, finalizedEpoch) if err != nil { return nil } diff --git a/beacon-chain/forkchoice/doubly-linked-tree/forkchoice.go b/beacon-chain/forkchoice/doubly-linked-tree/forkchoice.go index c2e99e911ce..c8878c31a58 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/forkchoice.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/forkchoice.go @@ -136,12 +136,12 @@ func (f *ForkChoice) InsertNode(ctx context.Context, state state.BeaconState, ro if roblock.Version() >= version.EPBS { return nil } - if roblock.Version() > version.Bellatrix { + if roblock.Version() >= version.Bellatrix { e, err := roblock.Block().Body().Execution() if err != nil { return errors.Wrap(err, "could not get execution data") } - return f.insertExecutionPayload(node, e) + f.insertExecutionPayload(node, e) } return nil } @@ -180,11 +180,16 @@ func (f *ForkChoice) HasHash(hash [32]byte) bool { } // IsCanonical returns true if the given root is part of the canonical chain. +// TODO: change the API to pass also full/empty func (f *ForkChoice) IsCanonical(root [32]byte) bool { node, ok := f.store.emptyNodeByRoot[root] if !ok || node == nil { return false } + fullNode, ok := f.store.fullNodeByPayload[node.block.payloadHash] + if ok { + node = fullNode + } if node.bestDescendant == nil { if f.store.headNode.bestDescendant == nil { diff --git a/beacon-chain/forkchoice/doubly-linked-tree/forkchoice_test.go b/beacon-chain/forkchoice/doubly-linked-tree/forkchoice_test.go index a6385dab0c6..8bc57028c9d 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/forkchoice_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/forkchoice_test.go @@ -28,6 +28,7 @@ import ( // bellatrix, it cannot be used for ePBS tests. func prepareForkchoiceState( _ context.Context, + f *ForkChoice, slot primitives.Slot, blockRoot [32]byte, parentRoot [32]byte, @@ -50,7 +51,11 @@ func prepareForkchoiceState( finalizedCheckpoint := ðpb.Checkpoint{ Epoch: finalizedEpoch, } - + var parentHash [32]byte + parentNode, ok := f.store.emptyNodeByRoot[parentRoot] + if ok { + parentHash = parentNode.block.payloadHash + } base := ðpb.BeaconStateBellatrix{ Slot: slot, RandaoMixes: make([][]byte, params.BeaconConfig().EpochsPerHistoricalVector), @@ -70,7 +75,8 @@ func prepareForkchoiceState( ParentRoot: parentRoot[:], Body: ðpb.BeaconBlockBodyBellatrix{ ExecutionPayload: &enginev1.ExecutionPayload{ - BlockHash: payloadHash[:], + BlockHash: payloadHash[:], + ParentHash: parentHash[:], }, }, }, @@ -94,13 +100,13 @@ func setAllBits(bv bitfield.Bitvector512) bitfield.Bitvector512 { func TestForkChoice_UpdateBalancesPositiveChange(t *testing.T) { f := setup(0, 0) ctx := context.Background() - st, roblock, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + st, roblock, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) + st, roblock, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) + st, roblock, err = prepareForkchoiceState(ctx, f, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) @@ -123,13 +129,13 @@ func TestForkChoice_UpdateBalancesPositiveChange(t *testing.T) { func TestForkChoice_UpdateBalancesNegativeChange(t *testing.T) { f := setup(0, 0) ctx := context.Background() - st, roblock, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + st, roblock, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) + st, roblock, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) + st, roblock, err = prepareForkchoiceState(ctx, f, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) s := f.store @@ -154,13 +160,13 @@ func TestForkChoice_UpdateBalancesNegativeChange(t *testing.T) { func TestForkChoice_UpdateBalancesUnderflow(t *testing.T) { f := setup(0, 0) ctx := context.Background() - st, roblock, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + st, roblock, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) + st, roblock, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) + st, roblock, err = prepareForkchoiceState(ctx, f, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) s := f.store @@ -185,22 +191,22 @@ func TestForkChoice_UpdateBalancesUnderflow(t *testing.T) { func TestForkChoice_IsCanonical(t *testing.T) { f := setup(1, 1) ctx := context.Background() - st, roblock, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 2, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 3, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 3, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 4, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 4, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 5, indexToHash(5), indexToHash(4), params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 5, indexToHash(5), indexToHash(4), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 6, indexToHash(6), indexToHash(5), params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 6, indexToHash(6), indexToHash(5), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) @@ -216,22 +222,22 @@ func TestForkChoice_IsCanonical(t *testing.T) { func TestForkChoice_IsCanonicalReorg(t *testing.T) { f := setup(1, 1) ctx := context.Background() - st, roblock, err := prepareForkchoiceState(ctx, 1, [32]byte{'1'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err := prepareForkchoiceState(ctx, f, 1, [32]byte{'1'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 2, [32]byte{'2'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 2, [32]byte{'2'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 3, [32]byte{'3'}, [32]byte{'1'}, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 3, [32]byte{'3'}, [32]byte{'1'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 4, [32]byte{'4'}, [32]byte{'2'}, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 4, [32]byte{'4'}, [32]byte{'2'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 5, [32]byte{'5'}, [32]byte{'4'}, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 5, [32]byte{'5'}, [32]byte{'4'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 6, [32]byte{'6'}, [32]byte{'5'}, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 6, [32]byte{'6'}, [32]byte{'5'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) @@ -262,13 +268,13 @@ func TestForkChoice_IsCanonicalReorg(t *testing.T) { func TestForkChoice_AncestorRoot(t *testing.T) { f := setup(1, 1) ctx := context.Background() - st, roblock, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 5, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 5, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) f.store.treeRootNode = f.store.emptyNodeByRoot[indexToHash(1)] @@ -294,10 +300,10 @@ func TestForkChoice_AncestorRoot(t *testing.T) { func TestForkChoice_AncestorEqualSlot(t *testing.T) { f := setup(1, 1) ctx := context.Background() - st, roblock, err := prepareForkchoiceState(ctx, 100, [32]byte{'1'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err := prepareForkchoiceState(ctx, f, 100, [32]byte{'1'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 101, [32]byte{'3'}, [32]byte{'1'}, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'3'}, [32]byte{'1'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) @@ -309,10 +315,10 @@ func TestForkChoice_AncestorEqualSlot(t *testing.T) { func TestForkChoice_AncestorLowerSlot(t *testing.T) { f := setup(1, 1) ctx := context.Background() - st, roblock, err := prepareForkchoiceState(ctx, 100, [32]byte{'1'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err := prepareForkchoiceState(ctx, f, 100, [32]byte{'1'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 200, [32]byte{'3'}, [32]byte{'1'}, params.BeaconConfig().ZeroHash, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 200, [32]byte{'3'}, [32]byte{'1'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) @@ -325,7 +331,7 @@ func TestForkChoice_RemoveEquivocating(t *testing.T) { ctx := context.Background() f := setup(1, 1) // Insert a block it will be head - st, roblock, err := prepareForkchoiceState(ctx, 1, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + st, roblock, err := prepareForkchoiceState(ctx, f, 1, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) head, err := f.Head(ctx) @@ -333,10 +339,10 @@ func TestForkChoice_RemoveEquivocating(t *testing.T) { require.Equal(t, [32]byte{'a'}, head) // Insert two extra blocks - st, roblock, err = prepareForkchoiceState(ctx, 2, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 2, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 3, [32]byte{'c'}, [32]byte{'a'}, [32]byte{'C'}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 3, [32]byte{'c'}, [32]byte{'a'}, [32]byte{'C'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) head, err = f.Head(ctx) @@ -407,34 +413,34 @@ func TestStore_CommonAncestor(t *testing.T) { // \-- c -- f // \-- g // \ -- h -- i -- j - st, roblock, err := prepareForkchoiceState(ctx, 0, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + st, roblock, err := prepareForkchoiceState(ctx, f, 0, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 1, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 1, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 2, [32]byte{'c'}, [32]byte{'a'}, [32]byte{'C'}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 2, [32]byte{'c'}, [32]byte{'a'}, [32]byte{'C'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 3, [32]byte{'d'}, [32]byte{'b'}, [32]byte{}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 3, [32]byte{'d'}, [32]byte{'b'}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 4, [32]byte{'e'}, [32]byte{'d'}, [32]byte{}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 4, [32]byte{'e'}, [32]byte{'d'}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 5, [32]byte{'f'}, [32]byte{'c'}, [32]byte{}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 5, [32]byte{'f'}, [32]byte{'c'}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 6, [32]byte{'g'}, [32]byte{'c'}, [32]byte{}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 6, [32]byte{'g'}, [32]byte{'c'}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 7, [32]byte{'h'}, [32]byte{'c'}, [32]byte{}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 7, [32]byte{'h'}, [32]byte{'c'}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 8, [32]byte{'i'}, [32]byte{'h'}, [32]byte{}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 8, [32]byte{'i'}, [32]byte{'h'}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 9, [32]byte{'j'}, [32]byte{'i'}, [32]byte{}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 9, [32]byte{'j'}, [32]byte{'i'}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) @@ -527,16 +533,16 @@ func TestStore_CommonAncestor(t *testing.T) { // a -- b -- c -- d f = setup(0, 0) - st, roblock, err = prepareForkchoiceState(ctx, 0, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 0, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 1, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 1, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 2, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 2, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 3, [32]byte{'d'}, [32]byte{'c'}, [32]byte{}, 1, 1) + st, roblock, err = prepareForkchoiceState(ctx, f, 3, [32]byte{'d'}, [32]byte{'c'}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) tests = []struct { @@ -705,23 +711,23 @@ func TestForkChoice_UpdateCheckpoints(t *testing.T) { fcs.store.finalizedCheckpoint = tt.finalized fcs.store.genesisTime = uint64(time.Now().Unix()) - uint64(tt.currentSlot)*params.BeaconConfig().SecondsPerSlot - st, roblock, err := prepareForkchoiceState(ctx, 32, [32]byte{'f'}, + st, roblock, err := prepareForkchoiceState(ctx, fcs, 32, [32]byte{'f'}, [32]byte{}, [32]byte{}, tt.finalized.Epoch, tt.finalized.Epoch) require.NoError(t, err) require.NoError(t, fcs.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 64, [32]byte{'j'}, + st, roblock, err = prepareForkchoiceState(ctx, fcs, 64, [32]byte{'j'}, [32]byte{'f'}, [32]byte{}, tt.justified.Epoch, tt.finalized.Epoch) require.NoError(t, err) require.NoError(t, fcs.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 96, [32]byte{'b'}, + st, roblock, err = prepareForkchoiceState(ctx, fcs, 96, [32]byte{'b'}, [32]byte{'j'}, [32]byte{}, tt.newJustified.Epoch, tt.newFinalized.Epoch) require.NoError(t, err) require.NoError(t, fcs.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 96, [32]byte{'c'}, + st, roblock, err = prepareForkchoiceState(ctx, fcs, 96, [32]byte{'c'}, [32]byte{'f'}, [32]byte{}, tt.newJustified.Epoch, tt.newFinalized.Epoch) require.NoError(t, err) require.NoError(t, fcs.InsertNode(ctx, st, roblock)) - st, roblock, err = prepareForkchoiceState(ctx, 65, [32]byte{'h'}, + st, roblock, err = prepareForkchoiceState(ctx, fcs, 65, [32]byte{'h'}, [32]byte{'f'}, [32]byte{}, tt.newFinalized.Epoch, tt.newFinalized.Epoch) require.NoError(t, err) require.NoError(t, fcs.InsertNode(ctx, st, roblock)) @@ -750,7 +756,7 @@ func TestWeight(t *testing.T) { f := setup(0, 0) root := [32]byte{'a'} - st, roblock, err := prepareForkchoiceState(ctx, 0, root, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + st, roblock, err := prepareForkchoiceState(ctx, f, 0, root, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) @@ -782,7 +788,7 @@ func TestForkChoice_UnrealizedJustifiedPayloadBlockHash(t *testing.T) { ctx := context.Background() f := setup(0, 0) - st, roblock, err := prepareForkchoiceState(ctx, 0, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + st, roblock, err := prepareForkchoiceState(ctx, f, 0, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, roblock)) @@ -795,7 +801,7 @@ func TestForkChoiceIsViableForCheckpoint(t *testing.T) { f := setup(0, 0) ctx := context.Background() - st, blk, err := prepareForkchoiceState(ctx, 0, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) + st, blk, err := prepareForkchoiceState(ctx, f, 0, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) require.NoError(t, err) // No Node viable, err := f.IsViableForCheckpoint(&forkchoicetypes.Checkpoint{Root: blk.Root()}) @@ -816,7 +822,7 @@ func TestForkChoiceIsViableForCheckpoint(t *testing.T) { require.NoError(t, err) require.Equal(t, true, viable) - st, blk2, err := prepareForkchoiceState(ctx, 1, [32]byte{'b'}, blk.Root(), [32]byte{'B'}, 0, 0) + st, blk2, err := prepareForkchoiceState(ctx, f, 1, [32]byte{'b'}, blk.Root(), [32]byte{'B'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk2)) @@ -838,7 +844,7 @@ func TestForkChoiceIsViableForCheckpoint(t *testing.T) { require.NoError(t, err) require.Equal(t, true, viable) - st, blk3, err := prepareForkchoiceState(ctx, 2, [32]byte{'c'}, blk2.Root(), [32]byte{'C'}, 0, 0) + st, blk3, err := prepareForkchoiceState(ctx, f, 2, [32]byte{'c'}, blk2.Root(), [32]byte{'C'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk3)) @@ -851,7 +857,7 @@ func TestForkChoiceIsViableForCheckpoint(t *testing.T) { require.NoError(t, err) require.Equal(t, false, viable) - st, blk4, err := prepareForkchoiceState(ctx, params.BeaconConfig().SlotsPerEpoch, [32]byte{'d'}, blk2.Root(), [32]byte{'D'}, 0, 0) + st, blk4, err := prepareForkchoiceState(ctx, f, params.BeaconConfig().SlotsPerEpoch, [32]byte{'d'}, blk2.Root(), [32]byte{'D'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk4)) @@ -874,7 +880,7 @@ func TestForkChoiceIsViableForCheckpoint(t *testing.T) { require.Equal(t, false, viable) // Children in next epoch - st, blk5, err := prepareForkchoiceState(ctx, params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'e'}, blk2.Root(), [32]byte{'E'}, 0, 0) + st, blk5, err := prepareForkchoiceState(ctx, f, params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'e'}, blk2.Root(), [32]byte{'E'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk5)) @@ -886,7 +892,7 @@ func TestForkChoiceIsViableForCheckpoint(t *testing.T) { func TestForkChoiceSlot(t *testing.T) { f := setup(0, 0) ctx := context.Background() - st, blk, err := prepareForkchoiceState(ctx, 3, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) + st, blk, err := prepareForkchoiceState(ctx, f, 3, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) require.NoError(t, err) // No Node _, err = f.Slot(blk.Root()) @@ -902,12 +908,12 @@ func TestForkchoiceParentRoot(t *testing.T) { f := setup(0, 0) ctx := context.Background() root1 := [32]byte{'a'} - st, blk, err := prepareForkchoiceState(ctx, 3, root1, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) + st, blk, err := prepareForkchoiceState(ctx, f, 3, root1, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk)) root2 := [32]byte{'b'} - st, blk, err = prepareForkchoiceState(ctx, 3, root2, root1, [32]byte{'A'}, 0, 0) + st, blk, err = prepareForkchoiceState(ctx, f, 3, root2, root1, [32]byte{'A'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk)) @@ -927,7 +933,7 @@ func TestForkchoiceParentRoot(t *testing.T) { func TestForkChoice_CleanupInserting(t *testing.T) { f := setup(0, 0) ctx := context.Background() - st, roblock, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 2, 2) + st, roblock, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 2, 2) f.SetBalancesByRooter(func(_ context.Context, _ [32]byte) ([]uint64, error) { return f.justifiedBalances, errors.New("mock err") }) diff --git a/beacon-chain/forkchoice/doubly-linked-tree/last_root_test.go b/beacon-chain/forkchoice/doubly-linked-tree/last_root_test.go index 767dcad85ee..9812d525ee5 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/last_root_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/last_root_test.go @@ -12,22 +12,22 @@ func TestLastRoot(t *testing.T) { f := setup(0, 0) ctx := context.Background() - st, root, err := prepareForkchoiceState(ctx, 1, [32]byte{'1'}, params.BeaconConfig().ZeroHash, [32]byte{'1'}, 0, 0) + st, root, err := prepareForkchoiceState(ctx, f, 1, [32]byte{'1'}, params.BeaconConfig().ZeroHash, [32]byte{'1'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, root)) - st, root, err = prepareForkchoiceState(ctx, 2, [32]byte{'2'}, [32]byte{'1'}, [32]byte{'2'}, 0, 0) + st, root, err = prepareForkchoiceState(ctx, f, 2, [32]byte{'2'}, [32]byte{'1'}, [32]byte{'2'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, root)) - st, root, err = prepareForkchoiceState(ctx, 3, [32]byte{'3'}, [32]byte{'1'}, [32]byte{'3'}, 0, 0) + st, root, err = prepareForkchoiceState(ctx, f, 3, [32]byte{'3'}, [32]byte{'1'}, [32]byte{'3'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, root)) - st, root, err = prepareForkchoiceState(ctx, 32, [32]byte{'4'}, [32]byte{'3'}, [32]byte{'4'}, 0, 0) + st, root, err = prepareForkchoiceState(ctx, f, 32, [32]byte{'4'}, [32]byte{'3'}, [32]byte{'4'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, root)) - st, root, err = prepareForkchoiceState(ctx, 33, [32]byte{'5'}, [32]byte{'2'}, [32]byte{'5'}, 0, 0) + st, root, err = prepareForkchoiceState(ctx, f, 33, [32]byte{'5'}, [32]byte{'2'}, [32]byte{'5'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, root)) - st, root, err = prepareForkchoiceState(ctx, 34, [32]byte{'6'}, [32]byte{'5'}, [32]byte{'6'}, 0, 0) + st, root, err = prepareForkchoiceState(ctx, f, 34, [32]byte{'6'}, [32]byte{'5'}, [32]byte{'6'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, root)) headNode, _ := f.store.emptyNodeByRoot[[32]byte{'6'}] diff --git a/beacon-chain/forkchoice/doubly-linked-tree/no_vote_test.go b/beacon-chain/forkchoice/doubly-linked-tree/no_vote_test.go index a35ce0447a1..e0fff86cefa 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/no_vote_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/no_vote_test.go @@ -25,7 +25,7 @@ func TestNoVote_CanFindHead(t *testing.T) { // 0 // / // 2 <- head - state, blkRoot, err := prepareForkchoiceState(context.Background(), 0, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err := prepareForkchoiceState(context.Background(), f, 0, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) r, err = f.Head(context.Background()) @@ -36,7 +36,7 @@ func TestNoVote_CanFindHead(t *testing.T) { // 0 // / \ // head -> 2 1 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) r, err = f.Head(context.Background()) @@ -49,7 +49,7 @@ func TestNoVote_CanFindHead(t *testing.T) { // head -> 2 1 // | // 3 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) r, err = f.Head(context.Background()) @@ -62,7 +62,7 @@ func TestNoVote_CanFindHead(t *testing.T) { // 2 1 // | | // head -> 4 3 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) r, err = f.Head(context.Background()) @@ -77,7 +77,7 @@ func TestNoVote_CanFindHead(t *testing.T) { // head -> 4 3 // | // 5 <- justified epoch = 2 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(5), indexToHash(4), params.BeaconConfig().ZeroHash, 2, 1) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(5), indexToHash(4), params.BeaconConfig().ZeroHash, 2, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) r, err = f.Head(context.Background()) @@ -94,7 +94,7 @@ func TestNoVote_CanFindHead(t *testing.T) { // 5 // | // 6 <- head - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(6), indexToHash(5), params.BeaconConfig().ZeroHash, 2, 1) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(6), indexToHash(5), params.BeaconConfig().ZeroHash, 2, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) r, err = f.Head(context.Background()) diff --git a/beacon-chain/forkchoice/doubly-linked-tree/node_test.go b/beacon-chain/forkchoice/doubly-linked-tree/node_test.go index 2151f99c972..62975374cd8 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/node_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/node_test.go @@ -14,13 +14,13 @@ import ( func TestNode_ApplyWeightChanges_PositiveChange(t *testing.T) { f := setup(0, 0) ctx := context.Background() - state, blk, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blk, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) + state, blk, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) + state, blk, err = prepareForkchoiceState(ctx, f, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) @@ -41,13 +41,13 @@ func TestNode_ApplyWeightChanges_PositiveChange(t *testing.T) { func TestNode_ApplyWeightChanges_NegativeChange(t *testing.T) { f := setup(0, 0) ctx := context.Background() - state, blk, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blk, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) + state, blk, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) + state, blk, err = prepareForkchoiceState(ctx, f, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) @@ -72,7 +72,7 @@ func TestNode_UpdateBestDescendant_NonViableChild(t *testing.T) { f := setup(1, 1) ctx := context.Background() // Input child is not viable. - state, blk, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 2, 3) + state, blk, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 2, 3) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) @@ -87,7 +87,7 @@ func TestNode_UpdateBestDescendant_ViableChild(t *testing.T) { f := setup(1, 1) ctx := context.Background() // Input child is the best descendant - state, blk, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) @@ -100,10 +100,10 @@ func TestNode_UpdateBestDescendant_HigherWeightChild(t *testing.T) { f := setup(1, 1) ctx := context.Background() // Input child is the best descendant - state, blk, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 2, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) @@ -120,10 +120,10 @@ func TestNode_UpdateBestDescendant_LowerWeightChild(t *testing.T) { f := setup(1, 1) ctx := context.Background() // Input child is the best descendant - state, blk, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 2, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) @@ -159,19 +159,19 @@ func TestNode_ViableForHead(t *testing.T) { func TestNode_LeadsToViableHead(t *testing.T) { f := setup(4, 3) ctx := context.Background() - state, blk, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 2, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 3, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 3, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 4, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 4, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) - state, blk, err = prepareForkchoiceState(ctx, 5, indexToHash(5), indexToHash(3), params.BeaconConfig().ZeroHash, 4, 3) + state, blk, err = prepareForkchoiceState(ctx, f, 5, indexToHash(5), indexToHash(3), params.BeaconConfig().ZeroHash, 4, 3) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) @@ -192,25 +192,25 @@ func TestNode_SetFullyValidated(t *testing.T) { // \ // -- 5 (true) // - state, blk, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) storeNodes[1] = f.store.emptyNodeByRoot[blk.Root()] require.NoError(t, f.SetOptimisticToValid(ctx, params.BeaconConfig().ZeroHash)) - state, blk, err = prepareForkchoiceState(ctx, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) storeNodes[2] = f.store.emptyNodeByRoot[blk.Root()] require.NoError(t, f.SetOptimisticToValid(ctx, indexToHash(1))) - state, blk, err = prepareForkchoiceState(ctx, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 3, indexToHash(3), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) storeNodes[3] = f.store.emptyNodeByRoot[blk.Root()] - state, blk, err = prepareForkchoiceState(ctx, 4, indexToHash(4), indexToHash(3), params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 4, indexToHash(4), indexToHash(3), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) storeNodes[4] = f.store.emptyNodeByRoot[blk.Root()] - state, blk, err = prepareForkchoiceState(ctx, 5, indexToHash(5), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 5, indexToHash(5), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) storeNodes[5] = f.store.emptyNodeByRoot[blk.Root()] @@ -265,7 +265,7 @@ func TestNode_TimeStampsChecks(t *testing.T) { driftGenesisTime(f, 1, 1) root := [32]byte{'a'} f.justifiedBalances = []uint64{10} - state, blk, err := prepareForkchoiceState(ctx, 1, root, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) + state, blk, err := prepareForkchoiceState(ctx, f, 1, root, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) headRoot, err := f.Head(ctx) @@ -282,7 +282,7 @@ func TestNode_TimeStampsChecks(t *testing.T) { // late block driftGenesisTime(f, 2, orphanLateBlockFirstThreshold+1) root = [32]byte{'b'} - state, blk, err = prepareForkchoiceState(ctx, 2, root, [32]byte{'a'}, [32]byte{'B'}, 0, 0) + state, blk, err = prepareForkchoiceState(ctx, f, 2, root, [32]byte{'a'}, [32]byte{'B'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) headRoot, err = f.Head(ctx) @@ -298,7 +298,7 @@ func TestNode_TimeStampsChecks(t *testing.T) { // very late block driftGenesisTime(f, 3, ProcessAttestationsThreshold+1) root = [32]byte{'c'} - state, blk, err = prepareForkchoiceState(ctx, 3, root, [32]byte{'b'}, [32]byte{'C'}, 0, 0) + state, blk, err = prepareForkchoiceState(ctx, f, 3, root, [32]byte{'b'}, [32]byte{'C'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) headRoot, err = f.Head(ctx) @@ -313,7 +313,7 @@ func TestNode_TimeStampsChecks(t *testing.T) { // block from the future root = [32]byte{'d'} - state, blk, err = prepareForkchoiceState(ctx, 5, root, [32]byte{'c'}, [32]byte{'D'}, 1, 1) + state, blk, err = prepareForkchoiceState(ctx, f, 5, root, [32]byte{'c'}, [32]byte{'D'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) headRoot, err = f.Head(ctx) diff --git a/beacon-chain/forkchoice/doubly-linked-tree/on_tick_test.go b/beacon-chain/forkchoice/doubly-linked-tree/on_tick_test.go index 36268df8bd7..0605fcde820 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/on_tick_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/on_tick_test.go @@ -59,19 +59,19 @@ func TestStore_NewSlot(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { f := setup(test.args.justified.Epoch, test.args.finalized.Epoch) - state, blkRoot, err := prepareForkchoiceState(ctx, 0, [32]byte{}, [32]byte{}, [32]byte{}, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 0, [32]byte{}, [32]byte{}, [32]byte{}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // genesis - state, blkRoot, err = prepareForkchoiceState(ctx, 32, [32]byte{'a'}, [32]byte{}, [32]byte{}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 32, [32]byte{'a'}, [32]byte{}, [32]byte{}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // finalized - state, blkRoot, err = prepareForkchoiceState(ctx, 64, [32]byte{'b'}, [32]byte{'a'}, [32]byte{}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 64, [32]byte{'b'}, [32]byte{'a'}, [32]byte{}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // justified - state, blkRoot, err = prepareForkchoiceState(ctx, 96, bj, [32]byte{'a'}, [32]byte{}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 96, bj, [32]byte{'a'}, [32]byte{}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // best justified - state, blkRoot, err = prepareForkchoiceState(ctx, 97, [32]byte{'d'}, [32]byte{}, [32]byte{}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 97, [32]byte{'d'}, [32]byte{}, [32]byte{}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // bad diff --git a/beacon-chain/forkchoice/doubly-linked-tree/optimistic_sync.go b/beacon-chain/forkchoice/doubly-linked-tree/optimistic_sync.go index 3f5da99549d..f138e942792 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/optimistic_sync.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/optimistic_sync.go @@ -20,7 +20,20 @@ func (s *Store) removeNodeIfSynced(ctx context.Context, node *Node) ([][32]byte, } func (s *Store) setOptimisticToInvalid(ctx context.Context, root, parentRoot, lastValidHash [32]byte) ([][32]byte, error) { - node := s.emptyNodeByRoot[root] + invalidRoots := make([][32]byte, 0) + node, ok := s.emptyNodeByRoot[root] + // Check if we have a full node for this root + if ok { + fullNode, ok := s.fullNodeByPayload[node.block.payloadHash] + if ok { + node = fullNode + } + // check consistency of the input + parent := node.block.parent + if parent.block.root != parentRoot { + return invalidRoots, errInvalidParentRoot + } + } // if the last valid hash is not known or null, prune only the incoming // block. lastValid, ok := s.fullNodeByPayload[lastValidHash] @@ -29,7 +42,6 @@ func (s *Store) setOptimisticToInvalid(ctx context.Context, root, parentRoot, la } // We have a valid hash, find if it's in the same fork as the last valid // root. - invalidRoots := make([][32]byte, 0) ancestor, err := s.ancestorRoot(ctx, parentRoot, lastValid.block.slot) if err != nil { return invalidRoots, errors.Wrap(err, "could not set block as invalid") @@ -118,12 +130,6 @@ func (s *Store) removeNodeAndChildren(ctx context.Context, node *Node, invalidRo s.previousProposerBoostRoot = params.BeaconConfig().ZeroHash s.previousProposerBoostScore = 0 } - if node.block.root == s.payloadWithholdBoostRoot { - s.payloadWithholdBoostRoot = [32]byte{} - } - if node.block.root == s.payloadRevealBoostRoot { - s.payloadRevealBoostRoot = [32]byte{} - } delete(s.emptyNodeByRoot, node.block.root) delete(s.fullNodeByPayload, node.block.payloadHash) return invalidRoots, nil diff --git a/beacon-chain/forkchoice/doubly-linked-tree/optimistic_sync_test.go b/beacon-chain/forkchoice/doubly-linked-tree/optimistic_sync_test.go index ab6848bdd21..e1d9fdc60e9 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/optimistic_sync_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/optimistic_sync_test.go @@ -6,10 +6,31 @@ import ( "testing" "github.com/prysmaticlabs/prysm/v5/config/params" - "github.com/prysmaticlabs/prysm/v5/encoding/bytesutil" "github.com/prysmaticlabs/prysm/v5/testing/require" ) +func sortRoots(roots [][32]uint8) { + sort.Slice(roots, func(i, j int) bool { + return roots[i][0] < roots[j][0] + }) +} + +func deduplicateRoots(roots [][32]uint8) [][32]uint8 { + if len(roots) == 0 { + return roots + } + unique := make(map[[32]uint8]struct{}) + var result [][32]uint8 + + for _, root := range roots { + if _, exists := unique[root]; !exists { + unique[root] = struct{}{} + result = append(result, root) + } + } + return result +} + // We test the algorithm to update a node from SYNCING to INVALID // We start with the same diagram as above: // @@ -175,47 +196,50 @@ func TestPruneInvalid(t *testing.T) { ctx := context.Background() f := setup(1, 1) - state, blkRoot, err := prepareForkchoiceState(ctx, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 102, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 102, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 102, [32]byte{'j'}, [32]byte{'b'}, [32]byte{'J'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 102, [32]byte{'j'}, [32]byte{'b'}, [32]byte{'J'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 103, [32]byte{'d'}, [32]byte{'c'}, [32]byte{'D'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 103, [32]byte{'d'}, [32]byte{'c'}, [32]byte{'D'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 104, [32]byte{'e'}, [32]byte{'d'}, [32]byte{'E'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 104, [32]byte{'e'}, [32]byte{'d'}, [32]byte{'E'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 104, [32]byte{'g'}, [32]byte{'d'}, [32]byte{'G'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 104, [32]byte{'g'}, [32]byte{'d'}, [32]byte{'G'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 105, [32]byte{'f'}, [32]byte{'e'}, [32]byte{'F'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 105, [32]byte{'f'}, [32]byte{'e'}, [32]byte{'F'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 105, [32]byte{'h'}, [32]byte{'g'}, [32]byte{'H'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 105, [32]byte{'h'}, [32]byte{'g'}, [32]byte{'H'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 105, [32]byte{'k'}, [32]byte{'g'}, [32]byte{'K'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 105, [32]byte{'k'}, [32]byte{'g'}, [32]byte{'K'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 106, [32]byte{'i'}, [32]byte{'h'}, [32]byte{'I'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 106, [32]byte{'i'}, [32]byte{'h'}, [32]byte{'I'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 106, [32]byte{'l'}, [32]byte{'k'}, [32]byte{'L'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 106, [32]byte{'l'}, [32]byte{'k'}, [32]byte{'L'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) roots, err := f.store.setOptimisticToInvalid(context.Background(), tc.root, tc.parentRoot, tc.payload) + deduped := deduplicateRoots(roots) if tc.wantedErr == nil { require.NoError(t, err) - require.DeepEqual(t, tc.wantedRoots, roots) + sortRoots(tc.wantedRoots) + sortRoots(deduped) + require.DeepEqual(t, tc.wantedRoots, deduped) require.Equal(t, tc.wantedNodeNumber, f.NodeCount()) } else { require.ErrorIs(t, tc.wantedErr, err) @@ -228,13 +252,13 @@ func TestSetOptimisticToInvalid_ProposerBoost(t *testing.T) { ctx := context.Background() f := setup(1, 1) - state, blkRoot, err := prepareForkchoiceState(ctx, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 101, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) f.store.proposerBoostRoot = [32]byte{'c'} @@ -260,129 +284,25 @@ func TestSetOptimisticToInvalid_CorrectChildren(t *testing.T) { ctx := context.Background() f := setup(1, 1) - state, blkRoot, err := prepareForkchoiceState(ctx, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 102, [32]byte{'c'}, [32]byte{'a'}, [32]byte{'C'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 102, [32]byte{'c'}, [32]byte{'a'}, [32]byte{'C'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 103, [32]byte{'d'}, [32]byte{'a'}, [32]byte{'D'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 103, [32]byte{'d'}, [32]byte{'a'}, [32]byte{'D'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) _, err = f.store.setOptimisticToInvalid(ctx, [32]byte{'d'}, [32]byte{'a'}, [32]byte{'A'}) require.NoError(t, err) - require.Equal(t, 2, len(f.store.emptyNodeByRoot[[32]byte{'a'}].children)) - -} - -// Pow | Pos -// -// CA -- A -- B -- C-----D -// \ \--------------E -// \ -// ----------------------F -- G -// -// B is INVALID -func TestSetOptimisticToInvalid_ForkAtMerge(t *testing.T) { - ctx := context.Background() - f := setup(1, 1) - - st, root, err := prepareForkchoiceState(ctx, 100, [32]byte{'r'}, [32]byte{}, [32]byte{}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 101, [32]byte{'a'}, [32]byte{'r'}, [32]byte{}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 102, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 103, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 104, [32]byte{'d'}, [32]byte{'c'}, [32]byte{'D'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 105, [32]byte{'e'}, [32]byte{'b'}, [32]byte{'E'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 106, [32]byte{'f'}, [32]byte{'r'}, [32]byte{'F'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) + // There are 5 valid children of A, two for B and C and one for D (the + // empty D block is valid) + require.Equal(t, 5, len(f.store.fullNodeByPayload[[32]byte{'A'}].children)) - st, root, err = prepareForkchoiceState(ctx, 107, [32]byte{'g'}, [32]byte{'f'}, [32]byte{'G'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - roots, err := f.SetOptimisticToInvalid(ctx, [32]byte{'x'}, [32]byte{'d'}, [32]byte{}) - require.NoError(t, err) - require.Equal(t, 4, len(roots)) - sort.Slice(roots, func(i, j int) bool { - return bytesutil.BytesToUint64BigEndian(roots[i][:]) < bytesutil.BytesToUint64BigEndian(roots[j][:]) - }) - require.DeepEqual(t, roots, [][32]byte{{'b'}, {'c'}, {'d'}, {'e'}}) -} - -// Pow | Pos -// -// CA -------- B -- C-----D -// \ \--------------E -// \ -// --A -------------------------F -- G -// -// B is INVALID -func TestSetOptimisticToInvalid_ForkAtMerge_bis(t *testing.T) { - ctx := context.Background() - f := setup(1, 1) - - st, root, err := prepareForkchoiceState(ctx, 100, [32]byte{'r'}, [32]byte{}, [32]byte{}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 101, [32]byte{'a'}, [32]byte{'r'}, [32]byte{}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 102, [32]byte{'b'}, [32]byte{}, [32]byte{'B'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 103, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 104, [32]byte{'d'}, [32]byte{'c'}, [32]byte{'D'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 105, [32]byte{'e'}, [32]byte{'b'}, [32]byte{'E'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 106, [32]byte{'f'}, [32]byte{'a'}, [32]byte{'F'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - st, root, err = prepareForkchoiceState(ctx, 107, [32]byte{'g'}, [32]byte{'f'}, [32]byte{'G'}, 1, 1) - require.NoError(t, err) - require.NoError(t, f.InsertNode(ctx, st, root)) - - roots, err := f.SetOptimisticToInvalid(ctx, [32]byte{'x'}, [32]byte{'d'}, [32]byte{}) - require.NoError(t, err) - require.Equal(t, 4, len(roots)) - sort.Slice(roots, func(i, j int) bool { - return bytesutil.BytesToUint64BigEndian(roots[i][:]) < bytesutil.BytesToUint64BigEndian(roots[j][:]) - }) - require.DeepEqual(t, roots, [][32]byte{{'b'}, {'c'}, {'d'}, {'e'}}) } func TestSetOptimisticToValid(t *testing.T) { diff --git a/beacon-chain/forkchoice/doubly-linked-tree/proposer_boost_test.go b/beacon-chain/forkchoice/doubly-linked-tree/proposer_boost_test.go index a90f6f0306b..f61f6bb4a63 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/proposer_boost_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/proposer_boost_test.go @@ -53,6 +53,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { f.store.proposerBoostRoot = [32]byte{} state, blkRoot, err := prepareForkchoiceState( ctx, + f, slot, newRoot, headRoot, @@ -81,6 +82,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { f.store.proposerBoostRoot = [32]byte{} state, blkRoot, err = prepareForkchoiceState( ctx, + f, slot, newRoot, headRoot, @@ -111,6 +113,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { f.store.proposerBoostRoot = [32]byte{} state, blkRoot, err = prepareForkchoiceState( ctx, + f, slot, newRoot, headRoot, @@ -142,6 +145,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { f.store.proposerBoostRoot = [32]byte{} state, blkRoot, err = prepareForkchoiceState( ctx, + f, slot, newRoot, indexToHash(2), @@ -212,6 +216,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { honestBlock := indexToHash(2) state, blkRoot, err := prepareForkchoiceState( ctx, + f, honestBlockSlot, honestBlock, zeroHash, @@ -229,6 +234,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { maliciouslyWithheldBlock := indexToHash(1) state, blkRoot, err = prepareForkchoiceState( ctx, + f, maliciouslyWithheldBlockSlot, maliciouslyWithheldBlock, zeroHash, @@ -282,6 +288,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { honestBlock := indexToHash(2) state, blkRoot, err := prepareForkchoiceState( ctx, + f, honestBlockSlot, honestBlock, zeroHash, @@ -301,6 +308,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { maliciouslyWithheldBlock := indexToHash(1) state, blkRoot, err = prepareForkchoiceState( ctx, + f, maliciouslyWithheldBlockSlot, maliciouslyWithheldBlock, zeroHash, @@ -359,6 +367,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { f.store.proposerBoostRoot = [32]byte{} state, blkRoot, err := prepareForkchoiceState( ctx, + f, cSlot, c, a, // parent @@ -379,6 +388,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { f.store.proposerBoostRoot = [32]byte{} state, blkRoot, err = prepareForkchoiceState( ctx, + f, bSlot, b, a, // parent @@ -406,6 +416,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { f.store.proposerBoostRoot = [32]byte{} state, blkRoot, err = prepareForkchoiceState( ctx, + f, dSlot, d, b, // parent @@ -427,6 +438,7 @@ func TestForkChoice_BoostProposerRoot_PreventsExAnteAttack(t *testing.T) { f.store.proposerBoostRoot = [32]byte{} state, blkRoot, err = prepareForkchoiceState( ctx, + f, dSlot, d2, b, // parent @@ -458,7 +470,7 @@ func TestForkChoice_BoostProposerRoot(t *testing.T) { slot := primitives.Slot(0) currentSlot := primitives.Slot(1) driftGenesisTime(f, currentSlot, 0) - state, blkRoot, err := prepareForkchoiceState(ctx, slot, root, zeroHash, zeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, slot, root, zeroHash, zeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.Equal(t, [32]byte{}, f.store.proposerBoostRoot) @@ -468,7 +480,7 @@ func TestForkChoice_BoostProposerRoot(t *testing.T) { slot := primitives.Slot(1) currentSlot := primitives.Slot(1) driftGenesisTime(f, currentSlot, params.BeaconConfig().SecondsPerSlot-1) - state, blkRoot, err := prepareForkchoiceState(ctx, slot, root, zeroHash, zeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, slot, root, zeroHash, zeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.Equal(t, [32]byte{}, f.store.proposerBoostRoot) @@ -478,7 +490,7 @@ func TestForkChoice_BoostProposerRoot(t *testing.T) { slot := primitives.Slot(1) currentSlot := primitives.Slot(1) driftGenesisTime(f, currentSlot, 0) - state, blkRoot, err := prepareForkchoiceState(ctx, slot, root, zeroHash, zeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, slot, root, zeroHash, zeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.Equal(t, root, f.store.proposerBoostRoot) @@ -488,7 +500,7 @@ func TestForkChoice_BoostProposerRoot(t *testing.T) { slot := primitives.Slot(1) currentSlot := primitives.Slot(1) driftGenesisTime(f, currentSlot, 1) - state, blkRoot, err := prepareForkchoiceState(ctx, slot, root, zeroHash, zeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, slot, root, zeroHash, zeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.Equal(t, root, f.store.proposerBoostRoot) @@ -505,7 +517,7 @@ func TestForkChoice_missingProposerBoostRoots(t *testing.T) { } f.justifiedBalances = balances driftGenesisTime(f, 1, 0) - st, blk, err := prepareForkchoiceState(ctx, 1, [32]byte{'r'}, [32]byte{}, [32]byte{}, 1, 1) + st, blk, err := prepareForkchoiceState(ctx, f, 1, [32]byte{'r'}, [32]byte{}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk)) @@ -517,7 +529,7 @@ func TestForkChoice_missingProposerBoostRoots(t *testing.T) { f.store.proposerBoostRoot = [32]byte{'p'} driftGenesisTime(f, 3, 0) - st, blk, err = prepareForkchoiceState(ctx, 2, [32]byte{'a'}, [32]byte{'r'}, [32]byte{}, 1, 1) + st, blk, err = prepareForkchoiceState(ctx, f, 2, [32]byte{'a'}, [32]byte{'r'}, [32]byte{}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk)) headRoot, err = f.Head(ctx) diff --git a/beacon-chain/forkchoice/doubly-linked-tree/reorg_late_blocks_test.go b/beacon-chain/forkchoice/doubly-linked-tree/reorg_late_blocks_test.go index adfbfdf0bce..57e6ceb5cb0 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/reorg_late_blocks_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/reorg_late_blocks_test.go @@ -19,7 +19,7 @@ func TestForkChoice_ShouldOverrideFCU(t *testing.T) { f.store.committeeWeight /= uint64(params.BeaconConfig().SlotsPerEpoch) ctx := context.Background() driftGenesisTime(f, 1, 0) - st, blk, err := prepareForkchoiceState(ctx, 1, [32]byte{'a'}, [32]byte{}, [32]byte{'A'}, 0, 0) + st, blk, err := prepareForkchoiceState(ctx, f, 1, [32]byte{'a'}, [32]byte{}, [32]byte{'A'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk)) attesters := make([]uint64, f.numActiveValidators-64) @@ -30,7 +30,7 @@ func TestForkChoice_ShouldOverrideFCU(t *testing.T) { orphanLateBlockFirstThreshold := params.BeaconConfig().SecondsPerSlot / params.BeaconConfig().IntervalsPerSlot driftGenesisTime(f, 2, orphanLateBlockFirstThreshold+1) - st, blk, err = prepareForkchoiceState(ctx, 2, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 0, 0) + st, blk, err = prepareForkchoiceState(ctx, f, 2, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk)) headRoot, err := f.Head(ctx) @@ -117,7 +117,7 @@ func TestForkChoice_GetProposerHead(t *testing.T) { ctx := context.Background() driftGenesisTime(f, 1, 0) parentRoot := [32]byte{'a'} - st, blk, err := prepareForkchoiceState(ctx, 1, parentRoot, [32]byte{}, [32]byte{'A'}, 0, 0) + st, blk, err := prepareForkchoiceState(ctx, f, 1, parentRoot, [32]byte{}, [32]byte{'A'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk)) attesters := make([]uint64, f.numActiveValidators-64) @@ -128,7 +128,7 @@ func TestForkChoice_GetProposerHead(t *testing.T) { driftGenesisTime(f, 3, 1) childRoot := [32]byte{'b'} - st, blk, err = prepareForkchoiceState(ctx, 2, childRoot, [32]byte{'a'}, [32]byte{'B'}, 0, 0) + st, blk, err = prepareForkchoiceState(ctx, f, 2, childRoot, [32]byte{'a'}, [32]byte{'B'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, st, blk)) headRoot, err := f.Head(ctx) diff --git a/beacon-chain/forkchoice/doubly-linked-tree/store.go b/beacon-chain/forkchoice/doubly-linked-tree/store.go index 62774a583b4..a834dcf7a35 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/store.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/store.go @@ -62,6 +62,47 @@ func (s *Store) head(ctx context.Context) ([32]byte, error) { return bestDescendant.block.root, nil } +func (s *Store) checkProposerBoost(block *BlockNode) { + timeNow := uint64(time.Now().Unix()) + if timeNow < s.genesisTime { + return + } + secondsIntoSlot := (timeNow - s.genesisTime) % params.BeaconConfig().SecondsPerSlot + currentSlot := slots.CurrentSlot(s.genesisTime) + boostThreshold := params.BeaconConfig().SecondsPerSlot / params.BeaconConfig().IntervalsPerSlot + isFirstBlock := s.proposerBoostRoot == [32]byte{} + if currentSlot == block.slot && secondsIntoSlot < boostThreshold && isFirstBlock { + s.proposerBoostRoot = block.root + } +} + +// insertChild perform the insertion tasks when the parent node was already in +// forkchoice, this is +// - add a child to the parent +// - apply proposer boost +// - update best descendants. +func (s *Store) insertChild(ctx context.Context, parent *Node, child *Node) error { + if parent == nil { + return nil + } + parent.children = append(parent.children, child) + if !child.full { + s.checkProposerBoost(child.block) + } + // Update best descendants + jEpoch := s.justifiedCheckpoint.Epoch + fEpoch := s.finalizedCheckpoint.Epoch + currentSlot := slots.CurrentSlot(s.genesisTime) + if err := s.treeRootNode.updateBestDescendant(ctx, jEpoch, fEpoch, slots.ToEpoch(currentSlot)); err != nil { + _, remErr := s.removeNode(ctx, child) + if remErr != nil { + log.WithError(remErr).Error("could not remove node") + } + return errors.Wrap(err, "could not update best descendants") + } + return nil +} + // insert registers a new block node to the fork choice store's node list. // It then updates the new node's parent with the best child and descendant node. func (s *Store) insert(ctx context.Context, @@ -144,32 +185,10 @@ func (s *Store) insert(ctx context.Context, } } s.emptyNodeByRoot[root] = emptyNode - if parent != nil { - parent.children = append(parent.children, emptyNode) - // Apply proposer boost - timeNow := uint64(time.Now().Unix()) - if timeNow < s.genesisTime { - return innerBlock, nil - } - secondsIntoSlot := (timeNow - s.genesisTime) % params.BeaconConfig().SecondsPerSlot - currentSlot := slots.CurrentSlot(s.genesisTime) - boostThreshold := params.BeaconConfig().SecondsPerSlot / params.BeaconConfig().IntervalsPerSlot - isFirstBlock := s.proposerBoostRoot == [32]byte{} - if currentSlot == slot && secondsIntoSlot < boostThreshold && isFirstBlock { - s.proposerBoostRoot = root - } - - // Update best descendants - jEpoch := s.justifiedCheckpoint.Epoch - fEpoch := s.finalizedCheckpoint.Epoch - if err := s.treeRootNode.updateBestDescendant(ctx, jEpoch, fEpoch, slots.ToEpoch(currentSlot)); err != nil { - _, remErr := s.removeNode(ctx, n) - if remErr != nil { - log.WithError(remErr).Error("could not remove node") - } - return nil, errors.Wrap(err, "could not update best descendants") - } + if err := s.insertChild(ctx, parent, n); err != nil { + return nil, err } + // Update metrics. processedBlockCount.Inc() nodeCount.Set(float64(len(s.emptyNodeByRoot))) diff --git a/beacon-chain/forkchoice/doubly-linked-tree/store_test.go b/beacon-chain/forkchoice/doubly-linked-tree/store_test.go index 2cf3d09a4f1..17719cea8bb 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/store_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/store_test.go @@ -26,7 +26,7 @@ func TestStore_FinalizedEpoch(t *testing.T) { func TestStore_NodeCount(t *testing.T) { f := setup(0, 0) - state, blk, err := prepareForkchoiceState(context.Background(), 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blk, err := prepareForkchoiceState(context.Background(), f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(context.Background(), state, blk)) require.Equal(t, 2, f.NodeCount()) @@ -35,10 +35,10 @@ func TestStore_NodeCount(t *testing.T) { func TestStore_NodeByRoot(t *testing.T) { f := setup(0, 0) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(context.Background(), 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(context.Background(), f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) node0 := f.store.treeRootNode @@ -62,7 +62,7 @@ func TestStore_NodeByRoot(t *testing.T) { func TestForkChoice_HasNode(t *testing.T) { f := setup(0, 0) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(context.Background(), 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(context.Background(), f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.Equal(t, true, f.HasNode(indexToHash(1))) @@ -78,7 +78,7 @@ func TestStore_Head_UnknownJustifiedRoot(t *testing.T) { func TestStore_Head_Itself(t *testing.T) { f := setup(0, 0) - state, blkRoot, err := prepareForkchoiceState(context.Background(), 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(context.Background(), f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(context.Background(), state, blkRoot)) @@ -93,16 +93,16 @@ func TestStore_Head_Itself(t *testing.T) { func TestStore_Head_BestDescendant(t *testing.T) { f := setup(0, 0) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(context.Background(), 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(context.Background(), f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 3, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 3, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 4, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 4, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) f.store.justifiedCheckpoint = &forkchoicetypes.Checkpoint{Epoch: 0, Root: indexToHash(1)} @@ -114,11 +114,11 @@ func TestStore_Head_BestDescendant(t *testing.T) { func TestStore_UpdateBestDescendant_ContextCancelled(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) f := setup(0, 0) - state, blkRoot, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) cancel() - state, blkRoot, err = prepareForkchoiceState(ctx, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), indexToHash(1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) err = f.InsertNode(ctx, state, blkRoot) require.ErrorContains(t, "context canceled", err) @@ -131,10 +131,12 @@ func TestStore_Insert(t *testing.T) { nodeByPayload := map[[32]byte]*Node{indexToHash(0): treeRootNode} jc := &forkchoicetypes.Checkpoint{Epoch: 0} fc := &forkchoicetypes.Checkpoint{Epoch: 0} + fcs := setup(0, 0) s := &Store{emptyNodeByRoot: nodeByRoot, treeRootNode: treeRootNode, fullNodeByPayload: nodeByPayload, justifiedCheckpoint: jc, finalizedCheckpoint: fc} + fcs.store = s payloadHash := [32]byte{'a'} ctx := context.Background() - _, blk, err := prepareForkchoiceState(ctx, 100, indexToHash(100), indexToHash(0), payloadHash, 1, 1) + _, blk, err := prepareForkchoiceState(ctx, fcs, 100, indexToHash(100), indexToHash(0), payloadHash, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -153,11 +155,11 @@ func TestStore_Prune_MoreThanThreshold(t *testing.T) { numOfNodes := uint64(100) f := setup(0, 0) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) for i := uint64(2); i < numOfNodes; i++ { - state, blkRoot, err = prepareForkchoiceState(ctx, primitives.Slot(i), indexToHash(i), indexToHash(i-1), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, primitives.Slot(i), indexToHash(i), indexToHash(i-1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) } @@ -175,11 +177,11 @@ func TestStore_Prune_MoreThanOnce(t *testing.T) { numOfNodes := uint64(100) f := setup(0, 0) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) for i := uint64(2); i < numOfNodes; i++ { - state, blkRoot, err = prepareForkchoiceState(ctx, primitives.Slot(i), indexToHash(i), indexToHash(i-1), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, primitives.Slot(i), indexToHash(i), indexToHash(i-1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) } @@ -202,11 +204,11 @@ func TestStore_Prune_ReturnEarly(t *testing.T) { numOfNodes := uint64(100) f := setup(0, 0) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) for i := uint64(2); i < numOfNodes; i++ { - state, blkRoot, err = prepareForkchoiceState(ctx, primitives.Slot(i), indexToHash(i), indexToHash(i-1), params.BeaconConfig().ZeroHash, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, primitives.Slot(i), indexToHash(i), indexToHash(i-1), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) } @@ -227,10 +229,10 @@ func TestStore_Prune_ReturnEarly(t *testing.T) { func TestStore_Prune_NoDanglingBranch(t *testing.T) { f := setup(0, 0) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, [32]byte{'1'}, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, [32]byte{'1'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 2, indexToHash(2), params.BeaconConfig().ZeroHash, [32]byte{'2'}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), params.BeaconConfig().ZeroHash, [32]byte{'2'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -255,40 +257,40 @@ func TestStore_tips(t *testing.T) { ctx := context.Background() f := setup(1, 1) - state, blkRoot, err := prepareForkchoiceState(ctx, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 101, [32]byte{'b'}, [32]byte{'a'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'b'}, [32]byte{'a'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 102, [32]byte{'c'}, [32]byte{'b'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 102, [32]byte{'c'}, [32]byte{'b'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 102, [32]byte{'j'}, [32]byte{'b'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 102, [32]byte{'j'}, [32]byte{'b'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 103, [32]byte{'d'}, [32]byte{'c'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 103, [32]byte{'d'}, [32]byte{'c'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 104, [32]byte{'e'}, [32]byte{'d'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 104, [32]byte{'e'}, [32]byte{'d'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 104, [32]byte{'g'}, [32]byte{'d'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 104, [32]byte{'g'}, [32]byte{'d'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 105, [32]byte{'f'}, [32]byte{'e'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 105, [32]byte{'f'}, [32]byte{'e'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 105, [32]byte{'h'}, [32]byte{'g'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 105, [32]byte{'h'}, [32]byte{'g'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 105, [32]byte{'k'}, [32]byte{'g'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 105, [32]byte{'k'}, [32]byte{'g'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 106, [32]byte{'i'}, [32]byte{'h'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 106, [32]byte{'i'}, [32]byte{'h'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 106, [32]byte{'l'}, [32]byte{'k'}, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 106, [32]byte{'l'}, [32]byte{'k'}, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) expectedMap := map[[32]byte]primitives.Slot{ @@ -308,10 +310,10 @@ func TestStore_tips(t *testing.T) { func TestStore_PruneMapsNodes(t *testing.T) { f := setup(0, 0) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(ctx, 1, indexToHash(1), params.BeaconConfig().ZeroHash, [32]byte{'1'}, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), params.BeaconConfig().ZeroHash, [32]byte{'1'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 2, indexToHash(2), params.BeaconConfig().ZeroHash, [32]byte{'2'}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 2, indexToHash(2), params.BeaconConfig().ZeroHash, [32]byte{'2'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -333,7 +335,7 @@ func TestForkChoice_ReceivedBlocksLastEpoch(t *testing.T) { // Make sure it doesn't underflow s.genesisTime = uint64(time.Now().Add(time.Duration(-1*int64(params.BeaconConfig().SecondsPerSlot)) * time.Second).Unix()) ctx := context.Background() - _, blk, err := prepareForkchoiceState(ctx, 1, [32]byte{'a'}, b, b, 1, 1) + _, blk, err := prepareForkchoiceState(ctx, f, 1, [32]byte{'a'}, b, b, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -346,7 +348,7 @@ func TestForkChoice_ReceivedBlocksLastEpoch(t *testing.T) { // 64 // Received block last epoch is 1 - _, blk, err = prepareForkchoiceState(ctx, 64, [32]byte{'A'}, b, b, 1, 1) + _, blk, err = prepareForkchoiceState(ctx, f, 64, [32]byte{'A'}, b, b, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -360,7 +362,7 @@ func TestForkChoice_ReceivedBlocksLastEpoch(t *testing.T) { // 64 65 // Received block last epoch is 2 - _, blk, err = prepareForkchoiceState(ctx, 65, [32]byte{'B'}, b, b, 1, 1) + _, blk, err = prepareForkchoiceState(ctx, f, 65, [32]byte{'B'}, b, b, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -374,7 +376,7 @@ func TestForkChoice_ReceivedBlocksLastEpoch(t *testing.T) { // 64 65 66 // Received block last epoch is 3 - _, blk, err = prepareForkchoiceState(ctx, 66, [32]byte{'C'}, b, b, 1, 1) + _, blk, err = prepareForkchoiceState(ctx, f, 66, [32]byte{'C'}, b, b, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -388,7 +390,7 @@ func TestForkChoice_ReceivedBlocksLastEpoch(t *testing.T) { // 64 65 66 // 98 // Received block last epoch is 1 - _, blk, err = prepareForkchoiceState(ctx, 98, [32]byte{'D'}, b, b, 1, 1) + _, blk, err = prepareForkchoiceState(ctx, f, 98, [32]byte{'D'}, b, b, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -403,7 +405,7 @@ func TestForkChoice_ReceivedBlocksLastEpoch(t *testing.T) { // 98 // 132 // Received block last epoch is 1 - _, blk, err = prepareForkchoiceState(ctx, 132, [32]byte{'E'}, b, b, 1, 1) + _, blk, err = prepareForkchoiceState(ctx, f, 132, [32]byte{'E'}, b, b, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -419,7 +421,7 @@ func TestForkChoice_ReceivedBlocksLastEpoch(t *testing.T) { // 132 // 99 // Received block last epoch is still 1. 99 is outside the window - _, blk, err = prepareForkchoiceState(ctx, 99, [32]byte{'F'}, b, b, 1, 1) + _, blk, err = prepareForkchoiceState(ctx, f, 99, [32]byte{'F'}, b, b, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -435,7 +437,7 @@ func TestForkChoice_ReceivedBlocksLastEpoch(t *testing.T) { // 132 // 99 100 // Received block last epoch is still 1. 100 is at the same position as 132 - _, blk, err = prepareForkchoiceState(ctx, 100, [32]byte{'G'}, b, b, 1, 1) + _, blk, err = prepareForkchoiceState(ctx, f, 100, [32]byte{'G'}, b, b, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -451,7 +453,7 @@ func TestForkChoice_ReceivedBlocksLastEpoch(t *testing.T) { // 132 // 99 100 101 // Received block last epoch is 2. 101 is within the window - _, blk, err = prepareForkchoiceState(ctx, 101, [32]byte{'H'}, b, b, 1, 1) + _, blk, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'H'}, b, b, 1, 1) require.NoError(t, err) _, err = s.insert(ctx, blk, 1, 1) require.NoError(t, err) @@ -476,14 +478,14 @@ func TestStore_TargetRootForEpoch(t *testing.T) { ctx := context.Background() f := setup(1, 1) - state, blk, err := prepareForkchoiceState(ctx, params.BeaconConfig().SlotsPerEpoch, [32]byte{'a'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blk, err := prepareForkchoiceState(ctx, f, params.BeaconConfig().SlotsPerEpoch, [32]byte{'a'}, params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk)) target, err := f.TargetRootForEpoch(blk.Root(), 1) require.NoError(t, err) require.Equal(t, target, blk.Root()) - state, blk1, err := prepareForkchoiceState(ctx, params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'b'}, blk.Root(), params.BeaconConfig().ZeroHash, 1, 1) + state, blk1, err := prepareForkchoiceState(ctx, f, params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'b'}, blk.Root(), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk1)) target, err = f.TargetRootForEpoch(blk1.Root(), 1) @@ -492,14 +494,14 @@ func TestStore_TargetRootForEpoch(t *testing.T) { // Insert a block for the next epoch (missed slot 0) - state, blk2, err := prepareForkchoiceState(ctx, 2*params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'c'}, blk1.Root(), params.BeaconConfig().ZeroHash, 1, 1) + state, blk2, err := prepareForkchoiceState(ctx, f, 2*params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'c'}, blk1.Root(), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk2)) target, err = f.TargetRootForEpoch(blk2.Root(), 2) require.NoError(t, err) require.Equal(t, target, blk1.Root()) - state, blk3, err := prepareForkchoiceState(ctx, 2*params.BeaconConfig().SlotsPerEpoch+2, [32]byte{'d'}, blk2.Root(), params.BeaconConfig().ZeroHash, 1, 1) + state, blk3, err := prepareForkchoiceState(ctx, f, 2*params.BeaconConfig().SlotsPerEpoch+2, [32]byte{'d'}, blk2.Root(), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk3)) target, err = f.TargetRootForEpoch(blk2.Root(), 2) @@ -516,14 +518,14 @@ func TestStore_TargetRootForEpoch(t *testing.T) { // Insert a block for next epoch (slot 0 present) - state, blk4, err := prepareForkchoiceState(ctx, 3*params.BeaconConfig().SlotsPerEpoch, [32]byte{'e'}, blk1.Root(), params.BeaconConfig().ZeroHash, 1, 1) + state, blk4, err := prepareForkchoiceState(ctx, f, 3*params.BeaconConfig().SlotsPerEpoch, [32]byte{'e'}, blk1.Root(), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk4)) target, err = f.TargetRootForEpoch(blk4.Root(), 3) require.NoError(t, err) require.Equal(t, target, blk4.Root()) - state, blk5, err := prepareForkchoiceState(ctx, 3*params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'f'}, blk4.Root(), params.BeaconConfig().ZeroHash, 1, 1) + state, blk5, err := prepareForkchoiceState(ctx, f, 3*params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'f'}, blk4.Root(), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk5)) target, err = f.TargetRootForEpoch(blk5.Root(), 3) @@ -541,7 +543,7 @@ func TestStore_TargetRootForEpoch(t *testing.T) { require.Equal(t, blk1.Root(), target) // the parent of root4 in epoch 3 is root 1 in epoch 1 // Target root where the target is two epochs ago, slot 0 was missed - state, blk6, err := prepareForkchoiceState(ctx, 4*params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'g'}, blk5.Root(), params.BeaconConfig().ZeroHash, 1, 1) + state, blk6, err := prepareForkchoiceState(ctx, f, 4*params.BeaconConfig().SlotsPerEpoch+1, [32]byte{'g'}, blk5.Root(), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blk6)) target, err = f.TargetRootForEpoch(blk6.Root(), 4) @@ -562,7 +564,7 @@ func TestStore_TargetRootForEpoch(t *testing.T) { func TestStore_CleanupInserting(t *testing.T) { f := setup(0, 0) ctx := context.Background() - st, blk, err := prepareForkchoiceState(ctx, 1, indexToHash(1), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) + st, blk, err := prepareForkchoiceState(ctx, f, 1, indexToHash(1), indexToHash(2), params.BeaconConfig().ZeroHash, 0, 0) require.NoError(t, err) require.NotNil(t, f.InsertNode(ctx, st, blk)) require.Equal(t, false, f.HasNode(blk.Root())) diff --git a/beacon-chain/forkchoice/doubly-linked-tree/types.go b/beacon-chain/forkchoice/doubly-linked-tree/types.go index e8f470e3117..a5b8ea29e57 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/types.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/types.go @@ -41,9 +41,6 @@ type Store struct { highestReceivedNode *Node // The highest slot node. receivedBlocksLastEpoch [fieldparams.SlotsPerEpoch]primitives.Slot // Using `highestReceivedSlot`. The slot of blocks received in the last epoch. allTipsAreInvalid bool // tracks if all tips are not viable for head - payloadWithholdBoostRoot [fieldparams.RootLength]byte // the root of the block that receives the withhold boost - payloadWithholdBoostFull bool // Indicator of whether the block receiving the withhold boost is full or empty - payloadRevealBoostRoot [fieldparams.RootLength]byte // the root of the block that receives the reveal boost } // BlockNode defines the individual block which includes its block parent, ancestor and how much weight accounted for it. diff --git a/beacon-chain/forkchoice/doubly-linked-tree/unrealized_justification_test.go b/beacon-chain/forkchoice/doubly-linked-tree/unrealized_justification_test.go index 8b31543e945..8f9130a0821 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/unrealized_justification_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/unrealized_justification_test.go @@ -14,13 +14,13 @@ import ( func TestStore_SetUnrealizedEpochs(t *testing.T) { f := setup(1, 1) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(ctx, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 102, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 102, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.Equal(t, primitives.Epoch(1), f.store.emptyNodeByRoot[[32]byte{'b'}].block.unrealizedJustifiedEpoch) @@ -37,13 +37,13 @@ func TestStore_SetUnrealizedEpochs(t *testing.T) { func TestStore_UpdateUnrealizedCheckpoints(t *testing.T) { f := setup(1, 1) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(ctx, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 102, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 102, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -64,14 +64,14 @@ func TestStore_UpdateUnrealizedCheckpoints(t *testing.T) { func TestStore_LongFork(t *testing.T) { f := setup(1, 1) ctx := context.Background() - state, blkRoot, err := prepareForkchoiceState(ctx, 75, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 75, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 80, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 80, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.NoError(t, f.store.setUnrealizedJustifiedEpoch([32]byte{'b'}, 2)) - state, blkRoot, err = prepareForkchoiceState(ctx, 95, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 95, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.NoError(t, f.store.setUnrealizedJustifiedEpoch([32]byte{'c'}, 2)) @@ -88,7 +88,7 @@ func TestStore_LongFork(t *testing.T) { // c remains the head even if a block d with higher realized justification is seen ha := [32]byte{'a'} - state, blkRoot, err = prepareForkchoiceState(ctx, 103, [32]byte{'d'}, [32]byte{'b'}, [32]byte{'D'}, 2, 1) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 103, [32]byte{'d'}, [32]byte{'b'}, [32]byte{'D'}, 2, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.NoError(t, f.UpdateJustifiedCheckpoint(ctx, &forkchoicetypes.Checkpoint{Epoch: 2, Root: ha})) @@ -119,36 +119,36 @@ func TestStore_NoDeadLock(t *testing.T) { ctx := context.Background() // Epoch 1 blocks - state, blkRoot, err := prepareForkchoiceState(ctx, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 100, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 101, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 102, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 102, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 103, [32]byte{'d'}, [32]byte{'c'}, [32]byte{'D'}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 103, [32]byte{'d'}, [32]byte{'c'}, [32]byte{'D'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // Epoch 2 Blocks - state, blkRoot, err = prepareForkchoiceState(ctx, 104, [32]byte{'e'}, [32]byte{'d'}, [32]byte{'E'}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 104, [32]byte{'e'}, [32]byte{'d'}, [32]byte{'E'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.NoError(t, f.store.setUnrealizedJustifiedEpoch([32]byte{'e'}, 1)) - state, blkRoot, err = prepareForkchoiceState(ctx, 105, [32]byte{'f'}, [32]byte{'e'}, [32]byte{'F'}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 105, [32]byte{'f'}, [32]byte{'e'}, [32]byte{'F'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.NoError(t, f.store.setUnrealizedJustifiedEpoch([32]byte{'f'}, 1)) - state, blkRoot, err = prepareForkchoiceState(ctx, 106, [32]byte{'g'}, [32]byte{'f'}, [32]byte{'G'}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 106, [32]byte{'g'}, [32]byte{'f'}, [32]byte{'G'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.NoError(t, f.store.setUnrealizedJustifiedEpoch([32]byte{'g'}, 2)) require.NoError(t, f.store.setUnrealizedFinalizedEpoch([32]byte{'g'}, 1)) f.store.unrealizedJustifiedCheckpoint = &forkchoicetypes.Checkpoint{Epoch: 2} f.store.unrealizedFinalizedCheckpoint = &forkchoicetypes.Checkpoint{Epoch: 1} - state, blkRoot, err = prepareForkchoiceState(ctx, 107, [32]byte{'h'}, [32]byte{'g'}, [32]byte{'H'}, 0, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 107, [32]byte{'h'}, [32]byte{'g'}, [32]byte{'H'}, 0, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.NoError(t, f.store.setUnrealizedJustifiedEpoch([32]byte{'h'}, 2)) @@ -166,7 +166,7 @@ func TestStore_NoDeadLock(t *testing.T) { // Insert Block I, it becomes Head hr := [32]byte{'i'} - state, blkRoot, err = prepareForkchoiceState(ctx, 108, hr, [32]byte{'f'}, [32]byte{'I'}, 1, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 108, hr, [32]byte{'f'}, [32]byte{'I'}, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) ha := [32]byte{'a'} @@ -201,27 +201,27 @@ func TestStore_ForkNextEpoch(t *testing.T) { ctx := context.Background() // Epoch 1 blocks (D does not arrive) - state, blkRoot, err := prepareForkchoiceState(ctx, 92, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 0) + state, blkRoot, err := prepareForkchoiceState(ctx, f, 92, [32]byte{'a'}, params.BeaconConfig().ZeroHash, [32]byte{'A'}, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 93, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 93, [32]byte{'b'}, [32]byte{'a'}, [32]byte{'B'}, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 94, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 94, [32]byte{'c'}, [32]byte{'b'}, [32]byte{'C'}, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) // Epoch 2 blocks - state, blkRoot, err = prepareForkchoiceState(ctx, 96, [32]byte{'e'}, [32]byte{'c'}, [32]byte{'E'}, 1, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 96, [32]byte{'e'}, [32]byte{'c'}, [32]byte{'E'}, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 97, [32]byte{'f'}, [32]byte{'e'}, [32]byte{'F'}, 1, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 97, [32]byte{'f'}, [32]byte{'e'}, [32]byte{'F'}, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 98, [32]byte{'g'}, [32]byte{'f'}, [32]byte{'G'}, 1, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 98, [32]byte{'g'}, [32]byte{'f'}, [32]byte{'G'}, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(ctx, 99, [32]byte{'h'}, [32]byte{'g'}, [32]byte{'H'}, 1, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 99, [32]byte{'h'}, [32]byte{'g'}, [32]byte{'H'}, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -234,7 +234,7 @@ func TestStore_ForkNextEpoch(t *testing.T) { require.Equal(t, primitives.Epoch(1), f.JustifiedCheckpoint().Epoch) // D arrives late, D is head - state, blkRoot, err = prepareForkchoiceState(ctx, 95, [32]byte{'d'}, [32]byte{'c'}, [32]byte{'D'}, 1, 0) + state, blkRoot, err = prepareForkchoiceState(ctx, f, 95, [32]byte{'d'}, [32]byte{'c'}, [32]byte{'D'}, 1, 0) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) require.NoError(t, f.store.setUnrealizedJustifiedEpoch([32]byte{'d'}, 2)) @@ -261,13 +261,13 @@ func TestStore_PullTips_Heuristics(t *testing.T) { ctx := context.Background() t.Run("Current epoch is justified", func(tt *testing.T) { f := setup(1, 1) - st, root, err := prepareForkchoiceState(ctx, 65, [32]byte{'p'}, [32]byte{}, [32]byte{}, 1, 1) + st, root, err := prepareForkchoiceState(ctx, f, 65, [32]byte{'p'}, [32]byte{}, [32]byte{}, 1, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) f.store.emptyNodeByRoot[[32]byte{'p'}].block.unrealizedJustifiedEpoch = primitives.Epoch(2) driftGenesisTime(f, 66, 0) - st, root, err = prepareForkchoiceState(ctx, 66, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 1, 1) + st, root, err = prepareForkchoiceState(ctx, f, 66, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 1, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) require.Equal(tt, primitives.Epoch(2), f.store.emptyNodeByRoot[[32]byte{'h'}].block.unrealizedJustifiedEpoch) @@ -276,13 +276,13 @@ func TestStore_PullTips_Heuristics(t *testing.T) { t.Run("Previous Epoch is justified and too early for current", func(tt *testing.T) { f := setup(1, 1) - st, root, err := prepareForkchoiceState(ctx, 95, [32]byte{'p'}, [32]byte{}, [32]byte{}, 1, 1) + st, root, err := prepareForkchoiceState(ctx, f, 95, [32]byte{'p'}, [32]byte{}, [32]byte{}, 1, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) f.store.emptyNodeByRoot[[32]byte{'p'}].block.unrealizedJustifiedEpoch = primitives.Epoch(2) driftGenesisTime(f, 96, 0) - st, root, err = prepareForkchoiceState(ctx, 96, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 1, 1) + st, root, err = prepareForkchoiceState(ctx, f, 96, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 1, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) require.Equal(tt, primitives.Epoch(2), f.store.emptyNodeByRoot[[32]byte{'h'}].block.unrealizedJustifiedEpoch) @@ -290,13 +290,13 @@ func TestStore_PullTips_Heuristics(t *testing.T) { }) t.Run("Previous Epoch is justified and not too early for current", func(tt *testing.T) { f := setup(1, 1) - st, root, err := prepareForkchoiceState(ctx, 95, [32]byte{'p'}, [32]byte{}, [32]byte{}, 1, 1) + st, root, err := prepareForkchoiceState(ctx, f, 95, [32]byte{'p'}, [32]byte{}, [32]byte{}, 1, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) f.store.emptyNodeByRoot[[32]byte{'p'}].block.unrealizedJustifiedEpoch = primitives.Epoch(2) driftGenesisTime(f, 127, 0) - st, root, err = prepareForkchoiceState(ctx, 127, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 1, 1) + st, root, err = prepareForkchoiceState(ctx, f, 127, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 1, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) // Check that the justification point is not the parent's. @@ -307,13 +307,13 @@ func TestStore_PullTips_Heuristics(t *testing.T) { }) t.Run("Block from previous Epoch", func(tt *testing.T) { f := setup(1, 1) - st, root, err := prepareForkchoiceState(ctx, 94, [32]byte{'p'}, [32]byte{}, [32]byte{}, 1, 1) + st, root, err := prepareForkchoiceState(ctx, f, 94, [32]byte{'p'}, [32]byte{}, [32]byte{}, 1, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) f.store.emptyNodeByRoot[[32]byte{'p'}].block.unrealizedJustifiedEpoch = primitives.Epoch(2) driftGenesisTime(f, 96, 0) - st, root, err = prepareForkchoiceState(ctx, 95, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 1, 1) + st, root, err = prepareForkchoiceState(ctx, f, 95, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 1, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) // Check that the justification point is not the parent's. @@ -324,12 +324,12 @@ func TestStore_PullTips_Heuristics(t *testing.T) { }) t.Run("Previous Epoch is not justified", func(tt *testing.T) { f := setup(1, 1) - st, root, err := prepareForkchoiceState(ctx, 128, [32]byte{'p'}, [32]byte{}, [32]byte{}, 2, 1) + st, root, err := prepareForkchoiceState(ctx, f, 128, [32]byte{'p'}, [32]byte{}, [32]byte{}, 2, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) driftGenesisTime(f, 129, 0) - st, root, err = prepareForkchoiceState(ctx, 129, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 2, 1) + st, root, err = prepareForkchoiceState(ctx, f, 129, [32]byte{'h'}, [32]byte{'p'}, [32]byte{}, 2, 1) require.NoError(tt, err) require.NoError(tt, f.InsertNode(ctx, st, root)) // Check that the justification point is not the parent's. diff --git a/beacon-chain/forkchoice/doubly-linked-tree/vote_test.go b/beacon-chain/forkchoice/doubly-linked-tree/vote_test.go index a402a43c3eb..52ebb11a7aa 100644 --- a/beacon-chain/forkchoice/doubly-linked-tree/vote_test.go +++ b/beacon-chain/forkchoice/doubly-linked-tree/vote_test.go @@ -23,7 +23,7 @@ func TestVotes_CanFindHead(t *testing.T) { // 0 // / // 2 <- head - state, blkRoot, err := prepareForkchoiceState(context.Background(), 0, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err := prepareForkchoiceState(context.Background(), f, 0, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -35,7 +35,7 @@ func TestVotes_CanFindHead(t *testing.T) { // 0 // / \ // head -> 2 1 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -67,7 +67,7 @@ func TestVotes_CanFindHead(t *testing.T) { // head -> 2 1 // | // 3 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -105,7 +105,7 @@ func TestVotes_CanFindHead(t *testing.T) { // 3 // | // 4 <- head - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(4), indexToHash(3), params.BeaconConfig().ZeroHash, 1, 1) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(4), indexToHash(3), params.BeaconConfig().ZeroHash, 1, 1) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -125,7 +125,7 @@ func TestVotes_CanFindHead(t *testing.T) { // 5 <- head, justified epoch = 2 // // We set this node's slot to be 64 so that when pruning below we do not prune its child - state, blkRoot, err = prepareForkchoiceState(context.Background(), 2*params.BeaconConfig().SlotsPerEpoch, indexToHash(5), indexToHash(4), params.BeaconConfig().ZeroHash, 2, 2) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 2*params.BeaconConfig().SlotsPerEpoch, indexToHash(5), indexToHash(4), params.BeaconConfig().ZeroHash, 2, 2) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -143,7 +143,7 @@ func TestVotes_CanFindHead(t *testing.T) { // 4 // / \ // 5 6 <- head, justified epoch = 3 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(6), indexToHash(4), params.BeaconConfig().ZeroHash, 3, 2) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(6), indexToHash(4), params.BeaconConfig().ZeroHash, 3, 2) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) r, err = f.Head(context.Background()) @@ -168,10 +168,10 @@ func TestVotes_CanFindHead(t *testing.T) { // 7 // | // 8 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(7), indexToHash(5), params.BeaconConfig().ZeroHash, 2, 2) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(7), indexToHash(5), params.BeaconConfig().ZeroHash, 2, 2) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(8), indexToHash(7), params.BeaconConfig().ZeroHash, 2, 2) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(8), indexToHash(7), params.BeaconConfig().ZeroHash, 2, 2) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) r, err = f.Head(context.Background()) @@ -195,7 +195,7 @@ func TestVotes_CanFindHead(t *testing.T) { // 8 // | // 10 <- head - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(10), indexToHash(8), params.BeaconConfig().ZeroHash, 3, 2) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(10), indexToHash(8), params.BeaconConfig().ZeroHash, 3, 2) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) r, err = f.Head(context.Background()) @@ -218,7 +218,7 @@ func TestVotes_CanFindHead(t *testing.T) { // 8 // / \ // 9 10 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(9), indexToHash(8), params.BeaconConfig().ZeroHash, 3, 2) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(9), indexToHash(8), params.BeaconConfig().ZeroHash, 3, 2) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot)) @@ -303,7 +303,7 @@ func TestVotes_CanFindHead(t *testing.T) { // 10 9 // | // head-> 11 - state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(11), indexToHash(10), params.BeaconConfig().ZeroHash, 3, 2) + state, blkRoot, err = prepareForkchoiceState(context.Background(), f, 0, indexToHash(11), indexToHash(10), params.BeaconConfig().ZeroHash, 3, 2) require.NoError(t, err) require.NoError(t, f.InsertNode(ctx, state, blkRoot))