Skip to main content

ab_client_block_import/
lib.rs

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