2021-03-11 10:10:57 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2021-03-11 10:10:57 +00:00
|
|
|
import
|
|
|
|
std/math,
|
|
|
|
stew/results,
|
|
|
|
chronicles, chronos, metrics,
|
2021-07-19 11:58:30 +00:00
|
|
|
../spec/datatypes/[phase0, altair],
|
|
|
|
../spec/[crypto, digest, forkedbeaconstate_helpers],
|
2021-03-11 10:10:57 +00:00
|
|
|
../consensus_object_pools/[block_clearance, blockchain_dag, attestation_pool],
|
|
|
|
./consensus_manager,
|
2021-04-06 11:59:11 +00:00
|
|
|
".."/[beacon_clock, beacon_node_types],
|
|
|
|
../ssz/sszdump
|
2021-03-11 10:10:57 +00:00
|
|
|
|
2021-07-15 19:01:07 +00:00
|
|
|
export sszdump
|
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
# Block Processor
|
2021-03-11 10:10:57 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2021-05-28 16:34:00 +00:00
|
|
|
# The block processor moves blocks from "Incoming" to "Consensus verified"
|
2021-03-11 10:10:57 +00:00
|
|
|
|
|
|
|
declareHistogram beacon_store_block_duration_seconds,
|
|
|
|
"storeBlock() duration", buckets = [0.25, 0.5, 1, 2, 4, 8, Inf]
|
|
|
|
|
|
|
|
type
|
|
|
|
BlockEntry* = object
|
2021-07-15 19:01:07 +00:00
|
|
|
blck*: ForkedSignedBeaconBlock
|
2021-05-28 16:34:00 +00:00
|
|
|
resfut*: Future[Result[void, BlockError]]
|
|
|
|
queueTick*: Moment # Moment when block was enqueued
|
|
|
|
validationDur*: Duration # Time it took to perform gossip validation
|
2021-03-11 10:10:57 +00:00
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
BlockProcessor* = object
|
|
|
|
## This manages the processing of blocks from different sources
|
2021-03-11 10:10:57 +00:00
|
|
|
## Blocks and attestations are enqueued in a gossip-validated state
|
|
|
|
##
|
|
|
|
## from:
|
|
|
|
## - Gossip (when synced)
|
|
|
|
## - SyncManager (during sync)
|
|
|
|
## - RequestManager (missing ancestor blocks)
|
|
|
|
##
|
|
|
|
## are then consensus-verified and added to:
|
|
|
|
## - the blockchain DAG
|
|
|
|
## - database
|
|
|
|
## - attestation pool
|
|
|
|
## - fork choice
|
|
|
|
|
|
|
|
# Config
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
dumpEnabled: bool
|
|
|
|
dumpDirInvalid: string
|
|
|
|
dumpDirIncoming: string
|
|
|
|
|
|
|
|
# Producers
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
blocksQueue*: AsyncQueue[BlockEntry] # Exported for "test_sync_manager"
|
|
|
|
|
|
|
|
# Consumer
|
|
|
|
# ----------------------------------------------------------------
|
|
|
|
consensusManager: ref ConsensusManager
|
|
|
|
## Blockchain DAG, AttestationPool and Quarantine
|
2021-07-07 09:09:47 +00:00
|
|
|
getTime: GetTimeFn
|
2021-03-11 10:10:57 +00:00
|
|
|
|
|
|
|
# Initialization
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
proc new*(T: type BlockProcessor,
|
2021-04-06 11:59:11 +00:00
|
|
|
dumpEnabled: bool,
|
|
|
|
dumpDirInvalid, dumpDirIncoming: string,
|
2021-03-11 10:10:57 +00:00
|
|
|
consensusManager: ref ConsensusManager,
|
2021-07-07 09:09:47 +00:00
|
|
|
getTime: GetTimeFn): ref BlockProcessor =
|
2021-05-28 16:34:00 +00:00
|
|
|
(ref BlockProcessor)(
|
2021-04-06 11:59:11 +00:00
|
|
|
dumpEnabled: dumpEnabled,
|
|
|
|
dumpDirInvalid: dumpDirInvalid,
|
|
|
|
dumpDirIncoming: dumpDirIncoming,
|
2021-04-26 20:39:44 +00:00
|
|
|
blocksQueue: newAsyncQueue[BlockEntry](),
|
2021-04-06 11:59:11 +00:00
|
|
|
consensusManager: consensusManager,
|
2021-07-07 09:09:47 +00:00
|
|
|
getTime: getTime)
|
|
|
|
|
|
|
|
proc getCurrentBeaconTime*(self: BlockProcessor): BeaconTime =
|
|
|
|
self.consensusManager.dag.beaconClock.toBeaconTime(self.getTime())
|
2021-03-11 10:10:57 +00:00
|
|
|
|
|
|
|
# Sync callbacks
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
proc done*(entry: BlockEntry) =
|
|
|
|
## Send signal to [Sync/Request]Manager that the block ``entry`` has passed
|
2021-03-11 10:10:57 +00:00
|
|
|
## verification successfully.
|
2021-05-28 16:34:00 +00:00
|
|
|
if entry.resfut != nil:
|
|
|
|
entry.resfut.complete(Result[void, BlockError].ok())
|
2021-03-11 10:10:57 +00:00
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
proc fail*(entry: BlockEntry, error: BlockError) =
|
2021-03-11 10:10:57 +00:00
|
|
|
## Send signal to [Sync/Request]Manager that the block ``blk`` has NOT passed
|
|
|
|
## verification with specific ``error``.
|
2021-05-28 16:34:00 +00:00
|
|
|
if entry.resfut != nil:
|
|
|
|
entry.resfut.complete(Result[void, BlockError].err(error))
|
2021-03-11 10:10:57 +00:00
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
proc hasBlocks*(self: BlockProcessor): bool =
|
2021-04-26 20:39:44 +00:00
|
|
|
self.blocksQueue.len() > 0
|
|
|
|
|
2021-03-11 10:10:57 +00:00
|
|
|
# Enqueue
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
proc addBlock*(
|
2021-07-15 19:01:07 +00:00
|
|
|
self: var BlockProcessor, blck: ForkedSignedBeaconBlock,
|
2021-05-28 16:34:00 +00:00
|
|
|
resfut: Future[Result[void, BlockError]] = nil,
|
|
|
|
validationDur = Duration()) =
|
2021-03-11 10:10:57 +00:00
|
|
|
## Enqueue a Gossip-validated block for consensus verification
|
|
|
|
# Backpressure:
|
2021-04-26 20:39:44 +00:00
|
|
|
# There is no backpressure here - producers must wait for the future in the
|
2021-05-28 16:34:00 +00:00
|
|
|
# BlockEntry to constrain their own processing
|
2021-03-11 10:10:57 +00:00
|
|
|
# Producers:
|
|
|
|
# - Gossip (when synced)
|
|
|
|
# - SyncManager (during sync)
|
|
|
|
# - RequestManager (missing ancestor blocks)
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
# addLast doesn't fail with unbounded queues, but we'll add asyncSpawn as a
|
|
|
|
# sanity check
|
2021-05-28 16:34:00 +00:00
|
|
|
try:
|
|
|
|
self.blocksQueue.addLastNoWait(BlockEntry(
|
2021-07-19 11:58:30 +00:00
|
|
|
blck: blck,
|
|
|
|
resfut: resfut, queueTick: Moment.now(),
|
2021-05-28 16:34:00 +00:00
|
|
|
validationDur: validationDur))
|
|
|
|
except AsyncQueueFullError:
|
|
|
|
raiseAssert "unbounded queue"
|
2021-03-11 10:10:57 +00:00
|
|
|
|
|
|
|
# Storage
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc dumpBlock*[T](
|
2021-07-15 19:01:07 +00:00
|
|
|
self: BlockProcessor,
|
|
|
|
signedBlock: phase0.SignedBeaconBlock | altair.SignedBeaconBlock,
|
2021-03-11 10:10:57 +00:00
|
|
|
res: Result[T, (ValidationResult, BlockError)]) =
|
|
|
|
if self.dumpEnabled and res.isErr:
|
|
|
|
case res.error[1]
|
|
|
|
of Invalid:
|
|
|
|
dump(
|
|
|
|
self.dumpDirInvalid, signedBlock)
|
|
|
|
of MissingParent:
|
|
|
|
dump(
|
|
|
|
self.dumpDirIncoming, signedBlock)
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc storeBlock(
|
2021-07-15 19:01:07 +00:00
|
|
|
self: var BlockProcessor,
|
|
|
|
signedBlock: phase0.SignedBeaconBlock | altair.SignedBeaconBlock,
|
2021-03-11 10:10:57 +00:00
|
|
|
wallSlot: Slot): Result[void, BlockError] =
|
|
|
|
let
|
|
|
|
attestationPool = self.consensusManager.attestationPool
|
|
|
|
|
2021-07-15 19:01:07 +00:00
|
|
|
type Trusted = typeof signedBlock.asTrusted()
|
2021-06-01 11:13:40 +00:00
|
|
|
let blck = self.consensusManager.dag.addRawBlock(
|
2021-05-28 16:34:00 +00:00
|
|
|
self.consensusManager.quarantine, signedBlock) do (
|
2021-07-15 19:01:07 +00:00
|
|
|
blckRef: BlockRef, trustedBlock: Trusted, epochRef: EpochRef):
|
2021-03-11 10:10:57 +00:00
|
|
|
# Callback add to fork choice if valid
|
|
|
|
attestationPool[].addForkChoice(
|
|
|
|
epochRef, blckRef, trustedBlock.message, wallSlot)
|
|
|
|
|
|
|
|
self.dumpBlock(signedBlock, blck)
|
|
|
|
|
|
|
|
# There can be a scenario where we receive a block we already received.
|
|
|
|
# However this block was before the last finalized epoch and so its parent
|
|
|
|
# was pruned from the ForkChoice.
|
|
|
|
if blck.isErr:
|
|
|
|
return err(blck.error[1])
|
|
|
|
ok()
|
|
|
|
|
|
|
|
# Event Loop
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
proc processBlock(self: var BlockProcessor, entry: BlockEntry) =
|
2021-03-11 10:10:57 +00:00
|
|
|
logScope:
|
2021-05-28 16:34:00 +00:00
|
|
|
blockRoot = shortLog(entry.blck.root)
|
2021-03-11 10:10:57 +00:00
|
|
|
|
|
|
|
let
|
2021-07-07 09:09:47 +00:00
|
|
|
wallTime = self.getCurrentBeaconTime()
|
2021-03-11 10:10:57 +00:00
|
|
|
(afterGenesis, wallSlot) = wallTime.toSlot()
|
|
|
|
|
|
|
|
if not afterGenesis:
|
|
|
|
error "Processing block before genesis, clock turned back?"
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
let
|
2021-05-28 16:34:00 +00:00
|
|
|
startTick = Moment.now()
|
2021-07-15 19:01:07 +00:00
|
|
|
res = withBlck(entry.blck): self.storeBlock(blck, wallSlot)
|
2021-05-28 19:03:20 +00:00
|
|
|
storeBlockTick = Moment.now()
|
2021-03-11 10:10:57 +00:00
|
|
|
|
|
|
|
if res.isOk():
|
|
|
|
# Eagerly update head in case the new block gets selected
|
2021-05-28 16:34:00 +00:00
|
|
|
self.consensusManager[].updateHead(wallSlot)
|
|
|
|
|
|
|
|
let
|
|
|
|
updateHeadTick = Moment.now()
|
|
|
|
queueDur = startTick - entry.queueTick
|
2021-05-28 19:03:20 +00:00
|
|
|
storeBlockDur = storeBlockTick - startTick
|
|
|
|
updateHeadDur = updateHeadTick - storeBlockTick
|
2021-05-28 16:34:00 +00:00
|
|
|
|
|
|
|
beacon_store_block_duration_seconds.observe(storeBlockDur.toFloatSeconds())
|
|
|
|
|
2021-03-11 10:10:57 +00:00
|
|
|
debug "Block processed",
|
2021-06-01 11:13:40 +00:00
|
|
|
localHeadSlot = self.consensusManager.dag.head.slot,
|
2021-07-15 19:01:07 +00:00
|
|
|
blockSlot = entry.blck.slot,
|
2021-05-28 16:34:00 +00:00
|
|
|
validationDur = entry.validationDur,
|
|
|
|
queueDur, storeBlockDur, updateHeadDur
|
|
|
|
|
|
|
|
entry.done()
|
2021-03-11 10:10:57 +00:00
|
|
|
elif res.error() in {BlockError.Duplicate, BlockError.Old}:
|
2021-05-28 16:34:00 +00:00
|
|
|
# Duplicate and old blocks are ok from a sync point of view, so we mark
|
|
|
|
# them as successful
|
|
|
|
entry.done()
|
2021-03-11 10:10:57 +00:00
|
|
|
else:
|
2021-05-28 16:34:00 +00:00
|
|
|
entry.fail(res.error())
|
2021-03-11 10:10:57 +00:00
|
|
|
|
2021-05-28 16:34:00 +00:00
|
|
|
proc runQueueProcessingLoop*(self: ref BlockProcessor) {.async.} =
|
2021-03-11 10:10:57 +00:00
|
|
|
while true:
|
2021-04-26 20:39:44 +00:00
|
|
|
# Cooperative concurrency: one block per loop iteration - because
|
2021-03-11 10:10:57 +00:00
|
|
|
# we run both networking and CPU-heavy things like block processing
|
|
|
|
# on the same thread, we need to make sure that there is steady progress
|
|
|
|
# on the networking side or we get long lockups that lead to timeouts.
|
|
|
|
const
|
|
|
|
# We cap waiting for an idle slot in case there's a lot of network traffic
|
|
|
|
# taking up all CPU - we don't want to _completely_ stop processing blocks
|
2021-04-26 20:39:44 +00:00
|
|
|
# in this case - doing so also allows us to benefit from more batching /
|
|
|
|
# larger network reads when under load.
|
2021-03-11 10:10:57 +00:00
|
|
|
idleTimeout = 10.milliseconds
|
|
|
|
|
|
|
|
discard await idleAsync().withTimeout(idleTimeout)
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
self[].processBlock(await self[].blocksQueue.popFirst())
|