Skip to content

Commit

Permalink
feat: simplify envelope conversion (#14146)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Feb 1, 2025
1 parent 6e0fbb4 commit fd4ca7d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 51 deletions.
13 changes: 13 additions & 0 deletions crates/ethereum/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,19 @@ impl From<TxEnvelope> for TransactionSigned {
}
}

impl From<TransactionSigned> 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<TransactionSigned> for Signed<Transaction> {
fn from(value: TransactionSigned) -> Self {
let (tx, sig, hash) = value.into_parts();
Expand Down
13 changes: 13 additions & 0 deletions crates/optimism/primitives/src/transaction/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,19 @@ impl From<OpTxEnvelope> for OpTransactionSigned {
}
}

impl From<OpTransactionSigned> 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<OpTransactionSigned> for Signed<OpTypedTransaction> {
fn from(value: OpTransactionSigned) -> Self {
let (tx, sig, hash) = value.into_parts();
Expand Down
48 changes: 20 additions & 28 deletions crates/optimism/rpc/src/eth/transaction.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -87,39 +86,32 @@ where
tx: Recovered<OpTransactionSigned>,
tx_info: TransactionInfo,
) -> Result<Self::Transaction, Self::Error> {
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;
}
});

OpTxEnvelope::Deposit(tx.seal_unchecked(hash))
}
};
let inner: OpTxEnvelope = tx.into();

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
Expand Down
26 changes: 3 additions & 23 deletions crates/rpc/rpc/src/eth/helpers/types.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,27 +42,8 @@ where
tx: Recovered<TransactionSigned>,
tx_info: TransactionInfo,
) -> Result<Self::Transaction, Self::Error> {
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, ..
Expand Down

0 comments on commit fd4ca7d

Please sign in to comment.