From 5254f16e07965686ebf2b906c1e6fc7b04863f3e Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Mon, 26 Aug 2024 02:25:27 +0800 Subject: [PATCH] feat: add impl From BlockExecutionOutput for ExecutionOutcome (#10507) --- .../src/commands/debug_cmd/build_block.rs | 13 ++++--------- .../commands/debug_cmd/in_memory_merkle.rs | 7 +++---- crates/blockchain-tree/src/chain.rs | 13 ++++++------- crates/consensus/auto-seal/src/lib.rs | 19 +++++-------------- crates/engine/tree/src/tree/mod.rs | 11 +++-------- crates/evm/execution-types/src/execute.rs | 2 -- .../execution-types/src/execution_outcome.rs | 12 ++++++++++++ 7 files changed, 33 insertions(+), 44 deletions(-) diff --git a/bin/reth/src/commands/debug_cmd/build_block.rs b/bin/reth/src/commands/debug_cmd/build_block.rs index 9f03806052..0c3b661486 100644 --- a/bin/reth/src/commands/debug_cmd/build_block.rs +++ b/bin/reth/src/commands/debug_cmd/build_block.rs @@ -15,7 +15,7 @@ use reth_cli_runner::CliContext; use reth_consensus::Consensus; use reth_db::DatabaseEnv; use reth_errors::RethResult; -use reth_evm::execute::{BlockExecutionOutput, BlockExecutorProvider, Executor}; +use reth_evm::execute::{BlockExecutorProvider, Executor}; use reth_execution_types::ExecutionOutcome; use reth_fs_util as fs; use reth_node_api::PayloadBuilderAttributes; @@ -273,15 +273,10 @@ impl Command { let db = StateProviderDatabase::new(blockchain_db.latest()?); let executor = block_executor!(provider_factory.chain_spec()).executor(db); - let BlockExecutionOutput { state, receipts, requests, .. } = + let block_execution_output = executor.execute((&block_with_senders.clone().unseal(), U256::MAX).into())?; - let execution_outcome = ExecutionOutcome::new( - state, - receipts.into(), - block.number, - vec![requests.into()], - ); - + let execution_outcome = + ExecutionOutcome::from((block_execution_output, block.number)); debug!(target: "reth::cli", ?execution_outcome, "Executed block"); let hashed_post_state = execution_outcome.hash_state_slow(); diff --git a/bin/reth/src/commands/debug_cmd/in_memory_merkle.rs b/bin/reth/src/commands/debug_cmd/in_memory_merkle.rs index 0b8c7ade22..86857bebe1 100644 --- a/bin/reth/src/commands/debug_cmd/in_memory_merkle.rs +++ b/bin/reth/src/commands/debug_cmd/in_memory_merkle.rs @@ -10,7 +10,7 @@ use reth_cli_util::get_secret_key; use reth_config::Config; use reth_db::DatabaseEnv; use reth_errors::BlockValidationError; -use reth_evm::execute::{BlockExecutionOutput, BlockExecutorProvider, Executor}; +use reth_evm::execute::{BlockExecutorProvider, Executor}; use reth_execution_types::ExecutionOutcome; use reth_network::{BlockDownloaderProvider, NetworkHandle}; use reth_network_api::NetworkInfo; @@ -136,7 +136,7 @@ impl Command { let merkle_block_td = provider.header_td_by_number(merkle_block_number)?.unwrap_or_default(); - let BlockExecutionOutput { state, receipts, requests, .. } = executor.execute( + let block_execution_output = executor.execute( ( &block .clone() @@ -147,8 +147,7 @@ impl Command { ) .into(), )?; - let execution_outcome = - ExecutionOutcome::new(state, receipts.into(), block.number, vec![requests.into()]); + let execution_outcome = ExecutionOutcome::from((block_execution_output, block.number)); // Unpacked `BundleState::state_root_slow` function let (in_memory_state_root, in_memory_updates) = StateRoot::overlay_root_with_updates( diff --git a/crates/blockchain-tree/src/chain.rs b/crates/blockchain-tree/src/chain.rs index dbc0c1d04b..9c09c5e157 100644 --- a/crates/blockchain-tree/src/chain.rs +++ b/crates/blockchain-tree/src/chain.rs @@ -11,7 +11,7 @@ use reth_blockchain_tree_api::{ }; use reth_consensus::{Consensus, ConsensusError, PostExecutionInput}; use reth_db_api::database::Database; -use reth_evm::execute::{BlockExecutionOutput, BlockExecutorProvider, Executor}; +use reth_evm::execute::{BlockExecutorProvider, Executor}; use reth_execution_errors::BlockExecutionError; use reth_execution_types::{Chain, ExecutionOutcome}; use reth_primitives::{ @@ -210,13 +210,12 @@ impl AppendableChain { let block = block.unseal(); let state = executor.execute((&block, U256::MAX).into())?; - let BlockExecutionOutput { state, receipts, requests, .. } = state; - externals - .consensus - .validate_block_post_execution(&block, PostExecutionInput::new(&receipts, &requests))?; + externals.consensus.validate_block_post_execution( + &block, + PostExecutionInput::new(&state.receipts, &state.requests), + )?; - let initial_execution_outcome = - ExecutionOutcome::new(state, receipts.into(), block.number, vec![requests.into()]); + let initial_execution_outcome = ExecutionOutcome::from((state, block.number)); // check state root if the block extends the canonical chain __and__ if state root // validation was requested. diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index ae4b678d40..6f73dfae0f 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -45,7 +45,7 @@ mod task; pub use crate::client::AutoSealClient; pub use mode::{FixedBlockTimeMiner, MiningMode, ReadyTransactionMiner}; -use reth_evm::execute::{BlockExecutionOutput, BlockExecutorProvider, Executor}; +use reth_evm::execute::{BlockExecutorProvider, Executor}; pub use task::MiningTask; /// A consensus implementation intended for local development and testing purposes. @@ -374,19 +374,10 @@ impl StorageInner { ); // execute the block - let BlockExecutionOutput { - state, - receipts, - requests: block_execution_requests, - gas_used, - .. - } = executor.executor(&mut db).execute((&block, U256::ZERO).into())?; - let execution_outcome = ExecutionOutcome::new( - state, - receipts.into(), - block.number, - vec![block_execution_requests.into()], - ); + let block_execution_output = + executor.executor(&mut db).execute((&block, U256::ZERO).into())?; + let gas_used = block_execution_output.gas_used; + let execution_outcome = ExecutionOutcome::from((block_execution_output, block.number)); // todo(onbjerg): we should not pass requests around as this is building a block, which // means we need to extract the requests from the execution output and compute the requests diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index 93ca1695cb..c7074e3663 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -23,8 +23,8 @@ use reth_payload_builder::PayloadBuilderHandle; use reth_payload_primitives::{PayloadAttributes, PayloadBuilderAttributes}; use reth_payload_validator::ExecutionPayloadValidator; use reth_primitives::{ - Block, BlockNumHash, BlockNumber, GotExpected, Header, Receipts, Requests, SealedBlock, - SealedBlockWithSenders, SealedHeader, B256, U256, + Block, BlockNumHash, BlockNumber, GotExpected, Header, SealedBlock, SealedBlockWithSenders, + SealedHeader, B256, U256, }; use reth_provider::{ BlockReader, ExecutionOutcome, ProviderError, StateProviderBox, StateProviderFactory, @@ -1760,12 +1760,7 @@ where let executed = ExecutedBlock { block: sealed_block.clone(), senders: Arc::new(block.senders), - execution_output: Arc::new(ExecutionOutcome::new( - output.state, - Receipts::from(output.receipts), - block_number, - vec![Requests::from(output.requests)], - )), + execution_output: Arc::new(ExecutionOutcome::from((output, block_number))), hashed_state: Arc::new(hashed_state), trie: Arc::new(trie_output), }; diff --git a/crates/evm/execution-types/src/execute.rs b/crates/evm/execution-types/src/execute.rs index 7552e3090d..2933fd5981 100644 --- a/crates/evm/execution-types/src/execute.rs +++ b/crates/evm/execution-types/src/execute.rs @@ -26,8 +26,6 @@ impl<'a, Block> From<(&'a Block, U256)> for BlockExecutionInput<'a, Block> { /// The output of an ethereum block. /// /// Contains the state changes, transaction receipts, and total gas used in the block. -/// -/// TODO(mattsse): combine with `ExecutionOutcome` #[derive(Debug, Clone, PartialEq, Eq)] pub struct BlockExecutionOutput { /// The changed state of the block after execution. diff --git a/crates/evm/execution-types/src/execution_outcome.rs b/crates/evm/execution-types/src/execution_outcome.rs index 01b01cfdf8..46f4b816a7 100644 --- a/crates/evm/execution-types/src/execution_outcome.rs +++ b/crates/evm/execution-types/src/execution_outcome.rs @@ -1,3 +1,4 @@ +use crate::BlockExecutionOutput; use reth_primitives::{ logs_bloom, Account, Address, BlockNumber, Bloom, Bytecode, Log, Receipt, Receipts, Requests, StorageEntry, B256, U256, @@ -355,6 +356,17 @@ impl ExecutionOutcome { } } +impl From<(BlockExecutionOutput, BlockNumber)> for ExecutionOutcome { + fn from(value: (BlockExecutionOutput, BlockNumber)) -> Self { + Self { + bundle: value.0.state, + receipts: Receipts::from(value.0.receipts), + first_block: value.1, + requests: vec![Requests::from(value.0.requests)], + } + } +} + #[cfg(test)] mod tests { use super::*;