reth_provider/traits/
block.rs

1use alloy_primitives::BlockNumber;
2use reth_db_api::models::StoredBlockBodyIndices;
3use reth_execution_types::{Chain, ExecutionOutcome};
4use reth_node_types::NodePrimitives;
5use reth_primitives::RecoveredBlock;
6use reth_primitives_traits::Block;
7use reth_storage_api::{NodePrimitivesProvider, StorageLocation};
8use reth_storage_errors::provider::ProviderResult;
9use reth_trie::{updates::TrieUpdates, HashedPostStateSorted};
10
11/// `BlockExecution` Writer
12pub trait BlockExecutionWriter:
13    NodePrimitivesProvider<Primitives: NodePrimitives<Block = Self::Block>> + BlockWriter + Send + Sync
14{
15    /// Take all of the blocks above the provided number and their execution result
16    ///
17    /// The passed block number will stay in the database.
18    ///
19    /// Accepts [`StorageLocation`] specifying from where should transactions and receipts be
20    /// removed.
21    fn take_block_and_execution_above(
22        &self,
23        block: BlockNumber,
24        remove_from: StorageLocation,
25    ) -> ProviderResult<Chain<Self::Primitives>>;
26
27    /// Remove all of the blocks above the provided number and their execution result
28    ///
29    /// The passed block number will stay in the database.
30    ///
31    /// Accepts [`StorageLocation`] specifying from where should transactions and receipts be
32    /// removed.
33    fn remove_block_and_execution_above(
34        &self,
35        block: BlockNumber,
36        remove_from: StorageLocation,
37    ) -> ProviderResult<()>;
38}
39
40impl<T: BlockExecutionWriter> BlockExecutionWriter for &T {
41    fn take_block_and_execution_above(
42        &self,
43        block: BlockNumber,
44        remove_from: StorageLocation,
45    ) -> ProviderResult<Chain<Self::Primitives>> {
46        (*self).take_block_and_execution_above(block, remove_from)
47    }
48
49    fn remove_block_and_execution_above(
50        &self,
51        block: BlockNumber,
52        remove_from: StorageLocation,
53    ) -> ProviderResult<()> {
54        (*self).remove_block_and_execution_above(block, remove_from)
55    }
56}
57
58/// This just receives state, or [`ExecutionOutcome`], from the provider
59#[auto_impl::auto_impl(&, Arc, Box)]
60pub trait StateReader: Send + Sync {
61    /// Receipt type in [`ExecutionOutcome`].
62    type Receipt: Send + Sync;
63
64    /// Get the [`ExecutionOutcome`] for the given block
65    fn get_state(
66        &self,
67        block: BlockNumber,
68    ) -> ProviderResult<Option<ExecutionOutcome<Self::Receipt>>>;
69}
70
71/// Block Writer
72#[auto_impl::auto_impl(&, Arc, Box)]
73pub trait BlockWriter: Send + Sync {
74    /// The body this writer can write.
75    type Block: Block;
76    /// The receipt type for [`ExecutionOutcome`].
77    type Receipt: Send + Sync;
78
79    /// Insert full block and make it canonical. Parent tx num and transition id is taken from
80    /// parent block in database.
81    ///
82    /// Return [StoredBlockBodyIndices] that contains indices of the first and last transactions and
83    /// transition in the block.
84    ///
85    /// Accepts [`StorageLocation`] value which specifies where transactions and headers should be
86    /// written.
87    fn insert_block(
88        &self,
89        block: RecoveredBlock<Self::Block>,
90        write_to: StorageLocation,
91    ) -> ProviderResult<StoredBlockBodyIndices>;
92
93    /// Appends a batch of block bodies extending the canonical chain. This is invoked during
94    /// `Bodies` stage and does not write to `TransactionHashNumbers` and `TransactionSenders`
95    /// tables which are populated on later stages.
96    ///
97    /// Bodies are passed as [`Option`]s, if body is `None` the corresponding block is empty.
98    fn append_block_bodies(
99        &self,
100        bodies: Vec<(BlockNumber, Option<<Self::Block as Block>::Body>)>,
101        write_to: StorageLocation,
102    ) -> ProviderResult<()>;
103
104    /// Removes all blocks above the given block number from the database.
105    ///
106    /// Note: This does not remove state or execution data.
107    fn remove_blocks_above(
108        &self,
109        block: BlockNumber,
110        remove_from: StorageLocation,
111    ) -> ProviderResult<()>;
112
113    /// Removes all block bodies above the given block number from the database.
114    fn remove_bodies_above(
115        &self,
116        block: BlockNumber,
117        remove_from: StorageLocation,
118    ) -> ProviderResult<()>;
119
120    /// Appends a batch of sealed blocks to the blockchain, including sender information, and
121    /// updates the post-state.
122    ///
123    /// Inserts the blocks into the database and updates the state with
124    /// provided `BundleState`.
125    ///
126    /// # Parameters
127    ///
128    /// - `blocks`: Vector of `RecoveredBlock` instances to append.
129    /// - `state`: Post-state information to update after appending.
130    ///
131    /// # Returns
132    ///
133    /// Returns `Ok(())` on success, or an error if any operation fails.
134    fn append_blocks_with_state(
135        &self,
136        blocks: Vec<RecoveredBlock<Self::Block>>,
137        execution_outcome: &ExecutionOutcome<Self::Receipt>,
138        hashed_state: HashedPostStateSorted,
139        trie_updates: TrieUpdates,
140    ) -> ProviderResult<()>;
141}