Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tree): introduce executed_block_by_hash #10553

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use reth_primitives::{
};
use reth_provider::{
BlockReader, ExecutionOutcome, ProviderError, StateProviderBox, StateProviderFactory,
StateRootProvider,
StateReader, StateRootProvider, TransactionVariant,
};
use reth_revm::database::StateProviderDatabase;
use reth_rpc_types::{
Expand Down Expand Up @@ -109,6 +109,11 @@ impl TreeState {
self.blocks_by_hash.len()
}

/// Returns the [`ExecutedBlock`] by hash.
fn executed_block_by_hash(&self, hash: B256) -> Option<&ExecutedBlock> {
self.blocks_by_hash.get(&hash)
}

/// Returns the block by hash.
fn block_by_hash(&self, hash: B256) -> Option<Arc<SealedBlock>> {
self.blocks_by_hash.get(&hash).map(|b| b.block.clone())
Expand Down Expand Up @@ -547,7 +552,7 @@ impl<P: Debug, E: Debug, T: EngineTypes + Debug> std::fmt::Debug for EngineApiTr

impl<P, E, T> EngineApiTreeHandler<P, E, T>
where
P: BlockReader + StateProviderFactory + Clone + 'static,
P: BlockReader + StateProviderFactory + StateReader + Clone + 'static,
E: BlockExecutorProvider,
T: EngineTypes,
{
Expand Down Expand Up @@ -1337,6 +1342,45 @@ where
.remove_persisted_blocks(self.persistence_state.last_persisted_block_number);
}

/// Return an [`ExecutedBlock`] from database or in-memory state by hash.
///
/// NOTE: This cannot fetch [`ExecutedBlock`]s for _finalized_ blocks, instead it can only
/// fetch [`ExecutedBlock`]s for _canonical_ blocks, or blocks from sidechains that the node
/// has in memory.
///
/// For finalized blocks, this will return `None`.
#[allow(unused)]
fn executed_block_by_hash(&self, hash: B256) -> ProviderResult<Option<ExecutedBlock>> {
// check memory first
let block = self.state.tree_state.executed_block_by_hash(hash).cloned();

if block.is_some() {
return Ok(block)
}

let Some((_, updates)) = self.state.tree_state.persisted_trie_updates.get(&hash) else {
return Ok(None)
};

let SealedBlockWithSenders { block, senders } = self
.provider
.sealed_block_with_senders(hash.into(), TransactionVariant::WithHash)?
.ok_or_else(|| ProviderError::HeaderNotFound(hash.into()))?;
let execution_output = self
.provider
.get_state(block.number)?
.ok_or_else(|| ProviderError::StateForNumberNotFound(block.number))?;
let hashed_state = execution_output.hash_state_slow();

Ok(Some(ExecutedBlock {
block: Arc::new(block),
senders: Arc::new(senders),
trie: updates.clone(),
execution_output: Arc::new(execution_output),
hashed_state: Arc::new(hashed_state),
}))
}

/// Return sealed block from database or in-memory state by hash.
fn sealed_header_by_hash(&self, hash: B256) -> ProviderResult<Option<SealedHeader>> {
// check memory first
Expand Down Expand Up @@ -1705,7 +1749,7 @@ where
///
/// This is invoked on a valid forkchoice update, or if we can make the target block canonical.
fn on_canonical_chain_update(&mut self, chain_update: NewCanonicalChain) {
trace!(target: "engine", new_blocks = %chain_update.new_block_count(), reorged_blocks = %chain_update.reorged_block_count() ,"applying new chain update");
trace!(target: "engine", new_blocks = %chain_update.new_block_count(), reorged_blocks = %chain_update.reorged_block_count(), "applying new chain update");
let start = Instant::now();

// update the tracked canonical head
Expand Down
25 changes: 21 additions & 4 deletions crates/storage/provider/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use crate::{
traits::{BlockSource, ReceiptProvider},
AccountReader, BlockHashReader, BlockIdReader, BlockNumReader, BlockReader, BlockReaderIdExt,
ChainSpecProvider, ChangeSetReader, EvmEnvProvider, HeaderProvider, ReceiptProviderIdExt,
RequestsProvider, StateProvider, StateProviderBox, StateProviderFactory, StateRootProvider,
TransactionVariant, TransactionsProvider, WithdrawalsProvider,
AccountReader, BlockExecutionReader, BlockHashReader, BlockIdReader, BlockNumReader,
BlockReader, BlockReaderIdExt, ChainSpecProvider, ChangeSetReader, EvmEnvProvider,
HeaderProvider, ReceiptProviderIdExt, RequestsProvider, StateProvider, StateProviderBox,
StateProviderFactory, StateReader, StateRootProvider, TransactionVariant, TransactionsProvider,
WithdrawalsProvider,
};
use parking_lot::Mutex;
use reth_chainspec::{ChainInfo, ChainSpec};
use reth_db_api::models::{AccountBeforeTx, StoredBlockBodyIndices};
use reth_evm::ConfigureEvmEnv;
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_primitives::{
keccak256, Account, Address, Block, BlockHash, BlockHashOrNumber, BlockId, BlockNumber,
BlockNumberOrTag, BlockWithSenders, Bytecode, Bytes, Header, Receipt, SealedBlock,
Expand Down Expand Up @@ -787,3 +789,18 @@ impl ChangeSetReader for MockEthProvider {
Ok(Vec::default())
}
}

impl BlockExecutionReader for MockEthProvider {
fn get_block_and_execution_range(
&self,
_range: RangeInclusive<BlockNumber>,
) -> ProviderResult<Chain> {
Ok(Chain::default())
}
}

impl StateReader for MockEthProvider {
fn get_state(&self, _block: BlockNumber) -> ProviderResult<Option<ExecutionOutcome>> {
Ok(None)
}
}
Loading