2022-03-09 03:05:51 +00:00
|
|
|
package doublylinkedtree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/testing/require"
|
2022-03-09 03:05:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNoVote_CanFindHead(t *testing.T) {
|
|
|
|
f := setup(1, 1)
|
2023-02-15 14:19:46 +00:00
|
|
|
f.justifiedBalances = make([]uint64, 16)
|
2022-06-05 17:48:21 +00:00
|
|
|
ctx := context.Background()
|
2022-03-09 03:05:51 +00:00
|
|
|
|
|
|
|
// The head should always start at the finalized block.
|
2023-02-15 14:19:46 +00:00
|
|
|
r, err := f.Head(context.Background())
|
2022-03-09 03:05:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
if r != params.BeaconConfig().ZeroHash {
|
|
|
|
t.Errorf("Incorrect head with genesis")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert block 2 into the tree and verify head is at 2:
|
|
|
|
// 0
|
|
|
|
// /
|
|
|
|
// 2 <- head
|
2022-06-05 17:48:21 +00:00
|
|
|
state, blkRoot, err := prepareForkchoiceState(context.Background(), 0, indexToHash(2), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, f.InsertNode(ctx, state, blkRoot))
|
2023-02-15 14:19:46 +00:00
|
|
|
r, err = f.Head(context.Background())
|
2022-03-09 03:05:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, indexToHash(2), r, "Incorrect head for with justified epoch at 1")
|
|
|
|
|
|
|
|
// Insert block 1 into the tree and verify head is still at 2:
|
|
|
|
// 0
|
|
|
|
// / \
|
|
|
|
// head -> 2 1
|
2022-06-05 17:48:21 +00:00
|
|
|
state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(1), params.BeaconConfig().ZeroHash, params.BeaconConfig().ZeroHash, 1, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, f.InsertNode(ctx, state, blkRoot))
|
2023-02-15 14:19:46 +00:00
|
|
|
r, err = f.Head(context.Background())
|
2022-03-09 03:05:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, indexToHash(2), r, "Incorrect head for with justified epoch at 1")
|
|
|
|
|
|
|
|
// Insert block 3 into the tree and verify head is still at 2:
|
|
|
|
// 0
|
|
|
|
// / \
|
|
|
|
// head -> 2 1
|
|
|
|
// |
|
|
|
|
// 3
|
2022-06-05 17:48:21 +00:00
|
|
|
state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(3), indexToHash(1), params.BeaconConfig().ZeroHash, 1, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, f.InsertNode(ctx, state, blkRoot))
|
2023-02-15 14:19:46 +00:00
|
|
|
r, err = f.Head(context.Background())
|
2022-03-09 03:05:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, indexToHash(2), r, "Incorrect head for with justified epoch at 1")
|
|
|
|
|
|
|
|
// Insert block 4 into the tree and verify head is at 4:
|
|
|
|
// 0
|
|
|
|
// / \
|
|
|
|
// 2 1
|
|
|
|
// | |
|
|
|
|
// head -> 4 3
|
2022-06-05 17:48:21 +00:00
|
|
|
state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(4), indexToHash(2), params.BeaconConfig().ZeroHash, 1, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, f.InsertNode(ctx, state, blkRoot))
|
2023-02-15 14:19:46 +00:00
|
|
|
r, err = f.Head(context.Background())
|
2022-03-09 03:05:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, indexToHash(4), r, "Incorrect head for with justified epoch at 1")
|
|
|
|
|
2022-06-16 18:21:40 +00:00
|
|
|
// Insert block 5 with justified epoch of 2, verify head is 5
|
2022-03-09 03:05:51 +00:00
|
|
|
// 0
|
|
|
|
// / \
|
|
|
|
// 2 1
|
|
|
|
// | |
|
|
|
|
// head -> 4 3
|
|
|
|
// |
|
|
|
|
// 5 <- justified epoch = 2
|
2022-06-05 17:48:21 +00:00
|
|
|
state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(5), indexToHash(4), params.BeaconConfig().ZeroHash, 2, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, f.InsertNode(ctx, state, blkRoot))
|
2023-02-15 14:19:46 +00:00
|
|
|
r, err = f.Head(context.Background())
|
2022-03-09 03:05:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, indexToHash(5), r, "Incorrect head for with justified epoch at 2")
|
|
|
|
|
|
|
|
// Insert block 6 with justified epoch of 2, verify head is at 6.
|
|
|
|
// 0
|
|
|
|
// / \
|
|
|
|
// 2 1
|
|
|
|
// | |
|
|
|
|
// 4 3
|
|
|
|
// |
|
|
|
|
// 5
|
|
|
|
// |
|
|
|
|
// 6 <- head
|
2022-06-05 17:48:21 +00:00
|
|
|
state, blkRoot, err = prepareForkchoiceState(context.Background(), 0, indexToHash(6), indexToHash(5), params.BeaconConfig().ZeroHash, 2, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, f.InsertNode(ctx, state, blkRoot))
|
2023-02-15 14:19:46 +00:00
|
|
|
r, err = f.Head(context.Background())
|
2022-03-09 03:05:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, indexToHash(6), r, "Incorrect head for with justified epoch at 2")
|
|
|
|
}
|