From 3d7215137ae3d243b74c05209a14e5abb9057c29 Mon Sep 17 00:00:00 2001 From: Steven <112043913+stevencartavia@users.noreply.github.com> Date: Sat, 1 Feb 2025 01:52:53 -0600 Subject: [PATCH] [wip] feat: alloy tx type conversions back to OpTransactionSigned (#14121) Co-authored-by: Matthias Seitz --- .../primitives/src/transaction/signed.rs | 79 +++++++++++++------ 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/crates/optimism/primitives/src/transaction/signed.rs b/crates/optimism/primitives/src/transaction/signed.rs index bd59772c11dd..48dfed290d52 100644 --- a/crates/optimism/primitives/src/transaction/signed.rs +++ b/crates/optimism/primitives/src/transaction/signed.rs @@ -1,10 +1,9 @@ //! A signed Optimism transaction. -use crate::OpTxType; use alloc::vec::Vec; use alloy_consensus::{ - transaction::RlpEcdsaTx, SignableTransaction, Signed, Transaction, TxEip1559, TxEip2930, - TxEip7702, TxLegacy, Typed2718, + transaction::RlpEcdsaTx, Sealed, SignableTransaction, Signed, Transaction, TxEip1559, + TxEip2930, TxEip7702, TxLegacy, Typed2718, }; use alloy_eips::{ eip2718::{Decodable2718, Eip2718Error, Eip2718Result, Encodable2718}, @@ -20,7 +19,9 @@ use core::{ mem, }; use derive_more::{AsRef, Deref}; -use op_alloy_consensus::{DepositTransaction, OpPooledTransaction, OpTypedTransaction, TxDeposit}; +use op_alloy_consensus::{ + DepositTransaction, OpPooledTransaction, OpTxEnvelope, OpTypedTransaction, TxDeposit, +}; #[cfg(any(test, feature = "reth-codec"))] use proptest as _; use reth_primitives_traits::{ @@ -47,14 +48,9 @@ pub struct OpTransactionSigned { } impl OpTransactionSigned { - /// Calculates hash of given transaction and signature and returns new instance. - pub fn new(transaction: OpTypedTransaction, signature: Signature) -> Self { - let signed_tx = Self::new_unhashed(transaction, signature); - if signed_tx.ty() != OpTxType::Deposit { - signed_tx.hash.get_or_init(|| signed_tx.recalculate_hash()); - } - - signed_tx + /// Creates a new signed transaction from the given transaction, signature and hash. + pub fn new(transaction: OpTypedTransaction, signature: Signature, hash: B256) -> Self { + Self { hash: hash.into(), signature, transaction } } /// Consumes the type and returns the transaction. @@ -85,6 +81,12 @@ impl OpTransactionSigned { pub const fn is_deposit(&self) -> bool { matches!(self.transaction, OpTypedTransaction::Deposit(_)) } + + /// Splits the transaction into parts. + pub fn into_parts(self) -> (OpTypedTransaction, Signature, B256) { + let hash = *self.hash.get_or_init(|| self.recalculate_hash()); + (self.transaction, self.signature, hash) + } } impl SignedTransaction for OpTransactionSigned { @@ -141,6 +143,48 @@ impl SignedTransaction for OpTransactionSigned { } } +macro_rules! impl_from_signed { + ($($tx:ident),*) => { + $( + impl From> for OpTransactionSigned { + fn from(value: Signed<$tx>) -> Self { + let(tx,sig,hash) = value.into_parts(); + Self::new(tx.into(), sig, hash) + } + } + )* + }; +} + +impl_from_signed!(TxLegacy, TxEip2930, TxEip1559, TxEip7702, OpTypedTransaction); + +impl From for OpTransactionSigned { + fn from(value: OpTxEnvelope) -> Self { + match value { + OpTxEnvelope::Legacy(tx) => tx.into(), + OpTxEnvelope::Eip2930(tx) => tx.into(), + OpTxEnvelope::Eip1559(tx) => tx.into(), + OpTxEnvelope::Eip7702(tx) => tx.into(), + OpTxEnvelope::Deposit(tx) => tx.into(), + _ => unreachable!(), + } + } +} + +impl From for Signed { + fn from(value: OpTransactionSigned) -> Self { + let (tx, sig, hash) = value.into_parts(); + Self::new_unchecked(tx, sig, hash) + } +} + +impl From> for OpTransactionSigned { + fn from(value: Sealed) -> Self { + let (tx, hash) = value.into_parts(); + Self::new(OpTypedTransaction::Deposit(tx), TxDeposit::signature(), hash) + } +} + /// A trait that represents an optimism transaction, mainly used to indicate whether or not the /// transaction is a deposit transaction. pub trait OpTransaction { @@ -566,7 +610,7 @@ impl<'a> arbitrary::Arbitrary<'a> for OpTransactionSigned { let signature = if is_deposit(&transaction) { TxDeposit::signature() } else { signature }; - Ok(Self::new(transaction, signature)) + Ok(Self::new_unhashed(transaction, signature)) } } @@ -597,15 +641,6 @@ impl From for OpTransactionSigned { } } -impl> From> for OpTransactionSigned { - fn from(value: Signed) -> Self { - let (tx, sig, hash) = value.into_parts(); - let this = Self::new(tx.into(), sig); - this.hash.get_or_init(|| hash); - this - } -} - impl TryFrom for OpPooledTransaction { type Error = TransactionConversionError;