mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add block_body_indices for BlockchainProvider2 (#10106)
This commit is contained in:
@ -207,7 +207,7 @@ impl CanonicalInMemoryState {
|
||||
Self { inner: Arc::new(inner) }
|
||||
}
|
||||
|
||||
/// Returns the block hash corresponding to the given number
|
||||
/// Returns the block hash corresponding to the given number.
|
||||
pub fn hash_by_number(&self, number: u64) -> Option<B256> {
|
||||
self.inner.in_memory_state.hash_by_number(number)
|
||||
}
|
||||
|
||||
@ -100,8 +100,11 @@ pub enum ProviderError {
|
||||
#[error("unknown block {0}")]
|
||||
UnknownBlockHash(B256),
|
||||
/// Thrown when we were unable to find a state for a block hash.
|
||||
#[error("no state found for block {0}")]
|
||||
#[error("no state found for block hash {0}")]
|
||||
StateForHashNotFound(B256),
|
||||
/// Thrown when we were unable to find a state for a block number.
|
||||
#[error("no state found for block number {0}")]
|
||||
StateForNumberNotFound(u64),
|
||||
/// Unable to find the block number for a given transaction index.
|
||||
#[error("unable to find the block number for a given transaction index")]
|
||||
BlockNumberForTransactionIndexNotFound,
|
||||
|
||||
@ -428,7 +428,34 @@ where
|
||||
&self,
|
||||
number: BlockNumber,
|
||||
) -> ProviderResult<Option<StoredBlockBodyIndices>> {
|
||||
self.database.block_body_indices(number)
|
||||
if let Some(indices) = self.database.block_body_indices(number)? {
|
||||
Ok(Some(indices))
|
||||
} else if let Some(state) = self.canonical_in_memory_state.state_by_number(number) {
|
||||
// we have to construct the stored indices for the in memory blocks
|
||||
//
|
||||
// To calculate this we will fetch the anchor block and walk forward from all parents
|
||||
let mut parent_chain = state.parent_state_chain();
|
||||
parent_chain.reverse();
|
||||
let anchor_num = state.anchor().number;
|
||||
let mut stored_indices = self
|
||||
.database
|
||||
.block_body_indices(anchor_num)?
|
||||
.ok_or_else(|| ProviderError::BlockBodyIndicesNotFound(anchor_num))?;
|
||||
stored_indices.first_tx_num = stored_indices.next_tx_num();
|
||||
|
||||
for state in parent_chain {
|
||||
let txs = state.block().block.body.len() as u64;
|
||||
if state.block().block().number == number {
|
||||
stored_indices.tx_count = txs;
|
||||
} else {
|
||||
stored_indices.first_tx_num += txs;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Some(stored_indices))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the block with senders with matching number or hash from database.
|
||||
|
||||
Reference in New Issue
Block a user