2020-09-24 17:05:49 +00:00
|
|
|
# beacon_chain
|
2022-06-07 17:05:06 +00:00
|
|
|
# Copyright (c) 2020-2022 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
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "Exit 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
|
|
|
|
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
|
2021-10-18 16:37:27 +00:00
|
|
|
pool = newClone(ExitPool.init(dag))
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "addExitMessage/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:
|
2022-09-10 06:12:07 +00:00
|
|
|
pool[].getBeaconBlockExits(
|
|
|
|
cfg, forkyState.data).proposer_slashings.lenu64 ==
|
2021-10-18 16:37:27 +00:00
|
|
|
min(i + 1, MAX_PROPOSER_SLASHINGS)
|
2022-09-10 06:12:07 +00:00
|
|
|
pool[].getBeaconBlockExits(
|
|
|
|
cfg, forkyState.data).proposer_slashings.len == 0
|
2020-09-24 17:05:49 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "addExitMessage/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:
|
2022-09-10 06:12:07 +00:00
|
|
|
pool[].getBeaconBlockExits(
|
|
|
|
cfg, forkyState.data).attester_slashings.lenu64 ==
|
2021-10-18 16:37:27 +00:00
|
|
|
min(i + 1, MAX_ATTESTER_SLASHINGS)
|
2022-09-10 06:12:07 +00:00
|
|
|
pool[].getBeaconBlockExits(
|
|
|
|
cfg, forkyState.data).attester_slashings.len == 0
|
2020-09-24 17:05:49 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "addExitMessage/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
|
|
|
# Cannot exit until
|
|
|
|
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:
|
2022-09-10 06:12:07 +00:00
|
|
|
pool[].getBeaconBlockExits(
|
|
|
|
cfg, forkyState.data).voluntary_exits.lenu64 ==
|
2021-10-18 16:37:27 +00:00
|
|
|
min(i + 1, MAX_VOLUNTARY_EXITS)
|
2022-09-10 06:12:07 +00:00
|
|
|
pool[].getBeaconBlockExits(
|
|
|
|
cfg, forkyState.data).voluntary_exits.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
|
2022-09-10 06:12:07 +00:00
|
|
|
pool[].getBeaconBlockExits(
|
|
|
|
cfg, forkyState.data).voluntary_exits.lenu64 == 0
|