Skip to content

Commit

Permalink
feat: alloy tx type conversions back to OpTransactionSigned
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencartavia committed Jan 31, 2025
1 parent 4ce5a60 commit 89da667
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions crates/optimism/primitives/src/transaction/signed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! A signed Optimism transaction.
use crate::OpTxType;
use alloc::vec::Vec;
use alloy_consensus::{
transaction::RlpEcdsaTx, SignableTransaction, Signed, Transaction, TxEip1559, TxEip2930,
Expand All @@ -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::{
Expand All @@ -30,6 +31,19 @@ use reth_primitives_traits::{
InMemorySize, SignedTransaction,
};

macro_rules! impl_from_signed {
($($tx:ident),*) => {
$(
impl From<Signed<$tx>> for OpTransactionSigned {
fn from(value: Signed<$tx>) -> Self {
let(tx,sig,hash) = value.into_parts();
Self::new(tx.into(), sig, hash)
}
}
)*
};
}

/// Signed transaction.
#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(rlp))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
Expand All @@ -47,14 +61,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.
Expand Down Expand Up @@ -85,6 +94,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 {
Expand Down Expand Up @@ -141,6 +156,31 @@ impl SignedTransaction for OpTransactionSigned {
}
}

impl_from_signed!(TxLegacy, TxEip2930, TxEip1559, TxEip7702, OpTypedTransaction);

impl From<OpTxEnvelope> 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) => {
let (tx, hash) = tx.into_parts();
Self::new(OpTypedTransaction::Deposit(tx), TxDeposit::signature(), hash)
}
_ => panic!("Invalid OpTxEnvelope variant"),
}
}
}

impl From<OpTransactionSigned> for Signed<OpTypedTransaction> {
fn from(value: OpTransactionSigned) -> Self {
let (tx, sig, hash) = value.into_parts();
Self::new_unchecked(tx, sig, hash)
}
}

/// A trait that represents an optimism transaction, mainly used to indicate whether or not the
/// transaction is a deposit transaction.
pub trait OpTransaction {
Expand Down Expand Up @@ -565,8 +605,9 @@ impl<'a> arbitrary::Arbitrary<'a> for OpTransactionSigned {
}

let signature = if is_deposit(&transaction) { TxDeposit::signature() } else { signature };
let hash = if is_deposit(&transaction) { B256::ZERO } else { signature_hash(&transaction) };

Ok(Self::new(transaction, signature))
Ok(Self::new(transaction, signature, hash))
}
}

Expand Down Expand Up @@ -597,15 +638,6 @@ impl From<OpPooledTransaction> for OpTransactionSigned {
}
}

impl<T: Into<OpTypedTransaction>> From<Signed<T>> for OpTransactionSigned {
fn from(value: Signed<T>) -> Self {
let (tx, sig, hash) = value.into_parts();
let this = Self::new(tx.into(), sig);
this.hash.get_or_init(|| hash);
this
}
}

impl TryFrom<OpTransactionSigned> for OpPooledTransaction {
type Error = TransactionConversionError;

Expand Down

0 comments on commit 89da667

Please sign in to comment.