From faac78c803a16987281ba7b911348359cd39f98d Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Wed, 25 Dec 2024 01:20:53 +0400 Subject: [PATCH 1/2] chore: deduplicate FillTxEnv traits --- crates/primitives/src/transaction/compat.rs | 124 -------------------- crates/primitives/src/transaction/mod.rs | 37 +++++- 2 files changed, 34 insertions(+), 127 deletions(-) delete mode 100644 crates/primitives/src/transaction/compat.rs diff --git a/crates/primitives/src/transaction/compat.rs b/crates/primitives/src/transaction/compat.rs deleted file mode 100644 index 883c89c45f51..000000000000 --- a/crates/primitives/src/transaction/compat.rs +++ /dev/null @@ -1,124 +0,0 @@ -use crate::{Transaction, TransactionSigned}; -use alloy_primitives::{Address, TxKind, U256}; -#[cfg(feature = "optimism")] -use op_alloy_consensus::DepositTransaction; -use revm_primitives::{AuthorizationList, 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 = alloy_eips::eip2718::Encodable2718::encoded_2718(self); - - 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(); - tx_env.authorization_list = None; - } - 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.clone_from(&tx.access_list.0); - tx_env.blob_hashes.clear(); - tx_env.max_fee_per_blob_gas.take(); - tx_env.authorization_list = None; - } - 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.clone_from(&tx.access_list.0); - tx_env.blob_hashes.clear(); - tx_env.max_fee_per_blob_gas.take(); - tx_env.authorization_list = None; - } - 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.clone_from(&tx.access_list.0); - 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)); - tx_env.authorization_list = None; - } - Transaction::Eip7702(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.into(); - 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.clone_from(&tx.access_list.0); - tx_env.blob_hashes.clear(); - tx_env.max_fee_per_blob_gas.take(); - tx_env.authorization_list = - Some(AuthorizationList::Signed(tx.authorization_list.clone())); - } - #[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.authorization_list = 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()), - } - } - } -} diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index c729dda2146e..751d9d41d576 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -17,7 +17,7 @@ use alloy_primitives::{ keccak256, Address, Bytes, ChainId, PrimitiveSignature as Signature, TxHash, TxKind, B256, U256, }; use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header}; -pub use compat::FillTxEnv; +pub use reth_primitives_traits::FillTxEnv; use core::hash::{Hash, Hasher}; use derive_more::{AsRef, Deref}; pub use meta::TransactionMeta; @@ -50,7 +50,6 @@ pub mod signature; pub mod util; pub(crate) mod access_list; -mod compat; mod meta; mod pooled; mod tx_type; @@ -1028,6 +1027,9 @@ impl SignedTransaction for TransactionSigned { impl reth_primitives_traits::FillTxEnv for TransactionSigned { fn fill_tx_env(&self, tx_env: &mut TxEnv, sender: Address) { + #[cfg(feature = "optimism")] + let envelope = alloy_eips::eip2718::Encodable2718::encoded_2718(self); + tx_env.caller = sender; match self.as_ref() { Transaction::Legacy(tx) => { @@ -1102,7 +1104,36 @@ impl reth_primitives_traits::FillTxEnv for TransactionSigned { Some(AuthorizationList::Signed(tx.authorization_list.clone())); } #[cfg(feature = "optimism")] - Transaction::Deposit(_) => {} + 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.authorization_list = 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()), + } } } } From 32b2633895073a34f1f1e7a9b6bb88fd8278d712 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Wed, 25 Dec 2024 01:23:21 +0400 Subject: [PATCH 2/2] fmt --- crates/primitives/src/transaction/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 751d9d41d576..b41470a3e733 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -17,7 +17,6 @@ use alloy_primitives::{ keccak256, Address, Bytes, ChainId, PrimitiveSignature as Signature, TxHash, TxKind, B256, U256, }; use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header}; -pub use reth_primitives_traits::FillTxEnv; use core::hash::{Hash, Hasher}; use derive_more::{AsRef, Deref}; pub use meta::TransactionMeta; @@ -34,7 +33,7 @@ pub use reth_primitives_traits::{ transaction::error::{ InvalidTransactionError, TransactionConversionError, TryFromRecoveredTransactionError, }, - WithEncoded, + FillTxEnv, WithEncoded, }; use reth_primitives_traits::{InMemorySize, SignedTransaction}; use revm_primitives::{AuthorizationList, TxEnv};