Skip to main content

ab_client_block_import/
lib.rs

1pub mod beacon_chain;
2mod importing_blocks;
3#[cfg(test)]
4mod test_utils;
5
6use ab_client_api::{BlockOrigin, PersistBlockError};
7use ab_core_primitives::block::BlockRoot;
8use ab_core_primitives::hashes::Blake3Hash;
9
10/// Error for [`BlockImport`]
11#[derive(Debug, thiserror::Error)]
12pub enum BlockImportError {
13    /// Already importing
14    #[error("Already importing")]
15    AlreadyImporting,
16    /// Already imported
17    #[error("Already imported")]
18    AlreadyImported,
19    /// Unknown parent block
20    #[error("Unknown parent block: {block_root}")]
21    UnknownParentBlock {
22        /// Block root that was not found
23        block_root: BlockRoot,
24    },
25    /// Invalid parent MMR; this is an implementation bug and must never happen
26    #[error("Invalid parent MMR; this is an implementation bug and must never happen")]
27    ParentBlockMmrInvalid,
28    /// Can't extend MMR, too many blocks; this is an implementation bug and must never happen
29    #[error(
30        "Can't extend MMR, too many blocks; this is an implementation bug and must never happen"
31    )]
32    CantExtendMmr,
33    /// Parent block import failed
34    #[error("Parent block import failed")]
35    ParentBlockImportFailed,
36    /// Invalid state root
37    #[error("Invalid state root: expected {expected}, actual {actual}")]
38    InvalidStateRoot {
39        expected: Blake3Hash,
40        actual: Blake3Hash,
41    },
42    /// Block persisting error
43    #[error("Block persisting error: {error}")]
44    PersistBlockError {
45        /// Block persisting error
46        #[from]
47        error: PersistBlockError,
48    },
49    /// Custom import error
50    #[error("Custom import error: {error}")]
51    Custom {
52        /// Custom block import error
53        #[from]
54        error: anyhow::Error,
55    },
56}
57
58/// Notification about a successfully imported block
59#[derive(Debug, Clone)]
60pub struct BlockImportedNotification<Block> {
61    /// Imported block
62    pub block: Block,
63    /// Whether the block became the new best block on import.
64    ///
65    /// Notifications are sent in the same order as blocks are persisted, so the most recent
66    /// notification with this flag set corresponds to the current best block.
67    pub is_new_best: bool,
68}
69
70/// Block import interface
71pub trait BlockImport<Block>: Send + Sync {
72    /// Import provided block.
73    ///
74    /// Parent block must either be imported already or at least queued for import. Block import is
75    /// immediately added to the queue, but actual import may not happen unless the returned future
76    /// is polled.
77    fn import(
78        &self,
79        // TODO: Some way to attack state storage items
80        block: Block,
81        origin: BlockOrigin,
82    ) -> Result<impl Future<Output = Result<(), BlockImportError>> + Send, BlockImportError>;
83}