chore: remove usage of tx_env_with_recovered (#9222)

This commit is contained in:
joshieDo
2024-07-01 17:12:36 +02:00
committed by GitHub
parent d317b4a0fb
commit cf8a9163af
10 changed files with 158 additions and 184 deletions

View File

@ -14,9 +14,9 @@ extern crate alloc;
use reth_chainspec::{ChainSpec, Head}; use reth_chainspec::{ChainSpec, Head};
use reth_evm::{ConfigureEvm, ConfigureEvmEnv}; use reth_evm::{ConfigureEvm, ConfigureEvmEnv};
use reth_primitives::{Header, U256}; use reth_primitives::{transaction::FillTxEnv, Address, Header, TransactionSigned, U256};
use reth_revm::{Database, EvmBuilder}; use reth_revm::{Database, EvmBuilder};
use revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg}; use revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv};
mod config; mod config;
pub use config::{revm_spec, revm_spec_by_timestamp_after_merge}; pub use config::{revm_spec, revm_spec_by_timestamp_after_merge};
@ -57,6 +57,10 @@ impl ConfigureEvmEnv for EthEvmConfig {
cfg_env.handler_cfg.spec_id = spec_id; cfg_env.handler_cfg.spec_id = spec_id;
} }
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
transaction.fill_tx_env(tx_env, sender);
}
} }
impl ConfigureEvm for EthEvmConfig { impl ConfigureEvm for EthEvmConfig {

View File

@ -27,7 +27,6 @@ use reth_primitives::{
}, },
eip4844::calculate_excess_blob_gas, eip4844::calculate_excess_blob_gas,
proofs::{self, calculate_requests_root}, proofs::{self, calculate_requests_root},
revm::env::tx_env_with_recovered,
Block, EthereumHardforks, Header, IntoRecoveredTransaction, Receipt, EMPTY_OMMER_ROOT_HASH, Block, EthereumHardforks, Header, IntoRecoveredTransaction, Receipt, EMPTY_OMMER_ROOT_HASH,
U256, U256,
}; };
@ -343,7 +342,7 @@ where
let env = EnvWithHandlerCfg::new_with_cfg_env( let env = EnvWithHandlerCfg::new_with_cfg_env(
initialized_cfg.clone(), initialized_cfg.clone(),
initialized_block_env.clone(), initialized_block_env.clone(),
tx_env_with_recovered(&tx), evm_config.tx_env(&tx),
); );
// Configure the environment for the block. // Configure the environment for the block.

View File

@ -16,8 +16,8 @@ use core::ops::Deref;
use reth_chainspec::ChainSpec; use reth_chainspec::ChainSpec;
use reth_primitives::{ use reth_primitives::{
revm::env::{fill_block_env, fill_tx_env}, revm::env::fill_block_env, Address, Header, TransactionSigned, TransactionSignedEcRecovered,
Address, Header, TransactionSigned, TransactionSignedEcRecovered, U256, U256,
}; };
use revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector}; use revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, SpecId, TxEnv}; use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, SpecId, TxEnv};
@ -116,9 +116,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
} }
/// Fill transaction environment from a [`TransactionSigned`] and the given sender address. /// Fill transaction environment from a [`TransactionSigned`] and the given sender address.
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) { fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address);
fill_tx_env(tx_env, transaction, sender)
}
/// Fill [`CfgEnvWithHandlerCfg`] fields according to the chain spec and given header /// Fill [`CfgEnvWithHandlerCfg`] fields according to the chain spec and given header
fn fill_cfg_env( fn fill_cfg_env(

View File

@ -12,8 +12,8 @@
use reth_chainspec::ChainSpec; use reth_chainspec::ChainSpec;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv}; use reth_evm::{ConfigureEvm, ConfigureEvmEnv};
use reth_primitives::{ use reth_primitives::{
revm::env::fill_op_tx_env,
revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv}, revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv},
transaction::FillTxEnv,
Address, Head, Header, TransactionSigned, U256, Address, Head, Header, TransactionSigned, U256,
}; };
use reth_revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector}; use reth_revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector};
@ -35,9 +35,7 @@ pub struct OptimismEvmConfig;
impl ConfigureEvmEnv for OptimismEvmConfig { impl ConfigureEvmEnv for OptimismEvmConfig {
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) { fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
let mut buf = Vec::with_capacity(transaction.length_without_header()); transaction.fill_tx_env(tx_env, sender);
transaction.encode_enveloped(&mut buf);
fill_op_tx_env(tx_env, transaction, sender, buf.into());
} }
fn fill_cfg_env( fn fill_cfg_env(

View File

@ -12,9 +12,7 @@ use reth_payload_builder::error::PayloadBuilderError;
use reth_primitives::{ use reth_primitives::{
constants::{BEACON_NONCE, EMPTY_RECEIPTS, EMPTY_TRANSACTIONS}, constants::{BEACON_NONCE, EMPTY_RECEIPTS, EMPTY_TRANSACTIONS},
eip4844::calculate_excess_blob_gas, eip4844::calculate_excess_blob_gas,
proofs, proofs, Block, Header, IntoRecoveredTransaction, Receipt, TxType, EMPTY_OMMER_ROOT_HASH, U256,
revm::env::tx_env_with_recovered,
Block, Header, IntoRecoveredTransaction, Receipt, TxType, EMPTY_OMMER_ROOT_HASH, U256,
}; };
use reth_provider::StateProviderFactory; use reth_provider::StateProviderFactory;
use reth_revm::database::StateProviderDatabase; use reth_revm::database::StateProviderDatabase;
@ -324,7 +322,7 @@ where
} }
// Convert the transaction to a [TransactionSignedEcRecovered]. This is // Convert the transaction to a [TransactionSignedEcRecovered]. This is
// purely for the purposes of utilizing the [tx_env_with_recovered] function. // purely for the purposes of utilizing the `evm_config.tx_env`` function.
// Deposit transactions do not have signatures, so if the tx is a deposit, this // Deposit transactions do not have signatures, so if the tx is a deposit, this
// will just pull in its `from` address. // will just pull in its `from` address.
let sequencer_tx = sequencer_tx.clone().try_into_ecrecovered().map_err(|_| { let sequencer_tx = sequencer_tx.clone().try_into_ecrecovered().map_err(|_| {
@ -351,7 +349,7 @@ where
let env = EnvWithHandlerCfg::new_with_cfg_env( let env = EnvWithHandlerCfg::new_with_cfg_env(
initialized_cfg.clone(), initialized_cfg.clone(),
initialized_block_env.clone(), initialized_block_env.clone(),
tx_env_with_recovered(&sequencer_tx), evm_config.tx_env(&sequencer_tx),
); );
let mut evm = evm_config.evm_with_env(&mut db, env); let mut evm = evm_config.evm_with_env(&mut db, env);
@ -430,7 +428,7 @@ where
let env = EnvWithHandlerCfg::new_with_cfg_env( let env = EnvWithHandlerCfg::new_with_cfg_env(
initialized_cfg.clone(), initialized_cfg.clone(),
initialized_block_env.clone(), initialized_block_env.clone(),
tx_env_with_recovered(&tx), evm_config.tx_env(&tx),
); );
// Configure the environment for the block. // Configure the environment for the block.

View File

@ -1,7 +1,7 @@
use crate::{ use crate::{
recover_signer_unchecked, recover_signer_unchecked,
revm_primitives::{BlockEnv, Env, TxEnv}, revm_primitives::{BlockEnv, Env, TxEnv},
Address, Bytes, Header, Transaction, TransactionSignedEcRecovered, TxKind, B256, U256, Address, Bytes, Header, TxKind, B256, U256,
}; };
use reth_chainspec::{Chain, ChainSpec}; use reth_chainspec::{Chain, ChainSpec};
@ -107,28 +107,6 @@ pub fn recover_header_signer(header: &Header) -> Result<Address, CliqueSignerRec
.map_err(CliqueSignerRecoveryError::InvalidSignature) .map_err(CliqueSignerRecoveryError::InvalidSignature)
} }
/// Returns a new [`TxEnv`] filled with the transaction's data.
pub fn tx_env_with_recovered(transaction: &TransactionSignedEcRecovered) -> TxEnv {
let mut tx_env = TxEnv::default();
#[cfg(not(feature = "optimism"))]
fill_tx_env(&mut tx_env, transaction.as_ref(), transaction.signer());
#[cfg(feature = "optimism")]
{
let mut envelope_buf = Vec::with_capacity(transaction.length_without_header());
transaction.encode_enveloped(&mut envelope_buf);
fill_op_tx_env(
&mut tx_env,
transaction.as_ref(),
transaction.signer(),
envelope_buf.into(),
);
}
tx_env
}
/// Fill transaction environment with the EIP-4788 system contract message data. /// Fill transaction environment with the EIP-4788 system contract message data.
/// ///
/// This requirements for the beacon root contract call defined by /// This requirements for the beacon root contract call defined by
@ -218,144 +196,6 @@ fn fill_tx_env_with_system_contract_call(
env.block.basefee = U256::ZERO; env.block.basefee = U256::ZERO;
} }
/// Fill transaction environment from [`TransactionSignedEcRecovered`].
#[cfg(not(feature = "optimism"))]
pub fn fill_tx_env_with_recovered(tx_env: &mut TxEnv, transaction: &TransactionSignedEcRecovered) {
fill_tx_env(tx_env, transaction.as_ref(), transaction.signer());
}
/// Fill transaction environment from [`TransactionSignedEcRecovered`] and the given envelope.
#[cfg(feature = "optimism")]
pub fn fill_tx_env_with_recovered(
tx_env: &mut TxEnv,
transaction: &TransactionSignedEcRecovered,
envelope: Bytes,
) {
fill_op_tx_env(tx_env, transaction.as_ref(), transaction.signer(), envelope);
}
/// Fill transaction environment from a [Transaction] and the given sender address.
pub fn fill_tx_env<T>(tx_env: &mut TxEnv, transaction: T, sender: Address)
where
T: AsRef<Transaction>,
{
tx_env.caller = sender;
match transaction.as_ref() {
Transaction::Legacy(tx) => {
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::from(tx.gas_price);
tx_env.gas_priority_fee = None;
tx_env.transact_to = tx.to;
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = tx.chain_id;
tx_env.nonce = Some(tx.nonce);
tx_env.access_list.clear();
tx_env.blob_hashes.clear();
tx_env.max_fee_per_blob_gas.take();
}
Transaction::Eip2930(tx) => {
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::from(tx.gas_price);
tx_env.gas_priority_fee = None;
tx_env.transact_to = tx.to;
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = Some(tx.chain_id);
tx_env.nonce = Some(tx.nonce);
tx_env.access_list = tx
.access_list
.iter()
.map(|l| {
(l.address, l.storage_keys.iter().map(|k| U256::from_be_bytes(k.0)).collect())
})
.collect();
tx_env.blob_hashes.clear();
tx_env.max_fee_per_blob_gas.take();
}
Transaction::Eip1559(tx) => {
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::from(tx.max_fee_per_gas);
tx_env.gas_priority_fee = Some(U256::from(tx.max_priority_fee_per_gas));
tx_env.transact_to = tx.to;
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = Some(tx.chain_id);
tx_env.nonce = Some(tx.nonce);
tx_env.access_list = tx
.access_list
.iter()
.map(|l| {
(l.address, l.storage_keys.iter().map(|k| U256::from_be_bytes(k.0)).collect())
})
.collect();
tx_env.blob_hashes.clear();
tx_env.max_fee_per_blob_gas.take();
}
Transaction::Eip4844(tx) => {
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::from(tx.max_fee_per_gas);
tx_env.gas_priority_fee = Some(U256::from(tx.max_priority_fee_per_gas));
tx_env.transact_to = TxKind::Call(tx.to);
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = Some(tx.chain_id);
tx_env.nonce = Some(tx.nonce);
tx_env.access_list = tx
.access_list
.iter()
.map(|l| {
(l.address, l.storage_keys.iter().map(|k| U256::from_be_bytes(k.0)).collect())
})
.collect();
tx_env.blob_hashes.clone_from(&tx.blob_versioned_hashes);
tx_env.max_fee_per_blob_gas = Some(U256::from(tx.max_fee_per_blob_gas));
}
#[cfg(feature = "optimism")]
Transaction::Deposit(tx) => {
tx_env.access_list.clear();
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::ZERO;
tx_env.gas_priority_fee = None;
tx_env.transact_to = tx.to;
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = None;
tx_env.nonce = None;
}
}
}
/// Fill transaction environment from a [Transaction], envelope, and the given sender address.
#[cfg(feature = "optimism")]
#[inline(always)]
pub fn fill_op_tx_env<T: AsRef<Transaction>>(
tx_env: &mut TxEnv,
transaction: T,
sender: Address,
envelope: Bytes,
) {
fill_tx_env(tx_env, &transaction, sender);
match transaction.as_ref() {
Transaction::Deposit(tx) => {
tx_env.optimism = OptimismFields {
source_hash: Some(tx.source_hash),
mint: tx.mint,
is_system_transaction: Some(tx.is_system_transaction),
enveloped_tx: Some(envelope),
};
}
_ => {
tx_env.optimism = OptimismFields {
source_hash: None,
mint: None,
is_system_transaction: Some(false),
enveloped_tx: Some(envelope),
}
}
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@ -0,0 +1,131 @@
use crate::{Address, Transaction, TransactionSigned, TxKind, U256};
use revm_primitives::TxEnv;
/// Implements behaviour to fill a [`TxEnv`] from another transaction.
pub trait FillTxEnv {
/// Fills [`TxEnv`] with an [`Address`] and transaction.
fn fill_tx_env(&self, tx_env: &mut TxEnv, sender: Address);
}
impl FillTxEnv for TransactionSigned {
fn fill_tx_env(&self, tx_env: &mut TxEnv, sender: Address) {
#[cfg(feature = "optimism")]
let envelope = {
let mut envelope = Vec::with_capacity(self.length_without_header());
self.encode_enveloped(&mut envelope);
envelope
};
tx_env.caller = sender;
match self.as_ref() {
Transaction::Legacy(tx) => {
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::from(tx.gas_price);
tx_env.gas_priority_fee = None;
tx_env.transact_to = tx.to;
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = tx.chain_id;
tx_env.nonce = Some(tx.nonce);
tx_env.access_list.clear();
tx_env.blob_hashes.clear();
tx_env.max_fee_per_blob_gas.take();
}
Transaction::Eip2930(tx) => {
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::from(tx.gas_price);
tx_env.gas_priority_fee = None;
tx_env.transact_to = tx.to;
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = Some(tx.chain_id);
tx_env.nonce = Some(tx.nonce);
tx_env.access_list = tx
.access_list
.iter()
.map(|l| {
(
l.address,
l.storage_keys.iter().map(|k| U256::from_be_bytes(k.0)).collect(),
)
})
.collect();
tx_env.blob_hashes.clear();
tx_env.max_fee_per_blob_gas.take();
}
Transaction::Eip1559(tx) => {
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::from(tx.max_fee_per_gas);
tx_env.gas_priority_fee = Some(U256::from(tx.max_priority_fee_per_gas));
tx_env.transact_to = tx.to;
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = Some(tx.chain_id);
tx_env.nonce = Some(tx.nonce);
tx_env.access_list = tx
.access_list
.iter()
.map(|l| {
(
l.address,
l.storage_keys.iter().map(|k| U256::from_be_bytes(k.0)).collect(),
)
})
.collect();
tx_env.blob_hashes.clear();
tx_env.max_fee_per_blob_gas.take();
}
Transaction::Eip4844(tx) => {
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::from(tx.max_fee_per_gas);
tx_env.gas_priority_fee = Some(U256::from(tx.max_priority_fee_per_gas));
tx_env.transact_to = TxKind::Call(tx.to);
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = Some(tx.chain_id);
tx_env.nonce = Some(tx.nonce);
tx_env.access_list = tx
.access_list
.iter()
.map(|l| {
(
l.address,
l.storage_keys.iter().map(|k| U256::from_be_bytes(k.0)).collect(),
)
})
.collect();
tx_env.blob_hashes.clone_from(&tx.blob_versioned_hashes);
tx_env.max_fee_per_blob_gas = Some(U256::from(tx.max_fee_per_blob_gas));
}
#[cfg(feature = "optimism")]
Transaction::Deposit(tx) => {
tx_env.access_list.clear();
tx_env.gas_limit = tx.gas_limit;
tx_env.gas_price = U256::ZERO;
tx_env.gas_priority_fee = None;
tx_env.transact_to = tx.to;
tx_env.value = tx.value;
tx_env.data = tx.input.clone();
tx_env.chain_id = None;
tx_env.nonce = None;
tx_env.optimism = revm_primitives::OptimismFields {
source_hash: Some(tx.source_hash),
mint: tx.mint,
is_system_transaction: Some(tx.is_system_transaction),
enveloped_tx: Some(envelope.into()),
};
return;
}
}
#[cfg(feature = "optimism")]
if !self.is_deposit() {
tx_env.optimism = revm_primitives::OptimismFields {
source_hash: None,
mint: None,
is_system_transaction: Some(false),
enveloped_tx: Some(envelope.into()),
}
}
}
}

View File

@ -32,6 +32,7 @@ pub use sidecar::generate_blob_sidecar;
pub use sidecar::BlobTransactionValidationError; pub use sidecar::BlobTransactionValidationError;
pub use sidecar::{BlobTransaction, BlobTransactionSidecar}; pub use sidecar::{BlobTransaction, BlobTransactionSidecar};
pub use compat::FillTxEnv;
pub use signature::{extract_chain_id, Signature}; pub use signature::{extract_chain_id, Signature};
pub use tx_type::{ pub use tx_type::{
TxType, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, LEGACY_TX_TYPE_ID, TxType, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, LEGACY_TX_TYPE_ID,
@ -39,6 +40,7 @@ pub use tx_type::{
pub use variant::TransactionSignedVariant; pub use variant::TransactionSignedVariant;
mod access_list; mod access_list;
mod compat;
mod eip1559; mod eip1559;
mod eip2930; mod eip2930;
mod eip4844; mod eip4844;

View File

@ -19,12 +19,13 @@ use reth::{
tasks::TaskManager, tasks::TaskManager,
}; };
use reth_chainspec::{Chain, ChainSpec, Head}; use reth_chainspec::{Chain, ChainSpec, Head};
use reth_evm_ethereum::EthEvmConfig;
use reth_node_api::{ConfigureEvm, ConfigureEvmEnv, FullNodeTypes}; use reth_node_api::{ConfigureEvm, ConfigureEvmEnv, FullNodeTypes};
use reth_node_core::{args::RpcServerArgs, node_config::NodeConfig}; use reth_node_core::{args::RpcServerArgs, node_config::NodeConfig};
use reth_node_ethereum::{EthExecutorProvider, EthereumNode}; use reth_node_ethereum::{EthExecutorProvider, EthereumNode};
use reth_primitives::{ use reth_primitives::{
revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg}, revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv},
Header, U256, Address, Header, TransactionSigned, U256,
}; };
use reth_tracing::{RethTracer, Tracer}; use reth_tracing::{RethTracer, Tracer};
use std::sync::Arc; use std::sync::Arc;
@ -88,6 +89,10 @@ impl ConfigureEvmEnv for MyEvmConfig {
cfg_env.handler_cfg.spec_id = spec_id; cfg_env.handler_cfg.spec_id = spec_id;
} }
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
EthEvmConfig::default().fill_tx_env(tx_env, transaction, sender)
}
} }
impl ConfigureEvm for MyEvmConfig { impl ConfigureEvm for MyEvmConfig {

View File

@ -10,7 +10,6 @@ use reth_primitives::{
constants, constants,
eip4844::kzg_to_versioned_hash, eip4844::kzg_to_versioned_hash,
keccak256, keccak256,
revm::env::fill_tx_env,
revm_primitives::{CfgEnvWithHandlerCfg, EVMError, ExecutionResult, ResultAndState}, revm_primitives::{CfgEnvWithHandlerCfg, EVMError, ExecutionResult, ResultAndState},
Address, Block, BlockWithSenders, Bytes, EthereumHardfork, Header, Receipt, TransactionSigned, Address, Block, BlockWithSenders, Bytes, EthereumHardfork, Header, Receipt, TransactionSigned,
TxType, B256, U256, TxType, B256, U256,
@ -217,7 +216,7 @@ fn execute_transactions(
} }
// Execute transaction. // Execute transaction.
// Fill revm structure. // Fill revm structure.
fill_tx_env(evm.tx_mut(), &transaction, sender); EthEvmConfig::default().fill_tx_env(evm.tx_mut(), &transaction, sender);
let ResultAndState { result, state } = match evm.transact() { let ResultAndState { result, state } = match evm.transact() {
Ok(result) => result, Ok(result) => result,