2020-05-06 13:23:45 +00:00
|
|
|
# beacon_chain
|
2022-01-18 13:36:52 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2020-05-06 13:23:45 +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.
|
|
|
|
|
2021-03-26 06:52:01 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-05-06 13:23:45 +00:00
|
|
|
import
|
|
|
|
# Standard library
|
2021-03-23 06:57:10 +00:00
|
|
|
std/[os, osproc, sequtils, streams, tables],
|
2020-05-06 13:23:45 +00:00
|
|
|
|
|
|
|
# Nimble packages
|
2021-05-20 10:44:13 +00:00
|
|
|
stew/[assign2, byteutils, objects],
|
2021-03-26 14:11:06 +00:00
|
|
|
chronos, metrics,
|
2021-08-28 22:27:51 +00:00
|
|
|
chronicles, chronicles/timings,
|
2020-05-06 13:23:45 +00:00
|
|
|
json_serialization/std/[options, sets, net], serialization/errors,
|
2020-05-14 11:19:10 +00:00
|
|
|
eth/db/kvstore,
|
2021-05-12 12:31:02 +00:00
|
|
|
eth/keys, eth/p2p/discoveryv5/[protocol, enr],
|
2021-12-17 12:23:32 +00:00
|
|
|
web3/ethtypes,
|
2020-05-06 13:23:45 +00:00
|
|
|
|
|
|
|
# Local modules
|
2022-01-18 13:36:52 +00:00
|
|
|
../spec/datatypes/[phase0, altair, bellatrix],
|
2021-08-18 18:57:58 +00:00
|
|
|
../spec/[
|
2021-10-18 09:11:44 +00:00
|
|
|
eth2_merkleization, forks, helpers, network, signatures, state_transition,
|
|
|
|
validator],
|
2021-03-04 09:13:44 +00:00
|
|
|
../consensus_object_pools/[
|
2021-08-28 22:27:51 +00:00
|
|
|
spec_cache, blockchain_dag, block_clearance, attestation_pool, exit_pool,
|
|
|
|
sync_committee_msg_pool],
|
2021-03-03 06:23:05 +00:00
|
|
|
../eth1/eth1_monitor,
|
2021-03-05 13:12:00 +00:00
|
|
|
../networking/eth2_network,
|
2021-08-18 18:57:58 +00:00
|
|
|
../sszdump, ../sync/sync_manager,
|
2021-10-19 15:20:55 +00:00
|
|
|
../gossip_processing/[block_processor, consensus_manager],
|
2021-10-19 14:09:26 +00:00
|
|
|
".."/[conf, beacon_clock, beacon_node, version],
|
2021-08-24 19:49:51 +00:00
|
|
|
"."/[slashing_protection, validator_pool, keystore_management]
|
2020-05-06 13:23:45 +00:00
|
|
|
|
|
|
|
# Metrics for tracking attestation and beacon block loss
|
2020-11-11 13:39:36 +00:00
|
|
|
const delayBuckets = [-Inf, -4.0, -2.0, -1.0, -0.5, -0.1, -0.05,
|
2020-11-11 12:14:09 +00:00
|
|
|
0.05, 0.1, 0.5, 1.0, 2.0, 4.0, 8.0, Inf]
|
|
|
|
|
2020-05-06 13:23:45 +00:00
|
|
|
declareCounter beacon_attestations_sent,
|
|
|
|
"Number of beacon chain attestations sent by this peer"
|
2021-08-28 22:27:51 +00:00
|
|
|
|
2020-11-11 12:14:09 +00:00
|
|
|
declareHistogram beacon_attestation_sent_delay,
|
|
|
|
"Time(s) between slot start and attestation sent moment",
|
|
|
|
buckets = delayBuckets
|
2021-08-28 22:27:51 +00:00
|
|
|
|
|
|
|
declareCounter beacon_sync_committee_messages_sent,
|
|
|
|
"Number of sync committee messages sent by this peer"
|
|
|
|
|
|
|
|
declareCounter beacon_sync_committee_contributions_sent,
|
|
|
|
"Number of sync committee contributions sent by this peer"
|
|
|
|
|
|
|
|
declareHistogram beacon_sync_committee_message_sent_delay,
|
|
|
|
"Time(s) between slot start and sync committee message sent moment",
|
|
|
|
buckets = delayBuckets
|
|
|
|
|
2020-05-06 13:23:45 +00:00
|
|
|
declareCounter beacon_blocks_proposed,
|
|
|
|
"Number of beacon chain blocks sent by this peer"
|
|
|
|
|
2020-11-27 23:34:25 +00:00
|
|
|
declareGauge(attached_validator_balance,
|
|
|
|
"Validator balance at slot end of the first 64 validators, in Gwei",
|
|
|
|
labels = ["pubkey"])
|
2021-08-28 22:27:51 +00:00
|
|
|
|
2020-11-28 18:53:51 +00:00
|
|
|
declarePublicGauge(attached_validator_balance_total,
|
2020-11-27 23:34:25 +00:00
|
|
|
"Validator balance of all attached validators, in Gwei")
|
|
|
|
|
2020-05-06 13:23:45 +00:00
|
|
|
logScope: topics = "beacval"
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
type
|
|
|
|
SendResult* = Result[void, cstring]
|
|
|
|
SendBlockResult* = Result[bool, cstring]
|
2021-08-29 14:50:21 +00:00
|
|
|
ForkedBlockResult* = Result[ForkedBeaconBlock, string]
|
2021-08-23 10:41:48 +00:00
|
|
|
|
2021-12-22 12:37:31 +00:00
|
|
|
proc findValidator(validators: auto, pubkey: ValidatorPubKey):
|
2020-11-27 23:34:25 +00:00
|
|
|
Option[ValidatorIndex] =
|
2021-12-22 12:37:31 +00:00
|
|
|
let idx = validators.findIt(it.pubkey == pubkey)
|
2020-05-06 13:23:45 +00:00
|
|
|
if idx == -1:
|
|
|
|
# We allow adding a validator even if its key is not in the state registry:
|
|
|
|
# it might be that the deposit for this validator has not yet been processed
|
2021-12-22 12:37:31 +00:00
|
|
|
notice "Validator deposit not yet processed, monitoring", pubkey
|
2020-11-27 23:34:25 +00:00
|
|
|
none(ValidatorIndex)
|
|
|
|
else:
|
|
|
|
some(idx.ValidatorIndex)
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2022-02-07 20:36:09 +00:00
|
|
|
proc addLocalValidator(node: BeaconNode, validators: auto,
|
2021-12-22 12:37:31 +00:00
|
|
|
item: KeystoreData) =
|
2021-11-30 01:20:21 +00:00
|
|
|
let
|
2021-12-22 12:37:31 +00:00
|
|
|
pubkey = item.pubkey
|
|
|
|
index = findValidator(validators, pubkey)
|
2021-11-30 01:20:21 +00:00
|
|
|
node.attachedValidators[].addLocalValidator(item, index)
|
|
|
|
|
2022-02-07 20:36:09 +00:00
|
|
|
proc addRemoteValidator(pool: var ValidatorPool, validators: auto,
|
2021-12-22 12:37:31 +00:00
|
|
|
item: KeystoreData) =
|
2021-11-30 01:20:21 +00:00
|
|
|
let httpFlags =
|
|
|
|
block:
|
|
|
|
var res: set[HttpClientFlag]
|
|
|
|
if RemoteKeystoreFlag.IgnoreSSLVerification in item.flags:
|
|
|
|
res.incl({HttpClientFlag.NoVerifyHost,
|
|
|
|
HttpClientFlag.NoVerifyServerName})
|
|
|
|
res
|
|
|
|
let prestoFlags = {RestClientFlag.CommaSeparatedArray}
|
|
|
|
let client = RestClientRef.new($item.remoteUrl, prestoFlags, httpFlags)
|
|
|
|
if client.isErr():
|
|
|
|
warn "Unable to resolve remote signer address",
|
2021-12-22 12:37:31 +00:00
|
|
|
remote_url = $item.remoteUrl, validator = item.pubkey
|
2021-11-30 01:20:21 +00:00
|
|
|
return
|
2021-12-22 12:37:31 +00:00
|
|
|
let index = findValidator(validators, item.pubkey)
|
2022-02-07 20:36:09 +00:00
|
|
|
pool.addRemoteValidator(item, client.get(), index)
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
proc addLocalValidators*(node: BeaconNode,
|
2021-12-22 12:37:31 +00:00
|
|
|
validators: openArray[KeystoreData]) =
|
2021-10-18 09:11:44 +00:00
|
|
|
withState(node.dag.headState.data):
|
2021-11-30 01:20:21 +00:00
|
|
|
for item in validators:
|
|
|
|
node.addLocalValidator(state.data.validators.asSeq(), item)
|
|
|
|
|
|
|
|
proc addRemoteValidators*(node: BeaconNode,
|
2021-12-22 12:37:31 +00:00
|
|
|
validators: openArray[KeystoreData]) =
|
2021-11-30 01:20:21 +00:00
|
|
|
withState(node.dag.headState.data):
|
|
|
|
for item in validators:
|
2022-02-07 20:36:09 +00:00
|
|
|
node.attachedValidators[].addRemoteValidator(
|
|
|
|
state.data.validators.asSeq(), item)
|
2021-11-30 01:20:21 +00:00
|
|
|
|
|
|
|
proc addValidators*(node: BeaconNode) =
|
|
|
|
let (localValidators, remoteValidators) =
|
|
|
|
block:
|
2021-12-22 12:37:31 +00:00
|
|
|
var local, remote: seq[KeystoreData]
|
|
|
|
for keystore in listLoadableKeystores(node.config):
|
|
|
|
case keystore.kind
|
|
|
|
of KeystoreKind.Local:
|
|
|
|
local.add(keystore)
|
|
|
|
of KeystoreKind.Remote:
|
|
|
|
remote.add(keystore)
|
2021-11-30 01:20:21 +00:00
|
|
|
(local, remote)
|
|
|
|
node.addLocalValidators(localValidators)
|
|
|
|
node.addRemoteValidators(remoteValidators)
|
2020-09-01 13:44:40 +00:00
|
|
|
|
2020-08-10 13:21:31 +00:00
|
|
|
proc getAttachedValidator*(node: BeaconNode,
|
|
|
|
pubkey: ValidatorPubKey): AttachedValidator =
|
2021-02-22 16:17:48 +00:00
|
|
|
node.attachedValidators[].getValidator(pubkey)
|
2020-08-10 13:21:31 +00:00
|
|
|
|
2020-07-23 18:24:38 +00:00
|
|
|
proc getAttachedValidator*(node: BeaconNode,
|
2021-04-13 13:05:44 +00:00
|
|
|
state_validators: auto,
|
2020-06-11 12:13:12 +00:00
|
|
|
idx: ValidatorIndex): AttachedValidator =
|
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
|
|
|
if uint64(idx) < state_validators.lenu64:
|
2021-04-13 13:05:44 +00:00
|
|
|
let validator = node.getAttachedValidator(state_validators[idx].pubkey)
|
2021-06-29 15:09:29 +00:00
|
|
|
if validator != nil and validator.index != some(idx):
|
2020-11-27 23:34:25 +00:00
|
|
|
# Update index, in case the validator was activated!
|
|
|
|
notice "Validator activated", pubkey = validator.pubkey, index = idx
|
2021-06-29 15:09:29 +00:00
|
|
|
validator.index = some(idx)
|
2020-11-27 23:34:25 +00:00
|
|
|
validator
|
2020-08-10 13:21:31 +00:00
|
|
|
else:
|
|
|
|
warn "Validator index out of bounds",
|
2021-04-13 13:05:44 +00:00
|
|
|
idx, validators = state_validators.len
|
2020-08-10 13:21:31 +00:00
|
|
|
nil
|
|
|
|
|
|
|
|
proc getAttachedValidator*(node: BeaconNode,
|
|
|
|
epochRef: EpochRef,
|
|
|
|
idx: ValidatorIndex): AttachedValidator =
|
2021-06-10 07:37:02 +00:00
|
|
|
let key = epochRef.validatorKey(idx)
|
|
|
|
if key.isSome():
|
|
|
|
let validator = node.getAttachedValidator(key.get().toPubKey())
|
2021-12-20 11:21:17 +00:00
|
|
|
if validator != nil and validator.index != some(idx):
|
2020-11-27 23:34:25 +00:00
|
|
|
# Update index, in case the validator was activated!
|
|
|
|
notice "Validator activated", pubkey = validator.pubkey, index = idx
|
2021-12-20 11:21:17 +00:00
|
|
|
validator.index = some(idx)
|
2020-11-27 23:34:25 +00:00
|
|
|
validator
|
2020-08-10 13:21:31 +00:00
|
|
|
else:
|
2021-06-10 07:37:02 +00:00
|
|
|
warn "Validator key not found",
|
|
|
|
idx, epoch = epochRef.epoch
|
2020-08-10 13:21:31 +00:00
|
|
|
nil
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2020-07-08 10:11:22 +00:00
|
|
|
proc isSynced*(node: BeaconNode, head: BlockRef): bool =
|
2020-05-06 13:23:45 +00:00
|
|
|
## TODO This function is here as a placeholder for some better heurestics to
|
|
|
|
## determine if we're in sync and should be producing blocks and
|
|
|
|
## attestations. Generally, the problem is that slot time keeps advancing
|
|
|
|
## even when there are no blocks being produced, so there's no way to
|
|
|
|
## distinguish validators geniunely going missing from the node not being
|
|
|
|
## well connected (during a network split or an internet outage for
|
|
|
|
## example). It would generally be correct to simply keep running as if
|
|
|
|
## we were the only legit node left alive, but then we run into issues:
|
|
|
|
## with enough many empty slots, the validator pool is emptied leading
|
|
|
|
## to empty committees and lots of empty slot processing that will be
|
|
|
|
## thrown away as soon as we're synced again.
|
|
|
|
|
|
|
|
let
|
|
|
|
# The slot we should be at, according to the clock
|
|
|
|
beaconTime = node.beaconClock.now()
|
|
|
|
wallSlot = beaconTime.toSlot()
|
|
|
|
|
|
|
|
# TODO if everyone follows this logic, the network will not recover from a
|
|
|
|
# halt: nobody will be producing blocks because everone expects someone
|
|
|
|
# else to do it
|
2021-11-18 19:35:26 +00:00
|
|
|
if wallSlot.afterGenesis and head.slot + node.config.syncHorizon < wallSlot.slot:
|
2020-05-06 13:23:45 +00:00
|
|
|
false
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
|
2021-11-25 19:05:11 +00:00
|
|
|
func isGoodForSending(validationResult: ValidationRes): bool =
|
|
|
|
# Validator clients such as Vouch can be configured to work with multiple
|
|
|
|
# beacon nodes simultaneously. In this configuration, the validator client
|
|
|
|
# will try to broadcast the gossip messages through each of the connected
|
|
|
|
# beacon nodes which may lead to a situation where some of the nodes see a
|
|
|
|
# message arriving from the network before it arrives through the REST API.
|
|
|
|
# This should not be considered an error and the beacon node should still
|
|
|
|
# broadcast the message as the intented purpose of the Vouch strategy is
|
|
|
|
# to ensure that the message will reach as many peers as possible.
|
|
|
|
validationResult.isOk() or validationResult.error[0] == ValidationResult.Ignore
|
|
|
|
|
2020-06-23 10:38:59 +00:00
|
|
|
proc sendAttestation*(
|
2021-05-10 07:13:36 +00:00
|
|
|
node: BeaconNode, attestation: Attestation,
|
2021-11-05 15:39:47 +00:00
|
|
|
subnet_id: SubnetId, checkSignature: bool): Future[SendResult] {.async.} =
|
2021-05-10 07:13:36 +00:00
|
|
|
# Validate attestation before sending it via gossip - validation will also
|
|
|
|
# register the attestation with the attestation pool. Notably, although
|
|
|
|
# libp2p calls the data handler for any subscription on the subnet
|
|
|
|
# topic, it does not perform validation.
|
2021-11-05 15:39:47 +00:00
|
|
|
let res = await node.processor.attestationValidator(
|
2021-12-20 19:20:31 +00:00
|
|
|
MsgSource.api, attestation, subnet_id, checkSignature)
|
2021-05-10 07:13:36 +00:00
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
return
|
2021-11-25 19:05:11 +00:00
|
|
|
if res.isGoodForSending:
|
2021-08-23 10:41:48 +00:00
|
|
|
node.network.broadcastAttestation(subnet_id, attestation)
|
2021-05-10 07:13:36 +00:00
|
|
|
beacon_attestations_sent.inc()
|
2021-11-05 15:39:47 +00:00
|
|
|
ok()
|
2021-05-10 07:13:36 +00:00
|
|
|
else:
|
|
|
|
notice "Produced attestation failed validation",
|
|
|
|
attestation = shortLog(attestation),
|
2021-11-05 15:39:47 +00:00
|
|
|
error = res.error()
|
|
|
|
err(res.error()[1])
|
2020-06-05 09:57:40 +00:00
|
|
|
|
2021-08-28 22:27:51 +00:00
|
|
|
proc sendSyncCommitteeMessage*(
|
|
|
|
node: BeaconNode, msg: SyncCommitteeMessage,
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex,
|
2021-09-23 22:13:25 +00:00
|
|
|
checkSignature: bool): Future[SendResult] {.async.} =
|
2021-08-28 22:27:51 +00:00
|
|
|
# Validate sync committee message before sending it via gossip
|
|
|
|
# validation will also register the message with the sync committee
|
|
|
|
# message pool. Notably, although libp2p calls the data handler for
|
|
|
|
# any subscription on the subnet topic, it does not perform validation.
|
2021-12-20 19:20:31 +00:00
|
|
|
let res = await node.processor.syncCommitteeMessageValidator(
|
|
|
|
MsgSource.api, msg, subcommitteeIdx, checkSignature)
|
|
|
|
|
2021-09-23 22:13:25 +00:00
|
|
|
return
|
2021-11-25 19:05:11 +00:00
|
|
|
if res.isGoodForSending:
|
2021-11-05 15:39:47 +00:00
|
|
|
node.network.broadcastSyncCommitteeMessage(msg, subcommitteeIdx)
|
2021-08-28 22:27:51 +00:00
|
|
|
beacon_sync_committee_messages_sent.inc()
|
2021-09-23 22:13:25 +00:00
|
|
|
SendResult.ok()
|
2021-08-28 22:27:51 +00:00
|
|
|
else:
|
2021-09-23 22:13:25 +00:00
|
|
|
notice "Sync committee message failed validation",
|
2021-11-05 15:39:47 +00:00
|
|
|
msg, error = res.error()
|
|
|
|
SendResult.err(res.error()[1])
|
2021-09-23 22:13:25 +00:00
|
|
|
|
|
|
|
proc sendSyncCommitteeMessages*(node: BeaconNode,
|
|
|
|
msgs: seq[SyncCommitteeMessage]
|
|
|
|
): Future[seq[SendResult]] {.async.} =
|
2021-10-20 16:32:46 +00:00
|
|
|
return withState(node.dag.headState.data):
|
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
|
|
|
var statuses = newSeq[Option[SendResult]](len(msgs))
|
2021-09-23 22:13:25 +00:00
|
|
|
|
|
|
|
let
|
2021-10-20 16:32:46 +00:00
|
|
|
curPeriod = sync_committee_period(state.data.slot)
|
|
|
|
nextPeriod = curPeriod + 1
|
|
|
|
|
|
|
|
let (keysCur, keysNxt) =
|
|
|
|
block:
|
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
|
|
|
var resCur: Table[uint64, int]
|
|
|
|
var resNxt: Table[uint64, int]
|
2021-10-20 16:32:46 +00:00
|
|
|
|
|
|
|
for index, msg in msgs.pairs():
|
|
|
|
if msg.validator_index < lenu64(state.data.validators):
|
|
|
|
let msgPeriod = sync_committee_period(msg.slot)
|
|
|
|
if msgPeriod == curPeriod:
|
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
|
|
|
resCur[msg.validator_index] = index
|
2021-10-20 16:32:46 +00:00
|
|
|
elif msgPeriod == nextPeriod:
|
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
|
|
|
resNxt[msg.validator_index] = index
|
2021-10-20 16:32:46 +00:00
|
|
|
else:
|
|
|
|
statuses[index] =
|
|
|
|
some(SendResult.err("Message's slot out of state's head range"))
|
|
|
|
else:
|
|
|
|
statuses[index] = some(SendResult.err("Incorrect validator's index"))
|
|
|
|
if (len(resCur) == 0) and (len(resNxt) == 0):
|
|
|
|
return statuses.mapIt(it.get())
|
|
|
|
(resCur, resNxt)
|
|
|
|
|
|
|
|
let (pending, indices) = block:
|
2021-10-06 17:05:06 +00:00
|
|
|
var resFutures: seq[Future[SendResult]]
|
|
|
|
var resIndices: seq[int]
|
2022-01-08 23:28:49 +00:00
|
|
|
for subcommitteeIdx in SyncSubcommitteeIndex:
|
2021-10-06 17:05:06 +00:00
|
|
|
for valKey in syncSubcommittee(
|
2022-01-08 23:28:49 +00:00
|
|
|
node.dag.headSyncCommittees.current_sync_committee, subcommitteeIdx):
|
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
|
|
|
let index = keysCur.getOrDefault(uint64(valKey), -1)
|
2021-10-06 17:05:06 +00:00
|
|
|
if index >= 0:
|
|
|
|
resIndices.add(index)
|
|
|
|
resFutures.add(node.sendSyncCommitteeMessage(msgs[index],
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx, true))
|
|
|
|
for subcommitteeIdx in SyncSubcommitteeIndex:
|
2021-10-06 17:05:06 +00:00
|
|
|
for valKey in syncSubcommittee(
|
2022-01-08 23:28:49 +00:00
|
|
|
node.dag.headSyncCommittees.next_sync_committee, subcommitteeIdx):
|
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
|
|
|
let index = keysNxt.getOrDefault(uint64(valKey), -1)
|
2021-10-06 17:05:06 +00:00
|
|
|
if index >= 0:
|
|
|
|
resIndices.add(index)
|
|
|
|
resFutures.add(node.sendSyncCommitteeMessage(msgs[index],
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx, true))
|
2021-10-06 17:05:06 +00:00
|
|
|
(resFutures, resIndices)
|
2021-09-23 22:13:25 +00:00
|
|
|
|
2021-10-20 16:32:46 +00:00
|
|
|
await allFutures(pending)
|
|
|
|
|
|
|
|
for index, future in pending.pairs():
|
|
|
|
if future.done():
|
|
|
|
let fres = future.read()
|
|
|
|
if fres.isErr():
|
|
|
|
statuses[indices[index]] = some(SendResult.err(fres.error()))
|
|
|
|
else:
|
|
|
|
statuses[indices[index]] = some(SendResult.ok())
|
|
|
|
elif future.failed() or future.cancelled():
|
|
|
|
let exc = future.readError()
|
|
|
|
debug "Unexpected failure while sending committee message",
|
|
|
|
message = msgs[indices[index]], error = $exc.msg
|
|
|
|
statuses[indices[index]] = some(SendResult.err(
|
|
|
|
"Unexpected failure while sending committee message"))
|
2021-09-23 22:13:25 +00:00
|
|
|
|
|
|
|
var res: seq[SendResult]
|
|
|
|
for item in statuses:
|
|
|
|
if item.isSome():
|
|
|
|
res.add(item.get())
|
|
|
|
else:
|
|
|
|
res.add(SendResult.err("Message validator not in sync committee"))
|
|
|
|
res
|
2021-10-20 16:32:46 +00:00
|
|
|
else:
|
|
|
|
var res: seq[SendResult]
|
|
|
|
for _ in msgs:
|
|
|
|
res.add(SendResult.err("Waiting for altair fork"))
|
|
|
|
res
|
2021-08-28 22:27:51 +00:00
|
|
|
|
|
|
|
proc sendSyncCommitteeContribution*(
|
|
|
|
node: BeaconNode,
|
|
|
|
msg: SignedContributionAndProof,
|
2021-09-23 22:13:25 +00:00
|
|
|
checkSignature: bool): Future[SendResult] {.async.} =
|
2021-12-09 12:56:54 +00:00
|
|
|
let res = await node.processor.contributionValidator(
|
2021-12-20 19:20:31 +00:00
|
|
|
MsgSource.api, msg, checkSignature)
|
2021-08-28 22:27:51 +00:00
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
return
|
2021-11-25 19:05:11 +00:00
|
|
|
if res.isGoodForSending:
|
2021-08-28 22:27:51 +00:00
|
|
|
node.network.broadcastSignedContributionAndProof(msg)
|
|
|
|
beacon_sync_committee_contributions_sent.inc()
|
2021-11-05 15:39:47 +00:00
|
|
|
ok()
|
2021-08-28 22:27:51 +00:00
|
|
|
else:
|
2021-09-23 22:13:25 +00:00
|
|
|
notice "Sync committee contribution failed validation",
|
2021-11-05 15:39:47 +00:00
|
|
|
msg, error = res.error()
|
|
|
|
err(res.error()[1])
|
2021-08-28 22:27:51 +00:00
|
|
|
|
2020-06-05 09:57:40 +00:00
|
|
|
proc createAndSendAttestation(node: BeaconNode,
|
|
|
|
fork: Fork,
|
|
|
|
genesis_validators_root: Eth2Digest,
|
|
|
|
validator: AttachedValidator,
|
|
|
|
attestationData: AttestationData,
|
|
|
|
committeeLen: int,
|
2020-06-23 10:38:59 +00:00
|
|
|
indexInCommittee: int,
|
2021-05-10 07:13:36 +00:00
|
|
|
subnet_id: SubnetId) {.async.} =
|
|
|
|
try:
|
2021-11-30 01:20:21 +00:00
|
|
|
var attestation =
|
|
|
|
block:
|
|
|
|
let res = await validator.produceAndSignAttestation(
|
|
|
|
attestationData, committeeLen, indexInCommittee, fork,
|
|
|
|
genesis_validators_root)
|
|
|
|
if res.isErr():
|
|
|
|
error "Unable to sign attestation", validator = shortLog(validator),
|
|
|
|
error_msg = res.error()
|
|
|
|
return
|
|
|
|
res.get()
|
2021-05-10 07:13:36 +00:00
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
let res = await node.sendAttestation(
|
2021-05-10 07:13:36 +00:00
|
|
|
attestation, subnet_id, checkSignature = false)
|
2021-11-05 15:39:47 +00:00
|
|
|
if not res.isOk(): # Logged in sendAttestation
|
2021-05-10 07:13:36 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if node.config.dumpEnabled:
|
2021-07-13 11:15:07 +00:00
|
|
|
dump(node.config.dumpDirOutgoing, attestation.data,
|
2021-12-22 12:37:31 +00:00
|
|
|
validator.pubkey)
|
2021-05-10 07:13:36 +00:00
|
|
|
|
|
|
|
let wallTime = node.beaconClock.now()
|
2022-01-11 10:01:54 +00:00
|
|
|
let deadline = attestationData.slot.attestation_deadline()
|
2021-05-10 07:13:36 +00:00
|
|
|
|
|
|
|
let (delayStr, delaySecs) =
|
|
|
|
if wallTime < deadline:
|
|
|
|
("-" & $(deadline - wallTime), -toFloatSeconds(deadline - wallTime))
|
|
|
|
else:
|
|
|
|
($(wallTime - deadline), toFloatSeconds(wallTime - deadline))
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
notice "Attestation sent",
|
|
|
|
attestation = shortLog(attestation), validator = shortLog(validator),
|
2021-10-20 09:16:48 +00:00
|
|
|
delay = delayStr, subnet_id
|
2021-05-10 07:13:36 +00:00
|
|
|
|
|
|
|
beacon_attestation_sent_delay.observe(delaySecs)
|
|
|
|
except CatchableError as exc:
|
|
|
|
# An error could happen here when the signature task fails - we must
|
|
|
|
# not leak the exception because this is an asyncSpawn task
|
|
|
|
notice "Error sending attestation", err = exc.msg
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2020-11-19 17:19:03 +00:00
|
|
|
proc getBlockProposalEth1Data*(node: BeaconNode,
|
2021-06-11 17:51:46 +00:00
|
|
|
state: ForkedHashedBeaconState):
|
|
|
|
BlockProposalEth1Data =
|
2020-11-19 17:19:03 +00:00
|
|
|
if node.eth1Monitor.isNil:
|
2021-04-14 09:34:35 +00:00
|
|
|
var pendingDepositsCount =
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(state, eth1_data).deposit_count -
|
|
|
|
getStateField(state, eth1_deposit_index)
|
2020-11-24 21:21:47 +00:00
|
|
|
if pendingDepositsCount > 0:
|
|
|
|
result.hasMissingDeposits = true
|
|
|
|
else:
|
2021-06-11 17:51:46 +00:00
|
|
|
result.vote = getStateField(state, eth1_data)
|
2020-11-19 17:19:03 +00:00
|
|
|
else:
|
2021-06-01 11:13:40 +00:00
|
|
|
let finalizedEpochRef = node.dag.getFinalizedEpochRef()
|
2020-11-24 21:21:47 +00:00
|
|
|
result = node.eth1Monitor.getBlockProposalData(
|
2021-06-11 17:51:46 +00:00
|
|
|
state, finalizedEpochRef.eth1_data,
|
2021-04-14 09:34:35 +00:00
|
|
|
finalizedEpochRef.eth1_deposit_index)
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2022-01-18 13:36:52 +00:00
|
|
|
proc forkchoice_updated(state: bellatrix.BeaconState,
|
2021-12-17 12:23:32 +00:00
|
|
|
head_block_hash: Eth2Digest,
|
|
|
|
finalized_block_hash: Eth2Digest,
|
|
|
|
fee_recipient: ethtypes.Address,
|
|
|
|
execution_engine: Web3DataProviderRef):
|
2022-01-18 13:36:52 +00:00
|
|
|
Future[Option[bellatrix.PayloadId]] {.async.} =
|
2021-12-17 12:23:32 +00:00
|
|
|
let
|
|
|
|
timestamp = compute_timestamp_at_slot(state, state.slot)
|
|
|
|
random = get_randao_mix(state, get_current_epoch(state))
|
|
|
|
payloadId =
|
|
|
|
(await execution_engine.forkchoiceUpdated(
|
|
|
|
head_block_hash, finalized_block_hash, timestamp, random.data,
|
|
|
|
fee_recipient)).payloadId
|
|
|
|
return if payloadId.isSome:
|
2022-01-18 13:36:52 +00:00
|
|
|
some(bellatrix.PayloadId(payloadId.get))
|
2021-12-17 12:23:32 +00:00
|
|
|
else:
|
2022-01-18 13:36:52 +00:00
|
|
|
none(bellatrix.PayloadId)
|
2021-12-17 12:23:32 +00:00
|
|
|
|
2020-05-22 17:04:52 +00:00
|
|
|
proc makeBeaconBlockForHeadAndSlot*(node: BeaconNode,
|
2020-10-22 10:53:33 +00:00
|
|
|
randao_reveal: ValidatorSig,
|
2020-05-22 17:04:52 +00:00
|
|
|
validator_index: ValidatorIndex,
|
2020-06-29 17:30:19 +00:00
|
|
|
graffiti: GraffitiBytes,
|
2021-08-27 09:00:06 +00:00
|
|
|
head: BlockRef, slot: Slot
|
2021-08-29 14:50:21 +00:00
|
|
|
): Future[ForkedBlockResult] {.async.} =
|
2020-10-22 10:53:33 +00:00
|
|
|
# Advance state to the slot that we're proposing for
|
2021-03-17 10:17:15 +00:00
|
|
|
let
|
2021-06-01 11:13:40 +00:00
|
|
|
proposalState = assignClone(node.dag.headState)
|
2021-03-17 10:17:15 +00:00
|
|
|
|
2022-01-05 18:38:04 +00:00
|
|
|
node.dag.withUpdatedState(proposalState[], head.atSlot(slot - 1)) do:
|
2021-11-18 12:02:43 +00:00
|
|
|
# Advance to the given slot without calculating state root - we'll only
|
|
|
|
# need a state root _with_ the block applied
|
|
|
|
var info: ForkedEpochInfo
|
2022-01-17 11:19:58 +00:00
|
|
|
|
|
|
|
process_slots(
|
|
|
|
node.dag.cfg, stateData.data, slot, cache, info,
|
|
|
|
{skipLastStateRootCalculation}).expect("advancing 1 slot should not fail")
|
2021-11-18 12:02:43 +00:00
|
|
|
|
2020-05-22 14:21:22 +00:00
|
|
|
let
|
2021-06-11 17:51:46 +00:00
|
|
|
eth1Proposal = node.getBlockProposalEth1Data(stateData.data)
|
2020-05-22 14:21:22 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
if eth1Proposal.hasMissingDeposits:
|
2022-01-17 11:19:58 +00:00
|
|
|
warn "Eth1 deposits not available. Skipping block proposal", slot
|
2021-08-29 14:50:21 +00:00
|
|
|
return ForkedBlockResult.err("Eth1 deposits not available")
|
|
|
|
|
2021-10-18 16:37:27 +00:00
|
|
|
let exits = withState(stateData.data):
|
|
|
|
node.exitPool[].getBeaconBlockExits(state.data)
|
2022-01-17 11:19:58 +00:00
|
|
|
let res = makeBeaconBlock(
|
2021-10-01 01:29:32 +00:00
|
|
|
node.dag.cfg,
|
|
|
|
stateData.data,
|
|
|
|
validator_index,
|
|
|
|
randao_reveal,
|
|
|
|
eth1Proposal.vote,
|
|
|
|
graffiti,
|
|
|
|
node.attestationPool[].getAttestationsForBlock(stateData.data, cache),
|
|
|
|
eth1Proposal.deposits,
|
2021-10-18 16:37:27 +00:00
|
|
|
exits,
|
2021-10-06 17:05:06 +00:00
|
|
|
if slot.epoch < node.dag.cfg.ALTAIR_FORK_EPOCH:
|
|
|
|
SyncAggregate.init()
|
2021-10-01 01:29:32 +00:00
|
|
|
else:
|
|
|
|
node.sync_committee_msg_pool[].produceSyncAggregate(head.root),
|
2022-01-18 13:36:52 +00:00
|
|
|
default(bellatrix.ExecutionPayload),
|
2021-11-18 12:02:43 +00:00
|
|
|
noRollback, # Temporary state - no need for rollback
|
2021-10-01 01:29:32 +00:00
|
|
|
cache)
|
2022-01-17 11:19:58 +00:00
|
|
|
if res.isErr():
|
|
|
|
# This is almost certainly a bug, but it's complex enough that there's a
|
|
|
|
# small risk it might happen even when most proposals succeed - thus we
|
|
|
|
# log instead of asserting
|
|
|
|
error "Cannot create block for proposal",
|
|
|
|
slot, head = shortLog(head), error = res.error()
|
|
|
|
return err($res.error)
|
|
|
|
return ok(res.get())
|
2022-01-05 18:38:04 +00:00
|
|
|
do:
|
2022-01-17 11:19:58 +00:00
|
|
|
error "Cannot get proposal state - skipping block production, database corrupt?",
|
2022-01-05 18:38:04 +00:00
|
|
|
head = shortLog(head),
|
|
|
|
slot
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2020-05-22 17:04:52 +00:00
|
|
|
proc proposeBlock(node: BeaconNode,
|
|
|
|
validator: AttachedValidator,
|
|
|
|
validator_index: ValidatorIndex,
|
|
|
|
head: BlockRef,
|
|
|
|
slot: Slot): Future[BlockRef] {.async.} =
|
|
|
|
if head.slot >= slot:
|
|
|
|
# We should normally not have a head newer than the slot we're proposing for
|
|
|
|
# but this can happen if block proposal is delayed
|
|
|
|
warn "Skipping proposal, have newer head already",
|
|
|
|
headSlot = shortLog(head.slot),
|
|
|
|
headBlockRoot = shortLog(head.root),
|
2020-07-16 13:16:51 +00:00
|
|
|
slot = shortLog(slot)
|
2020-05-22 17:04:52 +00:00
|
|
|
return head
|
2020-05-22 14:21:22 +00:00
|
|
|
|
2020-10-22 10:53:33 +00:00
|
|
|
let
|
2021-08-27 09:00:06 +00:00
|
|
|
fork = node.dag.forkAtEpoch(slot.epoch)
|
2020-10-22 10:53:33 +00:00
|
|
|
genesis_validators_root =
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(node.dag.headState.data, genesis_validators_root)
|
2021-11-30 01:20:21 +00:00
|
|
|
randao =
|
|
|
|
block:
|
|
|
|
let res = await validator.genRandaoReveal(fork, genesis_validators_root,
|
|
|
|
slot)
|
|
|
|
if res.isErr():
|
|
|
|
error "Unable to generate randao reveal",
|
|
|
|
validator = shortLog(validator), error_msg = res.error()
|
|
|
|
return head
|
|
|
|
res.get()
|
|
|
|
|
2021-08-29 14:50:21 +00:00
|
|
|
var newBlock = await makeBeaconBlockForHeadAndSlot(
|
|
|
|
node, randao, validator_index, node.graffitiBytes, head, slot)
|
2021-05-04 13:17:28 +00:00
|
|
|
|
2021-08-29 14:50:21 +00:00
|
|
|
if newBlock.isErr():
|
2020-05-22 17:04:52 +00:00
|
|
|
return head # already logged elsewhere!
|
2021-05-04 13:17:28 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
let forkedBlck = newBlock.get()
|
2021-08-29 14:50:21 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
withBlck(forkedBlck):
|
|
|
|
let
|
|
|
|
blockRoot = hash_tree_root(blck)
|
2021-12-09 12:56:54 +00:00
|
|
|
signing_root = compute_block_signing_root(
|
2021-12-03 13:58:12 +00:00
|
|
|
fork, genesis_validators_root, slot, blockRoot)
|
2021-08-29 14:50:21 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
notSlashable = node.attachedValidators
|
|
|
|
.slashingProtection
|
|
|
|
.registerBlock(validator_index, validator.pubkey, slot, signing_root)
|
2021-08-29 14:50:21 +00:00
|
|
|
|
|
|
|
if notSlashable.isErr:
|
|
|
|
warn "Slashing protection activated",
|
|
|
|
validator = validator.pubkey,
|
|
|
|
slot = slot,
|
|
|
|
existingProposal = notSlashable.error
|
|
|
|
return head
|
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
let
|
|
|
|
signature =
|
|
|
|
block:
|
|
|
|
let res = await validator.signBlockProposal(
|
|
|
|
fork, genesis_validators_root, slot, blockRoot, forkedBlck)
|
|
|
|
if res.isErr():
|
|
|
|
error "Unable to sign block proposal",
|
|
|
|
validator = shortLog(validator), error_msg = res.error()
|
|
|
|
return head
|
|
|
|
res.get()
|
|
|
|
signedBlock =
|
|
|
|
when blck is phase0.BeaconBlock:
|
|
|
|
phase0.SignedBeaconBlock(
|
|
|
|
message: blck, signature: signature, root: blockRoot)
|
|
|
|
elif blck is altair.BeaconBlock:
|
|
|
|
altair.SignedBeaconBlock(
|
|
|
|
message: blck, signature: signature, root: blockRoot)
|
2022-01-18 13:36:52 +00:00
|
|
|
elif blck is bellatrix.BeaconBlock:
|
|
|
|
bellatrix.SignedBeaconBlock(
|
2021-12-03 13:58:12 +00:00
|
|
|
message: blck, signature: signature, root: blockRoot)
|
|
|
|
else:
|
2022-02-13 15:21:55 +00:00
|
|
|
static: doAssert "Unknown SignedBeaconBlock type"
|
2021-08-29 14:50:21 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
# We produced the block using a state transition, meaning the block is valid
|
|
|
|
# enough that it will not be rejected by gossip - it is unlikely but
|
|
|
|
# possible that it will be ignored due to extreme timing conditions, for
|
|
|
|
# example a delay in signing.
|
|
|
|
# We'll start broadcasting it before integrating fully in the chaindag
|
|
|
|
# so that it can start propagating through the network ASAP.
|
|
|
|
node.network.broadcastBeaconBlock(signedBlock)
|
2021-08-29 14:50:21 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
let
|
|
|
|
wallTime = node.beaconClock.now()
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
# storeBlock puts the block in the chaindag, and if accepted, takes care
|
|
|
|
# of side effects such as event api notification
|
|
|
|
newBlockRef = node.blockProcessor[].storeBlock(
|
2021-12-20 19:20:31 +00:00
|
|
|
MsgSource.api, wallTime, signedBlock)
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
if newBlockRef.isErr:
|
|
|
|
warn "Unable to add proposed block to block pool",
|
|
|
|
blockRoot = shortLog(blockRoot), blck = shortLog(blck),
|
|
|
|
signature = shortLog(signature), validator = shortLog(validator)
|
2021-09-27 14:22:58 +00:00
|
|
|
return head
|
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
notice "Block proposed",
|
|
|
|
blockRoot = shortLog(blockRoot), blck = shortLog(blck),
|
|
|
|
signature = shortLog(signature), validator = shortLog(validator)
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
beacon_blocks_proposed.inc()
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
return newBlockRef.get()
|
2020-05-06 13:23:45 +00:00
|
|
|
|
|
|
|
proc handleAttestations(node: BeaconNode, head: BlockRef, slot: Slot) =
|
|
|
|
## Perform all attestations that the validators attached to this node should
|
|
|
|
## perform during the given slot
|
|
|
|
if slot + SLOTS_PER_EPOCH < head.slot:
|
|
|
|
# The latest block we know about is a lot newer than the slot we're being
|
|
|
|
# asked to attest to - this makes it unlikely that it will be included
|
|
|
|
# at all.
|
|
|
|
# TODO the oldest attestations allowed are those that are older than the
|
|
|
|
# finalized epoch.. also, it seems that posting very old attestations
|
|
|
|
# is risky from a slashing perspective. More work is needed here.
|
2020-10-01 18:56:42 +00:00
|
|
|
warn "Skipping attestation, head is too recent",
|
2022-01-05 18:38:04 +00:00
|
|
|
head = shortLog(head),
|
2020-05-06 13:23:45 +00:00
|
|
|
slot = shortLog(slot)
|
|
|
|
return
|
|
|
|
|
|
|
|
let attestationHead = head.atSlot(slot)
|
|
|
|
if head != attestationHead.blck:
|
|
|
|
# In rare cases, such as when we're busy syncing or just slow, we'll be
|
|
|
|
# attesting to a past state - we must then recreate the world as it looked
|
|
|
|
# like back then
|
|
|
|
notice "Attesting to a state in the past, falling behind?",
|
2022-01-05 18:38:04 +00:00
|
|
|
attestationHead = shortLog(attestationHead),
|
|
|
|
head = shortLog(head)
|
2020-05-06 13:23:45 +00:00
|
|
|
|
|
|
|
trace "Checking attestations",
|
2022-01-05 18:38:04 +00:00
|
|
|
attestationHead = shortLog(attestationHead),
|
|
|
|
head = shortLog(head)
|
2020-05-06 13:23:45 +00:00
|
|
|
|
|
|
|
# We need to run attestations exactly for the slot that we're attesting to.
|
|
|
|
# In case blocks went missing, this means advancing past the latest block
|
|
|
|
# using empty slots as fillers.
|
2022-01-29 01:05:39 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/phase0/validator.md#validator-assignments
|
2020-08-10 13:21:31 +00:00
|
|
|
let
|
2022-01-05 18:38:04 +00:00
|
|
|
epochRef = block:
|
|
|
|
let tmp = node.dag.getEpochRef(attestationHead.blck, slot.epoch, false)
|
|
|
|
if isErr(tmp):
|
|
|
|
warn "Cannot construct EpochRef for attestation head, report bug",
|
|
|
|
attestationHead = shortLog(attestationHead), slot
|
|
|
|
return
|
|
|
|
tmp.get()
|
2021-05-10 07:13:36 +00:00
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
2021-08-24 19:49:51 +00:00
|
|
|
fork = node.dag.forkAtEpoch(slot.epoch)
|
2020-08-10 13:21:31 +00:00
|
|
|
genesis_validators_root =
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(node.dag.headState.data, genesis_validators_root)
|
2020-08-10 13:21:31 +00:00
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
for committee_index in get_committee_indices(committees_per_slot):
|
2021-05-10 07:13:36 +00:00
|
|
|
let committee = get_beacon_committee(epochRef, slot, committee_index)
|
2020-08-10 13:21:31 +00:00
|
|
|
|
2021-05-04 13:17:28 +00:00
|
|
|
for index_in_committee, validator_index in committee:
|
|
|
|
let validator = node.getAttachedValidator(epochRef, validator_index)
|
2021-05-10 07:13:36 +00:00
|
|
|
if validator == nil:
|
|
|
|
continue
|
|
|
|
|
|
|
|
let
|
|
|
|
data = makeAttestationData(epochRef, attestationHead, committee_index)
|
|
|
|
# TODO signing_root is recomputed in produceAndSignAttestation/signAttestation just after
|
2021-12-09 12:56:54 +00:00
|
|
|
signing_root = compute_attestation_signing_root(
|
2021-05-10 07:13:36 +00:00
|
|
|
fork, genesis_validators_root, data)
|
|
|
|
registered = node.attachedValidators
|
|
|
|
.slashingProtection
|
|
|
|
.registerAttestation(
|
|
|
|
validator_index,
|
2021-07-13 11:15:07 +00:00
|
|
|
validator.pubkey,
|
2021-05-10 07:13:36 +00:00
|
|
|
data.source.epoch,
|
|
|
|
data.target.epoch,
|
|
|
|
signing_root)
|
|
|
|
if registered.isOk():
|
2021-10-20 09:16:48 +00:00
|
|
|
let subnet_id = compute_subnet_for_attestation(
|
2022-01-08 23:28:49 +00:00
|
|
|
committees_per_slot, data.slot, committee_index)
|
2021-05-10 07:13:36 +00:00
|
|
|
asyncSpawn createAndSendAttestation(
|
|
|
|
node, fork, genesis_validators_root, validator, data,
|
2021-10-20 09:16:48 +00:00
|
|
|
committee.len(), index_in_committee, subnet_id)
|
2021-05-10 07:13:36 +00:00
|
|
|
else:
|
|
|
|
warn "Slashing protection activated for attestation",
|
|
|
|
validator = validator.pubkey,
|
|
|
|
badVoteDetails = $registered.error()
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2021-08-30 00:58:30 +00:00
|
|
|
proc createAndSendSyncCommitteeMessage(node: BeaconNode,
|
|
|
|
slot: Slot,
|
|
|
|
validator: AttachedValidator,
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex,
|
2021-08-30 00:58:30 +00:00
|
|
|
head: BlockRef) {.async.} =
|
|
|
|
try:
|
|
|
|
let
|
|
|
|
fork = node.dag.forkAtEpoch(slot.epoch)
|
|
|
|
genesisValidatorsRoot = node.dag.genesisValidatorsRoot
|
2021-11-30 01:20:21 +00:00
|
|
|
msg =
|
|
|
|
block:
|
2021-12-09 12:56:54 +00:00
|
|
|
let res = await signSyncCommitteeMessage(validator, fork,
|
2021-11-30 01:20:21 +00:00
|
|
|
genesisValidatorsRoot,
|
2021-12-09 12:56:54 +00:00
|
|
|
slot, head.root)
|
2021-11-30 01:20:21 +00:00
|
|
|
if res.isErr():
|
|
|
|
error "Unable to sign committee message using remote signer",
|
|
|
|
validator = shortLog(validator), slot = slot,
|
|
|
|
block_root = shortLog(head.root)
|
|
|
|
return
|
|
|
|
res.get()
|
2021-08-30 00:58:30 +00:00
|
|
|
|
2021-09-23 22:13:25 +00:00
|
|
|
let res = await node.sendSyncCommitteeMessage(
|
2021-11-05 15:39:47 +00:00
|
|
|
msg, subcommitteeIdx, checkSignature = false)
|
2021-09-23 22:13:25 +00:00
|
|
|
if res.isErr():
|
|
|
|
# Logged in sendSyncCommitteeMessage
|
2021-08-30 00:58:30 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if node.config.dumpEnabled:
|
2021-12-22 12:37:31 +00:00
|
|
|
dump(node.config.dumpDirOutgoing, msg, validator.pubkey)
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
wallTime = node.beaconClock.now()
|
2022-01-11 10:01:54 +00:00
|
|
|
deadline = msg.slot.start_beacon_time() + syncCommitteeMessageSlotOffset
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
let (delayStr, delaySecs) =
|
|
|
|
if wallTime < deadline:
|
|
|
|
("-" & $(deadline - wallTime), -toFloatSeconds(deadline - wallTime))
|
|
|
|
else:
|
|
|
|
($(wallTime - deadline), toFloatSeconds(wallTime - deadline))
|
|
|
|
|
|
|
|
notice "Sync committee message sent",
|
|
|
|
message = shortLog(msg),
|
|
|
|
validator = shortLog(validator),
|
|
|
|
delay = delayStr
|
|
|
|
|
|
|
|
beacon_sync_committee_message_sent_delay.observe(delaySecs)
|
|
|
|
except CatchableError as exc:
|
|
|
|
# An error could happen here when the signature task fails - we must
|
|
|
|
# not leak the exception because this is an asyncSpawn task
|
|
|
|
notice "Error sending sync committee message", err = exc.msg
|
|
|
|
|
|
|
|
proc handleSyncCommitteeMessages(node: BeaconNode, head: BlockRef, slot: Slot) =
|
|
|
|
# TODO Use a view type to avoid the copy
|
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
|
|
|
var syncCommittee = node.dag.syncCommitteeParticipants(slot + 1)
|
2021-08-30 00:58:30 +00:00
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
for subcommitteeIdx in SyncSubcommitteeIndex:
|
|
|
|
for valIdx in syncSubcommittee(syncCommittee, subcommitteeIdx):
|
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
|
|
|
let validator = node.getAttachedValidator(
|
|
|
|
getStateField(node.dag.headState.data, validators), valIdx)
|
2021-09-23 22:13:25 +00:00
|
|
|
if isNil(validator) or validator.index.isNone():
|
2021-08-30 00:58:30 +00:00
|
|
|
continue
|
|
|
|
asyncSpawn createAndSendSyncCommitteeMessage(node, slot, validator,
|
2022-01-08 23:28:49 +00:00
|
|
|
subcommitteeIdx, head)
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
proc signAndSendContribution(node: BeaconNode,
|
|
|
|
validator: AttachedValidator,
|
|
|
|
contribution: SyncCommitteeContribution,
|
|
|
|
selectionProof: ValidatorSig) {.async.} =
|
|
|
|
try:
|
|
|
|
let msg = (ref SignedContributionAndProof)(
|
|
|
|
message: ContributionAndProof(
|
|
|
|
aggregator_index: uint64 validator.index.get,
|
|
|
|
contribution: contribution,
|
|
|
|
selection_proof: selectionProof))
|
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
let res = await validator.sign(
|
|
|
|
msg, node.dag.forkAtEpoch(contribution.slot.epoch),
|
|
|
|
node.dag.genesisValidatorsRoot)
|
|
|
|
|
|
|
|
if res.isErr():
|
|
|
|
error "Unable to sign sync committee contribution usign remote signer",
|
|
|
|
validator = shortLog(validator), error_msg = res.error()
|
|
|
|
return
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
# Failures logged in sendSyncCommitteeContribution
|
|
|
|
discard await node.sendSyncCommitteeContribution(msg[], false)
|
2021-11-04 18:22:24 +00:00
|
|
|
notice "Contribution sent", contribution = shortLog(msg[])
|
2021-08-30 00:58:30 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
# An error could happen here when the signature task fails - we must
|
|
|
|
# not leak the exception because this is an asyncSpawn task
|
|
|
|
notice "Error sending sync committee contribution", err = exc.msg
|
|
|
|
|
|
|
|
proc handleSyncCommitteeContributions(node: BeaconNode,
|
|
|
|
head: BlockRef, slot: Slot) {.async.} =
|
|
|
|
# TODO Use a view type to avoid the copy
|
|
|
|
let
|
|
|
|
fork = node.dag.forkAtEpoch(slot.epoch)
|
|
|
|
genesisValidatorsRoot = node.dag.genesisValidatorsRoot
|
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
|
|
|
syncCommittee = node.dag.syncCommitteeParticipants(slot + 1)
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
AggregatorCandidate = object
|
|
|
|
validator: AttachedValidator
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIdx: SyncSubcommitteeIndex
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
var candidateAggregators: seq[AggregatorCandidate]
|
2021-11-30 01:20:21 +00:00
|
|
|
var selectionProofs: seq[Future[SignatureResult]]
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
var time = timeIt:
|
2022-01-08 23:28:49 +00:00
|
|
|
for subcommitteeIdx in SyncSubcommitteeIndex:
|
2021-08-30 00:58:30 +00:00
|
|
|
# TODO Hoist outside of the loop with a view type
|
|
|
|
# to avoid the repeated offset calculations
|
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
|
|
|
for valIdx in syncSubcommittee(syncCommittee, subcommitteeIdx):
|
|
|
|
let validator = node.getAttachedValidator(
|
|
|
|
getStateField(node.dag.headState.data, validators), valIdx)
|
2021-08-30 00:58:30 +00:00
|
|
|
if validator == nil:
|
|
|
|
continue
|
|
|
|
|
|
|
|
candidateAggregators.add AggregatorCandidate(
|
|
|
|
validator: validator,
|
2021-11-05 15:39:47 +00:00
|
|
|
subcommitteeIdx: subcommitteeIdx)
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
selectionProofs.add validator.getSyncCommitteeSelectionProof(
|
2021-11-05 15:39:47 +00:00
|
|
|
fork, genesisValidatorsRoot, slot, subcommitteeIdx.asUInt64)
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
await allFutures(selectionProofs)
|
|
|
|
|
|
|
|
debug "Prepared contributions selection proofs",
|
|
|
|
count = selectionProofs.len, time
|
|
|
|
|
|
|
|
var contributionsSent = 0
|
2021-11-04 18:22:24 +00:00
|
|
|
|
2021-08-30 00:58:30 +00:00
|
|
|
time = timeIt:
|
2021-11-30 01:20:21 +00:00
|
|
|
for i, proof in selectionProofs.pairs():
|
|
|
|
if not proof.completed:
|
2021-08-30 00:58:30 +00:00
|
|
|
continue
|
|
|
|
|
2021-11-30 01:20:21 +00:00
|
|
|
let selectionProofRes = proof.read()
|
|
|
|
if selectionProofRes.isErr():
|
|
|
|
error "Unable to sign selection proof using remote signer",
|
|
|
|
validator = shortLog(candidateAggregators[i].validator),
|
|
|
|
slot, head, subnet_id = candidateAggregators[i].subcommitteeIdx
|
|
|
|
continue
|
|
|
|
let selectionProof = selectionProofRes.get()
|
2021-08-30 00:58:30 +00:00
|
|
|
if not is_sync_committee_aggregator(selectionProof):
|
|
|
|
continue
|
|
|
|
|
|
|
|
var contribution: SyncCommitteeContribution
|
|
|
|
let contributionWasProduced = node.syncCommitteeMsgPool[].produceContribution(
|
2021-11-04 18:22:24 +00:00
|
|
|
slot,
|
|
|
|
head.root,
|
|
|
|
candidateAggregators[i].subcommitteeIdx,
|
|
|
|
contribution)
|
2021-08-30 00:58:30 +00:00
|
|
|
|
|
|
|
if contributionWasProduced:
|
|
|
|
asyncSpawn signAndSendContribution(
|
|
|
|
node,
|
|
|
|
candidateAggregators[i].validator,
|
|
|
|
contribution,
|
|
|
|
selectionProof)
|
|
|
|
inc contributionsSent
|
|
|
|
else:
|
|
|
|
debug "Failure to produce contribution",
|
2021-11-05 15:39:47 +00:00
|
|
|
slot, head, subnet_id = candidateAggregators[i].subcommitteeIdx
|
2021-08-30 00:58:30 +00:00
|
|
|
|
2020-05-06 13:23:45 +00:00
|
|
|
proc handleProposal(node: BeaconNode, head: BlockRef, slot: Slot):
|
|
|
|
Future[BlockRef] {.async.} =
|
|
|
|
## Perform the proposal for the given slot, iff we have a validator attached
|
2020-11-20 14:16:04 +00:00
|
|
|
## that is supposed to do so, given the shuffling at that slot for the given
|
|
|
|
## head - to compute the proposer, we need to advance a state to the given
|
|
|
|
## slot
|
2021-06-01 11:13:40 +00:00
|
|
|
let proposer = node.dag.getProposer(head, slot)
|
2020-05-22 14:21:22 +00:00
|
|
|
if proposer.isNone():
|
2020-05-06 13:23:45 +00:00
|
|
|
return head
|
|
|
|
|
2021-06-01 11:13:40 +00:00
|
|
|
let
|
2021-08-27 09:00:06 +00:00
|
|
|
proposerKey = node.dag.validatorKey(proposer.get).get().toPubKey
|
2021-06-01 11:13:40 +00:00
|
|
|
validator = node.attachedValidators[].getValidator(proposerKey)
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2021-12-03 13:58:12 +00:00
|
|
|
return
|
|
|
|
if validator == nil:
|
|
|
|
debug "Expecting block proposal",
|
|
|
|
headRoot = shortLog(head.root),
|
|
|
|
slot = shortLog(slot),
|
|
|
|
proposer_index = proposer.get(),
|
|
|
|
proposer = shortLog(proposerKey)
|
|
|
|
|
|
|
|
head
|
|
|
|
else:
|
|
|
|
await proposeBlock(node, validator, proposer.get(), head, slot)
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2021-08-24 19:49:51 +00:00
|
|
|
proc makeAggregateAndProof*(
|
2022-02-04 07:33:20 +00:00
|
|
|
pool: var AttestationPool, epochRef: EpochRef, slot: Slot,
|
|
|
|
committee_index: CommitteeIndex,
|
|
|
|
validator_index: ValidatorIndex,
|
|
|
|
slot_signature: ValidatorSig): Opt[AggregateAndProof] =
|
|
|
|
doAssert validator_index in get_beacon_committee(epochRef, slot, committee_index)
|
2021-08-24 19:49:51 +00:00
|
|
|
|
|
|
|
# TODO for testing purposes, refactor this into the condition check
|
|
|
|
# and just calculation
|
2022-01-29 01:05:39 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/phase0/validator.md#aggregation-selection
|
2022-02-04 07:33:20 +00:00
|
|
|
if not is_aggregator(epochRef, slot, committee_index, slot_signature):
|
|
|
|
return err()
|
2021-08-24 19:49:51 +00:00
|
|
|
|
2022-02-04 07:33:20 +00:00
|
|
|
let maybe_slot_attestation = getAggregatedAttestation(pool, slot, committee_index)
|
2021-08-24 19:49:51 +00:00
|
|
|
if maybe_slot_attestation.isNone:
|
2022-02-04 07:33:20 +00:00
|
|
|
return err()
|
2021-08-24 19:49:51 +00:00
|
|
|
|
2022-01-29 01:05:39 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/phase0/validator.md#construct-aggregate
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/phase0/validator.md#aggregateandproof
|
2022-02-04 07:33:20 +00:00
|
|
|
ok(AggregateAndProof(
|
|
|
|
aggregator_index: validator_index.uint64,
|
2021-08-24 19:49:51 +00:00
|
|
|
aggregate: maybe_slot_attestation.get,
|
|
|
|
selection_proof: slot_signature))
|
|
|
|
|
2021-08-19 10:45:31 +00:00
|
|
|
proc sendAggregatedAttestations(
|
2022-01-05 18:38:04 +00:00
|
|
|
node: BeaconNode, head: BlockRef, slot: Slot) {.async.} =
|
|
|
|
# Aggregated attestations must be sent by members of the beacon committees for
|
|
|
|
# the given slot, for which `is_aggregator` returns `true.
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2020-10-22 10:53:33 +00:00
|
|
|
let
|
2022-01-05 18:38:04 +00:00
|
|
|
epochRef = block:
|
|
|
|
let tmp = node.dag.getEpochRef(head, slot.epoch, false)
|
|
|
|
if isErr(tmp): # Some unusual race condition perhaps?
|
|
|
|
warn "Cannot construct EpochRef for head, report bug",
|
|
|
|
head = shortLog(head), slot
|
|
|
|
return
|
|
|
|
tmp.get()
|
|
|
|
|
|
|
|
fork = node.dag.forkAtEpoch(slot.epoch)
|
2020-10-22 10:53:33 +00:00
|
|
|
genesis_validators_root =
|
2021-06-11 17:51:46 +00:00
|
|
|
getStateField(node.dag.headState.data, genesis_validators_root)
|
2020-10-22 10:53:33 +00:00
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
|
|
|
|
|
|
|
var
|
2021-11-30 01:20:21 +00:00
|
|
|
slotSigs: seq[Future[SignatureResult]] = @[]
|
2022-01-08 23:28:49 +00:00
|
|
|
slotSigsData: seq[tuple[committee_index: CommitteeIndex,
|
|
|
|
validator_index: ValidatorIndex,
|
2020-10-22 10:53:33 +00:00
|
|
|
v: AttachedValidator]] = @[]
|
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
for committee_index in get_committee_indices(committees_per_slot):
|
|
|
|
let committee = get_beacon_committee(epochRef, slot, committee_index)
|
2020-10-22 10:53:33 +00:00
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
for index_in_committee, validator_index in committee:
|
|
|
|
let validator = node.getAttachedValidator(epochRef, validator_index)
|
2020-10-22 10:53:33 +00:00
|
|
|
if validator != nil:
|
|
|
|
# the validator index and private key pair.
|
|
|
|
slotSigs.add getSlotSig(validator, fork,
|
2022-01-05 18:38:04 +00:00
|
|
|
genesis_validators_root, slot)
|
2022-01-08 23:28:49 +00:00
|
|
|
slotSigsData.add (committee_index, validator_index, validator)
|
2020-10-22 10:53:33 +00:00
|
|
|
|
|
|
|
await allFutures(slotSigs)
|
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
doAssert slotSigsData.len == slotSigs.len
|
|
|
|
for i in 0..<slotSigs.len:
|
|
|
|
let
|
|
|
|
data = slotSigsData[i]
|
2022-02-04 07:33:20 +00:00
|
|
|
slotSig = slotSigs[i].read().valueOr:
|
|
|
|
error "Unable to create slot signature using remote signer",
|
|
|
|
validator = shortLog(data.v),
|
|
|
|
slot, error = error
|
|
|
|
continue
|
|
|
|
aggregateAndProof = makeAggregateAndProof(
|
|
|
|
node.attestationPool[], epochRef, slot, data.committee_index,
|
|
|
|
data.validator_index, slotSig).valueOr:
|
|
|
|
# Don't broadcast when, e.g., this validator isn't aggregator
|
|
|
|
continue
|
|
|
|
|
|
|
|
sig = block:
|
|
|
|
let res = await signAggregateAndProof(data.v,
|
|
|
|
aggregateAndProof, fork, genesis_validators_root)
|
|
|
|
if res.isErr():
|
|
|
|
error "Unable to sign aggregated attestation using remote signer",
|
|
|
|
validator = shortLog(data.v), error_msg = res.error()
|
|
|
|
return
|
|
|
|
res.get()
|
|
|
|
signedAP = SignedAggregateAndProof(
|
|
|
|
message: aggregateAndProof,
|
2020-10-22 10:53:33 +00:00
|
|
|
signature: sig)
|
2022-02-04 07:33:20 +00:00
|
|
|
node.network.broadcastAggregateAndProof(signedAP)
|
|
|
|
|
|
|
|
# The subnet on which the attestations (should have) arrived
|
|
|
|
let subnet_id = compute_subnet_for_attestation(
|
|
|
|
committees_per_slot, slot, data.committee_index)
|
|
|
|
notice "Aggregated attestation sent",
|
|
|
|
aggregate = shortLog(signedAP.message.aggregate),
|
|
|
|
aggregator_index = signedAP.message.aggregator_index,
|
|
|
|
signature = shortLog(signedAP.signature),
|
|
|
|
validator = shortLog(data.v),
|
|
|
|
subnet_id
|
|
|
|
|
|
|
|
node.validatorMonitor[].registerAggregate(
|
|
|
|
MsgSource.api, node.beaconClock.now(), signedAP.message,
|
|
|
|
get_attesting_indices(
|
|
|
|
epochRef, slot,
|
|
|
|
data.committee_index,
|
|
|
|
aggregateAndProof.aggregate.aggregation_bits))
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2020-12-16 13:03:04 +00:00
|
|
|
proc updateValidatorMetrics*(node: BeaconNode) =
|
2021-08-28 22:27:51 +00:00
|
|
|
# Technically, this only needs to be done on epoch transitions and if there's
|
|
|
|
# a reorg that spans an epoch transition, but it's easier to implement this
|
|
|
|
# way for now.
|
|
|
|
|
|
|
|
# We'll limit labelled metrics to the first 64, so that we don't overload
|
|
|
|
# Prometheus.
|
|
|
|
|
|
|
|
var total: Gwei
|
|
|
|
var i = 0
|
|
|
|
for _, v in node.attachedValidators[].validators:
|
|
|
|
let balance =
|
|
|
|
if v.index.isNone():
|
|
|
|
0.Gwei
|
|
|
|
elif v.index.get().uint64 >=
|
|
|
|
getStateField(node.dag.headState.data, balances).lenu64:
|
|
|
|
debug "Cannot get validator balance, index out of bounds",
|
|
|
|
pubkey = shortLog(v.pubkey), index = v.index.get(),
|
|
|
|
balances = getStateField(node.dag.headState.data, balances).len,
|
|
|
|
stateRoot = getStateRoot(node.dag.headState.data)
|
|
|
|
0.Gwei
|
|
|
|
else:
|
2021-11-12 22:29:28 +00:00
|
|
|
getStateField(node.dag.headState.data, balances).asSeq()[v.index.get()]
|
2021-08-28 22:27:51 +00:00
|
|
|
|
|
|
|
if i < 64:
|
|
|
|
attached_validator_balance.set(
|
|
|
|
balance.toGaugeValue, labelValues = [shortLog(v.pubkey)])
|
|
|
|
|
|
|
|
inc i
|
|
|
|
total += balance
|
|
|
|
|
|
|
|
node.attachedValidatorBalanceTotal = total
|
|
|
|
attached_validator_balance_total.set(total.toGaugeValue)
|
2020-11-27 23:34:25 +00:00
|
|
|
|
2020-10-28 07:55:36 +00:00
|
|
|
proc handleValidatorDuties*(node: BeaconNode, lastSlot, slot: Slot) {.async.} =
|
2020-07-22 08:04:21 +00:00
|
|
|
## Perform validator duties - create blocks, vote and aggregate existing votes
|
2021-02-22 16:17:48 +00:00
|
|
|
if node.attachedValidators[].count == 0:
|
2020-05-06 13:23:45 +00:00
|
|
|
# Nothing to do because we have no validator attached
|
2020-06-10 06:58:12 +00:00
|
|
|
return
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2021-06-01 11:13:40 +00:00
|
|
|
# The dag head might be updated by sync while we're working due to the
|
2020-10-28 07:55:36 +00:00
|
|
|
# await calls, thus we use a local variable to keep the logic straight here
|
2021-06-01 11:13:40 +00:00
|
|
|
var head = node.dag.head
|
2020-05-06 13:23:45 +00:00
|
|
|
if not node.isSynced(head):
|
2022-02-04 11:25:32 +00:00
|
|
|
info "Syncing in progress; skipping validator duties for now",
|
|
|
|
slot, headSlot = head.slot
|
2020-12-16 13:03:04 +00:00
|
|
|
|
|
|
|
# Rewards will be growing though, as we sync..
|
|
|
|
updateValidatorMetrics(node)
|
|
|
|
|
2020-06-10 06:58:12 +00:00
|
|
|
return
|
2020-05-06 13:23:45 +00:00
|
|
|
|
|
|
|
var curSlot = lastSlot + 1
|
|
|
|
|
2021-01-29 12:38:52 +00:00
|
|
|
# If broadcastStartEpoch is 0, it hasn't had time to initialize yet, which
|
|
|
|
# means that it'd be okay not to continue, but it won't gossip regardless.
|
2022-01-03 21:18:49 +00:00
|
|
|
if curSlot.epoch <
|
2021-02-01 11:18:16 +00:00
|
|
|
node.processor[].doppelgangerDetection.broadcastStartEpoch and
|
2022-02-07 05:12:36 +00:00
|
|
|
node.processor[].doppelgangerDetection.nodeLaunchSlot > GENESIS_SLOT and
|
2021-02-03 17:11:42 +00:00
|
|
|
node.config.doppelgangerDetection:
|
2022-01-03 21:18:49 +00:00
|
|
|
let
|
2022-02-04 11:25:32 +00:00
|
|
|
nextAttestationSlot = node.actionTracker.getNextAttestationSlot(slot - 1)
|
|
|
|
nextProposalSlot = node.actionTracker.getNextProposalSlot(slot - 1)
|
2022-01-03 21:18:49 +00:00
|
|
|
|
|
|
|
if slot in [nextAttestationSlot, nextProposalSlot]:
|
|
|
|
notice "Doppelganger detection active - skipping validator duties while observing activity on the network",
|
2022-02-04 11:25:32 +00:00
|
|
|
slot, epoch = slot.epoch,
|
2022-01-03 21:18:49 +00:00
|
|
|
broadcastStartEpoch =
|
|
|
|
node.processor[].doppelgangerDetection.broadcastStartEpoch
|
|
|
|
else:
|
|
|
|
debug "Doppelganger detection active - skipping validator duties while observing activity on the network",
|
2022-02-04 11:25:32 +00:00
|
|
|
slot, epoch = slot.epoch,
|
2022-01-03 21:18:49 +00:00
|
|
|
broadcastStartEpoch =
|
|
|
|
node.processor[].doppelgangerDetection.broadcastStartEpoch
|
|
|
|
|
2020-10-27 17:21:35 +00:00
|
|
|
return
|
|
|
|
|
2020-05-06 13:23:45 +00:00
|
|
|
# Start by checking if there's work we should have done in the past that we
|
|
|
|
# can still meaningfully do
|
|
|
|
while curSlot < slot:
|
2020-10-01 18:56:42 +00:00
|
|
|
notice "Catching up on validator duties",
|
2020-05-06 13:23:45 +00:00
|
|
|
curSlot = shortLog(curSlot),
|
|
|
|
lastSlot = shortLog(lastSlot),
|
2020-07-16 13:16:51 +00:00
|
|
|
slot = shortLog(slot)
|
2020-05-06 13:23:45 +00:00
|
|
|
|
|
|
|
# For every slot we're catching up, we'll propose then send
|
|
|
|
# attestations - head should normally be advancing along the same branch
|
|
|
|
# in this case
|
|
|
|
head = await handleProposal(node, head, curSlot)
|
|
|
|
|
|
|
|
# For each slot we missed, we need to send out attestations - if we were
|
|
|
|
# proposing during this time, we'll use the newly proposed head, else just
|
|
|
|
# keep reusing the same - the attestation that goes out will actually
|
|
|
|
# rewind the state to what it looked like at the time of that slot
|
|
|
|
handleAttestations(node, head, curSlot)
|
|
|
|
|
|
|
|
curSlot += 1
|
|
|
|
|
|
|
|
head = await handleProposal(node, head, slot)
|
|
|
|
|
2021-03-01 16:36:06 +00:00
|
|
|
let
|
|
|
|
# The latest point in time when we'll be sending out attestations
|
2022-01-11 10:01:54 +00:00
|
|
|
attestationCutoff = node.beaconClock.fromNow(slot.attestation_deadline())
|
2021-03-01 16:36:06 +00:00
|
|
|
|
|
|
|
if attestationCutoff.inFuture:
|
|
|
|
debug "Waiting to send attestations",
|
|
|
|
head = shortLog(head),
|
|
|
|
attestationCutoff = shortLog(attestationCutoff.offset)
|
|
|
|
|
|
|
|
# Wait either for the block or the attestation cutoff time to arrive
|
2021-03-11 10:10:57 +00:00
|
|
|
if await node.consensusManager[].expectBlock(slot).withTimeout(attestationCutoff.offset):
|
2021-03-01 16:36:06 +00:00
|
|
|
# The expected block arrived (or expectBlock was called again which
|
2021-03-23 06:57:10 +00:00
|
|
|
# shouldn't happen as this is the only place we use it) - in our async
|
|
|
|
# loop however, we might have been doing other processing that caused delays
|
2021-03-01 16:36:06 +00:00
|
|
|
# here so we'll cap the waiting to the time when we would have sent out
|
|
|
|
# attestations had the block not arrived.
|
|
|
|
# An opposite case is that we received (or produced) a block that has
|
|
|
|
# not yet reached our neighbours. To protect against our attestations
|
|
|
|
# being dropped (because the others have not yet seen the block), we'll
|
2021-07-06 13:11:18 +00:00
|
|
|
# impose a minimum delay of 1000ms. The delay is enforced only when we're
|
2021-03-01 16:36:06 +00:00
|
|
|
# not hitting the "normal" cutoff time for sending out attestations.
|
2021-07-06 13:11:18 +00:00
|
|
|
# An earlier delay of 250ms has proven to be not enough, increasing the
|
|
|
|
# risk of losing attestations.
|
|
|
|
# Regardless, because we "just" received the block, we'll impose the
|
|
|
|
# delay.
|
2021-03-01 16:36:06 +00:00
|
|
|
|
2021-07-06 13:11:18 +00:00
|
|
|
const afterBlockDelay = 1000
|
2021-03-01 16:36:06 +00:00
|
|
|
let
|
|
|
|
afterBlockTime = node.beaconClock.now() + millis(afterBlockDelay)
|
|
|
|
afterBlockCutoff = node.beaconClock.fromNow(
|
2022-01-11 10:01:54 +00:00
|
|
|
min(afterBlockTime, slot.attestation_deadline() + millis(afterBlockDelay)))
|
2021-03-01 16:36:06 +00:00
|
|
|
|
|
|
|
if afterBlockCutoff.inFuture:
|
|
|
|
debug "Got block, waiting to send attestations",
|
|
|
|
head = shortLog(head),
|
|
|
|
afterBlockCutoff = shortLog(afterBlockCutoff.offset)
|
|
|
|
|
|
|
|
await sleepAsync(afterBlockCutoff.offset)
|
|
|
|
|
|
|
|
# Time passed - we might need to select a new head in that case
|
2021-03-11 10:10:57 +00:00
|
|
|
node.consensusManager[].updateHead(slot)
|
2021-06-01 11:13:40 +00:00
|
|
|
head = node.dag.head
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2021-11-25 12:20:36 +00:00
|
|
|
static: doAssert attestationSlotOffset == syncCommitteeMessageSlotOffset
|
|
|
|
|
2020-05-06 13:23:45 +00:00
|
|
|
handleAttestations(node, head, slot)
|
2021-08-30 00:58:30 +00:00
|
|
|
handleSyncCommitteeMessages(node, head, slot)
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2020-12-16 13:03:04 +00:00
|
|
|
updateValidatorMetrics(node) # the important stuff is done, update the vanity numbers
|
2020-11-27 23:34:25 +00:00
|
|
|
|
2022-01-29 01:05:39 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.9/specs/phase0/validator.md#broadcast-aggregate
|
2021-12-03 17:40:23 +00:00
|
|
|
# If the validator is selected to aggregate (`is_aggregator`), then they
|
|
|
|
# broadcast their best aggregate as a `SignedAggregateAndProof` to the global
|
|
|
|
# aggregate channel (`beacon_aggregate_and_proof`) `2 / INTERVALS_PER_SLOT`
|
|
|
|
# of the way through the `slot`-that is,
|
|
|
|
# `SECONDS_PER_SLOT * 2 / INTERVALS_PER_SLOT` seconds after the start of `slot`.
|
2020-05-06 13:23:45 +00:00
|
|
|
if slot > 2:
|
2022-01-11 10:01:54 +00:00
|
|
|
doAssert slot.aggregate_deadline() == slot.sync_contribution_deadline()
|
2021-03-01 16:36:06 +00:00
|
|
|
let
|
2022-01-11 10:01:54 +00:00
|
|
|
aggregateCutoff = node.beaconClock.fromNow(slot.aggregate_deadline())
|
2021-11-25 12:20:36 +00:00
|
|
|
if aggregateCutoff.inFuture:
|
2021-03-01 16:36:06 +00:00
|
|
|
debug "Waiting to send aggregate attestations",
|
2021-11-25 12:20:36 +00:00
|
|
|
aggregateCutoff = shortLog(aggregateCutoff.offset)
|
|
|
|
await sleepAsync(aggregateCutoff.offset)
|
2020-05-06 13:23:45 +00:00
|
|
|
|
2021-08-19 10:45:31 +00:00
|
|
|
let sendAggregatedAttestationsFut =
|
|
|
|
sendAggregatedAttestations(node, head, slot)
|
|
|
|
|
2021-08-30 00:58:30 +00:00
|
|
|
let handleSyncCommitteeContributionsFut =
|
|
|
|
handleSyncCommitteeContributions(node, head, slot)
|
|
|
|
|
|
|
|
await handleSyncCommitteeContributionsFut
|
2021-08-19 10:45:31 +00:00
|
|
|
await sendAggregatedAttestationsFut
|
2020-11-30 23:59:35 +00:00
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
proc sendAttestation*(node: BeaconNode,
|
|
|
|
attestation: Attestation): Future[SendResult] {.async.} =
|
|
|
|
# REST/JSON-RPC API helper procedure.
|
|
|
|
let
|
limit by-root requests to non-finalized blocks (#3293)
* limit by-root requests to non-finalized blocks
Presently, we keep a mapping from block root to `BlockRef` in memory -
this has simplified reasoning about the dag, but is not sustainable with
the chain growing.
We can distinguish between two cases where by-root access is useful:
* unfinalized blocks - this is where the beacon chain is operating
generally, by validating incoming data as interesting for future fork
choice decisions - bounded by the length of the unfinalized period
* finalized blocks - historical access in the REST API etc - no bounds,
really
In this PR, we limit the by-root block index to the first use case:
finalized chain data can more efficiently be addressed by slot number.
Future work includes:
* limiting the `BlockRef` horizon in general - each instance is 40
bytes+overhead which adds up - this needs further refactoring to deal
with the tail vs state problem
* persisting the finalized slot-to-hash index - this one also keeps
growing unbounded (albeit slowly)
Anyway, this PR easily shaves ~128mb of memory usage at the time of
writing.
* No longer honor `BeaconBlocksByRoot` requests outside of the
non-finalized period - previously, Nimbus would generously return any
block through this libp2p request - per the spec, finalized blocks
should be fetched via `BeaconBlocksByRange` instead.
* return `Opt[BlockRef]` instead of `nil` when blocks can't be found -
this becomes a lot more common now and thus deserves more attention
* `dag.blocks` -> `dag.forkBlocks` - this index only carries unfinalized
blocks from now - `finalizedBlocks` covers the other `BlockRef`
instances
* in backfill, verify that the last backfilled block leads back to
genesis, or panic
* add backfill timings to log
* fix missing check that `BlockRef` block can be fetched with
`getForkedBlock` reliably
* shortcut doppelganger check when feature is not enabled
* in REST/JSON-RPC, fetch blocks without involving `BlockRef`
* fix dag.blocks ref
2022-01-21 11:33:16 +00:00
|
|
|
target = node.dag.getBlockRef(attestation.data.target.root).valueOr:
|
|
|
|
notice "Attempt to send attestation for unknown target",
|
|
|
|
attestation = shortLog(attestation)
|
|
|
|
return SendResult.err(
|
|
|
|
"Attempt to send attestation for unknown block")
|
|
|
|
|
|
|
|
epochRef = node.dag.getEpochRef(
|
|
|
|
target, attestation.data.target.epoch, false).valueOr:
|
|
|
|
warn "Cannot construct EpochRef for attestation, skipping send - report bug",
|
|
|
|
target = shortLog(target),
|
|
|
|
attestation = shortLog(attestation)
|
|
|
|
return
|
|
|
|
committee_index =
|
|
|
|
epochRef.get_committee_index(attestation.data.index).valueOr:
|
2022-01-08 23:28:49 +00:00
|
|
|
notice "Invalid committee index in attestation",
|
|
|
|
attestation = shortLog(attestation)
|
|
|
|
return SendResult.err("Invalid committee index in attestation")
|
2021-10-20 09:16:48 +00:00
|
|
|
subnet_id = compute_subnet_for_attestation(
|
2021-08-23 10:41:48 +00:00
|
|
|
get_committee_count_per_slot(epochRef), attestation.data.slot,
|
2022-01-08 23:28:49 +00:00
|
|
|
committee_index)
|
2021-10-20 09:16:48 +00:00
|
|
|
res = await node.sendAttestation(attestation, subnet_id,
|
2021-08-23 10:41:48 +00:00
|
|
|
checkSignature = true)
|
2021-11-05 15:39:47 +00:00
|
|
|
if not res.isOk():
|
|
|
|
return res
|
2021-10-20 09:16:48 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
wallTime = node.processor.getCurrentBeaconTime()
|
2022-01-11 10:01:54 +00:00
|
|
|
deadline = attestation.data.slot.attestation_deadline()
|
2021-10-20 09:16:48 +00:00
|
|
|
(delayStr, delaySecs) =
|
|
|
|
if wallTime < deadline:
|
|
|
|
("-" & $(deadline - wallTime), -toFloatSeconds(deadline - wallTime))
|
|
|
|
else:
|
|
|
|
($(wallTime - deadline), toFloatSeconds(wallTime - deadline))
|
|
|
|
|
|
|
|
notice "Attestation sent",
|
|
|
|
attestation = shortLog(attestation), delay = delayStr, subnet_id
|
|
|
|
|
|
|
|
beacon_attestation_sent_delay.observe(delaySecs)
|
|
|
|
|
2021-08-23 10:41:48 +00:00
|
|
|
return SendResult.ok()
|
|
|
|
|
|
|
|
proc sendAggregateAndProof*(node: BeaconNode,
|
2021-10-20 09:16:48 +00:00
|
|
|
proof: SignedAggregateAndProof): Future[SendResult] {.
|
2021-08-23 10:41:48 +00:00
|
|
|
async.} =
|
|
|
|
# REST/JSON-RPC API helper procedure.
|
2021-12-20 19:20:31 +00:00
|
|
|
let res = await node.processor.aggregateValidator(MsgSource.api, proof)
|
2021-11-05 15:39:47 +00:00
|
|
|
return
|
2021-11-25 19:05:11 +00:00
|
|
|
if res.isGoodForSending:
|
2021-11-05 15:39:47 +00:00
|
|
|
node.network.broadcastAggregateAndProof(proof)
|
2021-10-20 09:16:48 +00:00
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
notice "Aggregated attestation sent",
|
|
|
|
attestation = shortLog(proof.message.aggregate),
|
|
|
|
aggregator_index = proof.message.aggregator_index,
|
|
|
|
signature = shortLog(proof.signature)
|
2021-10-20 09:16:48 +00:00
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
notice "Aggregate and proof failed validation",
|
|
|
|
proof = shortLog(proof.message.aggregate), error = res.error()
|
2021-10-20 09:16:48 +00:00
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
err(res.error()[1])
|
2021-08-23 10:41:48 +00:00
|
|
|
|
|
|
|
proc sendVoluntaryExit*(node: BeaconNode,
|
|
|
|
exit: SignedVoluntaryExit): SendResult =
|
|
|
|
# REST/JSON-RPC API helper procedure.
|
2021-12-20 19:20:31 +00:00
|
|
|
let res = node.processor[].voluntaryExitValidator(MsgSource.api, exit)
|
2021-11-25 19:05:11 +00:00
|
|
|
if res.isGoodForSending:
|
2021-08-23 10:41:48 +00:00
|
|
|
node.network.broadcastVoluntaryExit(exit)
|
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
notice "Voluntary exit request failed validation",
|
2021-11-05 15:39:47 +00:00
|
|
|
exit = shortLog(exit.message), error = res.error()
|
|
|
|
err(res.error()[1])
|
2021-08-23 10:41:48 +00:00
|
|
|
|
|
|
|
proc sendAttesterSlashing*(node: BeaconNode,
|
|
|
|
slashing: AttesterSlashing): SendResult =
|
|
|
|
# REST/JSON-RPC API helper procedure.
|
2021-12-20 19:20:31 +00:00
|
|
|
let res = node.processor[].attesterSlashingValidator(MsgSource.api, slashing)
|
2021-11-25 19:05:11 +00:00
|
|
|
if res.isGoodForSending:
|
2021-08-23 10:41:48 +00:00
|
|
|
node.network.broadcastAttesterSlashing(slashing)
|
|
|
|
ok()
|
|
|
|
else:
|
|
|
|
notice "Attester slashing request failed validation",
|
2021-11-05 15:39:47 +00:00
|
|
|
slashing = shortLog(slashing), error = res.error()
|
|
|
|
err(res.error()[1])
|
2021-08-23 10:41:48 +00:00
|
|
|
|
|
|
|
proc sendProposerSlashing*(node: BeaconNode,
|
|
|
|
slashing: ProposerSlashing): SendResult =
|
|
|
|
# REST/JSON-RPC API helper procedure.
|
2021-12-20 19:20:31 +00:00
|
|
|
let res = node.processor[].proposerSlashingValidator(MsgSource.api, slashing)
|
2021-11-25 19:05:11 +00:00
|
|
|
if res.isGoodForSending:
|
2021-08-23 10:41:48 +00:00
|
|
|
node.network.broadcastProposerSlashing(slashing)
|
2021-11-05 15:39:47 +00:00
|
|
|
ok()
|
2021-08-23 10:41:48 +00:00
|
|
|
else:
|
|
|
|
notice "Proposer slashing request failed validation",
|
2021-11-05 15:39:47 +00:00
|
|
|
slashing = shortLog(slashing), error = res.error()
|
|
|
|
err(res.error()[1])
|
2021-08-23 10:41:48 +00:00
|
|
|
|
|
|
|
proc sendBeaconBlock*(node: BeaconNode, forked: ForkedSignedBeaconBlock
|
|
|
|
): Future[SendBlockResult] {.async.} =
|
|
|
|
# REST/JSON-RPC API helper procedure.
|
2021-12-03 13:58:12 +00:00
|
|
|
block:
|
|
|
|
# Start with a quick gossip validation check such that broadcasting the
|
|
|
|
# block doesn't get the node into trouble
|
|
|
|
let res = withBlck(forked):
|
2022-02-13 15:21:55 +00:00
|
|
|
validateBeaconBlock(node.dag, node.quarantine, blck,
|
|
|
|
node.beaconClock.now(), {})
|
2021-12-03 13:58:12 +00:00
|
|
|
if not res.isGoodForSending():
|
|
|
|
return SendBlockResult.err(res.error()[1])
|
|
|
|
|
|
|
|
# The block passed basic gossip validation - we can "safely" broadcast it now.
|
|
|
|
# In fact, per the spec, we should broadcast it even if it later fails to
|
|
|
|
# apply to our state.
|
|
|
|
node.network.broadcastBeaconBlock(forked)
|
|
|
|
|
|
|
|
let
|
|
|
|
wallTime = node.beaconClock.now()
|
|
|
|
accepted = withBlck(forked):
|
|
|
|
let newBlockRef = node.blockProcessor[].storeBlock(
|
2021-12-20 19:20:31 +00:00
|
|
|
MsgSource.api, wallTime, blck)
|
2021-12-03 13:58:12 +00:00
|
|
|
|
|
|
|
# The boolean we return tells the caller whether the block was integrated
|
|
|
|
# into the chain
|
|
|
|
if newBlockRef.isOk():
|
|
|
|
notice "Block published",
|
|
|
|
blockRoot = shortLog(blck.root), blck = shortLog(blck.message),
|
|
|
|
signature = shortLog(blck.signature)
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
warn "Unable to add proposed block to block pool",
|
|
|
|
blockRoot = shortLog(blck.root), blck = shortLog(blck.message),
|
|
|
|
signature = shortLog(blck.signature), err = newBlockRef.error()
|
|
|
|
|
|
|
|
false
|
|
|
|
return SendBlockResult.ok(accepted)
|
2021-10-18 09:11:44 +00:00
|
|
|
|
|
|
|
proc registerDuty*(
|
2021-10-20 09:16:48 +00:00
|
|
|
node: BeaconNode, slot: Slot, subnet_id: SubnetId, vidx: ValidatorIndex,
|
2021-10-18 09:11:44 +00:00
|
|
|
isAggregator: bool) =
|
|
|
|
# Only register relevant duties
|
2021-10-20 09:16:48 +00:00
|
|
|
node.actionTracker.registerDuty(slot, subnet_id, vidx, isAggregator)
|
2021-10-18 09:11:44 +00:00
|
|
|
|
|
|
|
proc registerDuties*(node: BeaconNode, wallSlot: Slot) {.async.} =
|
|
|
|
## Register upcoming duties of attached validators with the duty tracker
|
|
|
|
|
|
|
|
if node.attachedValidators[].count() == 0 or not node.isSynced(node.dag.head):
|
|
|
|
# Nothing to do because we have no validator attached
|
|
|
|
return
|
|
|
|
|
|
|
|
let
|
|
|
|
genesis_validators_root =
|
|
|
|
getStateField(node.dag.headState.data, genesis_validators_root)
|
|
|
|
head = node.dag.head
|
|
|
|
|
|
|
|
# Getting the slot signature is expensive but cached - in "normal" cases we'll
|
|
|
|
# be getting the duties one slot at a time
|
|
|
|
for slot in wallSlot ..< wallSlot + SUBNET_SUBSCRIPTION_LEAD_TIME_SLOTS:
|
|
|
|
let
|
2022-01-05 18:38:04 +00:00
|
|
|
epochRef = block:
|
|
|
|
let tmp = node.dag.getEpochRef(head, slot.epoch, false)
|
|
|
|
if tmp.isErr(): # Shouldn't happen
|
|
|
|
warn "Cannot construct EpochRef for duties - report bug",
|
|
|
|
head = shortLog(head), slot
|
|
|
|
return
|
|
|
|
tmp.get()
|
|
|
|
let
|
2021-10-18 09:11:44 +00:00
|
|
|
fork = node.dag.forkAtEpoch(slot.epoch)
|
|
|
|
committees_per_slot = get_committee_count_per_slot(epochRef)
|
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
for committee_index in get_committee_indices(committees_per_slot):
|
|
|
|
let committee = get_beacon_committee(epochRef, slot, committee_index)
|
2021-10-18 09:11:44 +00:00
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
for index_in_committee, validator_index in committee:
|
|
|
|
let validator = node.getAttachedValidator(epochRef, validator_index)
|
2021-10-18 09:11:44 +00:00
|
|
|
if validator != nil:
|
|
|
|
let
|
2021-10-20 09:16:48 +00:00
|
|
|
subnet_id = compute_subnet_for_attestation(
|
2022-01-08 23:28:49 +00:00
|
|
|
committees_per_slot, slot, committee_index)
|
2021-11-30 01:20:21 +00:00
|
|
|
let slotSigRes = await getSlotSig(validator, fork,
|
|
|
|
genesis_validators_root, slot)
|
|
|
|
if slotSigRes.isErr():
|
|
|
|
error "Unable to create slot signature using remote signer",
|
|
|
|
validator = shortLog(validator),
|
|
|
|
error_msg = slotSigRes.error()
|
|
|
|
continue
|
|
|
|
let isAggregator = is_aggregator(committee.lenu64, slotSigRes.get())
|
2021-10-18 09:11:44 +00:00
|
|
|
|
2022-01-08 23:28:49 +00:00
|
|
|
node.registerDuty(slot, subnet_id, validator_index, isAggregator)
|