ab_client_block_import/
lib.rs

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