Skip to content

Commit

Permalink
refactor: rename BundleStateWithReceipts to BlockExecutionOutcome (
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Jun 11, 2024
1 parent 95719da commit a5d825e
Show file tree
Hide file tree
Showing 43 changed files with 443 additions and 409 deletions.
17 changes: 8 additions & 9 deletions bin/reth/src/commands/debug_cmd/build_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ use reth_primitives::{
SealedBlock, SealedBlockWithSenders, Transaction, TransactionSigned, TxEip4844, B256, U256,
};
use reth_provider::{
providers::BlockchainProvider, BlockHashReader, BlockReader, BlockWriter,
BundleStateWithReceipts, ChainSpecProvider, ProviderFactory, StageCheckpointReader,
StateProviderFactory,
providers::BlockchainProvider, BlockHashReader, BlockReader, BlockWriter, ChainSpecProvider,
ExecutionOutcome, ProviderFactory, StageCheckpointReader, StateProviderFactory,
};
use reth_revm::database::StateProviderDatabase;
use reth_rpc_types::engine::{BlobsBundleV1, PayloadAttributes};
Expand Down Expand Up @@ -273,17 +272,17 @@ impl Command {

let BlockExecutionOutput { state, receipts, requests, .. } =
executor.execute((&block_with_senders.clone().unseal(), U256::MAX).into())?;
let state = BundleStateWithReceipts::new(
let execution_outcome = ExecutionOutcome::new(
state,
receipts.into(),
block.number,
vec![requests.into()],
);

debug!(target: "reth::cli", ?state, "Executed block");
debug!(target: "reth::cli", ?execution_outcome, "Executed block");

let hashed_state = state.hash_state_slow();
let (state_root, trie_updates) = state
let hashed_post_state = execution_outcome.hash_state_slow();
let (state_root, trie_updates) = execution_outcome
.hash_state_slow()
.state_root_with_updates(provider_factory.provider()?.tx_ref())?;

Expand All @@ -299,8 +298,8 @@ impl Command {
let provider_rw = provider_factory.provider_rw()?;
provider_rw.append_blocks_with_state(
Vec::from([block_with_senders]),
state,
hashed_state,
execution_outcome,
hashed_post_state,
trie_updates,
None,
)?;
Expand Down
14 changes: 5 additions & 9 deletions bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use reth_network::NetworkHandle;
use reth_network_api::NetworkInfo;
use reth_primitives::BlockHashOrNumber;
use reth_provider::{
AccountExtReader, BundleStateWithReceipts, ChainSpecProvider, HashingWriter, HeaderProvider,
AccountExtReader, ChainSpecProvider, ExecutionOutcome, HashingWriter, HeaderProvider,
LatestStateProviderRef, OriginalValuesKnown, ProviderFactory, StageCheckpointReader,
StateWriter, StaticFileProviderFactory, StorageReader,
};
Expand Down Expand Up @@ -147,16 +147,12 @@ impl Command {
)
.into(),
)?;
let block_state = BundleStateWithReceipts::new(
state,
receipts.into(),
block.number,
vec![requests.into()],
);
let execution_outcome =
ExecutionOutcome::new(state, receipts.into(), block.number, vec![requests.into()]);

// Unpacked `BundleState::state_root_slow` function
let (in_memory_state_root, in_memory_updates) =
block_state.hash_state_slow().state_root_with_updates(provider.tx_ref())?;
execution_outcome.hash_state_slow().state_root_with_updates(provider.tx_ref())?;

if in_memory_state_root == block.state_root {
info!(target: "reth::cli", state_root = ?in_memory_state_root, "Computed in-memory state root matches");
Expand All @@ -173,7 +169,7 @@ impl Command {
.map_err(|_| BlockValidationError::SenderRecoveryError)?,
None,
)?;
block_state.write_to_storage(provider_rw.tx_ref(), None, OriginalValuesKnown::No)?;
execution_outcome.write_to_storage(provider_rw.tx_ref(), None, OriginalValuesKnown::No)?;
let storage_lists = provider_rw.changed_storages_with_range(block.number..=block.number)?;
let storages = provider_rw.plain_state_storages(storage_lists)?;
provider_rw.insert_storage_for_hashing(storages)?;
Expand Down
16 changes: 6 additions & 10 deletions bin/reth/src/commands/import_receipts_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use reth_node_core::version::SHORT_VERSION;
use reth_optimism_primitives::bedrock_import::is_dup_tx;
use reth_primitives::{Receipts, StaticFileSegment};
use reth_provider::{
BundleStateWithReceipts, OriginalValuesKnown, ProviderFactory, StageCheckpointReader,
StateWriter, StaticFileProviderFactory, StaticFileWriter, StatsReader,
ExecutionOutcome, OriginalValuesKnown, ProviderFactory, StageCheckpointReader, StateWriter,
StaticFileProviderFactory, StaticFileWriter, StatsReader,
};
use reth_stages::StageId;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -131,20 +131,16 @@ where
);

// We're reusing receipt writing code internal to
// `BundleStateWithReceipts::write_to_storage`, so we just use a default empty
// `ExecutionOutcome::write_to_storage`, so we just use a default empty
// `BundleState`.
let bundled_state = BundleStateWithReceipts::new(
Default::default(),
receipts,
first_block,
Default::default(),
);
let execution_outcome =
ExecutionOutcome::new(Default::default(), receipts, first_block, Default::default());

let static_file_producer =
static_file_provider.get_writer(first_block, StaticFileSegment::Receipts)?;

// finally, write the receipts
bundled_state.write_to_storage::<DB::TXMut>(
execution_outcome.write_to_storage::<DB::TXMut>(
&tx,
Some(static_file_producer),
OriginalValuesKnown::Yes,
Expand Down
32 changes: 16 additions & 16 deletions crates/blockchain-tree/src/blockchain_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
metrics::{MakeCanonicalAction, MakeCanonicalDurationsRecorder, TreeMetrics},
state::{BlockchainId, TreeState},
AppendableChain, BlockIndices, BlockchainTreeConfig, BundleStateData, TreeExternals,
AppendableChain, BlockIndices, BlockchainTreeConfig, ExecutionData, TreeExternals,
};
use reth_blockchain_tree_api::{
error::{BlockchainTreeError, CanonicalError, InsertBlockError, InsertBlockErrorKind},
Expand All @@ -18,10 +18,10 @@ use reth_primitives::{
SealedBlockWithSenders, SealedHeader, StaticFileSegment, B256, U256,
};
use reth_provider::{
BlockExecutionWriter, BlockNumReader, BlockWriter, BundleStateWithReceipts,
CanonStateNotification, CanonStateNotificationSender, CanonStateNotifications, Chain,
ChainSpecProvider, ChainSplit, ChainSplitTarget, DisplayBlocksChain, HeaderProvider,
ProviderError, StaticFileProviderFactory,
BlockExecutionWriter, BlockNumReader, BlockWriter, CanonStateNotification,
CanonStateNotificationSender, CanonStateNotifications, Chain, ChainSpecProvider, ChainSplit,
ChainSplitTarget, DisplayBlocksChain, ExecutionOutcome, HeaderProvider, ProviderError,
StaticFileProviderFactory,
};
use reth_prune_types::PruneModes;
use reth_stages_api::{MetricEvent, MetricEventsSender};
Expand Down Expand Up @@ -264,7 +264,7 @@ where
/// * block unknown.
/// * `chain_id` not present in state.
/// * there are no parent hashes stored.
pub fn post_state_data(&self, block_hash: BlockHash) -> Option<BundleStateData> {
pub fn post_state_data(&self, block_hash: BlockHash) -> Option<ExecutionData> {
trace!(target: "blockchain_tree", ?block_hash, "Searching for post state data");

let canonical_chain = self.state.block_indices.canonical_chain();
Expand All @@ -278,7 +278,7 @@ where
return None;
};
let block_number = chain.block_number(block_hash)?;
let state = chain.state_at_block(block_number)?;
let execution_outcome = chain.execution_outcome_at_block(block_number)?;

// get parent hashes
let mut parent_block_hashes = self.all_chain_hashes(chain_id);
Expand All @@ -295,15 +295,15 @@ where

// get canonical fork.
let canonical_fork = self.canonical_fork(chain_id)?;
return Some(BundleStateData { state, parent_block_hashes, canonical_fork })
return Some(ExecutionData { execution_outcome, parent_block_hashes, canonical_fork })
}

// check if there is canonical block
if let Some(canonical_number) = canonical_chain.canonical_number(&block_hash) {
trace!(target: "blockchain_tree", %block_hash, "Constructing post state data based on canonical chain");
return Some(BundleStateData {
return Some(ExecutionData {
canonical_fork: ForkBlock { number: canonical_number, hash: block_hash },
state: BundleStateWithReceipts::default(),
execution_outcome: ExecutionOutcome::default(),
parent_block_hashes: canonical_chain.inner().clone(),
})
}
Expand Down Expand Up @@ -629,8 +629,8 @@ where
let chains_to_bump = self.find_all_dependent_chains(&hash);
if !chains_to_bump.is_empty() {
// if there is such chain, revert state to this block.
let mut cloned_state = chain.state().clone();
cloned_state.revert_to(*number);
let mut cloned_execution_outcome = chain.execution_outcome().clone();
cloned_execution_outcome.revert_to(*number);

// prepend state to all chains that fork from this block.
for chain_id in chains_to_bump {
Expand All @@ -645,7 +645,7 @@ where
chain_tip = ?chain.tip().num_hash(),
"Prepend unwound block state to blockchain tree chain");

chain.prepend_state(cloned_state.state().clone())
chain.prepend_state(cloned_execution_outcome.state().clone())
}
}
}
Expand Down Expand Up @@ -1398,7 +1398,7 @@ mod tests {
use std::collections::HashMap;

fn setup_externals(
exec_res: Vec<BundleStateWithReceipts>,
exec_res: Vec<ExecutionOutcome>,
) -> TreeExternals<Arc<TempDatabase<DatabaseEnv>>, MockExecutorProvider> {
let chain_spec = Arc::new(
ChainSpecBuilder::default()
Expand Down Expand Up @@ -1961,12 +1961,12 @@ mod tests {
]))
.assert(&tree);
// chain 0 has two blocks so receipts and reverts len is 2
let chain0 = tree.state.chains.get(&0.into()).unwrap().state();
let chain0 = tree.state.chains.get(&0.into()).unwrap().execution_outcome();
assert_eq!(chain0.receipts().len(), 2);
assert_eq!(chain0.state().reverts.len(), 2);
assert_eq!(chain0.first_block(), block1.number);
// chain 1 has one block so receipts and reverts len is 1
let chain1 = tree.state.chains.get(&1.into()).unwrap().state();
let chain1 = tree.state.chains.get(&1.into()).unwrap().execution_outcome();
assert_eq!(chain1.receipts().len(), 1);
assert_eq!(chain1.state().reverts.len(), 1);
assert_eq!(chain1.first_block(), block2.number);
Expand Down
34 changes: 17 additions & 17 deletions crates/blockchain-tree/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! [`BundleStateDataProvider`] implementations used by the tree.
//! [`ExecutionDataProvider`] implementations used by the tree.
use reth_primitives::{BlockHash, BlockNumber, ForkBlock};
use reth_provider::{BundleStateDataProvider, BundleStateForkProvider, BundleStateWithReceipts};
use reth_provider::{BlockExecutionForkProvider, ExecutionDataProvider, ExecutionOutcome};
use std::collections::BTreeMap;

/// Structure that combines references of required data to be a [`BundleStateDataProvider`].
/// Structure that combines references of required data to be a [`ExecutionDataProvider`].
#[derive(Clone, Debug)]
pub struct BundleStateDataRef<'a> {
/// The wrapped state after execution of one or more transactions and/or blocks.
pub state: &'a BundleStateWithReceipts,
/// The execution outcome after execution of one or more transactions and/or blocks.
pub execution_outcome: &'a ExecutionOutcome,
/// The blocks in the sidechain.
pub sidechain_block_hashes: &'a BTreeMap<BlockNumber, BlockHash>,
/// The blocks in the canonical chain.
Expand All @@ -17,9 +17,9 @@ pub struct BundleStateDataRef<'a> {
pub canonical_fork: ForkBlock,
}

impl<'a> BundleStateDataProvider for BundleStateDataRef<'a> {
fn state(&self) -> &BundleStateWithReceipts {
self.state
impl<'a> ExecutionDataProvider for BundleStateDataRef<'a> {
fn execution_outcome(&self) -> &ExecutionOutcome {
self.execution_outcome
}

fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash> {
Expand All @@ -32,17 +32,17 @@ impl<'a> BundleStateDataProvider for BundleStateDataRef<'a> {
}
}

impl<'a> BundleStateForkProvider for BundleStateDataRef<'a> {
impl<'a> BlockExecutionForkProvider for BundleStateDataRef<'a> {
fn canonical_fork(&self) -> ForkBlock {
self.canonical_fork
}
}

/// Structure that owns the relevant data needs to be a [`BundleStateDataProvider`]
/// Structure that owns the relevant data needs to be a [`ExecutionDataProvider`]
#[derive(Clone, Debug)]
pub struct BundleStateData {
/// Post state with changes
pub state: BundleStateWithReceipts,
pub struct ExecutionData {
/// Execution outcome.
pub execution_outcome: ExecutionOutcome,
/// Parent block hashes needs for evm BLOCKHASH opcode.
/// NOTE: it does not mean that all hashes are there but all until finalized are there.
/// Other hashes can be obtained from provider
Expand All @@ -51,17 +51,17 @@ pub struct BundleStateData {
pub canonical_fork: ForkBlock,
}

impl BundleStateDataProvider for BundleStateData {
fn state(&self) -> &BundleStateWithReceipts {
&self.state
impl ExecutionDataProvider for ExecutionData {
fn execution_outcome(&self) -> &ExecutionOutcome {
&self.execution_outcome
}

fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash> {
self.parent_block_hashes.get(&block_number).cloned()
}
}

impl BundleStateForkProvider for BundleStateData {
impl BlockExecutionForkProvider for ExecutionData {
fn canonical_fork(&self) -> ForkBlock {
self.canonical_fork
}
Expand Down
Loading

0 comments on commit a5d825e

Please sign in to comment.