2021-03-01 10:22:16 +00:00
# Beacon Block Flow
This is a WIP document to explain the beacon block flows.
## Validation & Verification flow
Important distinction:
- We distinguish block `validation` which is defined in the P2P specs:
2024-04-17 03:51:16 +00:00
https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/p2p-interface.md#beacon_block.
2021-03-01 10:22:16 +00:00
A validated block can be forwarded on gossipsub.
- and we distinguish `verification` which is defined in consensus specs:
2024-08-09 16:24:49 +00:00
https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.4/specs/phase0/beacon-chain.md#block-processing
2021-03-01 10:22:16 +00:00
A block needs to be verified to enter fork choice, the DAG and the BeaconChainDB
In particular in terms of costly checks validating a block only requires checking:
- the **block proposer signature**
while verifying a block requires checking:
- the state transition
- the block proposer signature
- and also the signatures within a block:
- Randao Reveal
- Proposer slashings
- Attester slashings
- Attestations
- VoluntaryExits
### Inputs
Blocks can be received from the following sources:
- Gossipsub
2021-11-05 15:39:47 +00:00
- via `topicBeaconBlocks` (`& "/eth2/{$forkDigest}/{topicBeaconBlocksSuffix}"`). They are then validated by `blockValidator` (in `eth2_processor.nim` ) which calls `validateBeaconBlock`
2021-03-01 10:22:16 +00:00
- ETH2 RPC
- via SyncManager (when Nimbus syncs)
- via RequestManager (when Nimbus needs ancestor blocks)
- the NBC database
- a local validator block proposal
- Devtools: test suite, ncli, fuzzing
The related base types are:
- BeaconBlockBody
- BeaconBlock
- BeaconBlockBody
- + metadata (slot, blockchain state before/after, proposer)
- BeaconBlockHeader
- metadata (slot, blockchain state before/after, proposer)
2023-08-07 10:06:47 +00:00
- Merkle hash of the BeaconBlockBody
2021-03-01 10:22:16 +00:00
- SignedBeaconBlock
- BeaconBlock
- + BLS signature
The base types are defined in the Eth2 specs.
On top, Nimbus builds new types to represent the level of trust and validation we have with regards to each BeaconBlock.
Those types allow the Nim compiler to help us ensure proper usage at compile-time and zero runtime cost.
#### BeaconBlocks
Those are spec-defined types.
On deserialization the SSZ code guarantees that BeaconBlock are correctly max-sized
according to:
- MAX_PROPOSER_SLASHINGS
- MAX_ATTESTER_SLASHINGS
- MAX_ATTESTATIONS
- MAX_DEPOSITS
- MAX_VOLUNTARY_EXITS
#### TrustedBeaconBlocks
A block that has been fully checked to be sound
both in regards to the blockchain protocol and its cryptographic signatures is known as a `TrustedBeaconBlock` or `TrustedSignedBeaconBlock` .
This allows skipping expensive signature checks.
Blocks are considered trusted if they come from:
- the NBC database
- produced by a local validator
#### SigVerifiedBeaconBlocks
A block with a valid cryptographic signature is considered SigVerified.
This is a weaker guarantee than Trusted as the block might still be invalid according to the state transition function.
2024-01-03 12:48:08 +00:00
Such a block is produced if incoming gossip blocks' signatures are batched together for batch verification **before** being passed to state transition.
2021-03-01 10:22:16 +00:00
#### TransitionVerifiedBeaconBlocks
A block that passes the state transition checks and can be successfully applied to the beacon chain is considered `TransitionVerified` .
Such a block can be produced if incoming blocks' signatures are batched together for batch verification **after** successfully passing state transition.
_This is not used in Nimbus at the moment_
## Block processing architecture
How the various modules interact with block is described in a diagram:
![./block_flow.png ](./block_flow.png )
It is important to note that 3 data structures are sharing the same `AsyncQueue[BlockEntry]` :
2021-12-08 21:15:29 +00:00
- Eth2Processor.blockQueue
2021-03-01 10:22:16 +00:00
- SyncManager.outQueue
- RequestManager.outQueue
### Gossip flow in
2021-10-21 13:09:19 +00:00
Blocks are listened to via the gossipsub topic `/eth2/{$forkDigest}/beacon_block/ssz_snappy` (`topicBeaconBlocks` variable)
2021-03-01 10:22:16 +00:00
They are then:
2023-11-27 18:56:34 +00:00
- validated by `blockValidator()` in the Eth2Processor by `validateBeaconBlock()` according to spec https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.4/specs/phase0/p2p-interface.md#beacon_block
2021-03-01 10:22:16 +00:00
- Important: P2P validation is not full verification (state transition and internal cryptographic signatures were not checked)
- enqueued in the shared block queue `AsyncQueue[BlockEntry]` in case of success
- dropped in case of error
Logs:
- debug: "Dropping already-seen gossip block"
- debug: "Block received"
- trace: "Block validated"
### Gossip flow out
2024-04-17 03:51:16 +00:00
- After validation in `blockValidator()` in the Eth2Processor by `validateBeaconBlock()` according to spec https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/p2p-interface.md#beacon_block
2021-03-01 10:22:16 +00:00
- Important: P2P validation is not full verification (state transition and internal cryptographic signatures were not checked)
- We jump into libp2p/protocols/pubsub/pubsub.nim in the method `validate(PubSub, message)`
- which was called by `rpcHandler(GossipSub, PubSubPeer, RPCMsg)`
2021-03-02 10:27:45 +00:00
### Block syncing
2021-03-01 10:22:16 +00:00
2021-03-02 10:27:45 +00:00
See [Block syncing ](../beacon_chain/sync/README.md )
for flow explanation and performance characteristics.
2021-03-01 10:22:16 +00:00
### Consuming the shared block queue
The SharedBlockQueue is consumed by the Eth2Processor via `runQueueProcessingLoop(Eth2Processor)`
To mitigate blocking networking and timeshare between Io and compute, blocks are processed 1 by 1 by `processBlock(Eth2Processor, BlockEntry)`
This in turn calls:
- `storeBlock(Eth2Processor, SignedBeaconBlock, Slot)`
Backfill support for ChainDAG (#3171)
In the ChainDAG, 3 block pointers are kept: genesis, tail and head. This
PR adds one more block pointer: the backfill block which represents the
block that has been backfilled so far.
When doing a checkpoint sync, a random block is given as starting point
- this is the tail block, and we require that the tail block has a
corresponding state.
When backfilling, we end up with blocks without corresponding states,
hence we cannot use `tail` as a backfill pointer - there is no state.
Nonetheless, we need to keep track of where we are in the backfill
process between restarts, such that we can answer GetBeaconBlocksByRange
requests.
This PR adds the basic support for backfill handling - it needs to be
integrated with backfill sync, and the REST API needs to be adjusted to
take advantage of the new backfilled blocks when responding to certain
requests.
Future work will also enable moving the tail in either direction:
* pruning means moving the tail forward in time and removing states
* backwards means recreating past states from genesis, such that
intermediate states are recreated step by step all the way to the tail -
at that point, tail, genesis and backfill will match up.
* backfilling is done when backfill != genesis - later, this will be the
WSS checkpoint instead
2021-12-13 13:36:06 +00:00
- `addHeadBlock(ChainDAGRef, var BatchVerifier, SignedBeaconBlock, forkChoiceCallback)`
2021-03-01 10:22:16 +00:00
- trigger sending attestation if relevant
2021-03-02 10:27:45 +00:00
### Steady state (synced to head)
2021-03-01 10:22:16 +00:00
#### Bottlenecks when synced
The SyncManager is deactivated
RequestManager and Gossip are activated.
##### Backpressure
There is no backpressure handling at the RequestManager and Gossip level with regards to the SharedBlockQueue
There is backpressure handling at the Quarantine level:
- Blocks in the SharedBlockQueue that are missing parents
2024-01-03 12:48:08 +00:00
are put in quarantine, only 16 can be stored and new candidates are dropped as long as the older ones are unresolved.
2021-03-01 10:22:16 +00:00
##### Latency & Throughput sensitiveness
When synced, blocks are a small part of the whole processing compared to attestations, there is no more throughput constraint however a single block should be processed ASAP as it blocks attestation flow.
Furthermore to ensure the stability of the gossip mesh, blocks should be validated and rebroadcasted ASAP as well.