feat: integrate TxEnv conversions (#14630)

This commit is contained in:
Arsenii Kulikov
2025-02-21 18:50:28 +04:00
committed by GitHub
parent b3bd69b224
commit 74a1151c07
16 changed files with 63 additions and 114 deletions

View File

@ -8,6 +8,7 @@ use alloc::{boxed::Box, sync::Arc, vec::Vec};
use alloy_consensus::{
transaction::Recovered, BlockHeader, Eip658Value, Header, Receipt, Transaction as _, TxReceipt,
};
use alloy_evm::FromRecoveredTx;
use op_alloy_consensus::OpDepositReceipt;
use reth_evm::{
execute::{
@ -96,7 +97,6 @@ where
evm,
block.sealed_block(),
&self.chain_spec,
&self.evm_config,
self.receipt_builder.as_ref(),
)
}
@ -134,19 +134,16 @@ impl<'a, B: Block> From<&'a SealedBlock<B>> for OpBlockExecutionInput {
/// Block execution strategy for Optimism.
#[derive(Debug)]
pub struct OpExecutionStrategy<'a, Evm, N: NodePrimitives, ChainSpec, EvmConfig: ConfigureEvm> {
pub struct OpExecutionStrategy<'a, E: Evm, N: NodePrimitives, ChainSpec> {
/// Chainspec.
chain_spec: ChainSpec,
/// How to configure the EVM.
evm_config: EvmConfig,
/// Receipt builder.
receipt_builder:
&'a dyn OpReceiptBuilder<N::SignedTx, HaltReasonFor<EvmConfig>, Receipt = N::Receipt>,
receipt_builder: &'a dyn OpReceiptBuilder<N::SignedTx, E::HaltReason, Receipt = N::Receipt>,
/// Input for block execution.
input: OpBlockExecutionInput,
/// The EVM used by strategy.
evm: Evm,
evm: E,
/// Receipts of executed transactions.
receipts: Vec<N::Receipt>,
/// Total gas used by executed transactions.
@ -157,23 +154,18 @@ pub struct OpExecutionStrategy<'a, Evm, N: NodePrimitives, ChainSpec, EvmConfig:
system_caller: SystemCaller<ChainSpec>,
}
impl<'a, Evm, N, ChainSpec, EvmConfig> OpExecutionStrategy<'a, Evm, N, ChainSpec, EvmConfig>
impl<'a, E, N, ChainSpec> OpExecutionStrategy<'a, E, N, ChainSpec>
where
E: Evm,
N: NodePrimitives,
ChainSpec: OpHardforks,
EvmConfig: ConfigureEvm,
{
/// Creates a new [`OpExecutionStrategy`]
pub fn new(
evm: Evm,
evm: E,
input: impl Into<OpBlockExecutionInput>,
chain_spec: ChainSpec,
evm_config: EvmConfig,
receipt_builder: &'a dyn OpReceiptBuilder<
N::SignedTx,
HaltReasonFor<EvmConfig>,
Receipt = N::Receipt,
>,
receipt_builder: &'a dyn OpReceiptBuilder<N::SignedTx, E::HaltReason, Receipt = N::Receipt>,
) -> Self {
let input = input.into();
Self {
@ -181,7 +173,6 @@ where
evm,
system_caller: SystemCaller::new(chain_spec.clone()),
chain_spec,
evm_config,
receipt_builder,
receipts: Vec::new(),
gas_used: 0,
@ -190,14 +181,12 @@ where
}
}
impl<'db, DB, E, N, ChainSpec, EvmConfig> BlockExecutionStrategy
for OpExecutionStrategy<'_, E, N, ChainSpec, EvmConfig>
impl<'db, DB, E, N, ChainSpec> BlockExecutionStrategy for OpExecutionStrategy<'_, E, N, ChainSpec>
where
DB: Database + 'db,
E: Evm<DB = &'db mut State<DB>, Tx = EvmConfig::TxEnv, HaltReason = HaltReasonFor<EvmConfig>>,
E: Evm<DB = &'db mut State<DB>, Tx: FromRecoveredTx<N::SignedTx>>,
N: NodePrimitives<SignedTx: OpTransaction, Receipt: DepositReceipt>,
ChainSpec: OpHardforks,
EvmConfig: ConfigureEvmFor<N>,
{
type Primitives = N;
type Error = BlockExecutionError;
@ -248,12 +237,11 @@ where
.transpose()
.map_err(|_| OpBlockExecutionError::AccountLoadFailed(tx.signer()))?;
let tx_env = self.evm_config.tx_env(&tx);
let hash = tx.tx_hash();
// Execute transaction.
let result_and_state =
self.evm.transact(tx_env).map_err(move |err| BlockExecutionError::evm(err, *hash))?;
self.evm.transact(&tx).map_err(move |err| BlockExecutionError::evm(err, *hash))?;
trace!(
target: "evm",

View File

@ -11,10 +11,10 @@
extern crate alloc;
use alloc::sync::Arc;
use alloy_consensus::{transaction::Recovered, BlockHeader, Header};
use alloy_consensus::{BlockHeader, Header};
use alloy_op_evm::OpEvmFactory;
use alloy_primitives::U256;
use core::{borrow::Borrow, fmt::Debug};
use core::fmt::Debug;
use op_alloy_consensus::EIP1559ParamError;
use reth_chainspec::EthChainSpec;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv, EvmEnv, NextBlockEnvAttributes};
@ -22,7 +22,6 @@ use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_consensus::next_block_base_fee;
use reth_optimism_forks::OpHardforks;
use reth_optimism_primitives::OpTransactionSigned;
use reth_primitives_traits::FillTxEnv;
use revm::{
context::{BlockEnv, CfgEnv, TxEnv},
context_interface::block::BlobExcessGasAndPrice,
@ -74,17 +73,6 @@ impl<ChainSpec: EthChainSpec + OpHardforks + 'static> ConfigureEvmEnv for OpEvmC
type TxEnv = OpTransaction<TxEnv>;
type Spec = OpSpecId;
fn tx_env<T: Borrow<Self::Transaction>>(
&self,
transaction: impl Borrow<Recovered<T>>,
) -> Self::TxEnv {
let transaction = transaction.borrow();
let mut tx_env = Default::default();
transaction.tx().borrow().fill_tx_env(&mut tx_env, transaction.signer());
tx_env
}
fn evm_env(&self, header: &Self::Header) -> EvmEnv<Self::Spec> {
let spec = config::revm_spec(self.chain_spec(), header);

View File

@ -369,7 +369,6 @@ impl<Txs> OpBuilder<'_, Txs> {
beneficiary: ctx.evm_env.block_env.beneficiary,
},
&ctx.chain_spec,
&ctx.evm_config,
ctx.receipt_builder.as_ref(),
);

View File

@ -18,6 +18,7 @@ reth-codecs = { workspace = true, optional = true, features = ["op"] }
reth-zstd-compressors = { workspace = true, optional = true }
# ethereum
alloy-evm.workspace = true
alloy-primitives.workspace = true
alloy-consensus.workspace = true
alloy-rlp.workspace = true
@ -75,6 +76,7 @@ std = [
"alloy-serde?/std",
"revm-context/std",
"serde_json/std",
"alloy-evm/std",
]
alloy-compat = ["dep:alloy-network", "dep:alloy-serde", "dep:alloy-rpc-types-eth", "op-alloy-consensus/alloy-compat"]
reth-codec = [

View File

@ -10,6 +10,7 @@ use alloy_eips::{
eip2930::AccessList,
eip7702::SignedAuthorization,
};
use alloy_evm::FromRecoveredTx;
use alloy_primitives::{
keccak256, Address, Bytes, PrimitiveSignature as Signature, TxHash, TxKind, Uint, B256,
};
@ -29,7 +30,7 @@ use reth_primitives_traits::{
crypto::secp256k1::{recover_signer, recover_signer_unchecked},
sync::OnceLock,
transaction::{error::TransactionConversionError, signed::RecoveryError},
FillTxEnv, InMemorySize, SignedTransaction,
InMemorySize, SignedTransaction,
};
use revm_context::TxEnv;
use revm_optimism::transaction::deposit::DepositTransactionParts;
@ -213,11 +214,11 @@ impl OpTransaction for OpTransactionSigned {
}
}
impl FillTxEnv<revm_optimism::OpTransaction<TxEnv>> for OpTransactionSigned {
fn fill_tx_env(&self, tx_env: &mut revm_optimism::OpTransaction<TxEnv>, sender: Address) {
let envelope = self.encoded_2718();
impl FromRecoveredTx<OpTransactionSigned> for revm_optimism::OpTransaction<TxEnv> {
fn from_recovered_tx(tx: &OpTransactionSigned, sender: Address) -> Self {
let envelope = tx.encoded_2718();
let base = match &self.transaction {
let base = match &tx.transaction {
OpTypedTransaction::Legacy(tx) => TxEnv {
gas_limit: tx.gas_limit,
gas_price: tx.gas_price,
@ -300,10 +301,10 @@ impl FillTxEnv<revm_optimism::OpTransaction<TxEnv>> for OpTransactionSigned {
},
};
*tx_env = revm_optimism::OpTransaction {
Self {
base,
enveloped_tx: Some(envelope.into()),
deposit: if let OpTypedTransaction::Deposit(tx) = &self.transaction {
deposit: if let OpTypedTransaction::Deposit(tx) = &tx.transaction {
DepositTransactionParts {
is_system_transaction: tx.is_system_transaction,
source_hash: tx.source_hash,
@ -312,7 +313,7 @@ impl FillTxEnv<revm_optimism::OpTransaction<TxEnv>> for OpTransactionSigned {
} else {
Default::default()
},
};
}
}
}