2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2020-04-30 06:44:19 +00:00
|
|
|
# Copyright (c) 2018-2020 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
|
2019-11-14 10:47:55 +00:00
|
|
|
../../beacon_chain/spec/datatypes,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Test helpers
|
|
|
|
../mocking/[mock_genesis],
|
|
|
|
./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
|
|
|
|
|
2020-04-30 06:44:19 +00:00
|
|
|
proc finalizeOn234(state: var HashedBeaconState, epoch: Epoch, sufficient_support: bool) =
|
2019-08-28 12:07:00 +00:00
|
|
|
## Check finalization on rule 1 "234"
|
|
|
|
doAssert epoch > 4
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.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)
|
2020-04-30 06:44:19 +00:00
|
|
|
putCheckpointsInBlockRoots(state.data, [c1, c2, c3, c4])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Save for final checks
|
2020-04-30 06:44:19 +00:00
|
|
|
let old_finalized = state.data.finalized_checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Mock the state
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.previous_justified_checkpoint = c4
|
|
|
|
state.data.current_justified_checkpoint = c3
|
|
|
|
state.data.justification_bits = 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
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.justification_bits.setBit 1
|
|
|
|
state.data.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(
|
2020-04-30 06:44:19 +00:00
|
|
|
state.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
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.previous_justified_checkpoint == c3 # changed to old current
|
2019-08-28 12:07:00 +00:00
|
|
|
if sufficient_support:
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.current_justified_checkpoint == c2 # changed to second latest
|
|
|
|
doAssert state.data.finalized_checkpoint == c4 # finalized old previous justified epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.current_justified_checkpoint == c3 # still old current
|
|
|
|
doAssert state.data.finalized_checkpoint == old_finalized # no new finalized checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-04-30 06:44:19 +00:00
|
|
|
proc finalizeOn23(state: var HashedBeaconState, epoch: Epoch, sufficient_support: bool) =
|
2019-08-28 12:07:00 +00:00
|
|
|
## Check finalization on rule 2 "23"
|
|
|
|
doAssert epoch > 3
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.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)
|
2020-04-30 06:44:19 +00:00
|
|
|
putCheckpointsInBlockRoots(state.data, [c1, c2, c3])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Save for final checks
|
2020-04-30 06:44:19 +00:00
|
|
|
let old_finalized = state.data.finalized_checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Mock the state
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.previous_justified_checkpoint = c3
|
|
|
|
state.data.current_justified_checkpoint = c3
|
|
|
|
state.data.justification_bits = 0'u8 # Bitvector of length 4
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock 3rd as justified
|
|
|
|
# indices are pre-shift
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.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(
|
2020-04-30 06:44:19 +00:00
|
|
|
state.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
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.previous_justified_checkpoint == c3 # changed to old current
|
2019-08-28 12:07:00 +00:00
|
|
|
if sufficient_support:
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.current_justified_checkpoint == c2 # changed to second latest
|
|
|
|
doAssert state.data.finalized_checkpoint == c3 # finalized old previous justified epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.current_justified_checkpoint == c3 # still old current
|
|
|
|
doAssert state.data.finalized_checkpoint == old_finalized # no new finalized checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-04-30 06:44:19 +00:00
|
|
|
proc finalizeOn123(state: var HashedBeaconState, 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
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.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)
|
2020-04-30 06:44:19 +00:00
|
|
|
putCheckpointsInBlockRoots(state.data, [c1, c2, c3, c4, c5])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Save for final checks
|
2020-04-30 06:44:19 +00:00
|
|
|
let old_finalized = state.data.finalized_checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Mock the state
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.previous_justified_checkpoint = c5
|
|
|
|
state.data.current_justified_checkpoint = c3
|
|
|
|
state.data.justification_bits = 0'u8 # Bitvector of length 4
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock 3rd as justified
|
|
|
|
# indices are pre-shift
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.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(
|
2020-04-30 06:44:19 +00:00
|
|
|
state.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(
|
2020-04-30 06:44:19 +00:00
|
|
|
state.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
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.previous_justified_checkpoint == c3 # changed to old current
|
2019-08-28 12:07:00 +00:00
|
|
|
if sufficient_support:
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.current_justified_checkpoint == c1 # changed to second latest
|
|
|
|
doAssert state.data.finalized_checkpoint == c3 # finalized old previous justified epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.current_justified_checkpoint == c3 # still old current
|
|
|
|
doAssert state.data.finalized_checkpoint == old_finalized # no new finalized checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-04-30 06:44:19 +00:00
|
|
|
proc finalizeOn12(state: var HashedBeaconState, 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
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.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)
|
2020-04-30 06:44:19 +00:00
|
|
|
putCheckpointsInBlockRoots(state.data, [c1, c2])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Save for final checks
|
2020-04-30 06:44:19 +00:00
|
|
|
let old_finalized = state.data.finalized_checkpoint
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# Mock the state
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.previous_justified_checkpoint = c2
|
|
|
|
state.data.current_justified_checkpoint = c2
|
|
|
|
state.data.justification_bits = 0'u8 # Bitvector of length 4
|
2019-08-28 12:07:00 +00:00
|
|
|
# mock 3rd as justified
|
|
|
|
# indices are pre-shift
|
2020-04-30 06:44:19 +00:00
|
|
|
state.data.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(
|
2020-04-30 06:44:19 +00:00
|
|
|
state.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
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.previous_justified_checkpoint == c2 # changed to old current
|
2019-08-28 12:07:00 +00:00
|
|
|
if sufficient_support:
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.current_justified_checkpoint == c1 # changed to second latest
|
|
|
|
doAssert state.data.finalized_checkpoint == c2 # finalized old previous justified epoch
|
2019-08-28 12:07:00 +00:00
|
|
|
else:
|
2020-04-30 06:44:19 +00:00
|
|
|
doAssert state.data.current_justified_checkpoint == c2 # still old current
|
|
|
|
doAssert state.data.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
|
2020-05-28 08:28:14 +00:00
|
|
|
let genesisState = newClone(initGenesisState(NumValidators))
|
2020-07-26 18:55:48 +00:00
|
|
|
doAssert genesisState.data.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()
|