Skip to content

Commit

Permalink
add BlockchainProvider3
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo committed Oct 14, 2024
1 parent 3700a15 commit 2539f1d
Show file tree
Hide file tree
Showing 4 changed files with 1,815 additions and 6 deletions.
57 changes: 52 additions & 5 deletions crates/chain-state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
CanonStateNotification, CanonStateNotificationSender, CanonStateNotifications,
ChainInfoTracker, MemoryOverlayStateProvider,
};
use alloy_eips::BlockNumHash;
use alloy_eips::{BlockHashOrNumber, BlockNumHash};
use alloy_primitives::{map::HashMap, Address, TxHash, B256};
use parking_lot::RwLock;
use reth_chainspec::ChainInfo;
Expand Down Expand Up @@ -706,10 +706,8 @@ impl BlockState {
/// Returns a vector of `BlockStates` representing the entire in memory chain.
/// The block state order in the output vector is newest to oldest (highest to lowest),
/// including self as the first element.
pub fn chain(&self) -> Vec<&Self> {
let mut chain = vec![self];
self.append_parent_chain(&mut chain);
chain
pub fn chain(&self) -> impl Iterator<Item = &Self> {
std::iter::successors(Some(self), |state| state.parent.as_deref())
}

/// Appends the parent chain of this [`BlockState`] to the given vector.
Expand All @@ -723,6 +721,55 @@ impl BlockState {
pub fn iter(self: Arc<Self>) -> impl Iterator<Item = Arc<Self>> {
std::iter::successors(Some(self), |state| state.parent.clone())
}

/// Tries to find a block by [`BlockHashOrNumber`] in the chain ending at this block.
pub fn block_on_chain(&self, hash_or_num: BlockHashOrNumber) -> Option<&Self> {
self.chain().find(|block| match hash_or_num {
BlockHashOrNumber::Hash(hash) => block.hash() == hash,
BlockHashOrNumber::Number(number) => block.number() == number,
})
}

/// Tries to find a transaction by [`TxHash`] in the chain ending at this block.
pub fn transaction_on_chain(&self, hash: TxHash) -> Option<TransactionSigned> {
for block_state in self.chain() {
if let Some(tx) =
block_state.block_ref().block().body.transactions().find(|tx| tx.hash() == hash)
{
return Some(tx.clone())
}
}
None
}

/// Tries to find a transaction with meta by [`TxHash`] in the chain ending at this block.
pub fn transaction_meta_on_chain(
&self,
tx_hash: TxHash,
) -> Option<(TransactionSigned, TransactionMeta)> {
for block_state in self.chain() {
if let Some((index, tx)) = block_state
.block_ref()
.block()
.body
.transactions()
.enumerate()
.find(|(_, tx)| tx.hash() == tx_hash)
{
let meta = TransactionMeta {
tx_hash,
index: index as u64,
block_hash: block_state.hash(),
block_number: block_state.block_ref().block.number,
base_fee: block_state.block_ref().block.header.base_fee_per_gas,
timestamp: block_state.block_ref().block.timestamp,
excess_blob_gas: block_state.block_ref().block.excess_blob_gas,
};
return Some((tx.clone(), meta))
}
}
None
}
}

/// Represents an executed block stored in-memory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ impl<N: ProviderNodeTypes> BlockReader for BlockchainProvider2<N> {
stored_indices.tx_count = 0;

// Iterate from the lowest block in memory until our target block
for state in block_state.chain().into_iter().rev() {
for state in block_state.chain().collect::<Vec<_>>().into_iter().rev() {
let block_tx_count = state.block_ref().block.body.transactions.len() as u64;
if state.block_ref().block().number == number {
stored_indices.tx_count = block_tx_count;
Expand Down
Loading

0 comments on commit 2539f1d

Please sign in to comment.