Skip to content

Commit

Permalink
chore: remove usage of tx_env_with_recovered (#9222)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo authored Jul 1, 2024
1 parent d317b4a commit cf8a916
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 184 deletions.
8 changes: 6 additions & 2 deletions crates/ethereum/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ extern crate alloc;

use reth_chainspec::{ChainSpec, Head};
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 revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg};
use revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv};

mod config;
pub use config::{revm_spec, revm_spec_by_timestamp_after_merge};
Expand Down Expand Up @@ -57,6 +57,10 @@ impl ConfigureEvmEnv for EthEvmConfig {

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 {
Expand Down
3 changes: 1 addition & 2 deletions crates/ethereum/payload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use reth_primitives::{
},
eip4844::calculate_excess_blob_gas,
proofs::{self, calculate_requests_root},
revm::env::tx_env_with_recovered,
Block, EthereumHardforks, Header, IntoRecoveredTransaction, Receipt, EMPTY_OMMER_ROOT_HASH,
U256,
};
Expand Down Expand Up @@ -343,7 +342,7 @@ where
let env = EnvWithHandlerCfg::new_with_cfg_env(
initialized_cfg.clone(),
initialized_block_env.clone(),
tx_env_with_recovered(&tx),
evm_config.tx_env(&tx),
);

// Configure the environment for the block.
Expand Down
8 changes: 3 additions & 5 deletions crates/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use core::ops::Deref;

use reth_chainspec::ChainSpec;
use reth_primitives::{
revm::env::{fill_block_env, fill_tx_env},
Address, Header, TransactionSigned, TransactionSignedEcRecovered, U256,
revm::env::fill_block_env, Address, Header, TransactionSigned, TransactionSignedEcRecovered,
U256,
};
use revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector};
use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, SpecId, TxEnv};
Expand Down Expand Up @@ -116,9 +116,7 @@ pub trait ConfigureEvmEnv: Send + Sync + Unpin + Clone + 'static {
}

/// Fill transaction environment from a [`TransactionSigned`] and the given sender address.
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
fill_tx_env(tx_env, transaction, sender)
}
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address);

/// Fill [`CfgEnvWithHandlerCfg`] fields according to the chain spec and given header
fn fill_cfg_env(
Expand Down
6 changes: 2 additions & 4 deletions crates/optimism/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use reth_chainspec::ChainSpec;
use reth_evm::{ConfigureEvm, ConfigureEvmEnv};
use reth_primitives::{
revm::env::fill_op_tx_env,
revm_primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv},
transaction::FillTxEnv,
Address, Head, Header, TransactionSigned, U256,
};
use reth_revm::{inspector_handle_register, Database, Evm, EvmBuilder, GetInspector};
Expand All @@ -35,9 +35,7 @@ pub struct OptimismEvmConfig;

impl ConfigureEvmEnv for OptimismEvmConfig {
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
let mut buf = Vec::with_capacity(transaction.length_without_header());
transaction.encode_enveloped(&mut buf);
fill_op_tx_env(tx_env, transaction, sender, buf.into());
transaction.fill_tx_env(tx_env, sender);
}

fn fill_cfg_env(
Expand Down
10 changes: 4 additions & 6 deletions crates/optimism/payload/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use reth_payload_builder::error::PayloadBuilderError;
use reth_primitives::{
constants::{BEACON_NONCE, EMPTY_RECEIPTS, EMPTY_TRANSACTIONS},
eip4844::calculate_excess_blob_gas,
proofs,
revm::env::tx_env_with_recovered,
Block, Header, IntoRecoveredTransaction, Receipt, TxType, EMPTY_OMMER_ROOT_HASH, U256,
proofs, Block, Header, IntoRecoveredTransaction, Receipt, TxType, EMPTY_OMMER_ROOT_HASH, U256,
};
use reth_provider::StateProviderFactory;
use reth_revm::database::StateProviderDatabase;
Expand Down Expand Up @@ -324,7 +322,7 @@ where
}

// 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
// will just pull in its `from` address.
let sequencer_tx = sequencer_tx.clone().try_into_ecrecovered().map_err(|_| {
Expand All @@ -351,7 +349,7 @@ where
let env = EnvWithHandlerCfg::new_with_cfg_env(
initialized_cfg.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);
Expand Down Expand Up @@ -430,7 +428,7 @@ where
let env = EnvWithHandlerCfg::new_with_cfg_env(
initialized_cfg.clone(),
initialized_block_env.clone(),
tx_env_with_recovered(&tx),
evm_config.tx_env(&tx),
);

// Configure the environment for the block.
Expand Down
162 changes: 1 addition & 161 deletions crates/primitives/src/revm/env.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
recover_signer_unchecked,
revm_primitives::{BlockEnv, Env, TxEnv},
Address, Bytes, Header, Transaction, TransactionSignedEcRecovered, TxKind, B256, U256,
Address, Bytes, Header, TxKind, B256, U256,
};
use reth_chainspec::{Chain, ChainSpec};

Expand Down Expand Up @@ -107,28 +107,6 @@ pub fn recover_header_signer(header: &Header) -> Result<Address, CliqueSignerRec
.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.
///
/// This requirements for the beacon root contract call defined by
Expand Down Expand Up @@ -218,144 +196,6 @@ fn fill_tx_env_with_system_contract_call(
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)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit cf8a916

Please sign in to comment.