Skip to content

Commit

Permalink
feat: add clone into consensus (paradigmxyz#12999)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Nov 29, 2024
1 parent b6ba822 commit a01e031
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/net/network/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,7 @@ impl<T: SignedTransaction> PropagateTransaction<T> {
P: PoolTransaction<Consensus: Into<T>>,
{
let size = tx.encoded_length();
let transaction = tx.transaction.clone().into_consensus().into();
let transaction = tx.transaction.clone_into_consensus().into();
let transaction = Arc::new(transaction);
Self { size, transaction }
}
Expand Down
2 changes: 1 addition & 1 deletion crates/optimism/node/src/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ where
let l1_block_info = self.block_info.l1_block_info.read().clone();

let mut encoded = Vec::with_capacity(valid_tx.transaction().encoded_length());
let tx: TransactionSigned = valid_tx.transaction().clone().into_consensus().into();
let tx: TransactionSigned = valid_tx.transaction().clone_into_consensus().into();
tx.encode_2718(&mut encoded);

let cost_addition = match l1_block_info.l1_tx_data_fee(
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-eth-api/src/helpers/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub trait EthTransactions: LoadTransaction<Provider: BlockReaderIdExt> {
if let Some(tx) =
RpcNodeCore::pool(self).get_transaction_by_sender_and_nonce(sender, nonce)
{
let transaction = tx.transaction.clone().into_consensus();
let transaction = tx.transaction.clone_into_consensus();
return Ok(Some(from_recovered(transaction.into(), self.tx_resp_builder())?));
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rpc/rpc/src/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ where
{
content.entry(tx.sender()).or_default().insert(
tx.nonce().to_string(),
from_recovered(tx.clone().into_consensus().into(), resp_builder)?,
from_recovered(tx.clone_into_consensus().into(), resp_builder)?,
);

Ok(())
Expand Down Expand Up @@ -101,7 +101,7 @@ where
inspect: &mut BTreeMap<Address, BTreeMap<String, TxpoolInspectSummary>>,
) {
let entry = inspect.entry(tx.sender()).or_default();
let tx: TransactionSignedEcRecovered = tx.clone().into_consensus().into();
let tx: TransactionSignedEcRecovered = tx.clone_into_consensus().into();
entry.insert(
tx.nonce().to_string(),
TxpoolInspectSummary {
Expand Down
2 changes: 1 addition & 1 deletion crates/transaction-pool/src/maintain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ where
.into_iter()
.map(|tx| {
let recovered: TransactionSignedEcRecovered =
tx.transaction.clone().into_consensus().into();
tx.transaction.clone_into_consensus().into();
recovered.into_signed()
})
.collect::<Vec<_>>();
Expand Down
11 changes: 11 additions & 0 deletions crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,13 @@ pub trait PoolTransaction: fmt::Debug + Send + Sync + Clone {
tx.try_into()
}

/// Clone the transaction into a consensus variant.
///
/// This method is preferred when the [`PoolTransaction`] already wraps the consensus variant.
fn clone_into_consensus(&self) -> Self::Consensus {
self.clone().into_consensus()
}

/// Define a method to convert from the `Self` type to `Consensus`
fn into_consensus(self) -> Self::Consensus {
self.into()
Expand Down Expand Up @@ -1237,6 +1244,10 @@ impl PoolTransaction for EthPooledTransaction {

type Pooled = PooledTransactionsElementEcRecovered;

fn clone_into_consensus(&self) -> Self::Consensus {
self.transaction().clone()
}

fn try_consensus_into_pooled(
tx: Self::Consensus,
) -> Result<Self::Pooled, Self::TryFromConsensusError> {
Expand Down
9 changes: 8 additions & 1 deletion crates/transaction-pool/src/validate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ impl<T: PoolTransaction> ValidPoolTransaction<T> {
self.is_eip4844() != other.is_eip4844()
}

/// Converts to this type into the consensus transaction of the pooled transaction.
///
/// Note: this takes `&self` since indented usage is via `Arc<Self>`.
pub fn to_consensus(&self) -> T::Consensus {
self.transaction.clone_into_consensus()
}

/// Determines whether a candidate transaction (`maybe_replacement`) is underpriced compared to
/// an existing transaction in the pool.
///
Expand Down Expand Up @@ -433,7 +440,7 @@ impl<T: PoolTransaction<Consensus: Into<TransactionSignedEcRecovered>>> ValidPoo
///
/// Note: this takes `&self` since indented usage is via `Arc<Self>`.
pub fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered {
self.transaction.clone().into_consensus().into()
self.to_consensus().into()
}
}

Expand Down

0 comments on commit a01e031

Please sign in to comment.