chore: Move FillTxEnv::fill_tx_env into SignedTransaction trait and implement in TransactionSigned (#12186)

Co-authored-by: garwah <garwah@garwah>
Co-authored-by: Emilia Hane <elsaemiliaevahane@gmail.com>
This commit is contained in:
garwah
2024-11-04 18:01:40 +11:00
committed by GitHub
parent 21d911abb2
commit bb03578eed
2 changed files with 121 additions and 0 deletions

View File

@ -6,6 +6,7 @@ use core::hash::Hash;
use alloy_consensus::Transaction;
use alloy_eips::eip2718::{Decodable2718, Encodable2718};
use alloy_primitives::{keccak256, Address, Signature, TxHash, B256};
use revm_primitives::TxEnv;
/// A signed transaction.
pub trait SignedTransaction:
@ -64,4 +65,7 @@ pub trait SignedTransaction:
fn recalculate_hash(&self) -> B256 {
keccak256(self.encoded_2718())
}
/// Fills [`TxEnv`] with an [`Address`] and transaction.
fn fill_tx_env(&self, tx_env: &mut TxEnv, sender: Address);
}

View File

@ -64,6 +64,8 @@ use tx_type::{
};
use alloc::vec::Vec;
use reth_primitives_traits::SignedTransaction;
use revm_primitives::{AuthorizationList, TxEnv};
/// Either a transaction hash or number.
pub type TxHashOrNumber = BlockHashOrNumber;
@ -1123,6 +1125,11 @@ impl TransactionSigned {
&self.signature
}
/// Transaction
pub const fn transaction(&self) -> &Transaction {
&self.transaction
}
/// Transaction hash. Used to identify transaction.
pub const fn hash(&self) -> TxHash {
self.hash
@ -1398,6 +1405,116 @@ impl alloy_consensus::Transaction for TransactionSigned {
}
}
impl SignedTransaction for TransactionSigned {
type Transaction = Transaction;
fn tx_hash(&self) -> &TxHash {
Self::hash_ref(self)
}
fn transaction(&self) -> &Self::Transaction {
Self::transaction(self)
}
fn signature(&self) -> &Signature {
Self::signature(self)
}
fn recover_signer(&self) -> Option<Address> {
Self::recover_signer(self)
}
fn recover_signer_unchecked(&self) -> Option<Address> {
Self::recover_signer_unchecked(self)
}
fn from_transaction_and_signature(
transaction: Self::Transaction,
signature: Signature,
) -> Self {
Self::from_transaction_and_signature(transaction, signature)
}
fn fill_tx_env(&self, tx_env: &mut TxEnv, sender: Address) {
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(_) => {}
}
}
}
impl From<TransactionSignedEcRecovered> for TransactionSigned {
fn from(recovered: TransactionSignedEcRecovered) -> Self {
recovered.signed_transaction