2021-04-26 20:39:44 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# 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.}
|
|
|
|
|
|
|
|
import
|
2021-09-28 07:44:20 +00:00
|
|
|
# Standard library
|
|
|
|
std/sequtils,
|
2021-04-26 20:39:44 +00:00
|
|
|
# Status lib
|
|
|
|
unittest2,
|
2021-08-18 18:57:58 +00:00
|
|
|
chronicles, chronos,
|
2021-09-17 00:13:52 +00:00
|
|
|
eth/keys, taskpools,
|
2021-04-26 20:39:44 +00:00
|
|
|
# Internal
|
2021-10-19 14:09:26 +00:00
|
|
|
../beacon_chain/[beacon_clock],
|
2021-04-26 20:39:44 +00:00
|
|
|
../beacon_chain/gossip_processing/[gossip_validation, batch_validation],
|
|
|
|
../beacon_chain/fork_choice/[fork_choice_types, fork_choice],
|
|
|
|
../beacon_chain/consensus_object_pools/[
|
2021-09-28 07:44:20 +00:00
|
|
|
block_quarantine, blockchain_dag, block_clearance, attestation_pool,
|
|
|
|
sync_committee_msg_pool],
|
|
|
|
../beacon_chain/spec/datatypes/[phase0, altair],
|
2021-11-05 07:34:34 +00:00
|
|
|
../beacon_chain/spec/[state_transition, helpers, network, validator],
|
2021-09-28 07:44:20 +00:00
|
|
|
../beacon_chain/validators/validator_pool,
|
2021-04-26 20:39:44 +00:00
|
|
|
# Test utilities
|
2021-04-28 16:41:02 +00:00
|
|
|
./testutil, ./testdbutil, ./testblockutil
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
proc pruneAtFinalization(dag: ChainDAGRef, attPool: AttestationPool) =
|
|
|
|
if dag.needStateCachesAndForkChoicePruning():
|
|
|
|
dag.pruneStateCachesDAG()
|
|
|
|
# pool[].prune() # We test logic without att_1_0 pool / fork choice pruning
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "Gossip validation " & preset():
|
2021-04-26 20:39:44 +00:00
|
|
|
setup:
|
|
|
|
# Genesis state that results in 3 members per committee
|
|
|
|
var
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
dag = init(ChainDAGRef, defaultRuntimeConfig, makeTestDB(SLOTS_PER_EPOCH * 3), {})
|
2021-09-17 00:13:52 +00:00
|
|
|
taskpool = Taskpool.new()
|
|
|
|
quarantine = QuarantineRef.init(keys.newRng(), taskpool)
|
2021-06-01 11:13:40 +00:00
|
|
|
pool = newClone(AttestationPool.init(dag, quarantine))
|
|
|
|
state = newClone(dag.headState)
|
2021-04-26 20:39:44 +00:00
|
|
|
cache = StateCache()
|
2021-10-13 14:24:36 +00:00
|
|
|
info = ForkedEpochInfo()
|
2021-09-17 00:13:52 +00:00
|
|
|
batchCrypto = BatchCrypto.new(keys.newRng(), eager = proc(): bool = false, taskpool)
|
2021-04-26 20:39:44 +00:00
|
|
|
# Slot 0 is a finalized slot - won't be making attestations for it..
|
|
|
|
check:
|
2021-06-11 17:51:46 +00:00
|
|
|
process_slots(
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
defaultRuntimeConfig, state.data, getStateField(state.data, slot) + 1,
|
2021-10-13 14:24:36 +00:00
|
|
|
cache, info, {})
|
2021-04-26 20:39:44 +00:00
|
|
|
|
2021-08-28 09:00:00 +00:00
|
|
|
test "Any committee index is valid":
|
|
|
|
template committee(idx: uint64): untyped =
|
|
|
|
get_beacon_committee(
|
|
|
|
dag.headState.data, dag.head.slot, idx.CommitteeIndex, cache)
|
|
|
|
|
|
|
|
template committeeLen(idx: uint64): untyped =
|
|
|
|
get_beacon_committee_len(
|
|
|
|
dag.headState.data, dag.head.slot, idx.CommitteeIndex, cache)
|
|
|
|
|
|
|
|
check:
|
|
|
|
committee(0).len > 0
|
|
|
|
committee(10000).len == 0
|
|
|
|
committee(uint64.high).len == 0
|
|
|
|
|
|
|
|
check:
|
|
|
|
committeeLen(2) > 0
|
|
|
|
committeeLen(10000) == 0
|
|
|
|
committeeLen(uint64.high) == 0
|
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
test "Validation sanity":
|
2021-04-26 20:39:44 +00:00
|
|
|
# TODO: refactor tests to avoid skipping BLS validation
|
2021-06-01 11:13:40 +00:00
|
|
|
dag.updateFlags.incl {skipBLSValidation}
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
cache: StateCache
|
|
|
|
for blck in makeTestBlocks(
|
2021-06-01 11:13:40 +00:00
|
|
|
dag.headState.data, dag.head.root, cache,
|
2021-04-26 20:39:44 +00:00
|
|
|
int(SLOTS_PER_EPOCH * 5), false):
|
2021-10-18 16:37:27 +00:00
|
|
|
let added = dag.addRawBlock(quarantine, blck.phase0Data) do (
|
2021-08-12 13:08:20 +00:00
|
|
|
blckRef: BlockRef, signedBlock: phase0.TrustedSignedBeaconBlock,
|
2021-06-21 08:35:24 +00:00
|
|
|
epochRef: EpochRef):
|
2021-04-26 20:39:44 +00:00
|
|
|
# Callback add to fork choice if valid
|
|
|
|
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
|
|
|
|
|
|
|
check: added.isOk()
|
2021-06-01 11:13:40 +00:00
|
|
|
dag.updateHead(added[], quarantine)
|
|
|
|
pruneAtFinalization(dag, pool[])
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
# Create attestations for slot 1
|
|
|
|
beacon_committee = get_beacon_committee(
|
2021-06-11 17:51:46 +00:00
|
|
|
dag.headState.data, dag.head.slot, 0.CommitteeIndex, cache)
|
2021-04-26 20:39:44 +00:00
|
|
|
att_1_0 = makeAttestation(
|
2021-06-11 17:51:46 +00:00
|
|
|
dag.headState.data, dag.head.root, beacon_committee[0], cache)
|
2021-04-26 20:39:44 +00:00
|
|
|
att_1_1 = makeAttestation(
|
2021-06-11 17:51:46 +00:00
|
|
|
dag.headState.data, dag.head.root, beacon_committee[1], cache)
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
committees_per_slot =
|
2021-06-11 17:51:46 +00:00
|
|
|
get_committee_count_per_slot(dag.headState.data,
|
2021-05-21 09:23:28 +00:00
|
|
|
att_1_0.data.slot.epoch, cache)
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
subnet = compute_subnet_for_attestation(
|
|
|
|
committees_per_slot,
|
|
|
|
att_1_0.data.slot, att_1_0.data.index.CommitteeIndex)
|
|
|
|
|
|
|
|
beaconTime = att_1_0.data.slot.toBeaconTime()
|
|
|
|
|
|
|
|
check:
|
|
|
|
validateAttestation(pool, batchCrypto, att_1_0, beaconTime, subnet, true).waitFor().isOk
|
|
|
|
|
|
|
|
# Same validator again
|
|
|
|
validateAttestation(pool, batchCrypto, att_1_0, beaconTime, subnet, true).waitFor().error()[0] ==
|
|
|
|
ValidationResult.Ignore
|
|
|
|
|
|
|
|
pool[].nextAttestationEpoch.setLen(0) # reset for test
|
|
|
|
check:
|
|
|
|
# Wrong subnet
|
2021-05-10 07:13:36 +00:00
|
|
|
validateAttestation(
|
|
|
|
pool, batchCrypto, att_1_0, beaconTime, SubnetId(subnet.uint8 + 1), true).waitFor().isErr
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
pool[].nextAttestationEpoch.setLen(0) # reset for test
|
|
|
|
check:
|
|
|
|
# Too far in the future
|
|
|
|
validateAttestation(
|
2021-05-10 07:13:36 +00:00
|
|
|
pool, batchCrypto, att_1_0, beaconTime - 1.seconds, subnet, true).waitFor().isErr
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
pool[].nextAttestationEpoch.setLen(0) # reset for test
|
|
|
|
check:
|
|
|
|
# Too far in the past
|
|
|
|
validateAttestation(
|
|
|
|
pool, batchCrypto, att_1_0,
|
|
|
|
beaconTime - (SECONDS_PER_SLOT * SLOTS_PER_EPOCH - 1).int.seconds,
|
2021-05-10 07:13:36 +00:00
|
|
|
subnet, true).waitFor().isErr
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
block:
|
|
|
|
var broken = att_1_0
|
|
|
|
broken.signature.blob[0] += 1
|
|
|
|
pool[].nextAttestationEpoch.setLen(0) # reset for test
|
|
|
|
check:
|
|
|
|
# Invalid signature
|
|
|
|
validateAttestation(
|
|
|
|
pool, batchCrypto, broken, beaconTime, subnet, true).waitFor().
|
|
|
|
error()[0] == ValidationResult.Reject
|
|
|
|
|
|
|
|
block:
|
|
|
|
var broken = att_1_0
|
|
|
|
broken.signature.blob[5] += 1
|
|
|
|
pool[].nextAttestationEpoch.setLen(0) # reset for test
|
|
|
|
# One invalid, one valid (batched)
|
|
|
|
let
|
|
|
|
fut_1_0 = validateAttestation(
|
|
|
|
pool, batchCrypto, broken, beaconTime, subnet, true)
|
|
|
|
fut_1_1 = validateAttestation(
|
|
|
|
pool, batchCrypto, att_1_1, beaconTime, subnet, true)
|
|
|
|
|
|
|
|
check:
|
|
|
|
fut_1_0.waitFor().error()[0] == ValidationResult.Reject
|
|
|
|
fut_1_1.waitFor().isOk()
|
|
|
|
|
|
|
|
block:
|
|
|
|
var broken = att_1_0
|
|
|
|
# This shouldn't deserialize, which is a different way to break it
|
|
|
|
broken.signature.blob = default(type broken.signature.blob)
|
|
|
|
pool[].nextAttestationEpoch.setLen(0) # reset for test
|
|
|
|
# One invalid, one valid (batched)
|
|
|
|
let
|
|
|
|
fut_1_0 = validateAttestation(
|
|
|
|
pool, batchCrypto, broken, beaconTime, subnet, true)
|
|
|
|
fut_1_1 = validateAttestation(
|
|
|
|
pool, batchCrypto, att_1_1, beaconTime, subnet, true)
|
|
|
|
|
|
|
|
check:
|
|
|
|
fut_1_0.waitFor().error()[0] == ValidationResult.Reject
|
|
|
|
fut_1_1.waitFor().isOk()
|
2021-09-28 07:44:20 +00:00
|
|
|
|
|
|
|
suite "Gossip validation - Extra": # Not based on preset config
|
|
|
|
test "validateSyncCommitteeMessage":
|
|
|
|
const num_validators = SLOTS_PER_EPOCH
|
2021-10-07 13:19:47 +00:00
|
|
|
let
|
2021-09-28 07:44:20 +00:00
|
|
|
cfg = block:
|
|
|
|
var cfg = defaultRuntimeConfig
|
|
|
|
cfg.ALTAIR_FORK_EPOCH = (GENESIS_EPOCH + 1).Epoch
|
|
|
|
cfg
|
|
|
|
dag = block:
|
2021-10-07 13:19:47 +00:00
|
|
|
let
|
2021-09-28 07:44:20 +00:00
|
|
|
dag = ChainDAGRef.init(cfg, makeTestDB(num_validators), {})
|
|
|
|
taskpool = Taskpool.new()
|
|
|
|
quarantine = QuarantineRef.init(keys.newRng(), taskpool)
|
|
|
|
var cache = StateCache()
|
|
|
|
for blck in makeTestBlocks(
|
|
|
|
dag.headState.data, dag.head.root, cache,
|
|
|
|
int(SLOTS_PER_EPOCH), false, cfg = cfg):
|
2021-10-07 13:19:47 +00:00
|
|
|
let added =
|
2021-09-28 07:44:20 +00:00
|
|
|
case blck.kind
|
|
|
|
of BeaconBlockFork.Phase0:
|
|
|
|
const nilCallback = OnPhase0BlockAdded(nil)
|
2021-10-18 16:37:27 +00:00
|
|
|
dag.addRawBlock(quarantine, blck.phase0Data, nilCallback)
|
2021-09-28 07:44:20 +00:00
|
|
|
of BeaconBlockFork.Altair:
|
|
|
|
const nilCallback = OnAltairBlockAdded(nil)
|
2021-10-18 16:37:27 +00:00
|
|
|
dag.addRawBlock(quarantine, blck.altairData, nilCallback)
|
2021-09-28 07:44:20 +00:00
|
|
|
of BeaconBlockFork.Merge:
|
|
|
|
const nilCallback = OnMergeBlockAdded(nil)
|
2021-10-18 16:37:27 +00:00
|
|
|
dag.addRawBlock(quarantine, blck.mergeData, nilCallback)
|
2021-09-28 07:44:20 +00:00
|
|
|
check: added.isOk()
|
|
|
|
dag.updateHead(added[], quarantine)
|
|
|
|
dag
|
2021-11-05 07:34:34 +00:00
|
|
|
state = assignClone(dag.headState.data.altairData)
|
2021-11-04 18:22:24 +00:00
|
|
|
slot = state[].data.slot
|
2021-09-28 07:44:20 +00:00
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIdx = 0.SyncSubcommitteeIndex
|
2021-11-04 18:22:24 +00:00
|
|
|
syncCommittee = @(dag.syncCommitteeParticipants(slot))
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommittee = toSeq(syncCommittee.syncSubcommittee(subcommitteeIdx))
|
2021-09-28 07:44:20 +00:00
|
|
|
|
|
|
|
pubkey = subcommittee[0]
|
|
|
|
expectedCount = subcommittee.count(pubkey)
|
|
|
|
index = ValidatorIndex(
|
|
|
|
state[].data.validators.mapIt(it.pubkey).find(pubKey))
|
2021-10-04 19:08:31 +00:00
|
|
|
privateItem = ValidatorPrivateItem(privateKey: MockPrivKeys[index])
|
|
|
|
validator = AttachedValidator(pubKey: pubkey,
|
|
|
|
kind: ValidatorKind.Local, data: privateItem, index: some(index))
|
2021-09-28 07:44:20 +00:00
|
|
|
msg = waitFor signSyncCommitteeMessage(
|
2021-11-04 18:22:24 +00:00
|
|
|
validator, slot,
|
2021-09-28 07:44:20 +00:00
|
|
|
state[].data.fork, state[].data.genesis_validators_root, state[].root)
|
|
|
|
|
|
|
|
syncCommitteeMsgPool = newClone(SyncCommitteeMsgPool.init())
|
|
|
|
res = validateSyncCommitteeMessage(
|
2021-11-05 15:39:47 +00:00
|
|
|
dag, syncCommitteeMsgPool[], msg, subcommitteeIdx,
|
2021-11-04 18:22:24 +00:00
|
|
|
slot.toBeaconTime(), true)
|
2021-11-05 15:39:47 +00:00
|
|
|
(positions, cookedSig) = res.get()
|
|
|
|
|
|
|
|
syncCommitteeMsgPool[].addSyncCommitteeMsg(
|
|
|
|
msg.slot,
|
|
|
|
msg.beacon_block_root,
|
|
|
|
msg.validator_index,
|
|
|
|
cookedSig,
|
|
|
|
subcommitteeIdx,
|
|
|
|
positions)
|
|
|
|
|
|
|
|
let
|
2021-09-28 07:44:20 +00:00
|
|
|
contribution = block:
|
2021-11-05 15:39:47 +00:00
|
|
|
var contribution = (ref SignedContributionAndProof)()
|
2021-09-28 07:44:20 +00:00
|
|
|
check: syncCommitteeMsgPool[].produceContribution(
|
2021-11-04 18:22:24 +00:00
|
|
|
slot, state[].root, subcommitteeIdx,
|
2021-11-05 15:39:47 +00:00
|
|
|
contribution.message.contribution)
|
2021-09-28 07:44:20 +00:00
|
|
|
syncCommitteeMsgPool[].addSyncContribution(
|
2021-11-05 15:39:47 +00:00
|
|
|
contribution[], contribution.message.contribution.signature.load.get)
|
|
|
|
waitFor validator.sign(
|
|
|
|
contribution, state[].data.fork, state[].data.genesis_validators_root)
|
2021-09-28 07:44:20 +00:00
|
|
|
contribution
|
|
|
|
aggregate = syncCommitteeMsgPool[].produceSyncAggregate(state[].root)
|
|
|
|
|
|
|
|
check:
|
|
|
|
expectedCount > 1 # Cover edge case
|
|
|
|
res.isOk
|
2021-11-05 15:39:47 +00:00
|
|
|
contribution.message.contribution.aggregation_bits.countOnes == expectedCount
|
2021-09-28 07:44:20 +00:00
|
|
|
aggregate.sync_committee_bits.countOnes == expectedCount
|
2021-11-05 15:39:47 +00:00
|
|
|
|
|
|
|
# Same message twice should be ignored
|
|
|
|
validateSyncCommitteeMessage(
|
|
|
|
dag, syncCommitteeMsgPool[], msg, subcommitteeIdx,
|
|
|
|
state[].data.slot.toBeaconTime(), true).isErr()
|