reth_storage_api/
block_writer.rs

1use crate::{NodePrimitivesProvider, StorageLocation};
2use alloc::vec::Vec;
3use alloy_primitives::BlockNumber;
4use reth_db_models::StoredBlockBodyIndices;
5use reth_execution_types::{Chain, ExecutionOutcome};
6use reth_primitives_traits::{Block, NodePrimitives, RecoveredBlock};
7use reth_storage_errors::provider::ProviderResult;
8use reth_trie_common::{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/// Block Writer
58#[auto_impl::auto_impl(&, Arc, Box)]
59pub trait BlockWriter: Send + Sync {
60    /// The body this writer can write.
61    type Block: Block;
62    /// The receipt type for [`ExecutionOutcome`].
63    type Receipt: Send + Sync;
64
65    /// Insert full block and make it canonical. Parent tx num and transition id is taken from
66    /// parent block in database.
67    ///
68    /// Return [StoredBlockBodyIndices] that contains indices of the first and last transactions and
69    /// transition in the block.
70    ///
71    /// Accepts [`StorageLocation`] value which specifies where transactions and headers should be
72    /// written.
73    fn insert_block(
74        &self,
75        block: RecoveredBlock<Self::Block>,
76        write_to: StorageLocation,
77    ) -> ProviderResult<StoredBlockBodyIndices>;
78
79    /// Appends a batch of block bodies extending the canonical chain. This is invoked during
80    /// `Bodies` stage and does not write to `TransactionHashNumbers` and `TransactionSenders`
81    /// tables which are populated on later stages.
82    ///
83    /// Bodies are passed as [`Option`]s, if body is `None` the corresponding block is empty.
84    fn append_block_bodies(
85        &self,
86        bodies: Vec<(BlockNumber, Option<<Self::Block as Block>::Body>)>,
87        write_to: StorageLocation,
88    ) -> ProviderResult<()>;
89
90    /// Removes all blocks above the given block number from the database.
91    ///
92    /// Note: This does not remove state or execution data.
93    fn remove_blocks_above(
94        &self,
95        block: BlockNumber,
96        remove_from: StorageLocation,
97    ) -> ProviderResult<()>;
98
99    /// Removes all block bodies above the given block number from the database.
100    fn remove_bodies_above(
101        &self,
102        block: BlockNumber,
103        remove_from: StorageLocation,
104    ) -> ProviderResult<()>;
105
106    /// Appends a batch of sealed blocks to the blockchain, including sender information, and
107    /// updates the post-state.
108    ///
109    /// Inserts the blocks into the database and updates the state with
110    /// provided `BundleState`.
111    ///
112    /// # Parameters
113    ///
114    /// - `blocks`: Vector of `RecoveredBlock` instances to append.
115    /// - `state`: Post-state information to update after appending.
116    ///
117    /// # Returns
118    ///
119    /// Returns `Ok(())` on success, or an error if any operation fails.
120    fn append_blocks_with_state(
121        &self,
122        blocks: Vec<RecoveredBlock<Self::Block>>,
123        execution_outcome: &ExecutionOutcome<Self::Receipt>,
124        hashed_state: HashedPostStateSorted,
125        trie_updates: TrieUpdates,
126    ) -> ProviderResult<()>;
127}