Skip to main content

ab_client_block_builder/
lib.rs

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