feat: add additional conversion trait for pooled tx element (#4279)

This commit is contained in:
Matthias Seitz
2023-08-21 14:35:59 +02:00
committed by GitHub
parent e45a0d3e43
commit 2523154260
5 changed files with 47 additions and 12 deletions

View File

@ -91,11 +91,12 @@ pub use storage::StorageEntry;
pub use transaction::{ pub use transaction::{
util::secp256k1::{public_key_to_address, recover_signer, sign_message}, util::secp256k1::{public_key_to_address, recover_signer, sign_message},
AccessList, AccessListItem, AccessListWithGasUsed, BlobTransaction, BlobTransactionSidecar, AccessList, AccessListItem, AccessListWithGasUsed, BlobTransaction, BlobTransactionSidecar,
FromRecoveredTransaction, IntoRecoveredTransaction, InvalidTransactionError, FromRecoveredPooledTransaction, FromRecoveredTransaction, IntoRecoveredTransaction,
PooledTransactionsElement, PooledTransactionsElementEcRecovered, Signature, Transaction, InvalidTransactionError, PooledTransactionsElement, PooledTransactionsElementEcRecovered,
TransactionKind, TransactionMeta, TransactionSigned, TransactionSignedEcRecovered, Signature, Transaction, TransactionKind, TransactionMeta, TransactionSigned,
TransactionSignedNoHash, TxEip1559, TxEip2930, TxEip4844, TxLegacy, TxType, EIP1559_TX_TYPE_ID, TransactionSignedEcRecovered, TransactionSignedNoHash, TxEip1559, TxEip2930, TxEip4844,
EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, LEGACY_TX_TYPE_ID, TxLegacy, TxType, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID,
LEGACY_TX_TYPE_ID,
}; };
pub use withdrawal::Withdrawal; pub use withdrawal::Withdrawal;

View File

@ -1089,6 +1089,16 @@ impl FromRecoveredTransaction for TransactionSignedEcRecovered {
} }
} }
/// A transaction type that can be created from a [`PooledTransactionsElementEcRecovered`]
/// transaction.
///
/// This is a conversion trait that'll ensure transactions received via P2P can be converted to the
/// transaction type that the transaction pool uses.
pub trait FromRecoveredPooledTransaction {
/// Converts to this type from the given [`PooledTransactionsElementEcRecovered`].
fn from_recovered_transaction(tx: PooledTransactionsElementEcRecovered) -> Self;
}
/// The inverse of [`FromRecoveredTransaction`] that ensure the transaction can be sent over the /// The inverse of [`FromRecoveredTransaction`] that ensure the transaction can be sent over the
/// network /// network
pub trait IntoRecoveredTransaction { pub trait IntoRecoveredTransaction {

View File

@ -373,6 +373,12 @@ impl PooledTransactionsElementEcRecovered {
self.transaction self.transaction
} }
/// Transform back to [`PooledTransactionsElement`]
pub fn into_ecrecovered_transaction(self) -> TransactionSignedEcRecovered {
let (tx, signer) = self.into_components();
tx.into_ecrecovered_transaction(signer)
}
/// Desolve Self to its component /// Desolve Self to its component
pub fn into_components(self) -> (PooledTransactionsElement, Address) { pub fn into_components(self) -> (PooledTransactionsElement, Address) {
(self.transaction, self.signer) (self.transaction, self.signer)

View File

@ -12,10 +12,10 @@ use rand::{
prelude::Distribution, prelude::Distribution,
}; };
use reth_primitives::{ use reth_primitives::{
constants::MIN_PROTOCOL_BASE_FEE, hex, Address, FromRecoveredTransaction, constants::MIN_PROTOCOL_BASE_FEE, hex, Address, FromRecoveredPooledTransaction,
IntoRecoveredTransaction, Signature, Transaction, TransactionKind, TransactionSigned, FromRecoveredTransaction, IntoRecoveredTransaction, PooledTransactionsElementEcRecovered,
TransactionSignedEcRecovered, TxEip1559, TxEip2930, TxEip4844, TxHash, TxLegacy, TxType, H256, Signature, Transaction, TransactionKind, TransactionSigned, TransactionSignedEcRecovered,
U128, U256, TxEip1559, TxEip2930, TxEip4844, TxHash, TxLegacy, TxType, H256, U128, U256,
}; };
use std::{ops::Range, sync::Arc, time::Instant}; use std::{ops::Range, sync::Arc, time::Instant};
@ -523,6 +523,12 @@ impl FromRecoveredTransaction for MockTransaction {
} }
} }
impl FromRecoveredPooledTransaction for MockTransaction {
fn from_recovered_transaction(tx: PooledTransactionsElementEcRecovered) -> Self {
FromRecoveredTransaction::from_recovered_transaction(tx.into_ecrecovered_transaction())
}
}
impl IntoRecoveredTransaction for MockTransaction { impl IntoRecoveredTransaction for MockTransaction {
fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered { fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered {
let tx = Transaction::Legacy(TxLegacy { let tx = Transaction::Legacy(TxLegacy {

View File

@ -6,8 +6,9 @@ use crate::{
}; };
use futures_util::{ready, Stream}; use futures_util::{ready, Stream};
use reth_primitives::{ use reth_primitives::{
Address, BlobTransactionSidecar, FromRecoveredTransaction, IntoRecoveredTransaction, PeerId, Address, BlobTransactionSidecar, FromRecoveredPooledTransaction, FromRecoveredTransaction,
PooledTransactionsElement, PooledTransactionsElementEcRecovered, Transaction, TransactionKind, IntoRecoveredTransaction, PeerId, PooledTransactionsElement,
PooledTransactionsElementEcRecovered, Transaction, TransactionKind,
TransactionSignedEcRecovered, TxHash, EIP1559_TX_TYPE_ID, EIP4844_TX_TYPE_ID, H256, U256, TransactionSignedEcRecovered, TxHash, EIP1559_TX_TYPE_ID, EIP4844_TX_TYPE_ID, H256, U256,
}; };
use reth_rlp::Encodable; use reth_rlp::Encodable;
@ -511,7 +512,12 @@ impl<T> BestTransactions for std::iter::Empty<T> {
/// Trait for transaction types used inside the pool /// Trait for transaction types used inside the pool
pub trait PoolTransaction: pub trait PoolTransaction:
fmt::Debug + Send + Sync + FromRecoveredTransaction + IntoRecoveredTransaction fmt::Debug
+ Send
+ Sync
+ FromRecoveredPooledTransaction
+ FromRecoveredTransaction
+ IntoRecoveredTransaction
{ {
/// Hash of the transaction. /// Hash of the transaction.
fn hash(&self) -> &TxHash; fn hash(&self) -> &TxHash;
@ -758,6 +764,12 @@ impl FromRecoveredTransaction for EthPooledTransaction {
} }
} }
impl FromRecoveredPooledTransaction for EthPooledTransaction {
fn from_recovered_transaction(tx: PooledTransactionsElementEcRecovered) -> Self {
EthPooledTransaction::from(tx)
}
}
impl IntoRecoveredTransaction for EthPooledTransaction { impl IntoRecoveredTransaction for EthPooledTransaction {
fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered { fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered {
self.transaction.clone() self.transaction.clone()