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;
12
13/// Error for [`BlockImport`]
14#[derive(Debug, thiserror::Error)]
15pub enum BlockImportError {
16    /// Already importing
17    #[error("Already importing")]
18    AlreadyImporting,
19    /// Already importing
20    #[error("Already imported")]
21    AlreadyImported,
22    /// Unknown parent block
23    #[error("Unknown parent block: {block_root}")]
24    UnknownParentBlock {
25        // Block root that was not found
26        block_root: BlockRoot,
27    },
28    /// Parent block MMR missing; this is an implementation bug and must never happen
29    #[error("Parent block MMR missing; this is an implementation bug and must never happen")]
30    ParentBlockMmrMissing,
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    /// 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/// Block import interface
59pub trait BlockImport<Block>: Send + Sync {
60    /// Import provided block.
61    ///
62    /// Parent block must either be imported already or at least queued for import. Block import is
63    /// immediately added to the queue, but actual import may not happen unless the returned future
64    /// is polled.
65    fn import(
66        &self,
67        // TODO: Some way to attack state storage items
68        block: Block,
69        origin: BlockOrigin,
70    ) -> Result<impl Future<Output = Result<(), BlockImportError>> + Send, BlockImportError>;
71}