From fd4ca7d6d600ff28beb8c98765c0693fabd0f63f Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sat, 1 Feb 2025 15:29:41 +0100 Subject: [PATCH] feat: simplify envelope conversion (#14146) --- crates/ethereum/primitives/src/transaction.rs | 13 ++++++ .../primitives/src/transaction/signed.rs | 13 ++++++ crates/optimism/rpc/src/eth/transaction.rs | 46 ++++++++----------- crates/rpc/rpc/src/eth/helpers/types.rs | 26 ++--------- 4 files changed, 48 insertions(+), 50 deletions(-) diff --git a/crates/ethereum/primitives/src/transaction.rs b/crates/ethereum/primitives/src/transaction.rs index 973f516a85..159be023b4 100644 --- a/crates/ethereum/primitives/src/transaction.rs +++ b/crates/ethereum/primitives/src/transaction.rs @@ -544,6 +544,19 @@ impl From for TransactionSigned { } } +impl From for TxEnvelope { + fn from(value: TransactionSigned) -> Self { + let (tx, signature, hash) = value.into_parts(); + match tx { + Transaction::Legacy(tx) => Signed::new_unchecked(tx, signature, hash).into(), + Transaction::Eip2930(tx) => Signed::new_unchecked(tx, signature, hash).into(), + Transaction::Eip1559(tx) => Signed::new_unchecked(tx, signature, hash).into(), + Transaction::Eip4844(tx) => Signed::new_unchecked(tx, signature, hash).into(), + Transaction::Eip7702(tx) => Signed::new_unchecked(tx, signature, hash).into(), + } + } +} + impl From for Signed { fn from(value: TransactionSigned) -> Self { let (tx, sig, hash) = value.into_parts(); diff --git a/crates/optimism/primitives/src/transaction/signed.rs b/crates/optimism/primitives/src/transaction/signed.rs index 48dfed290d..513dd6660f 100644 --- a/crates/optimism/primitives/src/transaction/signed.rs +++ b/crates/optimism/primitives/src/transaction/signed.rs @@ -171,6 +171,19 @@ impl From for OpTransactionSigned { } } +impl From for OpTxEnvelope { + fn from(value: OpTransactionSigned) -> Self { + let (tx, signature, hash) = value.into_parts(); + match tx { + OpTypedTransaction::Legacy(tx) => Signed::new_unchecked(tx, signature, hash).into(), + OpTypedTransaction::Eip2930(tx) => Signed::new_unchecked(tx, signature, hash).into(), + OpTypedTransaction::Eip1559(tx) => Signed::new_unchecked(tx, signature, hash).into(), + OpTypedTransaction::Deposit(tx) => Sealed::new_unchecked(tx, hash).into(), + OpTypedTransaction::Eip7702(tx) => Signed::new_unchecked(tx, signature, hash).into(), + } + } +} + impl From for Signed { fn from(value: OpTransactionSigned) -> Self { let (tx, sig, hash) = value.into_parts(); diff --git a/crates/optimism/rpc/src/eth/transaction.rs b/crates/optimism/rpc/src/eth/transaction.rs index 96ae2f9b07..60a0295417 100644 --- a/crates/optimism/rpc/src/eth/transaction.rs +++ b/crates/optimism/rpc/src/eth/transaction.rs @@ -1,14 +1,13 @@ //! Loads and formats OP transaction RPC response. -use alloy_consensus::{Signed, Transaction as _}; +use alloy_consensus::Transaction as _; use alloy_primitives::{Bytes, PrimitiveSignature as Signature, Sealable, Sealed, B256}; use alloy_rpc_types_eth::TransactionInfo; -use op_alloy_consensus::{OpTxEnvelope, OpTypedTransaction}; +use op_alloy_consensus::OpTxEnvelope; use op_alloy_rpc_types::{OpTransactionRequest, Transaction}; use reth_node_api::FullNodeComponents; use reth_optimism_primitives::{OpReceipt, OpTransactionSigned}; use reth_primitives::Recovered; -use reth_primitives_traits::transaction::signed::SignedTransaction; use reth_provider::{ BlockReader, BlockReaderIdExt, ProviderTx, ReceiptProvider, TransactionsProvider, }; @@ -87,39 +86,32 @@ where tx: Recovered, tx_info: TransactionInfo, ) -> Result { - let from = tx.signer(); - let hash = *tx.tx_hash(); - let (transaction, signature) = tx.into_tx().split(); + let (tx, from) = tx.into_parts(); let mut deposit_receipt_version = None; let mut deposit_nonce = None; - let inner = match transaction { - OpTypedTransaction::Legacy(tx) => Signed::new_unchecked(tx, signature, hash).into(), - OpTypedTransaction::Eip2930(tx) => Signed::new_unchecked(tx, signature, hash).into(), - OpTypedTransaction::Eip1559(tx) => Signed::new_unchecked(tx, signature, hash).into(), - OpTypedTransaction::Eip7702(tx) => Signed::new_unchecked(tx, signature, hash).into(), - OpTypedTransaction::Deposit(tx) => { - self.inner - .eth_api - .provider() - .receipt_by_hash(hash) - .map_err(Self::Error::from_eth_err)? - .inspect(|receipt| { - if let OpReceipt::Deposit(receipt) = receipt { - deposit_receipt_version = receipt.deposit_receipt_version; - deposit_nonce = receipt.deposit_nonce; - } - }); + let inner: OpTxEnvelope = tx.into(); - OpTxEnvelope::Deposit(tx.seal_unchecked(hash)) - } - }; + if inner.is_deposit() { + // for depost tx we need to fetch the receipt + self.inner + .eth_api + .provider() + .receipt_by_hash(inner.tx_hash()) + .map_err(Self::Error::from_eth_err)? + .inspect(|receipt| { + if let OpReceipt::Deposit(receipt) = receipt { + deposit_receipt_version = receipt.deposit_receipt_version; + deposit_nonce = receipt.deposit_nonce; + } + }); + } let TransactionInfo { block_hash, block_number, index: transaction_index, base_fee, .. } = tx_info; - let effective_gas_price = if matches!(inner, OpTxEnvelope::Deposit(_)) { + let effective_gas_price = if inner.is_deposit() { // For deposits, we must always set the `gasPrice` field to 0 in rpc // deposit tx don't have a gas price field, but serde of `Transaction` will take care of // it diff --git a/crates/rpc/rpc/src/eth/helpers/types.rs b/crates/rpc/rpc/src/eth/helpers/types.rs index cea2ce202f..3d734bc40e 100644 --- a/crates/rpc/rpc/src/eth/helpers/types.rs +++ b/crates/rpc/rpc/src/eth/helpers/types.rs @@ -1,12 +1,11 @@ //! L1 `eth` API types. -use alloy_consensus::{Signed, Transaction as _, TxEip4844Variant, TxEnvelope}; +use alloy_consensus::{Transaction as _, TxEip4844Variant, TxEnvelope}; use alloy_network::{Ethereum, Network}; use alloy_primitives::PrimitiveSignature as Signature; use alloy_rpc_types::TransactionRequest; use alloy_rpc_types_eth::{Transaction, TransactionInfo}; use reth_primitives::{Recovered, TransactionSigned}; -use reth_primitives_traits::SignedTransaction; use reth_rpc_eth_api::EthApiTypes; use reth_rpc_eth_types::EthApiError; use reth_rpc_types_compat::TransactionCompat; @@ -43,27 +42,8 @@ where tx: Recovered, tx_info: TransactionInfo, ) -> Result { - let from = tx.signer(); - let hash = *tx.tx_hash(); - let signature = *tx.signature(); - - let inner: TxEnvelope = match tx.into_tx().into_transaction() { - reth_primitives::Transaction::Legacy(tx) => { - Signed::new_unchecked(tx, signature, hash).into() - } - reth_primitives::Transaction::Eip2930(tx) => { - Signed::new_unchecked(tx, signature, hash).into() - } - reth_primitives::Transaction::Eip1559(tx) => { - Signed::new_unchecked(tx, signature, hash).into() - } - reth_primitives::Transaction::Eip4844(tx) => { - Signed::new_unchecked(tx, signature, hash).into() - } - reth_primitives::Transaction::Eip7702(tx) => { - Signed::new_unchecked(tx, signature, hash).into() - } - }; + let (tx, from) = tx.into_parts(); + let inner: TxEnvelope = tx.into(); let TransactionInfo { block_hash, block_number, index: transaction_index, base_fee, ..