reth_storage_api/block_writer.rs
1use crate::NodePrimitivesProvider;
2use alloc::vec::Vec;
3use alloy_primitives::BlockNumber;
4use reth_db_models::StoredBlockBodyIndices;
5use reth_execution_types::ExecutionOutcome;
6use reth_primitives_traits::{Block, NodePrimitives, RecoveredBlock};
7use reth_storage_errors::provider::{ProviderError, ProviderResult};
8use reth_trie_common::HashedPostStateSorted;
9
10/// `BlockExecution` Writer
11pub trait BlockExecutionWriter:
12 NodePrimitivesProvider<Primitives: NodePrimitives<Block = Self::Block>> + BlockWriter
13{
14 /// Remove 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 /// Returns the persistence frontiers after removal.
19 fn remove_block_and_execution_above(
20 &self,
21 block: BlockNumber,
22 ) -> ProviderResult<PersistenceFrontiers>;
23}
24
25impl<T: BlockExecutionWriter> BlockExecutionWriter for &T {
26 fn remove_block_and_execution_above(
27 &self,
28 block: BlockNumber,
29 ) -> ProviderResult<PersistenceFrontiers> {
30 (*self).remove_block_and_execution_above(block)
31 }
32}
33
34/// The database and state/trie persistence frontiers.
35///
36/// The state/trie frontier is never ahead of the database frontier.
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct PersistenceFrontiers {
39 /// The highest block whose non-state/trie outputs are persisted.
40 pub db_tip: BlockNumber,
41 /// The highest block whose state/trie outputs are persisted.
42 pub partial_state_trie: BlockNumber,
43}
44
45/// Block Writer
46#[auto_impl::auto_impl(&, Arc, Box)]
47pub trait BlockWriter {
48 /// The body this writer can write.
49 type Block: Block;
50 /// The receipt type for [`ExecutionOutcome`].
51 type Receipt: Send + Sync;
52
53 /// Insert full block and make it canonical. Parent tx num and transition id is taken from
54 /// parent block in database.
55 ///
56 /// Return [`StoredBlockBodyIndices`] that contains indices of the first and last transactions
57 /// and transition in the block.
58 fn insert_block(
59 &self,
60 block: &RecoveredBlock<Self::Block>,
61 ) -> ProviderResult<StoredBlockBodyIndices>;
62
63 /// Appends a batch of block bodies extending the canonical chain. This is invoked during
64 /// `Bodies` stage and does not write to `TransactionHashNumbers` and `TransactionSenders`
65 /// tables which are populated on later stages.
66 ///
67 /// Bodies are passed as [`Option`]s, if body is `None` the corresponding block is empty.
68 fn append_block_bodies(
69 &self,
70 bodies: Vec<(BlockNumber, Option<&<Self::Block as Block>::Body>)>,
71 ) -> ProviderResult<()>;
72
73 /// Removes all blocks above the given block number from the database.
74 ///
75 /// Note: This does not remove state or execution data.
76 fn remove_blocks_above(&self, block: BlockNumber) -> ProviderResult<()>;
77
78 /// Removes all block bodies above the given block number from the database.
79 fn remove_bodies_above(&self, block: BlockNumber) -> ProviderResult<()>;
80
81 /// Appends a batch of sealed blocks to the blockchain, including sender information, and
82 /// updates the post-state.
83 ///
84 /// Inserts the blocks into the database and updates the state with
85 /// provided execution state. The database's trie state is _not_ updated.
86 ///
87 /// # Parameters
88 ///
89 /// - `blocks`: Vector of `RecoveredBlock` instances to append.
90 /// - `state`: Post-state information to update after appending.
91 ///
92 /// # Returns
93 ///
94 /// Returns `Ok(())` on success, or an error if any operation fails.
95 fn append_blocks_with_state(
96 &self,
97 blocks: Vec<RecoveredBlock<Self::Block>>,
98 execution_outcome: &ExecutionOutcome<Self::Receipt>,
99 hashed_state: HashedPostStateSorted,
100 ) -> ProviderResult<()>;
101
102 /// Clears the active transaction lookup index. `RocksDB` changes take effect immediately.
103 ///
104 /// Errors by default, so writers without a lookup index don't have to implement it.
105 fn clear_transaction_lookup(&self) -> ProviderResult<()> {
106 Err(ProviderError::UnsupportedProvider)
107 }
108}