2020-09-24 17:05:49 +00:00
|
|
|
# beacon_chain
|
2023-01-19 22:00:40 +00:00
|
|
|
# Copyright (c) 2020-2023 Status Research & Development GmbH
|
2020-09-24 17:05:49 +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.
|
|
|
|
|
|
|
|
{.used.}
|
|
|
|
|
2022-08-23 15:30:46 +00:00
|
|
|
import
|
|
|
|
../beacon_chain/spec/[
|
|
|
|
datatypes/base, forks, presets, signatures, state_transition],
|
|
|
|
../beacon_chain/consensus_object_pools/[
|
|
|
|
block_quarantine, blockchain_dag, exit_pool],
|
|
|
|
"."/[testutil, testblockutil, testdbutil]
|
|
|
|
|
|
|
|
func makeSignedBeaconBlockHeader(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
|
|
|
proposer_index: uint64, parent_root: Eth2Digest): SignedBeaconBlockHeader =
|
|
|
|
let tmp = BeaconBlockHeader(
|
|
|
|
slot: slot, proposer_index: proposer_index, parent_root: parent_root)
|
|
|
|
|
|
|
|
SignedBeaconBlockHeader(
|
|
|
|
message: tmp,
|
|
|
|
signature: get_block_signature(
|
|
|
|
fork, genesis_validators_root, slot, hash_tree_root(tmp),
|
|
|
|
MockPrivKeys[proposer_index]).toValidatorSig())
|
|
|
|
|
|
|
|
func makeIndexedAttestation(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
|
|
|
validator_index: uint64, beacon_block_root: Eth2Digest): IndexedAttestation =
|
|
|
|
let tmp = AttestationData(slot: slot, beacon_block_root: beacon_block_root)
|
|
|
|
|
|
|
|
IndexedAttestation(
|
|
|
|
data: tmp,
|
|
|
|
attesting_indices: List[uint64, Limit MAX_VALIDATORS_PER_COMMITTEE](@[validator_index]),
|
|
|
|
signature: get_attestation_signature(
|
|
|
|
fork, genesis_validators_root, tmp,
|
|
|
|
MockPrivKeys[validator_index]).toValidatorSig)
|
|
|
|
|
|
|
|
func makeSignedVoluntaryExit(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, epoch: Epoch,
|
|
|
|
validator_index: uint64): SignedVoluntaryExit =
|
|
|
|
let tmp = VoluntaryExit(epoch: epoch, validator_index: validator_index)
|
|
|
|
|
|
|
|
SignedVoluntaryExit(
|
|
|
|
message: tmp,
|
|
|
|
signature: get_voluntary_exit_signature(
|
|
|
|
fork, genesis_validators_root, tmp,
|
|
|
|
MockPrivKeys[validator_index]).toValidatorSig)
|
2020-09-24 17:05:49 +00:00
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
from std/sequtils import allIt
|
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
suite "Validator change pool testing suite":
|
2020-09-24 17:05:49 +00:00
|
|
|
setup:
|
2021-10-18 16:37:27 +00:00
|
|
|
let
|
2022-08-23 15:30:46 +00:00
|
|
|
cfg = block:
|
|
|
|
var tmp = defaultRuntimeConfig
|
|
|
|
tmp.ALTAIR_FORK_EPOCH = Epoch(tmp.SHARD_COMMITTEE_PERIOD)
|
|
|
|
tmp.BELLATRIX_FORK_EPOCH = Epoch(tmp.SHARD_COMMITTEE_PERIOD) + 1
|
2023-01-19 22:00:40 +00:00
|
|
|
tmp.CAPELLA_FORK_EPOCH = Epoch(tmp.SHARD_COMMITTEE_PERIOD) + 2
|
2022-08-23 15:30:46 +00:00
|
|
|
tmp
|
|
|
|
|
2021-12-20 19:20:31 +00:00
|
|
|
validatorMonitor = newClone(ValidatorMonitor.init())
|
2021-10-18 16:37:27 +00:00
|
|
|
dag = init(
|
2022-08-23 15:30:46 +00:00
|
|
|
ChainDAGRef, cfg, makeTestDB(SLOTS_PER_EPOCH * 3),
|
2021-12-20 19:20:31 +00:00
|
|
|
validatorMonitor, {})
|
2022-08-23 15:30:46 +00:00
|
|
|
fork = dag.forkAtEpoch(Epoch(0))
|
|
|
|
genesis_validators_root = dag.genesis_validators_root
|
2023-01-19 22:00:40 +00:00
|
|
|
pool = newClone(ValidatorChangePool.init(dag))
|
2021-10-18 16:37:27 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
test "addValidatorChangeMessage/getProposerSlashingMessage":
|
2020-09-24 17:05:49 +00:00
|
|
|
for i in 0'u64 .. MAX_PROPOSER_SLASHINGS + 5:
|
|
|
|
for j in 0'u64 .. i:
|
2022-08-23 15:30:46 +00:00
|
|
|
let
|
|
|
|
msg = ProposerSlashing(
|
|
|
|
signed_header_1:
|
|
|
|
makeSignedBeaconBlockHeader(
|
|
|
|
fork, genesis_validators_root, Slot(1), j, makeFakeHash(0)),
|
|
|
|
signed_header_2:
|
|
|
|
makeSignedBeaconBlockHeader(
|
|
|
|
fork, genesis_validators_root, Slot(1), j, makeFakeHash(1)))
|
2021-11-05 15:39:47 +00:00
|
|
|
|
|
|
|
if i == 0:
|
|
|
|
check not pool[].isSeen(msg)
|
|
|
|
|
|
|
|
pool[].addMessage(msg)
|
|
|
|
check: pool[].isSeen(msg)
|
2022-03-16 07:20:40 +00:00
|
|
|
withState(dag.headState):
|
2021-10-18 16:37:27 +00:00
|
|
|
check:
|
2023-01-19 22:00:40 +00:00
|
|
|
pool[].getBeaconBlockValidatorChanges(
|
2022-09-10 06:12:07 +00:00
|
|
|
cfg, forkyState.data).proposer_slashings.lenu64 ==
|
2021-10-18 16:37:27 +00:00
|
|
|
min(i + 1, MAX_PROPOSER_SLASHINGS)
|
2023-01-19 22:00:40 +00:00
|
|
|
pool[].getBeaconBlockValidatorChanges(
|
2022-09-10 06:12:07 +00:00
|
|
|
cfg, forkyState.data).proposer_slashings.len == 0
|
2020-09-24 17:05:49 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
test "addValidatorChangeMessage/getAttesterSlashingMessage":
|
2020-09-24 17:05:49 +00:00
|
|
|
for i in 0'u64 .. MAX_ATTESTER_SLASHINGS + 5:
|
|
|
|
for j in 0'u64 .. i:
|
2022-08-23 15:30:46 +00:00
|
|
|
let
|
|
|
|
msg = AttesterSlashing(
|
|
|
|
attestation_1: makeIndexedAttestation(
|
|
|
|
fork, genesis_validators_root, Slot(1), j, makeFakeHash(0)),
|
|
|
|
attestation_2: makeIndexedAttestation(
|
|
|
|
fork, genesis_validators_root, Slot(1), j, makeFakeHash(1)))
|
2021-11-05 15:39:47 +00:00
|
|
|
|
|
|
|
if i == 0:
|
|
|
|
check not pool[].isSeen(msg)
|
|
|
|
|
|
|
|
pool[].addMessage(msg)
|
|
|
|
check: pool[].isSeen(msg)
|
2022-03-16 07:20:40 +00:00
|
|
|
withState(dag.headState):
|
2021-10-18 16:37:27 +00:00
|
|
|
check:
|
2023-01-19 22:00:40 +00:00
|
|
|
pool[].getBeaconBlockValidatorChanges(
|
2022-09-10 06:12:07 +00:00
|
|
|
cfg, forkyState.data).attester_slashings.lenu64 ==
|
2021-10-18 16:37:27 +00:00
|
|
|
min(i + 1, MAX_ATTESTER_SLASHINGS)
|
2023-01-19 22:00:40 +00:00
|
|
|
pool[].getBeaconBlockValidatorChanges(
|
2022-09-10 06:12:07 +00:00
|
|
|
cfg, forkyState.data).attester_slashings.len == 0
|
2020-09-24 17:05:49 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
test "addValidatorChangeMessage/getVoluntaryExitMessage":
|
2022-08-23 15:30:46 +00:00
|
|
|
# Need to advance state or it will not accept voluntary exits
|
|
|
|
var
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
process_slots(
|
|
|
|
dag.cfg, dag.headState,
|
|
|
|
Epoch(dag.cfg.SHARD_COMMITTEE_PERIOD).start_slot + 1, cache, info,
|
|
|
|
{}).expect("ok")
|
|
|
|
let
|
|
|
|
fork = dag.forkAtEpoch(dag.headState.get_current_epoch())
|
|
|
|
|
2020-09-24 17:05:49 +00:00
|
|
|
for i in 0'u64 .. MAX_VOLUNTARY_EXITS + 5:
|
|
|
|
for j in 0'u64 .. i:
|
2022-08-23 15:30:46 +00:00
|
|
|
let msg = makeSignedVoluntaryExit(
|
|
|
|
fork, genesis_validators_root, dag.headState.get_current_epoch(), j)
|
2021-11-05 15:39:47 +00:00
|
|
|
if i == 0:
|
|
|
|
check not pool[].isSeen(msg)
|
|
|
|
|
|
|
|
pool[].addMessage(msg)
|
|
|
|
check: pool[].isSeen(msg)
|
2022-08-23 15:30:46 +00:00
|
|
|
|
2022-03-16 07:20:40 +00:00
|
|
|
withState(dag.headState):
|
2021-10-18 16:37:27 +00:00
|
|
|
check:
|
2023-01-19 22:00:40 +00:00
|
|
|
pool[].getBeaconBlockValidatorChanges(
|
2022-09-10 06:12:07 +00:00
|
|
|
cfg, forkyState.data).voluntary_exits.lenu64 ==
|
2021-10-18 16:37:27 +00:00
|
|
|
min(i + 1, MAX_VOLUNTARY_EXITS)
|
2023-01-19 22:00:40 +00:00
|
|
|
pool[].getBeaconBlockValidatorChanges(
|
2022-09-10 06:12:07 +00:00
|
|
|
cfg, forkyState.data).voluntary_exits.len == 0
|
2022-08-23 15:30:46 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
test "addValidatorChangeMessage/getBlsToExecutionChange (pre-capella)":
|
|
|
|
# Need to advance state or it will not accept voluntary exits
|
|
|
|
var
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
process_slots(
|
|
|
|
dag.cfg, dag.headState,
|
|
|
|
Epoch(dag.cfg.SHARD_COMMITTEE_PERIOD).start_slot + 1 + SLOTS_PER_EPOCH * 1,
|
|
|
|
cache, info, {}).expect("ok")
|
|
|
|
let fork = dag.forkAtEpoch(dag.headState.get_current_epoch())
|
|
|
|
|
|
|
|
for i in 0'u64 .. MAX_BLS_TO_EXECUTION_CHANGES + 5:
|
|
|
|
for j in 0'u64 .. i:
|
2023-02-03 15:28:28 +00:00
|
|
|
var msg = SignedBLSToExecutionChange(
|
|
|
|
message: BLSToExecutionChange(
|
|
|
|
validator_index: j,
|
|
|
|
from_bls_pubkey: MockPubKeys[j]))
|
|
|
|
msg.signature = toValidatorSig(get_bls_to_execution_change_signature(
|
|
|
|
dag.cfg.genesisFork(), dag.genesis_validators_root, msg.message,
|
|
|
|
MockPrivKeys[msg.message.validator_index]))
|
2023-01-19 22:00:40 +00:00
|
|
|
if i == 0:
|
|
|
|
check not pool[].isSeen(msg)
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
pool[].addMessage(msg, false)
|
2023-01-19 22:00:40 +00:00
|
|
|
check: pool[].isSeen(msg)
|
|
|
|
|
|
|
|
withState(dag.headState):
|
|
|
|
# Too early to get BLS to execution changes for blocks
|
|
|
|
check pool[].getBeaconBlockValidatorChanges(
|
|
|
|
cfg, forkyState.data).bls_to_execution_changes.len == 0
|
|
|
|
|
|
|
|
test "addValidatorChangeMessage/getBlsToExecutionChange (post-capella)":
|
|
|
|
# Need to advance state or it will not accept voluntary exits
|
|
|
|
var
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
process_slots(
|
|
|
|
dag.cfg, dag.headState,
|
|
|
|
Epoch(dag.cfg.SHARD_COMMITTEE_PERIOD).start_slot + 1 + SLOTS_PER_EPOCH * 2,
|
|
|
|
cache, info, {}).expect("ok")
|
|
|
|
let fork = dag.forkAtEpoch(dag.headState.get_current_epoch())
|
|
|
|
|
|
|
|
for i in 0'u64 .. MAX_BLS_TO_EXECUTION_CHANGES + 5:
|
2023-02-03 15:28:28 +00:00
|
|
|
var priorityMessages: seq[SignedBLSToExecutionChange]
|
2023-01-19 22:00:40 +00:00
|
|
|
for j in 0'u64 .. i:
|
2023-02-03 15:28:28 +00:00
|
|
|
var msg = SignedBLSToExecutionChange(
|
|
|
|
message: BLSToExecutionChange(
|
|
|
|
validator_index: j,
|
|
|
|
from_bls_pubkey: MockPubKeys[j]))
|
|
|
|
msg.signature = toValidatorSig(get_bls_to_execution_change_signature(
|
|
|
|
dag.cfg.genesisFork(), dag.genesis_validators_root, msg.message,
|
|
|
|
MockPrivKeys[msg.message.validator_index]))
|
2023-01-19 22:00:40 +00:00
|
|
|
if i == 0:
|
|
|
|
check not pool[].isSeen(msg)
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
let isPriorityMessage = i mod 2 == 0
|
|
|
|
pool[].addMessage(msg, localPriorityMessage = isPriorityMessage)
|
|
|
|
if isPriorityMessage:
|
|
|
|
priorityMessages.add msg
|
2023-01-19 22:00:40 +00:00
|
|
|
check: pool[].isSeen(msg)
|
|
|
|
|
|
|
|
withState(dag.headState):
|
2023-02-03 15:28:28 +00:00
|
|
|
let blsToExecutionChanges = pool[].getBeaconBlockValidatorChanges(
|
|
|
|
cfg, forkyState.data).bls_to_execution_changes
|
2023-01-19 22:00:40 +00:00
|
|
|
check:
|
2023-02-03 15:28:28 +00:00
|
|
|
blsToExecutionChanges.lenu64 == min(i + 1, MAX_BLS_TO_EXECUTION_CHANGES)
|
|
|
|
|
|
|
|
# Ensure priority of API to gossip messages is observed
|
|
|
|
allIt(priorityMessages, pool[].isSeen(it))
|
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
pool[].getBeaconBlockValidatorChanges(
|
|
|
|
cfg, forkyState.data).bls_to_execution_changes.len == 0
|
|
|
|
|
2022-08-23 15:30:46 +00:00
|
|
|
test "pre-pre-fork voluntary exit":
|
|
|
|
var
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
process_slots(
|
|
|
|
dag.cfg, dag.headState,
|
|
|
|
Epoch(dag.cfg.SHARD_COMMITTEE_PERIOD).start_slot + 1, cache, info,
|
|
|
|
{}).expect("ok")
|
|
|
|
|
|
|
|
let msg = makeSignedVoluntaryExit(
|
|
|
|
fork, genesis_validators_root, dag.headState.get_current_epoch(), 0)
|
|
|
|
|
|
|
|
pool[].addMessage(msg)
|
|
|
|
check: pool[].isSeen(msg)
|
|
|
|
|
|
|
|
process_slots(
|
|
|
|
dag.cfg, dag.headState,
|
|
|
|
(Epoch(dag.cfg.SHARD_COMMITTEE_PERIOD) + 1).start_slot + 1, cache, info,
|
|
|
|
{}).expect("ok")
|
|
|
|
|
|
|
|
withState(dag.headState):
|
|
|
|
check:
|
|
|
|
# Message signed with a (fork-2) domain can no longer be added as that
|
|
|
|
# fork is not present in the BeaconState and thus fails transition
|
2023-01-19 22:00:40 +00:00
|
|
|
pool[].getBeaconBlockValidatorChanges(
|
2022-09-10 06:12:07 +00:00
|
|
|
cfg, forkyState.data).voluntary_exits.lenu64 == 0
|