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_eth_wire_types::broadcast::NewBlockHashes;
5use reth_network_peers::PeerId;
6use std::{
7    error::Error,
8    task::{Context, Poll},
9};
10
11/// Abstraction over block import.
12pub trait BlockImport<B = reth_ethereum_primitives::Block>: std::fmt::Debug + Send + Sync {
13    /// Invoked for a received block announcement from the peer.
14    ///
15    /// For a `NewBlock` message:
16    /// > When a `NewBlock` announcement message is received from a peer, the client first verifies
17    /// > the basic header validity of the block, checking whether the proof-of-work value is valid.
18    ///
19    /// For a `NewBlockHashes` message, hash announcement should be processed accordingly.
20    ///
21    /// The results are expected to be returned via [`BlockImport::poll`].
22    fn on_new_block(&mut self, peer_id: PeerId, incoming_block: NewBlockEvent<B>);
23
24    /// Returns the results of a [`BlockImport::on_new_block`]
25    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<BlockImportEvent<B>>;
26}
27
28/// Represents different types of block announcement events from the network.
29#[derive(Debug, Clone)]
30pub enum NewBlockEvent<B = reth_ethereum_primitives::Block> {
31    /// A new full block announcement
32    Block(NewBlockMessage<B>),
33    /// Only the hashes of new blocks
34    Hashes(NewBlockHashes),
35}
36
37/// Represents different types of block import events
38#[derive(Debug)]
39pub enum BlockImportEvent<B = reth_ethereum_primitives::Block> {
40    /// General block announcement and validation status
41    Announcement(BlockValidation<B>),
42    /// Result of a peer-specific block import
43    Outcome(BlockImportOutcome<B>),
44}
45
46/// Outcome of the [`BlockImport`]'s block handling.
47#[derive(Debug)]
48pub struct BlockImportOutcome<B = reth_ethereum_primitives::Block> {
49    /// Sender of the block announcement message.
50    pub peer: PeerId,
51    /// The result after validating the block
52    pub result: Result<BlockValidation<B>, BlockImportError>,
53}
54
55/// Represents the successful validation of a received block announcement.
56#[derive(Debug)]
57pub enum BlockValidation<B> {
58    /// Basic Header validity check, after which the block should be relayed to peers via a
59    /// `NewBlock` message
60    ValidHeader {
61        /// received block
62        block: NewBlockMessage<B>,
63    },
64    /// Successfully imported: state-root matches after execution. The block should be relayed via
65    /// `NewBlockHashes`
66    ValidBlock {
67        /// validated block.
68        block: NewBlockMessage<B>,
69    },
70}
71
72/// Represents the error case of a failed block import
73#[derive(Debug, thiserror::Error)]
74pub enum BlockImportError {
75    /// Consensus error
76    #[error(transparent)]
77    Consensus(#[from] reth_consensus::ConsensusError),
78    /// Other error
79    #[error(transparent)]
80    Other(#[from] Box<dyn Error + Send + Sync>),
81}
82
83/// An implementation of `BlockImport` used in Proof-of-Stake consensus that does nothing.
84///
85/// Block propagation over devp2p is invalid in POS: [EIP-3675](https://eips.ethereum.org/EIPS/eip-3675#devp2p)
86#[derive(Debug, Default)]
87#[non_exhaustive]
88pub struct ProofOfStakeBlockImport;
89
90impl<B> BlockImport<B> for ProofOfStakeBlockImport {
91    fn on_new_block(&mut self, _peer_id: PeerId, _incoming_block: NewBlockEvent<B>) {}
92
93    fn poll(&mut self, _cx: &mut Context<'_>) -> Poll<BlockImportEvent<B>> {
94        Poll::Pending
95    }
96}