2020-04-09 16:15:00 +00:00
|
|
|
# beacon_chain
|
2021-08-25 14:51:52 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2020-04-09 16:15:00 +00:00
|
|
|
# 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.
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
# import ../interpreter # included to be able to use "suite"
|
2020-04-09 16:15:00 +00:00
|
|
|
|
2021-08-25 14:51:52 +00:00
|
|
|
func setup_finality_02(): tuple[fork_choice: ForkChoiceBackend, ops: seq[Operation]] =
|
2020-04-09 16:15:00 +00:00
|
|
|
var balances = @[Gwei(1), Gwei(1)]
|
|
|
|
let GenesisRoot = fakeHash(0)
|
|
|
|
|
|
|
|
# Initialize the fork choice context
|
2020-08-18 14:56:32 +00:00
|
|
|
result.fork_choice = ForkChoiceBackend.init(
|
2021-11-25 18:41:39 +00:00
|
|
|
justifiedCheckpoint = Checkpoint(root: GenesisRoot, epoch: Epoch(1)),
|
|
|
|
finalizedCheckpoint = Checkpoint(root: GenesisRoot, epoch: Epoch(1))
|
2020-08-18 14:56:32 +00:00
|
|
|
)
|
2020-04-09 16:15:00 +00:00
|
|
|
|
|
|
|
# ----------------------------------
|
|
|
|
|
|
|
|
# Head should be genesis
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(1)),
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(1)),
|
2020-04-09 16:15:00 +00:00
|
|
|
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,
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(3),
|
|
|
|
parent_root: fakeHash(1),
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: fakeHash(1), epoch: Epoch(1)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(5),
|
|
|
|
parent_root: fakeHash(3),
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: fakeHash(1), epoch: Epoch(1)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(7),
|
|
|
|
parent_root: fakeHash(5),
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: fakeHash(1), epoch: Epoch(1)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(9),
|
|
|
|
parent_root: fakeHash(7),
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: fakehash(3), epoch: Epoch(2)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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,
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(4),
|
|
|
|
parent_root: fakeHash(2),
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(6),
|
|
|
|
parent_root: fakeHash(4),
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(8),
|
|
|
|
parent_root: fakeHash(6),
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: fakehash(2), epoch: Epoch(1)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(10),
|
|
|
|
parent_root: fakeHash(8),
|
2021-11-25 18:41:39 +00:00
|
|
|
blk_justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(2)),
|
|
|
|
blk_finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0))
|
2020-04-09 16:15:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(10)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Same with justified_epoch 2
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(2)),
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(10)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Justified epoch 3 is invalid
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: InvalidFindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(3)), # <--- Wrong epoch
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
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,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(9)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Same with justified_epoch 2
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: fakehash(3), epoch: Epoch(2)),
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(9)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Justified epoch 3 is invalid
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: InvalidFindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(3)), # <--- Wrong epoch
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
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,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(10)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Same with justified_epoch 2
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: fakeHash(4), epoch: Epoch(2)),
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(10)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Justified epoch 3 is invalid
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: InvalidFindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(3)), # <--- Wrong epoch
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
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,
|
2021-11-25 18:41:39 +00:00
|
|
|
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)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(9)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Same with justified_epoch 2
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: fakehash(3), epoch: Epoch(2)),
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(9)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Justified epoch 3 is invalid
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: InvalidFindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: fakehash(5), epoch: Epoch(3)), # <--- Wrong epoch
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
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,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: fakehash(2), epoch: Epoch(0)), # In production this can't happen
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(10)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Same with justified_epoch 2
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(2)),
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
|
|
|
expected_head: fakeHash(10)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Justified epoch 3 is invalid
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: InvalidFindHead,
|
2021-11-25 18:41:39 +00:00
|
|
|
justified_checkpoint: Checkpoint(root: fakehash(4), epoch: Epoch(3)), # <--- Wrong epoch
|
|
|
|
finalized_checkpoint: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances
|
|
|
|
)
|
|
|
|
|
|
|
|
proc test_ffg02() =
|
2021-04-28 16:41:02 +00:00
|
|
|
test "fork_choice - testing finality #02":
|
2020-04-09 16:15:00 +00:00
|
|
|
# for i in 0 ..< 12:
|
|
|
|
# echo " block (", i, ") hash: ", fakeHash(i)
|
|
|
|
# echo " ------------------------------------------------------"
|
|
|
|
|
|
|
|
var (ctx, ops) = setup_finality_02()
|
|
|
|
ctx.run(ops)
|
|
|
|
|
|
|
|
test_ffg02()
|