2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2021-06-11 17:51:46 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2019-08-28 12:07:00 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * 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).
|
2019-08-28 12:07:00 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2019-11-14 10:47:55 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-08-28 12:07:00 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
|
|
|
# Vendored packages
|
|
|
|
stew/bitops2,
|
|
|
|
# Specs
|
2021-06-21 08:35:24 +00:00
|
|
|
../../beacon_chain/spec/datatypes/base,
|
2021-08-10 20:46:35 +00:00
|
|
|
../../beacon_chain/spec/forks,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Test helpers
|
2021-06-11 17:51:46 +00:00
|
|
|
../mocking/mock_genesis,
|
2019-08-28 12:07:00 +00:00
|
|
|
./epoch_utils,
|
|
|
|
./justification_finalization_helpers,
|
|
|
|
../testutil
|
|
|
|
|
|
|
|
# See diagram: eth2-finalization.png
|
|
|
|
# (source) https://github.com/protolambda/eth2-docs#justification-and-finalization
|
|
|
|
# for a visualization of finalization rules
|
|
|
|
|
2021-06-11 17:51:46 +00:00
|
|
|
proc finalizeOn234(
|
|
|
|
state: var ForkedHashedBeaconState, epoch: Epoch, sufficient_support: bool) =
|
2019-08-28 12:07:00 +00:00
|
|
|
## Check finalization on rule 1 "234"
|
|
|
|
doAssert epoch > 4
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state, slot) = Slot((epoch * SLOTS_PER_EPOCH) - 1) # Skip ahead to just before epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# 43210 -- epochs ago
|
|
|
|
# 3210x -- justification bitfields indices
|
|
|
|
# 11*0. -- justification bitfield contents. . = this epoch, * is being justified now
|
|
|
|
|
|
|
|
# checkpoints for epochs ago
|
|
|
|
let (c1, c2, c3, c4, _) = getCheckpoints(epoch)
|
2021-10-18 16:37:27 +00:00
|
|
|
putCheckpointsInBlockRoots(state.phase0Data.data, [c1, c2, c3, c4])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Save for final checks
|
2021-06-11 17:51:46 +00:00
|
|
|
let old_finalized = getStateField(state, finalized_checkpoint)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Mock the state
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state, previous_justified_checkpoint) = c4
|
|
|
|
getStateField(state, current_justified_checkpoint) = c3
|
2022-01-06 07:38:40 +00:00
|
|
|
getStateField(state, justification_bits) = JustificationBits(0'u8) # Bitvector of length 4
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock 3rd and 4th latest epochs as justified
|
|
|
|
# indices are pre-shift
|
2022-01-06 07:38:40 +00:00
|
|
|
uint8(getStateField(state, justification_bits)).setBit 1
|
|
|
|
uint8(getStateField(state, justification_bits)).setBit 2
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock the 2nd latest epoch as justifiable, with 4th as the source
|
|
|
|
addMockAttestations(
|
2021-10-18 16:37:27 +00:00
|
|
|
state.phase0Data.data,
|
2019-09-24 14:56:12 +00:00
|
|
|
epoch = epoch - 2,
|
2019-08-28 12:07:00 +00:00
|
|
|
source = c4,
|
|
|
|
target = c2,
|
|
|
|
sufficient_support = sufficient_support
|
|
|
|
)
|
|
|
|
|
|
|
|
# State transition
|
|
|
|
transitionEpochUntilJustificationFinalization(state)
|
|
|
|
|
|
|
|
# Checks
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, previous_justified_checkpoint) == c3 # changed to old current
|
2019-08-28 12:07:00 +00:00
|
|
|
if sufficient_support:
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, current_justified_checkpoint) == c2 # changed to second latest
|
|
|
|
doAssert getStateField(state, finalized_checkpoint) == c4 # finalized old previous justified epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, current_justified_checkpoint) == c3 # still old current
|
|
|
|
doAssert getStateField(state, finalized_checkpoint) == old_finalized # no new finalized checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-06-11 17:51:46 +00:00
|
|
|
proc finalizeOn23(state: var ForkedHashedBeaconState, epoch: Epoch, sufficient_support: bool) =
|
2019-08-28 12:07:00 +00:00
|
|
|
## Check finalization on rule 2 "23"
|
|
|
|
doAssert epoch > 3
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state, slot) = Slot((epoch * SLOTS_PER_EPOCH) - 1) # Skip ahead to just before epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# 43210 -- epochs ago
|
|
|
|
# 210xx -- justification bitfields indices preshift
|
|
|
|
# 3210x -- justification bitfield indices postshift
|
|
|
|
# 01*0. -- justification bitfield contents. . = this epoch, * is being justified now
|
|
|
|
|
|
|
|
# checkpoints for epochs ago
|
|
|
|
let (c1, c2, c3, _, _) = getCheckpoints(epoch)
|
2021-10-18 16:37:27 +00:00
|
|
|
putCheckpointsInBlockRoots(state.phase0Data.data, [c1, c2, c3])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Save for final checks
|
2021-06-11 17:51:46 +00:00
|
|
|
let old_finalized = getStateField(state, finalized_checkpoint)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Mock the state
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state, previous_justified_checkpoint) = c3
|
|
|
|
getStateField(state, current_justified_checkpoint) = c3
|
2022-01-06 07:38:40 +00:00
|
|
|
getStateField(state, justification_bits) = JustificationBits(0'u8) # Bitvector of length 4
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock 3rd as justified
|
|
|
|
# indices are pre-shift
|
2022-01-06 07:38:40 +00:00
|
|
|
uint8(getStateField(state, justification_bits)).setBit 1
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock the 2nd latest epoch as justifiable, with 3rd as the source
|
|
|
|
addMockAttestations(
|
2021-10-18 16:37:27 +00:00
|
|
|
state.phase0Data.data,
|
2019-08-28 12:07:00 +00:00
|
|
|
epoch = epoch - 2,
|
|
|
|
source = c3,
|
|
|
|
target = c2,
|
|
|
|
sufficient_support = sufficient_support
|
|
|
|
)
|
|
|
|
|
|
|
|
# State transition
|
|
|
|
transitionEpochUntilJustificationFinalization(state)
|
|
|
|
|
|
|
|
# Checks
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, previous_justified_checkpoint) == c3 # changed to old current
|
2019-08-28 12:07:00 +00:00
|
|
|
if sufficient_support:
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, current_justified_checkpoint) == c2 # changed to second latest
|
|
|
|
doAssert getStateField(state, finalized_checkpoint) == c3 # finalized old previous justified epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, current_justified_checkpoint) == c3 # still old current
|
|
|
|
doAssert getStateField(state, finalized_checkpoint) == old_finalized # no new finalized checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-06-11 17:51:46 +00:00
|
|
|
proc finalizeOn123(state: var ForkedHashedBeaconState, epoch: Epoch, sufficient_support: bool) =
|
2019-08-28 14:22:48 +00:00
|
|
|
## Check finalization on rule 3 "123"
|
2019-08-28 12:07:00 +00:00
|
|
|
doAssert epoch > 5
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state, slot) = Slot((epoch * SLOTS_PER_EPOCH) - 1) # Skip ahead to just before epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# 43210 -- epochs ago
|
|
|
|
# 210xx -- justification bitfields indices preshift
|
|
|
|
# 3210x -- justification bitfield indices postshift
|
|
|
|
# 0110*. -- justification bitfield contents. . = this epoch, * is being justified now
|
|
|
|
|
|
|
|
# checkpoints for epochs ago
|
|
|
|
let (c1, c2, c3, c4, c5) = getCheckpoints(epoch)
|
2021-10-18 16:37:27 +00:00
|
|
|
putCheckpointsInBlockRoots(state.phase0Data.data, [c1, c2, c3, c4, c5])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Save for final checks
|
2021-06-11 17:51:46 +00:00
|
|
|
let old_finalized = getStateField(state, finalized_checkpoint)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Mock the state
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state, previous_justified_checkpoint) = c5
|
|
|
|
getStateField(state, current_justified_checkpoint) = c3
|
2022-01-06 07:38:40 +00:00
|
|
|
getStateField(state, justification_bits) = JustificationBits(0'u8) # Bitvector of length 4
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock 3rd as justified
|
|
|
|
# indices are pre-shift
|
2022-01-06 07:38:40 +00:00
|
|
|
uint8(getStateField(state, justification_bits)).setBit 1
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock the 2nd latest epoch as justifiable, with 5th as the source
|
|
|
|
addMockAttestations(
|
2021-10-18 16:37:27 +00:00
|
|
|
state.phase0Data.data,
|
2019-08-28 12:07:00 +00:00
|
|
|
epoch = epoch - 2,
|
|
|
|
source = c5,
|
|
|
|
target = c2,
|
|
|
|
sufficient_support = sufficient_support
|
|
|
|
)
|
|
|
|
# mock the 1st latest epoch as justifiable with 3rd as source
|
|
|
|
addMockAttestations(
|
2021-10-18 16:37:27 +00:00
|
|
|
state.phase0Data.data,
|
2019-08-28 12:07:00 +00:00
|
|
|
epoch = epoch - 1,
|
|
|
|
source = c3,
|
|
|
|
target = c1,
|
|
|
|
sufficient_support = sufficient_support
|
|
|
|
)
|
|
|
|
|
|
|
|
# State transition
|
|
|
|
transitionEpochUntilJustificationFinalization(state)
|
|
|
|
|
|
|
|
# Checks
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, previous_justified_checkpoint) == c3 # changed to old current
|
2019-08-28 12:07:00 +00:00
|
|
|
if sufficient_support:
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, current_justified_checkpoint) == c1 # changed to second latest
|
|
|
|
doAssert getStateField(state, finalized_checkpoint) == c3 # finalized old previous justified epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, current_justified_checkpoint) == c3 # still old current
|
|
|
|
doAssert getStateField(state, finalized_checkpoint) == old_finalized # no new finalized checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-06-11 17:51:46 +00:00
|
|
|
proc finalizeOn12(state: var ForkedHashedBeaconState, epoch: Epoch, sufficient_support: bool) =
|
2019-08-28 14:22:48 +00:00
|
|
|
## Check finalization on rule 4 "12"
|
2019-08-28 12:07:00 +00:00
|
|
|
doAssert epoch > 2
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state, slot) = Slot((epoch * SLOTS_PER_EPOCH) - 1) # Skip ahead to just before epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# 43210 -- epochs ago
|
|
|
|
# 210xx -- justification bitfields indices preshift
|
|
|
|
# 3210x -- justification bitfield indices postshift
|
|
|
|
# 01*0. -- justification bitfield contents. . = this epoch, * is being justified now
|
|
|
|
|
|
|
|
# checkpoints for epochs ago
|
|
|
|
let (c1, c2, _, _, _) = getCheckpoints(epoch)
|
2021-10-18 16:37:27 +00:00
|
|
|
putCheckpointsInBlockRoots(state.phase0Data.data, [c1, c2])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Save for final checks
|
2021-06-11 17:51:46 +00:00
|
|
|
let old_finalized = getStateField(state, finalized_checkpoint)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Mock the state
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state, previous_justified_checkpoint) = c2
|
|
|
|
getStateField(state, current_justified_checkpoint) = c2
|
2022-01-06 07:38:40 +00:00
|
|
|
getStateField(state, justification_bits) = JustificationBits(0'u8) # Bitvector of length 4
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock 3rd as justified
|
|
|
|
# indices are pre-shift
|
2022-01-06 07:38:40 +00:00
|
|
|
uint8(getStateField(state, justification_bits)).setBit 0
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock the 2nd latest epoch as justifiable, with 3rd as the source
|
|
|
|
addMockAttestations(
|
2021-10-18 16:37:27 +00:00
|
|
|
state.phase0Data.data,
|
2019-08-28 12:07:00 +00:00
|
|
|
epoch = epoch - 1,
|
|
|
|
source = c2,
|
|
|
|
target = c1,
|
|
|
|
sufficient_support = sufficient_support
|
|
|
|
)
|
|
|
|
|
|
|
|
# State transition
|
|
|
|
transitionEpochUntilJustificationFinalization(state)
|
|
|
|
|
|
|
|
# Checks
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, previous_justified_checkpoint) == c2 # changed to old current
|
2019-08-28 12:07:00 +00:00
|
|
|
if sufficient_support:
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, current_justified_checkpoint) == c1 # changed to second latest
|
|
|
|
doAssert getStateField(state, finalized_checkpoint) == c2 # finalized old previous justified epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(state, current_justified_checkpoint) == c2 # still old current
|
|
|
|
doAssert getStateField(state, finalized_checkpoint) == old_finalized # no new finalized checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-04-22 23:35:55 +00:00
|
|
|
proc payload =
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "[Unit - Spec - Epoch processing] Justification and Finalization " & preset():
|
2020-04-22 23:35:55 +00:00
|
|
|
echo " Finalization rules are detailed at https://github.com/protolambda/eth2-docs#justification-and-finalization"
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-04-22 23:35:55 +00:00
|
|
|
const NumValidators = uint64(8) * SLOTS_PER_EPOCH
|
2021-09-13 22:56:37 +00:00
|
|
|
let genesisState = initGenesisState(NumValidators)
|
2021-06-11 17:51:46 +00:00
|
|
|
doAssert getStateField(genesisState[], validators).lenu64 == NumValidators
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-04-22 23:35:55 +00:00
|
|
|
setup:
|
2020-07-01 17:00:14 +00:00
|
|
|
var state = assignClone(genesisState[])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test " Rule I - 234 finalization with enough support":
|
2020-04-23 18:58:54 +00:00
|
|
|
finalizeOn234(state[], Epoch 5, sufficient_support = true)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test " Rule I - 234 finalization without support":
|
2020-04-23 18:58:54 +00:00
|
|
|
finalizeOn234(state[], Epoch 5, sufficient_support = false)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test " Rule II - 23 finalization with enough support":
|
2020-04-23 18:58:54 +00:00
|
|
|
finalizeOn23(state[], Epoch 4, sufficient_support = true)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test " Rule II - 23 finalization without support":
|
2020-04-23 18:58:54 +00:00
|
|
|
finalizeOn23(state[], Epoch 4, sufficient_support = false)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test " Rule III - 123 finalization with enough support":
|
2020-04-23 18:58:54 +00:00
|
|
|
finalizeOn123(state[], Epoch 6, sufficient_support = true)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test " Rule III - 123 finalization without support":
|
2020-04-23 18:58:54 +00:00
|
|
|
finalizeOn123(state[], Epoch 6, sufficient_support = false)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test " Rule IV - 12 finalization with enough support":
|
2020-04-23 18:58:54 +00:00
|
|
|
finalizeOn12(state[], Epoch 3, sufficient_support = true)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test " Rule IV - 12 finalization without support":
|
2020-04-23 18:58:54 +00:00
|
|
|
finalizeOn12(state[], Epoch 3, sufficient_support = false)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-04-22 23:35:55 +00:00
|
|
|
payload()
|