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