ab_client_block_builder/
lib.rs

1//! Block building implementation
2
3#![expect(incomplete_features, reason = "generic_const_exprs")]
4// TODO: This feature is not actually used in this crate, but is added as a workaround for
5//  https://github.com/rust-lang/rust/issues/141492
6#![feature(generic_const_exprs)]
7#![feature(async_fn_traits, unboxed_closures)]
8
9pub mod beacon_chain;
10
11use ab_client_api::BlockDetails;
12use ab_core_primitives::block::BlockRoot;
13use ab_core_primitives::block::header::owned::GenericOwnedBlockHeader;
14use ab_core_primitives::block::header::{BlockHeaderConsensusInfo, OwnedBlockHeaderSeal};
15use ab_core_primitives::block::owned::GenericOwnedBlock;
16use ab_core_primitives::hashes::Blake3Hash;
17use ab_core_primitives::pot::PotCheckpoints;
18
19/// Error for [`BlockBuilder`]
20#[derive(Debug, thiserror::Error)]
21pub enum BlockBuilderError {
22    /// Invalid parent MMR
23    #[error("Invalid parent MMR")]
24    InvalidParentMmr,
25    /// Custom builder error
26    #[error("Custom builder error: {error}")]
27    Custom {
28        // Custom block builder error
29        #[from]
30        error: anyhow::Error,
31    },
32    /// Failed to seal the block
33    #[error("Failed to seal the block")]
34    FailedToSeal,
35    /// Received invalid seal
36    #[error(
37        "Received invalid seal for pre-seal hash {pre_seal_hash} and public key hash \
38        {public_key_hash}"
39    )]
40    InvalidSeal {
41        /// Public key hash
42        public_key_hash: Blake3Hash,
43        /// Pre-seal hash
44        pre_seal_hash: Blake3Hash,
45    },
46    /// Can't extend MMR, too many blocks; this is an implementation bug and must never happen
47    #[error(
48        "Can't extend MMR, too many blocks; this is an implementation bug and must never happen"
49    )]
50    CantExtendMmr,
51}
52
53/// Result of block building
54#[derive(Debug, Clone)]
55pub struct BlockBuilderResult<Block> {
56    /// Block itself
57    pub block: Block,
58    /// Additional details about a block
59    pub block_details: BlockDetails,
60}
61
62/// Block builder interface
63pub trait BlockBuilder<Block>: Send
64where
65    Block: GenericOwnedBlock,
66{
67    /// Build a new block using provided parameters
68    fn build<SealBlock>(
69        &mut self,
70        parent_block_root: &BlockRoot,
71        parent_header: &<Block::Header as GenericOwnedBlockHeader>::Header<'_>,
72        parent_block_details: &BlockDetails,
73        consensus_info: &BlockHeaderConsensusInfo,
74        checkpoints: &[PotCheckpoints],
75        seal_block: SealBlock,
76    ) -> impl Future<Output = Result<BlockBuilderResult<Block>, BlockBuilderError>> + Send
77    where
78        SealBlock: AsyncFnOnce<(Blake3Hash,), Output = Option<OwnedBlockHeaderSeal>, CallOnceFuture: Send>
79            + Send;
80}