Skip to main content

reth_storage_api/
block_indices.rs

1use alloc::vec::Vec;
2use alloy_primitives::{BlockNumber, TxNumber};
3use core::ops::RangeInclusive;
4use reth_db_models::StoredBlockBodyIndices;
5use reth_storage_errors::provider::{ProviderError, ProviderResult};
6
7///  Client trait for fetching block body indices related data.
8#[auto_impl::auto_impl(&, Arc)]
9pub trait BlockBodyIndicesProvider: Send {
10    /// Returns the block body indices with matching number from database.
11    ///
12    /// Returns `None` if block is not found.
13    fn block_body_indices(&self, num: u64) -> ProviderResult<Option<StoredBlockBodyIndices>>;
14
15    /// Returns the block body indices within the requested range matching number from storage.
16    fn block_body_indices_range(
17        &self,
18        range: RangeInclusive<BlockNumber>,
19    ) -> ProviderResult<Vec<StoredBlockBodyIndices>>;
20
21    /// Returns the first transaction number after `block`, including when its body indices were
22    /// never written, e.g. at a snap sync pivot.
23    fn next_tx_num_after_block(&self, block: BlockNumber) -> ProviderResult<TxNumber> {
24        if let Some(indices) = self.block_body_indices(block)? {
25            return Ok(indices.next_tx_num())
26        }
27        if let Some(next) = block.checked_add(1) &&
28            let Some(indices) = self.block_body_indices(next)?
29        {
30            return Ok(indices.first_tx_num())
31        }
32        Err(ProviderError::BlockBodyIndicesNotFound(block))
33    }
34}