2020-04-09 16:15:00 +00:00
|
|
|
# beacon_chain
|
2022-02-04 11:59:40 +00:00
|
|
|
# Copyright (c) 2018-2022 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.
|
|
|
|
|
2022-07-29 10:53:42 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-07-06 10:33:02 +00:00
|
|
|
|
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_01(): tuple[fork_choice: ForkChoiceBackend, ops: seq[Operation]] =
|
2022-02-20 20:13:06 +00:00
|
|
|
let balances = @[Gwei(1), Gwei(1)]
|
2020-04-09 16:15:00 +00:00
|
|
|
let GenesisRoot = fakeHash(0)
|
|
|
|
|
|
|
|
# Initialize the fork choice context
|
2020-08-18 14:56:32 +00:00
|
|
|
result.fork_choice = ForkChoiceBackend.init(
|
2022-07-06 10:33:02 +00:00
|
|
|
FinalityCheckpoints(
|
|
|
|
justified: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
finalized: Checkpoint(root: GenesisRoot, epoch: Epoch(0))))
|
2020-04-09 16:15:00 +00:00
|
|
|
|
|
|
|
# ----------------------------------
|
|
|
|
|
|
|
|
# Head should be genesis
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2022-07-06 10:33:02 +00:00
|
|
|
checkpoints: FinalityCheckpoints(
|
|
|
|
justified: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
finalized: Checkpoint(root: GenesisRoot, epoch: Epoch(0))),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
2022-07-06 10:33:02 +00:00
|
|
|
expected_head: GenesisRoot)
|
2020-04-09 16:15:00 +00:00
|
|
|
|
|
|
|
# Build the following chain
|
|
|
|
#
|
|
|
|
# 0 <- just: 0, fin: 0
|
|
|
|
# |
|
|
|
|
# 1 <- just: 0, fin: 0
|
|
|
|
# |
|
|
|
|
# 2 <- just: 1, fin: 0
|
|
|
|
# |
|
|
|
|
# 3 <- just: 2, fin: 1
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(1),
|
|
|
|
parent_root: GenesisRoot,
|
2022-07-06 10:33:02 +00:00
|
|
|
blk_checkpoints: FinalityCheckpoints(
|
|
|
|
justified: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
finalized: Checkpoint(root: GenesisRoot, epoch: Epoch(0))))
|
|
|
|
|
2020-04-09 16:15:00 +00:00
|
|
|
result.ops.add Operation(
|
|
|
|
kind: ProcessBlock,
|
|
|
|
root: fakeHash(2),
|
|
|
|
parent_root: fakeHash(1),
|
2022-07-06 10:33:02 +00:00
|
|
|
blk_checkpoints: FinalityCheckpoints(
|
|
|
|
justified: Checkpoint(root: fakeHash(1), epoch: Epoch(1)),
|
|
|
|
finalized: 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(2),
|
2022-07-06 10:33:02 +00:00
|
|
|
blk_Checkpoints: FinalityCheckpoints(
|
|
|
|
justified: Checkpoint(root: fakeHash(2), epoch: Epoch(2)),
|
|
|
|
finalized: Checkpoint(root: fakeHash(1), epoch: Epoch(1))))
|
2020-04-09 16:15:00 +00:00
|
|
|
|
|
|
|
# Ensure that with justified epoch 0 we find 3
|
|
|
|
#
|
|
|
|
# 0 <- start
|
|
|
|
# |
|
|
|
|
# 1
|
|
|
|
# |
|
|
|
|
# 2
|
|
|
|
# |
|
|
|
|
# 3 <- head
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2022-07-06 10:33:02 +00:00
|
|
|
checkpoints: FinalityCheckpoints(
|
|
|
|
justified: Checkpoint(root: GenesisRoot, epoch: Epoch(0)),
|
|
|
|
finalized: Checkpoint(root: GenesisRoot, epoch: Epoch(0))),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
2022-07-06 10:33:02 +00:00
|
|
|
expected_head: fakeHash(3))
|
2020-04-09 16:15:00 +00:00
|
|
|
|
|
|
|
# Ensure that with justified epoch 1 we find 2
|
|
|
|
#
|
|
|
|
# 0
|
|
|
|
# |
|
|
|
|
# 1
|
|
|
|
# |
|
|
|
|
# 2 <- start
|
|
|
|
# |
|
|
|
|
# 3 <- head
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2022-07-06 10:33:02 +00:00
|
|
|
checkpoints: FinalityCheckpoints(
|
|
|
|
justified: Checkpoint(root: fakeHash(1), epoch: Epoch(1)),
|
|
|
|
finalized: Checkpoint(root: GenesisRoot, epoch: Epoch(0))),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
2022-07-06 10:33:02 +00:00
|
|
|
expected_head: fakeHash(2))
|
2020-04-09 16:15:00 +00:00
|
|
|
|
|
|
|
# Ensure that with justified epoch 2 we find 3
|
|
|
|
#
|
|
|
|
# 0
|
|
|
|
# |
|
|
|
|
# 1
|
|
|
|
# |
|
|
|
|
# 2
|
|
|
|
# |
|
|
|
|
# 3 <- start + head
|
|
|
|
result.ops.add Operation(
|
|
|
|
kind: FindHead,
|
2022-07-06 10:33:02 +00:00
|
|
|
checkpoints: FinalityCheckpoints(
|
|
|
|
justified: Checkpoint(root: fakeHash(2), epoch: Epoch(2)),
|
|
|
|
finalized: Checkpoint(root: fakeHash(1), epoch: Epoch(1))),
|
2020-04-09 16:15:00 +00:00
|
|
|
justified_state_balances: balances,
|
2022-07-06 10:33:02 +00:00
|
|
|
expected_head: fakeHash(3))
|
2020-04-09 16:15:00 +00:00
|
|
|
|
|
|
|
proc test_ffg01() =
|
2021-04-28 16:41:02 +00:00
|
|
|
test "fork_choice - testing finality #01":
|
2020-04-09 16:15:00 +00:00
|
|
|
# for i in 0 ..< 4:
|
|
|
|
# echo " block (", i, ") hash: ", fakeHash(i)
|
|
|
|
# echo " ------------------------------------------------------"
|
|
|
|
|
|
|
|
var (ctx, ops) = setup_finality_01()
|
|
|
|
ctx.run(ops)
|
|
|
|
|
|
|
|
test_ffg01()
|