Skip to main content

ab_client_block_import/
lib.rs

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