From 931fd6c41db904cee03fd01383b904ed8245c843 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Thu, 13 Feb 2025 14:24:00 +0400 Subject: [PATCH] refactor: remove `PostExecutionInput` (#14464) --- Cargo.lock | 5 +++- crates/consensus/consensus/Cargo.toml | 4 +-- crates/consensus/consensus/src/lib.rs | 20 ++------------ crates/consensus/consensus/src/noop.rs | 7 ++--- crates/consensus/consensus/src/test_utils.rs | 5 ++-- crates/engine/tree/src/tree/mod.rs | 7 ++--- crates/ethereum/consensus/Cargo.toml | 1 + crates/ethereum/consensus/src/lib.rs | 9 +++---- crates/evm/execution-types/Cargo.toml | 3 +++ crates/evm/execution-types/src/execute.rs | 27 ++++++++++++------- .../execution-types/src/execution_outcome.rs | 17 +++++------- crates/evm/src/execute.rs | 13 +++++---- crates/evm/src/test_utils.rs | 14 +++++----- crates/optimism/consensus/Cargo.toml | 7 ++++- crates/optimism/consensus/src/lib.rs | 9 +++---- crates/optimism/evm/src/execute.rs | 4 +-- crates/rpc/rpc/src/validation.rs | 7 ++--- crates/stages/stages/src/stages/execution.rs | 7 ++--- 18 files changed, 78 insertions(+), 88 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d6b7e24ebec1..1eedc3f60e9b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6881,10 +6881,10 @@ name = "reth-consensus" version = "1.2.0" dependencies = [ "alloy-consensus", - "alloy-eips", "alloy-primitives", "auto_impl", "derive_more", + "reth-execution-types", "reth-primitives-traits", ] @@ -7529,6 +7529,7 @@ dependencies = [ "reth-chainspec", "reth-consensus", "reth-consensus-common", + "reth-execution-types", "reth-primitives", "reth-primitives-traits", "tracing", @@ -7704,6 +7705,7 @@ dependencies = [ "alloy-primitives", "arbitrary", "bincode", + "derive_more", "rand 0.8.5", "reth-ethereum-primitives", "reth-execution-errors", @@ -8433,6 +8435,7 @@ dependencies = [ "reth-chainspec", "reth-consensus", "reth-consensus-common", + "reth-execution-types", "reth-optimism-chainspec", "reth-optimism-forks", "reth-optimism-primitives", diff --git a/crates/consensus/consensus/Cargo.toml b/crates/consensus/consensus/Cargo.toml index 0bbe53c42c92..2e81ce917315 100644 --- a/crates/consensus/consensus/Cargo.toml +++ b/crates/consensus/consensus/Cargo.toml @@ -12,10 +12,10 @@ workspace = true [dependencies] # reth +reth-execution-types.workspace = true reth-primitives-traits.workspace = true # ethereum -alloy-eips.workspace = true alloy-primitives.workspace = true alloy-consensus.workspace = true @@ -28,10 +28,10 @@ default = ["std"] std = [ "reth-primitives-traits/std", "alloy-primitives/std", - "alloy-eips/std", "alloy-consensus/std", "reth-primitives-traits/std", "derive_more/std", + "reth-execution-types/std", ] test-utils = [ "reth-primitives-traits/test-utils", diff --git a/crates/consensus/consensus/src/lib.rs b/crates/consensus/consensus/src/lib.rs index 0536037ebe5d..2aff0776a886 100644 --- a/crates/consensus/consensus/src/lib.rs +++ b/crates/consensus/consensus/src/lib.rs @@ -13,8 +13,8 @@ extern crate alloc; use alloc::{fmt::Debug, sync::Arc, vec::Vec}; use alloy_consensus::Header; -use alloy_eips::eip7685::Requests; use alloy_primitives::{BlockHash, BlockNumber, Bloom, B256, U256}; +use reth_execution_types::BlockExecutionResult; use reth_primitives_traits::{ constants::MINIMUM_GAS_LIMIT, transaction::error::InvalidTransactionError, Block, GotExpected, GotExpectedBoxed, NodePrimitives, RecoveredBlock, SealedBlock, SealedHeader, @@ -27,22 +27,6 @@ pub mod noop; /// test helpers for mocking consensus pub mod test_utils; -/// Post execution input passed to [`FullConsensus::validate_block_post_execution`]. -#[derive(Debug)] -pub struct PostExecutionInput<'a, R> { - /// Receipts of the block. - pub receipts: &'a [R], - /// EIP-7685 requests of the block. - pub requests: &'a Requests, -} - -impl<'a, R> PostExecutionInput<'a, R> { - /// Creates a new instance of `PostExecutionInput`. - pub const fn new(receipts: &'a [R], requests: &'a Requests) -> Self { - Self { receipts, requests } - } -} - /// [`Consensus`] implementation which knows full node primitives and is able to validation block's /// execution outcome. #[auto_impl::auto_impl(&, Arc)] @@ -56,7 +40,7 @@ pub trait FullConsensus: AsConsensus { fn validate_block_post_execution( &self, block: &RecoveredBlock, - input: PostExecutionInput<'_, N::Receipt>, + result: &BlockExecutionResult, ) -> Result<(), ConsensusError>; } diff --git a/crates/consensus/consensus/src/noop.rs b/crates/consensus/consensus/src/noop.rs index 40908c0e24d2..ecfb35d20d42 100644 --- a/crates/consensus/consensus/src/noop.rs +++ b/crates/consensus/consensus/src/noop.rs @@ -1,6 +1,7 @@ -use crate::{Consensus, ConsensusError, FullConsensus, HeaderValidator, PostExecutionInput}; +use crate::{Consensus, ConsensusError, FullConsensus, HeaderValidator}; use alloc::sync::Arc; use alloy_primitives::U256; +use reth_execution_types::BlockExecutionResult; use reth_primitives_traits::{Block, NodePrimitives, RecoveredBlock, SealedBlock, SealedHeader}; /// A Consensus implementation that does nothing. @@ -57,8 +58,8 @@ impl FullConsensus for NoopConsensus { fn validate_block_post_execution( &self, _block: &RecoveredBlock, - _input: PostExecutionInput<'_, N::Receipt>, - ) -> Result<(), Self::Error> { + _result: &BlockExecutionResult, + ) -> Result<(), ConsensusError> { Ok(()) } } diff --git a/crates/consensus/consensus/src/test_utils.rs b/crates/consensus/consensus/src/test_utils.rs index 044219bfdce9..a7ca0f72f3c4 100644 --- a/crates/consensus/consensus/src/test_utils.rs +++ b/crates/consensus/consensus/src/test_utils.rs @@ -1,6 +1,7 @@ -use crate::{Consensus, ConsensusError, FullConsensus, HeaderValidator, PostExecutionInput}; +use crate::{Consensus, ConsensusError, FullConsensus, HeaderValidator}; use alloy_primitives::U256; use core::sync::atomic::{AtomicBool, Ordering}; +use reth_execution_types::BlockExecutionResult; use reth_primitives_traits::{Block, NodePrimitives, RecoveredBlock, SealedBlock, SealedHeader}; /// Consensus engine implementation for testing @@ -50,7 +51,7 @@ impl FullConsensus for TestConsensus { fn validate_block_post_execution( &self, _block: &RecoveredBlock, - _input: PostExecutionInput<'_, N::Receipt>, + _result: &BlockExecutionResult, ) -> Result<(), ConsensusError> { if self.fail_validation() { Err(ConsensusError::BaseFeeMissing) diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index b0f83a0d7cc3..40a22b725c07 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -26,7 +26,7 @@ use reth_chain_state::{ CanonicalInMemoryState, ExecutedBlock, ExecutedBlockWithTrieUpdates, MemoryOverlayStateProvider, NewCanonicalChain, }; -use reth_consensus::{Consensus, FullConsensus, PostExecutionInput}; +use reth_consensus::{Consensus, FullConsensus}; pub use reth_engine_primitives::InvalidBlockHook; use reth_engine_primitives::{ BeaconConsensusEngineEvent, BeaconEngineMessage, BeaconOnNewPayloadError, EngineTypes, @@ -2527,10 +2527,7 @@ where // Ensure that prewarm tasks don't send proof messages after state root sender is dropped cancel_execution.cancel(); - if let Err(err) = self.consensus.validate_block_post_execution( - &block, - PostExecutionInput::new(&output.receipts, &output.requests), - ) { + if let Err(err) = self.consensus.validate_block_post_execution(&block, &output) { // call post-block hook self.invalid_block_hook.on_invalid_block(&parent_block, &block, &output, None); return Err(err.into()) diff --git a/crates/ethereum/consensus/Cargo.toml b/crates/ethereum/consensus/Cargo.toml index 8e6158ff46cb..6b73bf75336c 100644 --- a/crates/ethereum/consensus/Cargo.toml +++ b/crates/ethereum/consensus/Cargo.toml @@ -12,6 +12,7 @@ workspace = true [dependencies] # reth +reth-execution-types.workspace = true reth-chainspec.workspace = true reth-consensus-common.workspace = true reth-primitives.workspace = true diff --git a/crates/ethereum/consensus/src/lib.rs b/crates/ethereum/consensus/src/lib.rs index 7bdf241337ba..ba71dc84b07e 100644 --- a/crates/ethereum/consensus/src/lib.rs +++ b/crates/ethereum/consensus/src/lib.rs @@ -12,15 +12,14 @@ use alloy_consensus::EMPTY_OMMER_ROOT_HASH; use alloy_eips::merge::ALLOWED_FUTURE_BLOCK_TIME_SECONDS; use alloy_primitives::U256; use reth_chainspec::{EthChainSpec, EthereumHardforks}; -use reth_consensus::{ - Consensus, ConsensusError, FullConsensus, HeaderValidator, PostExecutionInput, -}; +use reth_consensus::{Consensus, ConsensusError, FullConsensus, HeaderValidator}; use reth_consensus_common::validation::{ validate_4844_header_standalone, validate_against_parent_4844, validate_against_parent_eip1559_base_fee, validate_against_parent_hash_number, validate_against_parent_timestamp, validate_block_pre_execution, validate_body_against_header, validate_header_base_fee, validate_header_extra_data, validate_header_gas, }; +use reth_execution_types::BlockExecutionResult; use reth_primitives::{NodePrimitives, RecoveredBlock, SealedBlock, SealedHeader}; use reth_primitives_traits::{ constants::{GAS_LIMIT_BOUND_DIVISOR, MINIMUM_GAS_LIMIT}, @@ -104,9 +103,9 @@ where fn validate_block_post_execution( &self, block: &RecoveredBlock, - input: PostExecutionInput<'_, N::Receipt>, + result: &BlockExecutionResult, ) -> Result<(), ConsensusError> { - validate_block_post_execution(block, &self.chain_spec, input.receipts, input.requests) + validate_block_post_execution(block, &self.chain_spec, &result.receipts, &result.requests) } } diff --git a/crates/evm/execution-types/Cargo.toml b/crates/evm/execution-types/Cargo.toml index de959e537c58..d504892b894f 100644 --- a/crates/evm/execution-types/Cargo.toml +++ b/crates/evm/execution-types/Cargo.toml @@ -26,6 +26,8 @@ alloy-eips.workspace = true serde = { workspace = true, optional = true } serde_with = { workspace = true, optional = true } +derive_more.workspace = true + [dev-dependencies] arbitrary.workspace = true bincode.workspace = true @@ -65,4 +67,5 @@ std = [ "reth-trie-common/std", "reth-ethereum-primitives/std", "reth-execution-errors/std", + "derive_more/std", ] diff --git a/crates/evm/execution-types/src/execute.rs b/crates/evm/execution-types/src/execute.rs index 009ebae5b891..463c5f64158e 100644 --- a/crates/evm/execution-types/src/execute.rs +++ b/crates/evm/execution-types/src/execute.rs @@ -13,17 +13,24 @@ pub struct BlockExecutionResult { pub gas_used: u64, } -/// The output of an ethereum block. -/// -/// Contains the state changes, transaction receipts, and total gas used in the block. -#[derive(Debug, Clone, PartialEq, Eq)] +/// [`BlockExecutionResult`] combined with state. +#[derive( + Debug, + Clone, + PartialEq, + Eq, + derive_more::AsRef, + derive_more::AsMut, + derive_more::Deref, + derive_more::DerefMut, +)] pub struct BlockExecutionOutput { + /// All the receipts of the transactions in the block. + #[as_ref] + #[as_mut] + #[deref] + #[deref_mut] + pub result: BlockExecutionResult, /// The changed state of the block after execution. pub state: BundleState, - /// All the receipts of the transactions in the block. - pub receipts: Vec, - /// All the EIP-7685 requests in the block. - pub requests: Requests, - /// The total gas used by the block. - pub gas_used: u64, } diff --git a/crates/evm/execution-types/src/execution_outcome.rs b/crates/evm/execution-types/src/execution_outcome.rs index 123704becd60..3cf72526d1ae 100644 --- a/crates/evm/execution-types/src/execution_outcome.rs +++ b/crates/evm/execution-types/src/execution_outcome.rs @@ -129,12 +129,12 @@ impl ExecutionOutcome { } /// Creates a new `ExecutionOutcome` from a single block execution result. - pub fn single(block_number: u64, result: BlockExecutionOutput) -> Self { + pub fn single(block_number: u64, output: BlockExecutionOutput) -> Self { Self { - bundle: result.state, - receipts: vec![result.receipts], + bundle: output.state, + receipts: vec![output.result.receipts], first_block: block_number, - requests: vec![result.requests], + requests: vec![output.result.requests], } } @@ -396,13 +396,8 @@ impl ExecutionOutcome { } impl From<(BlockExecutionOutput, BlockNumber)> for ExecutionOutcome { - fn from(value: (BlockExecutionOutput, BlockNumber)) -> Self { - Self { - bundle: value.0.state, - receipts: vec![value.0.receipts], - first_block: value.1, - requests: vec![value.0.requests], - } + fn from((output, block_number): (BlockExecutionOutput, BlockNumber)) -> Self { + Self::single(block_number, output) } } diff --git a/crates/evm/src/execute.rs b/crates/evm/src/execute.rs index 76b20b31e07a..7cb0e737852c 100644 --- a/crates/evm/src/execute.rs +++ b/crates/evm/src/execute.rs @@ -57,9 +57,9 @@ pub trait Executor: Sized { block: &RecoveredBlock<::Block>, ) -> Result::Receipt>, Self::Error> { - let BlockExecutionResult { receipts, requests, gas_used } = self.execute_one(block)?; + let result = self.execute_one(block)?; let mut state = self.into_state(); - Ok(BlockExecutionOutput { state: state.take_bundle(), receipts, requests, gas_used }) + Ok(BlockExecutionOutput { state: state.take_bundle(), result }) } /// Executes multiple inputs in the batch, and returns an aggregated [`ExecutionOutcome`]. @@ -96,10 +96,10 @@ pub trait Executor: Sized { where F: FnMut(&State), { - let BlockExecutionResult { receipts, requests, gas_used } = self.execute_one(block)?; + let result = self.execute_one(block)?; let mut state = self.into_state(); f(&state); - Ok(BlockExecutionOutput { state: state.take_bundle(), receipts, requests, gas_used }) + Ok(BlockExecutionOutput { state: state.take_bundle(), result }) } /// Executes the EVM with the given input and accepts a state hook closure that is invoked with @@ -112,10 +112,9 @@ pub trait Executor: Sized { where F: OnStateHook + 'static, { - let BlockExecutionResult { receipts, requests, gas_used } = - self.execute_one_with_state_hook(block, state_hook)?; + let result = self.execute_one_with_state_hook(block, state_hook)?; let mut state = self.into_state(); - Ok(BlockExecutionOutput { state: state.take_bundle(), receipts, requests, gas_used }) + Ok(BlockExecutionOutput { state: state.take_bundle(), result }) } /// Consumes the executor and returns the [`State`] containing all state changes. diff --git a/crates/evm/src/test_utils.rs b/crates/evm/src/test_utils.rs index 3f35ed994bde..12798fbf5c64 100644 --- a/crates/evm/src/test_utils.rs +++ b/crates/evm/src/test_utils.rs @@ -83,12 +83,14 @@ impl Executor for MockExecutorProvider { self.exec_results.lock().pop().unwrap(); Ok(BlockExecutionOutput { state: bundle, - receipts: receipts.into_iter().flatten().collect(), - requests: requests.into_iter().fold(Requests::default(), |mut reqs, req| { - reqs.extend(req); - reqs - }), - gas_used: 0, + result: BlockExecutionResult { + receipts: receipts.into_iter().flatten().collect(), + requests: requests.into_iter().fold(Requests::default(), |mut reqs, req| { + reqs.extend(req); + reqs + }), + gas_used: 0, + }, }) } diff --git a/crates/optimism/consensus/Cargo.toml b/crates/optimism/consensus/Cargo.toml index 2976ffd8e5dd..d0d01a3b3327 100644 --- a/crates/optimism/consensus/Cargo.toml +++ b/crates/optimism/consensus/Cargo.toml @@ -13,6 +13,7 @@ workspace = true [dependencies] # reth +reth-execution-types.workspace = true reth-chainspec.workspace = true reth-consensus-common.workspace = true reth-consensus.workspace = true @@ -54,5 +55,9 @@ std = [ "alloy-consensus/std", "alloy-trie/std", "op-alloy-consensus/std", + "reth-execution-types/std", +] +optimism = [ + "reth-optimism-primitives/optimism", + "reth-execution-types/optimism", ] -optimism = ["reth-optimism-primitives/optimism"] diff --git a/crates/optimism/consensus/src/lib.rs b/crates/optimism/consensus/src/lib.rs index 17ab584165b4..7c84d69c3883 100644 --- a/crates/optimism/consensus/src/lib.rs +++ b/crates/optimism/consensus/src/lib.rs @@ -18,15 +18,14 @@ use alloc::sync::Arc; use alloy_consensus::{BlockHeader as _, EMPTY_OMMER_ROOT_HASH}; use alloy_primitives::{B64, U256}; use reth_chainspec::{EthChainSpec, EthereumHardforks}; -use reth_consensus::{ - Consensus, ConsensusError, FullConsensus, HeaderValidator, PostExecutionInput, -}; +use reth_consensus::{Consensus, ConsensusError, FullConsensus, HeaderValidator}; use reth_consensus_common::validation::{ validate_against_parent_4844, validate_against_parent_eip1559_base_fee, validate_against_parent_hash_number, validate_against_parent_timestamp, validate_body_against_header, validate_cancun_gas, validate_header_base_fee, validate_header_extra_data, validate_header_gas, validate_shanghai_withdrawals, }; +use reth_execution_types::BlockExecutionResult; use reth_optimism_forks::OpHardforks; use reth_optimism_primitives::DepositReceipt; use reth_primitives::{GotExpected, NodePrimitives, RecoveredBlock, SealedHeader}; @@ -62,9 +61,9 @@ impl, - input: PostExecutionInput<'_, N::Receipt>, + result: &BlockExecutionResult, ) -> Result<(), ConsensusError> { - validate_block_post_execution(block.header(), &self.chain_spec, input.receipts) + validate_block_post_execution(block.header(), &self.chain_spec, &result.receipts) } } diff --git a/crates/optimism/evm/src/execute.rs b/crates/optimism/evm/src/execute.rs index e17f1d6a8115..3ae3c43c42bc 100644 --- a/crates/optimism/evm/src/execute.rs +++ b/crates/optimism/evm/src/execute.rs @@ -434,7 +434,7 @@ mod tests { )) .unwrap(); - let receipts = output.receipts; + let receipts = &output.receipts; let tx_receipt = &receipts[0]; let deposit_receipt = &receipts[1]; @@ -510,7 +510,7 @@ mod tests { )) .expect("Executing a block while canyon is active should not fail"); - let receipts = output.receipts; + let receipts = &output.receipts; let tx_receipt = &receipts[0]; let deposit_receipt = &receipts[1]; diff --git a/crates/rpc/rpc/src/validation.rs b/crates/rpc/rpc/src/validation.rs index c7ab92a7bb4b..5c58a60561d5 100644 --- a/crates/rpc/rpc/src/validation.rs +++ b/crates/rpc/rpc/src/validation.rs @@ -13,7 +13,7 @@ use alloy_rpc_types_engine::{ use async_trait::async_trait; use jsonrpsee::core::RpcResult; use reth_chainspec::{ChainSpecProvider, EthereumHardforks}; -use reth_consensus::{Consensus, FullConsensus, PostExecutionInput}; +use reth_consensus::{Consensus, FullConsensus}; use reth_engine_primitives::PayloadValidator; use reth_errors::{BlockExecutionError, ConsensusError, ProviderError}; use reth_evm::execute::{BlockExecutorProvider, Executor}; @@ -184,10 +184,7 @@ where return Err(ValidationApiError::Blacklist(account)) } - self.consensus.validate_block_post_execution( - &block, - PostExecutionInput::new(&output.receipts, &output.requests), - )?; + self.consensus.validate_block_post_execution(&block, &output)?; self.ensure_payment(&block, &output, &message)?; diff --git a/crates/stages/stages/src/stages/execution.rs b/crates/stages/stages/src/stages/execution.rs index a6c67bf553b4..85094df2898e 100644 --- a/crates/stages/stages/src/stages/execution.rs +++ b/crates/stages/stages/src/stages/execution.rs @@ -4,7 +4,7 @@ use alloy_eips::{eip1898::BlockWithParent, NumHash}; use alloy_primitives::BlockNumber; use num_traits::Zero; use reth_config::config::ExecutionConfig; -use reth_consensus::{ConsensusError, FullConsensus, PostExecutionInput}; +use reth_consensus::{ConsensusError, FullConsensus}; use reth_db::{static_file::HeaderMask, tables}; use reth_evm::{ execute::{BlockExecutorProvider, Executor}, @@ -355,10 +355,7 @@ where }) })?; - if let Err(err) = self.consensus.validate_block_post_execution( - &block, - PostExecutionInput::new(&result.receipts, &result.requests), - ) { + if let Err(err) = self.consensus.validate_block_post_execution(&block, &result) { return Err(StageError::Block { block: Box::new(BlockWithParent::new( block.header().parent_hash(),