2021-04-26 20:39:44 +00:00
|
|
|
# beacon_chain
|
2023-02-20 08:26:22 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2021-04-26 20:39:44 +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.}
|
|
|
|
|
|
|
|
import
|
2021-09-28 07:44:20 +00:00
|
|
|
# Standard library
|
|
|
|
std/sequtils,
|
2021-04-26 20:39:44 +00:00
|
|
|
# Status lib
|
|
|
|
unittest2,
|
2022-06-07 17:05:06 +00:00
|
|
|
chronos,
|
2023-06-19 22:43:50 +00:00
|
|
|
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],
|
2022-06-07 17:05:06 +00:00
|
|
|
../beacon_chain/fork_choice/fork_choice,
|
2021-04-26 20:39:44 +00:00
|
|
|
../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],
|
2023-05-14 14:18:50 +00:00
|
|
|
../beacon_chain/spec/[
|
|
|
|
beaconstate, 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
|
2023-06-19 22:43:50 +00:00
|
|
|
let rng = HmacDrbgContext.new()
|
2021-04-26 20:39:44 +00:00
|
|
|
var
|
2021-12-20 19:20:31 +00:00
|
|
|
validatorMonitor = newClone(ValidatorMonitor.init())
|
|
|
|
dag = init(
|
|
|
|
ChainDAGRef, defaultRuntimeConfig, makeTestDB(SLOTS_PER_EPOCH * 3),
|
|
|
|
validatorMonitor, {})
|
2021-09-17 00:13:52 +00:00
|
|
|
taskpool = Taskpool.new()
|
2023-08-03 08:36:45 +00:00
|
|
|
verifier = BatchVerifier.init(rng, taskpool)
|
2021-12-06 09:49:01 +00:00
|
|
|
quarantine = newClone(Quarantine.init())
|
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()
|
2023-02-20 08:26:22 +00:00
|
|
|
batchCrypto = BatchCrypto.new(
|
2023-06-19 22:43:50 +00:00
|
|
|
rng, eager = proc(): bool = false,
|
2023-08-03 08:36:45 +00:00
|
|
|
genesis_validators_root = dag.genesis_validators_root, taskpool).expect(
|
|
|
|
"working batcher")
|
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(
|
2022-03-16 07:20:40 +00:00
|
|
|
defaultRuntimeConfig, state[], getStateField(state[], slot) + 1,
|
2022-01-17 11:19:58 +00:00
|
|
|
cache, info, {}).isOk()
|
2021-04-26 20:39:44 +00:00
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
test "Empty committee when no committee for slot":
|
2021-08-28 09:00:00 +00:00
|
|
|
template committee(idx: uint64): untyped =
|
|
|
|
get_beacon_committee(
|
2022-03-16 07:20:40 +00:00
|
|
|
dag.headState, dag.head.slot, idx.CommitteeIndex, cache)
|
2021-08-28 09:00:00 +00:00
|
|
|
|
|
|
|
template committeeLen(idx: uint64): untyped =
|
|
|
|
get_beacon_committee_len(
|
2022-03-16 07:20:40 +00:00
|
|
|
dag.headState, dag.head.slot, idx.CommitteeIndex, cache)
|
2021-08-28 09:00:00 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
committee(0).len > 0
|
2022-01-08 23:28:49 +00:00
|
|
|
committee(63).len == 0
|
2021-08-28 09:00:00 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
committeeLen(2) > 0
|
2022-01-08 23:28:49 +00:00
|
|
|
committeeLen(63) == 0
|
2021-08-28 09:00:00 +00:00
|
|
|
|
Speed up altair block processing 2x (#3115)
* Speed up altair block processing >2x
Like #3089, this PR drastially speeds up historical REST queries and
other long state replays.
* cache sync committee validator indices
* use ~80mb less memory for validator pubkey mappings
* batch-verify sync aggregate signature (fixes #2985)
* document sync committee hack with head block vs sync message block
* add batch signature verification failure tests
Before:
```
../env.sh nim c -d:release -r ncli_db --db:mainnet_0/db bench --start-slot:-1000
All time are ms
Average, StdDev, Min, Max, Samples, Test
Validation is turned off meaning that no BLS operations are performed
5830.675, 0.000, 5830.675, 5830.675, 1, Initialize DB
0.481, 1.878, 0.215, 59.167, 981, Load block from database
8422.566, 0.000, 8422.566, 8422.566, 1, Load state from database
6.996, 1.678, 0.042, 14.385, 969, Advance slot, non-epoch
93.217, 8.318, 84.192, 122.209, 32, Advance slot, epoch
20.513, 23.665, 11.510, 201.561, 981, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database load
0.000, 0.000, 0.000, 0.000, 0, Database store
```
After:
```
7081.422, 0.000, 7081.422, 7081.422, 1, Initialize DB
0.553, 2.122, 0.175, 66.692, 981, Load block from database
5439.446, 0.000, 5439.446, 5439.446, 1, Load state from database
6.829, 1.575, 0.043, 12.156, 969, Advance slot, non-epoch
94.716, 2.749, 88.395, 100.026, 32, Advance slot, epoch
11.636, 23.766, 4.889, 205.250, 981, Apply block, no slot processing
0.000, 0.000, 0.000, 0.000, 0, Database load
0.000, 0.000, 0.000, 0.000, 0, Database store
```
* add comment
2021-11-24 12:43:50 +00:00
|
|
|
test "validateAttestation":
|
2023-05-15 15:41:30 +00:00
|
|
|
var cache: StateCache
|
2021-04-26 20:39:44 +00:00
|
|
|
for blck in makeTestBlocks(
|
2023-05-15 15:41:30 +00:00
|
|
|
dag.headState, cache, int(SLOTS_PER_EPOCH * 5), attested = false):
|
Backfill support for ChainDAG (#3171)
In the ChainDAG, 3 block pointers are kept: genesis, tail and head. This
PR adds one more block pointer: the backfill block which represents the
block that has been backfilled so far.
When doing a checkpoint sync, a random block is given as starting point
- this is the tail block, and we require that the tail block has a
corresponding state.
When backfilling, we end up with blocks without corresponding states,
hence we cannot use `tail` as a backfill pointer - there is no state.
Nonetheless, we need to keep track of where we are in the backfill
process between restarts, such that we can answer GetBeaconBlocksByRange
requests.
This PR adds the basic support for backfill handling - it needs to be
integrated with backfill sync, and the REST API needs to be adjusted to
take advantage of the new backfilled blocks when responding to certain
requests.
Future work will also enable moving the tail in either direction:
* pruning means moving the tail forward in time and removing states
* backwards means recreating past states from genesis, such that
intermediate states are recreated step by step all the way to the tail -
at that point, tail, genesis and backfill will match up.
* backfilling is done when backfill != genesis - later, this will be the
WSS checkpoint instead
2021-12-13 13:36:06 +00:00
|
|
|
let added = dag.addHeadBlock(verifier, blck.phase0Data) do (
|
2021-08-12 13:08:20 +00:00
|
|
|
blckRef: BlockRef, signedBlock: phase0.TrustedSignedBeaconBlock,
|
2022-07-06 10:33:02 +00:00
|
|
|
epochRef: EpochRef, unrealized: FinalityCheckpoints):
|
2021-04-26 20:39:44 +00:00
|
|
|
# Callback add to fork choice if valid
|
2021-12-21 18:56:08 +00:00
|
|
|
pool[].addForkChoice(
|
2022-07-06 10:33:02 +00:00
|
|
|
epochRef, blckRef, unrealized, signedBlock.message,
|
|
|
|
blckRef.slot.start_beacon_time)
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
check: added.isOk()
|
2023-03-02 16:13:35 +00:00
|
|
|
dag.updateHead(added[], quarantine[], [])
|
2021-06-01 11:13:40 +00:00
|
|
|
pruneAtFinalization(dag, pool[])
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
# Create attestations for slot 1
|
|
|
|
beacon_committee = get_beacon_committee(
|
2022-03-16 07:20:40 +00:00
|
|
|
dag.headState, dag.head.slot, 0.CommitteeIndex, cache)
|
2021-04-26 20:39:44 +00:00
|
|
|
att_1_0 = makeAttestation(
|
2022-03-16 07:20:40 +00:00
|
|
|
dag.headState, dag.head.root, beacon_committee[0], cache)
|
2021-04-26 20:39:44 +00:00
|
|
|
att_1_1 = makeAttestation(
|
2022-03-16 07:20:40 +00:00
|
|
|
dag.headState, dag.head.root, beacon_committee[1], cache)
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
committees_per_slot =
|
2022-03-16 07:20:40 +00:00
|
|
|
get_committee_count_per_slot(
|
|
|
|
dag.headState, 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)
|
|
|
|
|
2022-01-11 10:01:54 +00:00
|
|
|
beaconTime = att_1_0.data.slot.start_beacon_time()
|
2021-04-26 20:39:44 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-09-13 06:32:11 +00:00
|
|
|
suite "Gossip validation - Altair":
|
|
|
|
let cfg = block:
|
|
|
|
var res = defaultRuntimeConfig
|
|
|
|
res.ALTAIR_FORK_EPOCH = (EPOCHS_PER_SYNC_COMMITTEE_PERIOD - 2).Epoch
|
|
|
|
res
|
|
|
|
|
|
|
|
proc addBlock(
|
|
|
|
dag: ChainDAGRef,
|
|
|
|
cache: var StateCache,
|
|
|
|
verifier: var BatchVerifier,
|
|
|
|
quarantine: var Quarantine) =
|
|
|
|
for blck in makeTestBlocks(
|
|
|
|
dag.headState, cache, blocks = 1,
|
|
|
|
attested = false, cfg = cfg):
|
2023-09-13 17:57:54 +00:00
|
|
|
let added = withBlck(blck):
|
|
|
|
const nilCallback = (consensusFork.OnBlockAddedCallback)(nil)
|
2023-09-21 10:49:14 +00:00
|
|
|
dag.addHeadBlock(verifier, forkyBlck, nilCallback)
|
2023-09-13 06:32:11 +00:00
|
|
|
check: added.isOk()
|
|
|
|
dag.updateHead(added[], quarantine, [])
|
|
|
|
|
|
|
|
proc getFirstAggregator(dag: ChainDAGRef, signatureSlot: Slot): tuple[
|
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex,
|
|
|
|
indexInSubcommittee: int
|
|
|
|
] =
|
|
|
|
const indicesPerSubcommittee =
|
|
|
|
SYNC_COMMITTEE_SIZE div SYNC_COMMITTEE_SUBNET_COUNT
|
|
|
|
for i, index in dag.syncCommitteeParticipants(signatureSlot):
|
|
|
|
if (signatureSlot + 1).is_sync_committee_period:
|
|
|
|
var isAlsoInNextCommittee = false
|
|
|
|
for other in dag.syncCommitteeParticipants(signatureSlot + 1):
|
|
|
|
if other == index:
|
|
|
|
isAlsoInNextCommittee = true
|
|
|
|
break
|
|
|
|
if isAlsoInNextCommittee:
|
|
|
|
continue
|
|
|
|
let
|
|
|
|
subcommitteeIndex = SyncSubcommitteeIndex(i div indicesPerSubcommittee)
|
|
|
|
pubkey = getStateField(dag.headState, validators).item(index).pubkey
|
|
|
|
keystoreData = KeystoreData(
|
|
|
|
kind: KeystoreKind.Local,
|
|
|
|
pubkey: pubkey,
|
|
|
|
privateKey: MockPrivKeys[index])
|
|
|
|
validator = AttachedValidator(
|
|
|
|
kind: ValidatorKind.Local, data: keystoreData, index: Opt.some index)
|
|
|
|
proofFut = validator.getSyncCommitteeSelectionProof(
|
|
|
|
getStateField(dag.headState, fork),
|
|
|
|
getStateField(dag.headState, genesis_validators_root),
|
|
|
|
getStateField(dag.headState, slot),
|
|
|
|
subcommitteeIndex)
|
|
|
|
check proofFut.completed # Local signatures complete synchronously
|
|
|
|
let proof = proofFut.value
|
|
|
|
check proof.isOk
|
|
|
|
if is_sync_committee_aggregator(proof.get):
|
|
|
|
return (
|
|
|
|
subcommitteeIdx: subcommitteeIndex,
|
|
|
|
indexInSubcommittee: i mod indicesPerSubcommittee)
|
|
|
|
raiseAssert "No sync aggregator found who's not also part of next committee"
|
|
|
|
|
|
|
|
proc getSyncCommitteeMessage(
|
|
|
|
dag: ChainDAGRef,
|
|
|
|
msgSlot: Slot,
|
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex,
|
|
|
|
indexInSubcommittee: int,
|
|
|
|
signatureSlot = Opt.none(Slot)
|
|
|
|
): tuple[
|
|
|
|
validator: AttachedValidator,
|
|
|
|
numPresent: int,
|
|
|
|
msg: SyncCommitteeMessage
|
|
|
|
] =
|
2021-10-07 13:19:47 +00:00
|
|
|
let
|
2023-09-13 06:32:11 +00:00
|
|
|
signatureSlot = signatureSlot.get(msgSlot + 1)
|
|
|
|
syncCommittee = @(dag.syncCommitteeParticipants(signatureSlot))
|
|
|
|
subcommittee = toSeq(syncCommittee.syncSubcommittee(subcommitteeIdx))
|
|
|
|
index = subcommittee[indexInSubcommittee]
|
|
|
|
numPresent = subcommittee.count(index)
|
|
|
|
pubkey = getStateField(dag.headState, validators).item(index).pubkey
|
|
|
|
keystoreData = KeystoreData(
|
|
|
|
kind: KeystoreKind.Local,
|
|
|
|
pubkey: pubkey,
|
|
|
|
privateKey: MockPrivKeys[index])
|
|
|
|
validator = AttachedValidator(
|
|
|
|
kind: ValidatorKind.Local, data: keystoreData, index: Opt.some index)
|
|
|
|
msgFut = validator.getSyncCommitteeMessage(
|
|
|
|
getStateField(dag.headState, fork),
|
|
|
|
getStateField(dag.headState, genesis_validators_root),
|
|
|
|
msgSlot, dag.headState.latest_block_root)
|
|
|
|
check msgFut.completed # Local signatures complete synchronously
|
|
|
|
let msg = msgFut.value
|
|
|
|
check msg.isOk
|
|
|
|
(validator: validator, numPresent: numPresent, msg: msg.get)
|
|
|
|
|
|
|
|
setup:
|
|
|
|
let
|
|
|
|
validatorMonitor = newClone(ValidatorMonitor.init())
|
2021-12-09 12:56:54 +00:00
|
|
|
quarantine = newClone(Quarantine.init())
|
2023-06-19 22:43:50 +00:00
|
|
|
rng = HmacDrbgContext.new()
|
2023-09-13 06:32:11 +00:00
|
|
|
syncCommitteePool = newClone(SyncCommitteeMsgPool.init(rng, cfg))
|
2021-12-09 12:56:54 +00:00
|
|
|
var
|
2023-09-13 06:32:11 +00:00
|
|
|
taskpool = Taskpool.new()
|
2023-08-03 08:36:45 +00:00
|
|
|
verifier = BatchVerifier.init(rng, taskpool)
|
2023-02-20 08:26:22 +00:00
|
|
|
|
2023-09-13 06:32:11 +00:00
|
|
|
template prepare(numValidators: Natural): untyped {.dirty.} =
|
|
|
|
let
|
|
|
|
dag = ChainDAGRef.init(
|
|
|
|
cfg, makeTestDB(numValidators, cfg = cfg), validatorMonitor, {})
|
|
|
|
batchCrypto = BatchCrypto.new(
|
|
|
|
rng, eager = proc(): bool = false,
|
|
|
|
genesis_validators_root = dag.genesis_validators_root, taskpool).expect(
|
|
|
|
"working batcher")
|
2023-02-20 08:26:22 +00:00
|
|
|
var
|
2023-09-13 06:32:11 +00:00
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
|
|
|
doAssert process_slots(
|
|
|
|
cfg, dag.headState,
|
|
|
|
(cfg.ALTAIR_FORK_EPOCH - 1).start_slot(),
|
|
|
|
cache, info, flags = {}).isOk
|
|
|
|
for i in 0 ..< SLOTS_PER_EPOCH:
|
|
|
|
dag.addBlock(cache, verifier, quarantine[])
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
taskpool.shutdown()
|
|
|
|
|
|
|
|
test "Period boundary":
|
|
|
|
prepare(numValidators = SYNC_COMMITTEE_SIZE * 2)
|
|
|
|
|
|
|
|
# Advance to the last slot before period 2.
|
|
|
|
# The first two periods share the same sync committee,
|
|
|
|
# so are not suitable for the test
|
|
|
|
for i in 0 ..< SLOTS_PER_EPOCH:
|
|
|
|
dag.addBlock(cache, verifier, quarantine[])
|
|
|
|
doAssert process_slots(
|
|
|
|
cfg, dag.headState,
|
|
|
|
(2.SyncCommitteePeriod.start_epoch() - 1).start_slot(),
|
|
|
|
cache, info, flags = {}).isOk
|
|
|
|
for i in 0 ..< SLOTS_PER_EPOCH - 1:
|
|
|
|
dag.addBlock(cache, verifier, quarantine[])
|
|
|
|
let slot = getStateField(dag.headState, slot)
|
|
|
|
|
|
|
|
# The following slots determine what the sync committee signs:
|
|
|
|
# 1. `state.latest_block_header.slot` --> ConsensusFork of signed block
|
|
|
|
# 2. `state.slot` --> ForkDigest of signature
|
|
|
|
# 3. `state.slot + 1` --> Sync committee
|
|
|
|
proc checkWithSignatureSlot(
|
|
|
|
signatureSlot: Slot, expectValid: bool) =
|
|
|
|
warn "checkWithSignatureSlot", signatureSlot, expectValid
|
|
|
|
|
|
|
|
let
|
|
|
|
(subcommitteeIdx, indexInSubcommittee) =
|
|
|
|
dag.getFirstAggregator(signatureSlot)
|
|
|
|
(validator, expectedCount, msg) = dag.getSyncCommitteeMessage(
|
|
|
|
slot, subcommitteeIdx, indexInSubcommittee,
|
|
|
|
signatureSlot = Opt.some(signatureSlot))
|
|
|
|
msgVerdict = waitFor dag.validateSyncCommitteeMessage(
|
|
|
|
quarantine, batchCrypto, syncCommitteePool,
|
|
|
|
msg, subcommitteeIdx, slot.start_beacon_time(),
|
|
|
|
checkSignature = true)
|
|
|
|
check msgVerdict.isOk == expectValid
|
|
|
|
|
|
|
|
let (bid, cookedSig, positions) =
|
|
|
|
if msgVerdict.isOk:
|
|
|
|
msgVerdict.get
|
|
|
|
else:
|
|
|
|
let
|
|
|
|
blockRoot = msg.beacon_block_root
|
|
|
|
blck = dag.getBlockRef(blockRoot).expect("Block present")
|
|
|
|
sig = msg.signature.load().expect("Signature OK")
|
|
|
|
positionsInSubcommittee = dag.getSubcommitteePositions(
|
|
|
|
signatureSlot, subcommitteeIdx, msg.validator_index)
|
|
|
|
(blck.bid, sig, positionsInSubcommittee)
|
|
|
|
|
|
|
|
syncCommitteePool[] = SyncCommitteeMsgPool.init(rng, cfg)
|
|
|
|
syncCommitteePool[].addSyncCommitteeMessage(
|
|
|
|
msg.slot,
|
|
|
|
bid,
|
|
|
|
msg.validator_index,
|
|
|
|
cookedSig,
|
|
|
|
subcommitteeIdx,
|
|
|
|
positions)
|
|
|
|
let contrib = block:
|
|
|
|
let contrib = (ref SignedContributionAndProof)(
|
|
|
|
message: ContributionAndProof(
|
|
|
|
aggregator_index: distinctBase(validator.index.get),
|
|
|
|
selection_proof: validator.getSyncCommitteeSelectionProof(
|
|
|
|
getStateField(dag.headState, fork),
|
|
|
|
getStateField(dag.headState, genesis_validators_root),
|
|
|
|
getStateField(dag.headState, slot),
|
|
|
|
subcommitteeIdx).value.get))
|
|
|
|
check syncCommitteePool[].produceContribution(
|
|
|
|
slot, bid, subcommitteeIdx,
|
|
|
|
contrib.message.contribution)
|
|
|
|
syncCommitteePool[].addContribution(
|
|
|
|
contrib[], bid, contrib.message.contribution.signature.load.get)
|
|
|
|
let signRes = waitFor validator.getContributionAndProofSignature(
|
|
|
|
getStateField(dag.headState, fork),
|
|
|
|
getStateField(dag.headState, genesis_validators_root),
|
|
|
|
contrib[].message)
|
|
|
|
doAssert(signRes.isOk())
|
|
|
|
contrib[].signature = signRes.get()
|
|
|
|
contrib
|
|
|
|
syncCommitteePool[] = SyncCommitteeMsgPool.init(rng, cfg)
|
|
|
|
let contribVerdict = waitFor dag.validateContribution(
|
|
|
|
quarantine, batchCrypto, syncCommitteePool,
|
|
|
|
contrib[], slot.start_beacon_time(),
|
|
|
|
checkSignature = true)
|
|
|
|
check contribVerdict.isOk == expectValid
|
|
|
|
|
|
|
|
# We are at the last slot of a sync committee period:
|
|
|
|
check slot == (slot.sync_committee_period + 1).start_slot() - 1
|
|
|
|
|
|
|
|
# Therefore, messages from `current_sync_committee` are no longer allowed
|
|
|
|
checkWithSignatureSlot(
|
|
|
|
signatureSlot = slot,
|
|
|
|
expectValid = false)
|
2021-09-28 07:44:20 +00:00
|
|
|
|
2023-09-13 06:32:11 +00:00
|
|
|
# Messages signed from `next_sync_committee` are accepted
|
|
|
|
checkWithSignatureSlot(
|
|
|
|
signatureSlot = slot + 1,
|
|
|
|
expectValid = true)
|
|
|
|
|
|
|
|
test "validateSyncCommitteeMessage - Duplicate pubkey":
|
|
|
|
prepare(numValidators = SLOTS_PER_EPOCH)
|
|
|
|
|
|
|
|
for i in 0 ..< SLOTS_PER_EPOCH:
|
|
|
|
dag.addBlock(cache, verifier, quarantine[])
|
|
|
|
|
|
|
|
const
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIdx = 0.SyncSubcommitteeIndex
|
2023-09-13 06:32:11 +00:00
|
|
|
indexInSubcommittee = 0
|
|
|
|
let
|
|
|
|
state = assignClone(dag.headState.altairData)
|
|
|
|
slot = state[].data.slot
|
|
|
|
(validator, expectedCount, msg) = dag.getSyncCommitteeMessage(
|
|
|
|
slot, subcommitteeIdx, indexInSubcommittee)
|
2021-09-28 07:44:20 +00:00
|
|
|
|
2021-12-09 12:56:54 +00:00
|
|
|
res = waitFor validateSyncCommitteeMessage(
|
2023-05-17 04:55:55 +00:00
|
|
|
dag, quarantine, batchCrypto, syncCommitteePool,
|
2023-09-13 06:32:11 +00:00
|
|
|
msg, subcommitteeIdx, slot.start_beacon_time(),
|
|
|
|
checkSignature = true)
|
2023-05-17 04:55:55 +00:00
|
|
|
(bid, cookedSig, positions) = res.get()
|
2021-11-05 15:39:47 +00:00
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
syncCommitteePool[].addSyncCommitteeMessage(
|
2021-11-05 15:39:47 +00:00
|
|
|
msg.slot,
|
2023-05-17 04:55:55 +00:00
|
|
|
bid,
|
2021-11-05 15:39:47 +00:00
|
|
|
msg.validator_index,
|
|
|
|
cookedSig,
|
|
|
|
subcommitteeIdx,
|
|
|
|
positions)
|
|
|
|
|
|
|
|
let
|
2023-05-17 04:55:55 +00:00
|
|
|
contrib = block:
|
|
|
|
let contrib = (ref SignedContributionAndProof)()
|
2022-08-09 09:52:11 +00:00
|
|
|
check:
|
2023-05-17 04:55:55 +00:00
|
|
|
syncCommitteePool[].produceContribution(
|
|
|
|
slot, bid, subcommitteeIdx,
|
|
|
|
contrib.message.contribution)
|
|
|
|
syncCommitteePool[].addContribution(
|
|
|
|
contrib[], bid,
|
|
|
|
contrib.message.contribution.signature.load.get)
|
2022-06-29 16:53:59 +00:00
|
|
|
let signRes = waitFor validator.getContributionAndProofSignature(
|
|
|
|
state[].data.fork, state[].data.genesis_validators_root,
|
2023-05-17 04:55:55 +00:00
|
|
|
contrib[].message)
|
2021-11-30 01:20:21 +00:00
|
|
|
doAssert(signRes.isOk())
|
2023-05-17 04:55:55 +00:00
|
|
|
contrib[].signature = signRes.get()
|
|
|
|
contrib
|
2023-06-16 21:30:36 +00:00
|
|
|
aggregate = syncCommitteePool[].produceSyncAggregate(bid, slot + 1)
|
2021-09-28 07:44:20 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
expectedCount > 1 # Cover edge case
|
|
|
|
res.isOk
|
2023-05-17 04:55:55 +00:00
|
|
|
contrib.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(
|
2023-05-17 04:55:55 +00:00
|
|
|
dag, quarantine, batchCrypto, syncCommitteePool,
|
|
|
|
msg, subcommitteeIdx, state[].data.slot.start_beacon_time(), true
|
|
|
|
).waitFor().isErr()
|