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 importing
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    // TODO: Use or remove
26    // /// Parent block details are missing; this is an implementation bug and must never happen
27    // #[error(
28    //     "Parent block details are missing; this is an implementation bug and must never happen"
29    // )]
30    // ParentBlockDetailsMissing,
31    /// Invalid parent MMR; this is an implementation bug and must never happen
32    #[error("Invalid parent MMR; this is an implementation bug and must never happen")]
33    ParentBlockMmrInvalid,
34    /// Can't extend MMR, too many blocks; this is an implementation bug and must never happen
35    #[error(
36        "Can't extend MMR, too many blocks; this is an implementation bug and must never happen"
37    )]
38    CantExtendMmr,
39    /// Parent block import failed
40    #[error("Parent block import failed")]
41    ParentBlockImportFailed,
42    /// Invalid state root
43    #[error("Invalid state root: expected {expected}, actual {actual}")]
44    InvalidStateRoot {
45        expected: Blake3Hash,
46        actual: Blake3Hash,
47    },
48    /// Block persisting error
49    #[error("Block persisting error: {error}")]
50    PersistBlockError {
51        /// Block persisting error
52        #[from]
53        error: PersistBlockError,
54    },
55    /// Custom import error
56    #[error("Custom import error: {error}")]
57    Custom {
58        // Custom block import error
59        #[from]
60        error: anyhow::Error,
61    },
62}
63
64/// Block import interface
65pub trait BlockImport<Block>: Send + Sync {
66    /// Import provided block.
67    ///
68    /// Parent block must either be imported already or at least queued for import. Block import is
69    /// immediately added to the queue, but actual import may not happen unless the returned future
70    /// is polled.
71    fn import(
72        &self,
73        // TODO: Some way to attack state storage items
74        block: Block,
75        origin: BlockOrigin,
76    ) -> Result<impl Future<Output = Result<(), BlockImportError>> + Send, BlockImportError>;
77}