reth_network/
import.rs

1//! This module provides an abstraction over block import in the form of the `BlockImport` trait.
2
3use crate::message::NewBlockMessage;
4use reth_network_peers::PeerId;
5use std::task::{Context, Poll};
6
7/// Abstraction over block import.
8pub trait BlockImport<B = reth_primitives::Block>: std::fmt::Debug + Send + Sync {
9    /// Invoked for a received `NewBlock` broadcast message from the peer.
10    ///
11    /// > When a `NewBlock` announcement message is received from a peer, the client first verifies
12    /// > the basic header validity of the block, checking whether the proof-of-work value is valid.
13    ///
14    /// This is supposed to start verification. The results are then expected to be returned via
15    /// [`BlockImport::poll`].
16    fn on_new_block(&mut self, peer_id: PeerId, incoming_block: NewBlockMessage<B>);
17
18    /// Returns the results of a [`BlockImport::on_new_block`]
19    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<BlockImportOutcome<B>>;
20}
21
22/// Outcome of the [`BlockImport`]'s block handling.
23#[derive(Debug)]
24pub struct BlockImportOutcome<B = reth_primitives::Block> {
25    /// Sender of the `NewBlock` message.
26    pub peer: PeerId,
27    /// The result after validating the block
28    pub result: Result<BlockValidation<B>, BlockImportError>,
29}
30
31/// Represents the successful validation of a received `NewBlock` message.
32#[derive(Debug)]
33pub enum BlockValidation<B> {
34    /// Basic Header validity check, after which the block should be relayed to peers via a
35    /// `NewBlock` message
36    ValidHeader {
37        /// received block
38        block: NewBlockMessage<B>,
39    },
40    /// Successfully imported: state-root matches after execution. The block should be relayed via
41    /// `NewBlockHashes`
42    ValidBlock {
43        /// validated block.
44        block: NewBlockMessage<B>,
45    },
46}
47
48/// Represents the error case of a failed block import
49#[derive(Debug, thiserror::Error)]
50pub enum BlockImportError {
51    /// Consensus error
52    #[error(transparent)]
53    Consensus(#[from] reth_consensus::ConsensusError),
54}
55
56/// An implementation of `BlockImport` used in Proof-of-Stake consensus that does nothing.
57///
58/// Block propagation over devp2p is invalid in POS: [EIP-3675](https://eips.ethereum.org/EIPS/eip-3675#devp2p)
59#[derive(Debug, Default)]
60#[non_exhaustive]
61pub struct ProofOfStakeBlockImport;
62
63impl<B> BlockImport<B> for ProofOfStakeBlockImport {
64    fn on_new_block(&mut self, _peer_id: PeerId, _incoming_block: NewBlockMessage<B>) {}
65
66    fn poll(&mut self, _cx: &mut Context<'_>) -> Poll<BlockImportOutcome<B>> {
67        Poll::Pending
68    }
69}