374 lines
12 KiB
Nim
374 lines
12 KiB
Nim
# beacon_chain
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
|
# Licensed and distributed under either of
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
# import ../interpreter # included to be able to use "suite"
|
|
|
|
func setup_finality_02(): tuple[fork_choice: ForkChoiceBackend, ops: seq[Operation]] =
|
|
var balances = @[Gwei(1), Gwei(1)]
|
|
let GenesisRoot = fakeHash(0)
|
|
|
|
# Initialize the fork choice context
|
|
result.fork_choice = ForkChoiceBackend.init(
|
|
justifiedCheckpoint = Checkpoint(root: GenesisRoot, epoch: Epoch(1)),
|
|
finalizedCheckpoint = Checkpoint(root: GenesisRoot, epoch: Epoch(1)),
|
|
true # use proposer boosting, though the proposer boost root not set
|
|
)
|
|
|
|
# ----------------------------------
|
|
|
|
# Head should be genesis
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(1)),
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(1)),
|
|
justified_state_balances: balances,
|
|
expected_head: GenesisRoot
|
|
)
|
|
|
|
# Build the following tree.
|
|
#
|
|
# 0
|
|
# / \
|
|
# just: 0, fin: 0 -> 1 2 <- just: 0, fin: 0
|
|
# | |
|
|
# just: 1, fin: 0 -> 3 4 <- just: 0, fin: 0
|
|
# | |
|
|
# just: 1, fin: 0 -> 5 6 <- just: 0, fin: 0
|
|
# | |
|
|
# just: 1, fin: 0 -> 7 8 <- just: 1, fin: 0
|
|
# | |
|
|
# just: 2, fin: 0 -> 9 10 <- just: 2, fin: 0
|
|
|
|
# Left branch
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(1),
|
|
parent_root: GenesisRoot,
|
|
blk_justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(3),
|
|
parent_root: fakeHash(1),
|
|
blk_justified_checkpoint: Checkpoint(root: fakeHash(1), epoch: Epoch(1)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(5),
|
|
parent_root: fakeHash(3),
|
|
blk_justified_checkpoint: Checkpoint(root: fakeHash(1), epoch: Epoch(1)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(7),
|
|
parent_root: fakeHash(5),
|
|
blk_justified_checkpoint: Checkpoint(root: fakeHash(1), epoch: Epoch(1)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(9),
|
|
parent_root: fakeHash(7),
|
|
blk_justified_checkpoint: Checkpoint(root: fakehash(3), epoch: Epoch(2)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
|
|
# Build the following tree.
|
|
#
|
|
# 0
|
|
# / \
|
|
# just: 0, fin: 0 -> 1 2 <- just: 0, fin: 0
|
|
# | |
|
|
# just: 1, fin: 0 -> 3 4 <- just: 0, fin: 0
|
|
# | |
|
|
# just: 1, fin: 0 -> 5 6 <- just: 0, fin: 0
|
|
# | |
|
|
# just: 1, fin: 0 -> 7 8 <- just: 1, fin: 0
|
|
# | |
|
|
# just: 2, fin: 0 -> 9 10 <- just: 2, fin: 0
|
|
|
|
# Right branch
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(2),
|
|
parent_root: GenesisRoot,
|
|
blk_justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(4),
|
|
parent_root: fakeHash(2),
|
|
blk_justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(6),
|
|
parent_root: fakeHash(4),
|
|
blk_justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(8),
|
|
parent_root: fakeHash(6),
|
|
blk_justified_checkpoint: Checkpoint(root: fakehash(2), epoch: Epoch(1)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
result.ops.add Operation(
|
|
kind: ProcessBlock,
|
|
root: fakeHash(10),
|
|
parent_root: fakeHash(8),
|
|
blk_justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(2)),
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
|
)
|
|
|
|
# Ensure that if we start at 0 we find 10 (just: 0, fin: 0).
|
|
#
|
|
# 0 <-- start
|
|
# / \
|
|
# 1 2
|
|
# | |
|
|
# 3 4
|
|
# | |
|
|
# 5 6
|
|
# | |
|
|
# 7 8
|
|
# | |
|
|
# 9 10 <-- head
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(10)
|
|
)
|
|
|
|
# Same with justified_epoch 2
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(2)),
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(10)
|
|
)
|
|
|
|
# Justified epoch 3 is invalid
|
|
result.ops.add Operation(
|
|
kind: InvalidFindHead,
|
|
justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(3)), # <--- Wrong epoch
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances
|
|
)
|
|
|
|
# Add a vote to 1.
|
|
#
|
|
# 0
|
|
# / \
|
|
# +1 vote -> 1 2
|
|
# | |
|
|
# 3 4
|
|
# | |
|
|
# 5 6
|
|
# | |
|
|
# 7 8
|
|
# | |
|
|
# 9 10
|
|
result.ops.add Operation(
|
|
kind: ProcessAttestation,
|
|
validator_index: ValidatorIndex(0),
|
|
block_root: fake_hash(1),
|
|
target_epoch: Epoch(0)
|
|
)
|
|
|
|
# Ensure that if we start at 0 we find 9 (just: 0, fin: 0).
|
|
#
|
|
# 0 <-- start
|
|
# / \
|
|
# 1 2
|
|
# | |
|
|
# 3 4
|
|
# | |
|
|
# 5 6
|
|
# | |
|
|
# 7 8
|
|
# | |
|
|
# head -> 9 10
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(9)
|
|
)
|
|
|
|
# Same with justified_epoch 2
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: fakehash(3), epoch: Epoch(2)),
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(9)
|
|
)
|
|
|
|
# Justified epoch 3 is invalid
|
|
result.ops.add Operation(
|
|
kind: InvalidFindHead,
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(3)), # <--- Wrong epoch
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances
|
|
)
|
|
|
|
# Add a vote to 2.
|
|
#
|
|
# 0
|
|
# / \
|
|
# 1 2 <- +1 vote
|
|
# | |
|
|
# 3 4
|
|
# | |
|
|
# 5 6
|
|
# | |
|
|
# 7 8
|
|
# | |
|
|
# 9 10
|
|
result.ops.add Operation(
|
|
kind: ProcessAttestation,
|
|
validator_index: ValidatorIndex(1),
|
|
block_root: fake_hash(2),
|
|
target_epoch: Epoch(0)
|
|
)
|
|
|
|
# Ensure that if we start at 0 we find 10 again (just: 0, fin: 0).
|
|
#
|
|
# 0 <-- start
|
|
# / \
|
|
# 1 2
|
|
# | |
|
|
# 3 4
|
|
# | |
|
|
# 5 6
|
|
# | |
|
|
# 7 8
|
|
# | |
|
|
# 9 10 <-- head
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(10)
|
|
)
|
|
|
|
# Same with justified_epoch 2
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: fakeHash(4), epoch: Epoch(2)),
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(10)
|
|
)
|
|
|
|
# Justified epoch 3 is invalid
|
|
result.ops.add Operation(
|
|
kind: InvalidFindHead,
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(3)), # <--- Wrong epoch
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances
|
|
)
|
|
|
|
# Ensure that if we start at 1 (instead of 0) we find 9 (just: 0, fin: 0).
|
|
#
|
|
# 0
|
|
# / \
|
|
# start-> 1 2
|
|
# | |
|
|
# 3 4
|
|
# | |
|
|
# 5 6
|
|
# | |
|
|
# 7 8
|
|
# | |
|
|
# head -> 9 10
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: fakehash(1), epoch: Epoch(0)), # <- in production the root/epoch mismatch isn't used.
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(9)
|
|
)
|
|
|
|
# Same with justified_epoch 2
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: fakehash(3), epoch: Epoch(2)),
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(9)
|
|
)
|
|
|
|
# Justified epoch 3 is invalid
|
|
result.ops.add Operation(
|
|
kind: InvalidFindHead,
|
|
justified_checkpoint: Checkpoint(root: fakehash(5), epoch: Epoch(3)), # <--- Wrong epoch
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances
|
|
)
|
|
|
|
# Ensure that if we start at 2 (instead of 0) we find 10 (just: 0, fin: 0).
|
|
#
|
|
# 0
|
|
# / \
|
|
# 1 2 <- start
|
|
# | |
|
|
# 3 4
|
|
# | |
|
|
# 5 6
|
|
# | |
|
|
# 7 8
|
|
# | |
|
|
# 9 10 <- head
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: fakehash(2), epoch: Epoch(0)), # In production this can't happen
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(10)
|
|
)
|
|
|
|
# Same with justified_epoch 2
|
|
result.ops.add Operation(
|
|
kind: FindHead,
|
|
justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(2)),
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances,
|
|
expected_head: fakeHash(10)
|
|
)
|
|
|
|
# Justified epoch 3 is invalid
|
|
result.ops.add Operation(
|
|
kind: InvalidFindHead,
|
|
justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(3)), # <--- Wrong epoch
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
justified_state_balances: balances
|
|
)
|
|
|
|
proc test_ffg02() =
|
|
test "fork_choice - testing finality #02":
|
|
# for i in 0 ..< 12:
|
|
# echo " block (", i, ") hash: ", fakeHash(i)
|
|
# echo " ------------------------------------------------------"
|
|
|
|
var (ctx, ops) = setup_finality_02()
|
|
ctx.run(ops)
|
|
|
|
test_ffg02()
|