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

refactor: rename BundleStateWithReceipts to BlockExecutionOutcome #8730

Merged
merged 10 commits into from
Jun 11, 2024
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 @@ -30,9 +30,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, BlockExecutionOutcome, BlockHashReader, BlockReader,
BlockWriter, ChainSpecProvider, 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 block_execution_outcome = BlockExecutionOutcome::new(
state,
receipts.into(),
block.number,
vec![requests.into()],
);

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

let hashed_state = state.hash_state_slow();
let (state_root, trie_updates) = state
let hashed_post_state = block_execution_outcome.hash_state_slow();
let (state_root, trie_updates) = block_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,
block_execution_outcome,
hashed_post_state,
trie_updates,
None,
)?;
Expand Down
18 changes: 9 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::{stage::StageId, BlockHashOrNumber};
use reth_provider::{
AccountExtReader, BundleStateWithReceipts, ChainSpecProvider, HashingWriter, HeaderProvider,
AccountExtReader, BlockExecutionOutcome, ChainSpecProvider, HashingWriter, HeaderProvider,
LatestStateProviderRef, OriginalValuesKnown, ProviderFactory, StageCheckpointReader,
StateWriter, StaticFileProviderFactory, StorageReader,
};
Expand Down Expand Up @@ -146,16 +146,12 @@ impl Command {
)
.into(),
)?;
let block_state = BundleStateWithReceipts::new(
state,
receipts.into(),
block.number,
vec![requests.into()],
);
let block_execution_outcome =
BlockExecutionOutcome::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())?;
block_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 @@ -172,7 +168,11 @@ impl Command {
.map_err(|_| BlockValidationError::SenderRecoveryError)?,
None,
)?;
block_state.write_to_storage(provider_rw.tx_ref(), None, OriginalValuesKnown::No)?;
block_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
8 changes: 4 additions & 4 deletions bin/reth/src/commands/import_receipts_op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use reth_node_core::version::SHORT_VERSION;
use reth_optimism_primitives::bedrock_import::is_dup_tx;
use reth_primitives::{stage::StageId, Receipts, StaticFileSegment};
use reth_provider::{
BundleStateWithReceipts, OriginalValuesKnown, ProviderFactory, StageCheckpointReader,
BlockExecutionOutcome, OriginalValuesKnown, ProviderFactory, StageCheckpointReader,
StateWriter, StaticFileProviderFactory, StaticFileWriter, StatsReader,
};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -130,9 +130,9 @@ where
);

// We're reusing receipt writing code internal to
// `BundleStateWithReceipts::write_to_storage`, so we just use a default empty
// `BlockExecutionOutcome::write_to_storage`, so we just use a default empty
// `BundleState`.
let bundled_state = BundleStateWithReceipts::new(
let block_execution_outcome = BlockExecutionOutcome::new(
Default::default(),
receipts,
first_block,
Expand All @@ -143,7 +143,7 @@ where
static_file_provider.get_writer(first_block, StaticFileSegment::Receipts)?;

// finally, write the receipts
bundled_state.write_to_storage::<DB::TXMut>(
block_execution_outcome.write_to_storage::<DB::TXMut>(
&tx,
Some(static_file_producer),
OriginalValuesKnown::Yes,
Expand Down
30 changes: 17 additions & 13 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, BlockExecutionData, BlockIndices, BlockchainTreeConfig, TreeExternals,
};
use reth_blockchain_tree_api::{
error::{BlockchainTreeError, CanonicalError, InsertBlockError, InsertBlockErrorKind},
Expand All @@ -18,7 +18,7 @@ use reth_primitives::{
SealedBlockWithSenders, SealedHeader, StaticFileSegment, B256, U256,
};
use reth_provider::{
BlockExecutionWriter, BlockNumReader, BlockWriter, BundleStateWithReceipts,
BlockExecutionOutcome, BlockExecutionWriter, BlockNumReader, BlockWriter,
CanonStateNotification, CanonStateNotificationSender, CanonStateNotifications, Chain,
ChainSpecProvider, ChainSplit, ChainSplitTarget, DisplayBlocksChain, HeaderProvider,
ProviderError, StaticFileProviderFactory,
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<BlockExecutionData> {
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 block_execution_outcome = chain.block_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,19 @@ where

// get canonical fork.
let canonical_fork = self.canonical_fork(chain_id)?;
return Some(BundleStateData { state, parent_block_hashes, canonical_fork })
return Some(BlockExecutionData {
block_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(BlockExecutionData {
canonical_fork: ForkBlock { number: canonical_number, hash: block_hash },
state: BundleStateWithReceipts::default(),
block_execution_outcome: BlockExecutionOutcome::default(),
parent_block_hashes: canonical_chain.inner().clone(),
})
}
Expand Down Expand Up @@ -629,8 +633,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_block_execution_outcome = chain.block_execution_outcome().clone();
cloned_block_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 +649,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_block_execution_outcome.state().clone())
}
}
}
Expand Down Expand Up @@ -1398,7 +1402,7 @@ mod tests {
use std::collections::HashMap;

fn setup_externals(
exec_res: Vec<BundleStateWithReceipts>,
exec_res: Vec<BlockExecutionOutcome>,
) -> TreeExternals<Arc<TempDatabase<DatabaseEnv>>, MockExecutorProvider> {
let chain_spec = Arc::new(
ChainSpecBuilder::default()
Expand Down Expand Up @@ -1961,12 +1965,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().block_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().block_execution_outcome();
assert_eq!(chain1.receipts().len(), 1);
assert_eq!(chain1.state().reverts.len(), 1);
assert_eq!(chain1.first_block(), block2.number);
Expand Down
36 changes: 19 additions & 17 deletions crates/blockchain-tree/src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
//! [`BundleStateDataProvider`] implementations used by the tree.
//! [`BlockExecutionDataProvider`] implementations used by the tree.

use reth_primitives::{BlockHash, BlockNumber, ForkBlock};
use reth_provider::{BundleStateDataProvider, BundleStateForkProvider, BundleStateWithReceipts};
use reth_provider::{
BlockExecutionDataProvider, BlockExecutionForkProvider, BlockExecutionOutcome,
};
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 [`BlockExecutionDataProvider`].
#[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 block execution outcome after execution of one or more transactions and/or blocks.
pub block_execution_outcome: &'a BlockExecutionOutcome,
/// The blocks in the sidechain.
pub sidechain_block_hashes: &'a BTreeMap<BlockNumber, BlockHash>,
/// The blocks in the canonical chain.
Expand All @@ -17,9 +19,9 @@ pub struct BundleStateDataRef<'a> {
pub canonical_fork: ForkBlock,
}

impl<'a> BundleStateDataProvider for BundleStateDataRef<'a> {
fn state(&self) -> &BundleStateWithReceipts {
self.state
impl<'a> BlockExecutionDataProvider for BundleStateDataRef<'a> {
fn block_execution_outcome(&self) -> &BlockExecutionOutcome {
self.block_execution_outcome
}

fn block_hash(&self, block_number: BlockNumber) -> Option<BlockHash> {
Expand All @@ -32,17 +34,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 [`BlockExecutionDataProvider`]
#[derive(Clone, Debug)]
pub struct BundleStateData {
/// Post state with changes
pub state: BundleStateWithReceipts,
pub struct BlockExecutionData {
/// Block execution outcome.
pub block_execution_outcome: BlockExecutionOutcome,
/// 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 +53,17 @@ pub struct BundleStateData {
pub canonical_fork: ForkBlock,
}

impl BundleStateDataProvider for BundleStateData {
fn state(&self) -> &BundleStateWithReceipts {
&self.state
impl BlockExecutionDataProvider for BlockExecutionData {
fn block_execution_outcome(&self) -> &BlockExecutionOutcome {
&self.block_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 BlockExecutionData {
fn canonical_fork(&self) -> ForkBlock {
self.canonical_fork
}
Expand Down
Loading
Loading