Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: flip tx conversion impl #13208

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,34 @@ impl TransactionSigned {
&self.transaction
}

/// Tries to convert a [`TransactionSigned`] into a [`PooledTransactionsElement`].
///
/// This function used as a helper to convert from a decoded p2p broadcast message to
/// [`PooledTransactionsElement`]. Since [`BlobTransaction`] is disallowed to be broadcasted on
/// p2p, return an err if `tx` is [`Transaction::Eip4844`].
pub fn try_into_pooled(self) -> Result<PooledTransactionsElement, Self> {
let hash = self.hash();
match self {
Self { transaction: Transaction::Legacy(tx), signature, .. } => {
Ok(PooledTransactionsElement::Legacy(Signed::new_unchecked(tx, signature, hash)))
}
Self { transaction: Transaction::Eip2930(tx), signature, .. } => {
Ok(PooledTransactionsElement::Eip2930(Signed::new_unchecked(tx, signature, hash)))
}
Self { transaction: Transaction::Eip1559(tx), signature, .. } => {
Ok(PooledTransactionsElement::Eip1559(Signed::new_unchecked(tx, signature, hash)))
}
Self { transaction: Transaction::Eip7702(tx), signature, .. } => {
Ok(PooledTransactionsElement::Eip7702(Signed::new_unchecked(tx, signature, hash)))
}
// Not supported because missing blob sidecar
tx @ Self { transaction: Transaction::Eip4844(_), .. } => Err(tx),
#[cfg(feature = "optimism")]
// Not supported because deposit transactions are never pooled
tx @ Self { transaction: Transaction::Deposit(_), .. } => Err(tx),
}
}

/// Transaction hash. Used to identify transaction.
pub fn hash(&self) -> TxHash {
*self.tx_hash()
Expand Down
32 changes: 2 additions & 30 deletions crates/primitives/src/transaction/pooled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,34 +46,6 @@ pub enum PooledTransactionsElement {
}

impl PooledTransactionsElement {
/// Tries to convert a [`TransactionSigned`] into a [`PooledTransactionsElement`].
///
/// This function used as a helper to convert from a decoded p2p broadcast message to
/// [`PooledTransactionsElement`]. Since [`BlobTransaction`] is disallowed to be broadcasted on
/// p2p, return an err if `tx` is [`Transaction::Eip4844`].
pub fn try_from_broadcast(tx: TransactionSigned) -> Result<Self, TransactionSigned> {
let hash = tx.hash();
match tx {
TransactionSigned { transaction: Transaction::Legacy(tx), signature, .. } => {
Ok(Self::Legacy(Signed::new_unchecked(tx, signature, hash)))
}
TransactionSigned { transaction: Transaction::Eip2930(tx), signature, .. } => {
Ok(Self::Eip2930(Signed::new_unchecked(tx, signature, hash)))
}
TransactionSigned { transaction: Transaction::Eip1559(tx), signature, .. } => {
Ok(Self::Eip1559(Signed::new_unchecked(tx, signature, hash)))
}
TransactionSigned { transaction: Transaction::Eip7702(tx), signature, .. } => {
Ok(Self::Eip7702(Signed::new_unchecked(tx, signature, hash)))
}
// Not supported because missing blob sidecar
tx @ TransactionSigned { transaction: Transaction::Eip4844(_), .. } => Err(tx),
#[cfg(feature = "optimism")]
// Not supported because deposit transactions are never pooled
tx @ TransactionSigned { transaction: Transaction::Deposit(_), .. } => Err(tx),
}
}

/// Converts from an EIP-4844 [`RecoveredTx`] to a
/// [`PooledTransactionsElementEcRecovered`] with the given sidecar.
///
Expand Down Expand Up @@ -650,7 +622,7 @@ impl TryFrom<TransactionSigned> for PooledTransactionsElement {
type Error = TransactionConversionError;

fn try_from(tx: TransactionSigned) -> Result<Self, Self::Error> {
Self::try_from_broadcast(tx).map_err(|_| TransactionConversionError::UnsupportedForP2P)
tx.try_into_pooled().map_err(|_| TransactionConversionError::UnsupportedForP2P)
}
}

Expand Down Expand Up @@ -679,7 +651,7 @@ impl<'a> arbitrary::Arbitrary<'a> for PooledTransactionsElement {
// Attempt to create a `TransactionSigned` with arbitrary data.
let tx_signed = TransactionSigned::arbitrary(u)?;
// Attempt to create a `PooledTransactionsElement` with arbitrary data, handling the Result.
match Self::try_from_broadcast(tx_signed) {
match tx_signed.try_into_pooled() {
Ok(tx) => Ok(tx),
Err(tx) => {
let (tx, sig, hash) = tx.into_parts();
Expand Down
3 changes: 2 additions & 1 deletion crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,8 @@ impl PoolTransaction for EthPooledTransaction {
tx: RecoveredTx<Self::Consensus>,
) -> Result<RecoveredTx<Self::Pooled>, Self::TryFromConsensusError> {
let (tx, signer) = tx.to_components();
let pooled = PooledTransactionsElement::try_from_broadcast(tx)
let pooled = tx
.try_into_pooled()
.map_err(|_| TryFromRecoveredTransactionError::BlobSidecarMissing)?;
Ok(RecoveredTx::from_signed_transaction(pooled, signer))
}
Expand Down
Loading