Skip to content

Commit

Permalink
continuing
Browse files Browse the repository at this point in the history
  • Loading branch information
potuz committed Oct 29, 2024
1 parent 7c33962 commit f28a5e2
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 395 deletions.
1 change: 1 addition & 0 deletions beacon-chain/forkchoice/doubly-linked-tree/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
27 changes: 27 additions & 0 deletions beacon-chain/forkchoice/doubly-linked-tree/epbs_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 14 additions & 14 deletions beacon-chain/forkchoice/doubly-linked-tree/ffg_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 7 additions & 2 deletions beacon-chain/forkchoice/doubly-linked-tree/forkchoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 144 in beacon-chain/forkchoice/doubly-linked-tree/forkchoice.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `f.insertExecutionPayload` is not checked (errcheck)
}
return nil
}
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit f28a5e2

Please sign in to comment.