2020-09-24 17:05:49 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2020-2024 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.
|
|
|
|
|
2024-02-29 13:24:08 +00:00
|
|
|
{.push raises: [].}
|
2020-09-24 17:05:49 +00:00
|
|
|
{.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/[
|
2023-12-23 05:55:47 +00:00
|
|
|
block_quarantine, blockchain_dag, validator_change_pool],
|
2022-08-23 15:30:46 +00:00
|
|
|
"."/[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())
|
|
|
|
|
2024-09-24 09:01:40 +00:00
|
|
|
func makePhase0IndexedAttestation(
|
2022-08-23 15:30:46 +00:00
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
2024-04-21 05:49:11 +00:00
|
|
|
validator_index: uint64, beacon_block_root: Eth2Digest):
|
|
|
|
phase0.IndexedAttestation =
|
2022-08-23 15:30:46 +00:00
|
|
|
let tmp = AttestationData(slot: slot, beacon_block_root: beacon_block_root)
|
|
|
|
|
2024-04-21 05:49:11 +00:00
|
|
|
phase0.IndexedAttestation(
|
2022-08-23 15:30:46 +00:00
|
|
|
data: tmp,
|
2024-04-21 05:49:11 +00:00
|
|
|
attesting_indices:
|
|
|
|
List[uint64, Limit MAX_VALIDATORS_PER_COMMITTEE](@[validator_index]),
|
2022-08-23 15:30:46 +00:00
|
|
|
signature: get_attestation_signature(
|
|
|
|
fork, genesis_validators_root, tmp,
|
|
|
|
MockPrivKeys[validator_index]).toValidatorSig)
|
|
|
|
|
2024-09-24 09:01:40 +00:00
|
|
|
func makeElectraIndexedAttestation(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, slot: Slot,
|
|
|
|
validator_index: uint64, beacon_block_root: Eth2Digest):
|
|
|
|
electra.IndexedAttestation =
|
|
|
|
let tmp = AttestationData(slot: slot, beacon_block_root: beacon_block_root)
|
|
|
|
|
|
|
|
electra.IndexedAttestation(
|
|
|
|
data: tmp,
|
|
|
|
attesting_indices:
|
|
|
|
List[uint64, Limit MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT](
|
|
|
|
@[validator_index]),
|
|
|
|
signature: get_attestation_signature(
|
|
|
|
fork, genesis_validators_root, tmp,
|
|
|
|
MockPrivKeys[validator_index]).toValidatorSig)
|
|
|
|
|
2022-08-23 15:30:46 +00:00
|
|
|
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
|
2024-09-24 09:01:40 +00:00
|
|
|
tmp.DENEB_FORK_EPOCH = Epoch(tmp.SHARD_COMMITTEE_PERIOD) + 3
|
|
|
|
tmp.ELECTRA_FORK_EPOCH = Epoch(tmp.SHARD_COMMITTEE_PERIOD) + 4
|
2024-11-13 03:29:14 +00:00
|
|
|
tmp.FULU_FORK_EPOCH = Epoch(tmp.SHARD_COMMITTEE_PERIOD) + 5
|
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)
|
2020-09-24 17:05:49 +00:00
|
|
|
|
2024-09-24 09:01:40 +00:00
|
|
|
test "addValidatorChangeMessage/getAttesterSlashingMessage (Phase 0)":
|
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
|
2024-04-21 05:49:11 +00:00
|
|
|
msg = phase0.AttesterSlashing(
|
2024-09-24 09:01:40 +00:00
|
|
|
attestation_1: makePhase0IndexedAttestation(
|
2022-08-23 15:30:46 +00:00
|
|
|
fork, genesis_validators_root, Slot(1), j, makeFakeHash(0)),
|
2024-09-24 09:01:40 +00:00
|
|
|
attestation_2: makePhase0IndexedAttestation(
|
2022-08-23 15:30:46 +00:00
|
|
|
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(
|
2024-09-24 09:01:40 +00:00
|
|
|
cfg, forkyState.data).phase0_attester_slashings.lenu64 ==
|
2021-10-18 16:37:27 +00:00
|
|
|
min(i + 1, MAX_ATTESTER_SLASHINGS)
|
2020-09-24 17:05:49 +00:00
|
|
|
|
2024-09-24 09:01:40 +00:00
|
|
|
test "addValidatorChangeMessage/getAttesterSlashingMessage (Electra)":
|
|
|
|
var
|
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
process_slots(
|
|
|
|
dag.cfg, dag.headState,
|
|
|
|
Epoch(dag.cfg.SHARD_COMMITTEE_PERIOD).start_slot + 1 + SLOTS_PER_EPOCH * 5,
|
|
|
|
cache, info, {}).expect("ok")
|
|
|
|
let fork = dag.forkAtEpoch(dag.headState.get_current_epoch())
|
|
|
|
|
|
|
|
for i in 0'u64 .. MAX_ATTESTER_SLASHINGS_ELECTRA + 5:
|
|
|
|
for j in 0'u64 .. i:
|
|
|
|
let
|
|
|
|
msg = electra.AttesterSlashing(
|
|
|
|
attestation_1: makeElectraIndexedAttestation(
|
|
|
|
fork, genesis_validators_root, Slot(1), j, makeFakeHash(0)),
|
|
|
|
attestation_2: makeElectraIndexedAttestation(
|
|
|
|
fork, genesis_validators_root, Slot(1), j, makeFakeHash(1)))
|
|
|
|
|
|
|
|
if i == 0:
|
|
|
|
check not pool[].isSeen(msg)
|
|
|
|
|
|
|
|
pool[].addMessage(msg)
|
|
|
|
check: pool[].isSeen(msg)
|
|
|
|
withState(dag.headState):
|
|
|
|
check:
|
|
|
|
pool[].getBeaconBlockValidatorChanges(
|
|
|
|
cfg, forkyState.data).electra_attester_slashings.lenu64 ==
|
|
|
|
min(i + 1, MAX_ATTESTER_SLASHINGS_ELECTRA)
|
|
|
|
|
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")
|
2024-09-24 09:01:40 +00:00
|
|
|
let fork = dag.forkAtEpoch(dag.headState.get_current_epoch())
|
2022-08-23 15:30:46 +00:00
|
|
|
|
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)
|
2022-08-23 15:30:46 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
test "addValidatorChangeMessage/getBlsToExecutionChange (pre-capella)":
|
2024-09-24 09:01:40 +00:00
|
|
|
# Need to advance state or it will not accept execution changes
|
2023-01-19 22:00:40 +00:00
|
|
|
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)":
|
2024-09-24 09:01:40 +00:00
|
|
|
# Need to advance state or it will not accept execution changes
|
2023-01-19 22:00:40 +00:00
|
|
|
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))
|
|
|
|
|
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(
|
2024-04-21 05:49:11 +00:00
|
|
|
cfg, forkyState.data).voluntary_exits.lenu64 == 0
|