From 6d0bd9037e59f6ee39c8707e2e3c6141458140b7 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 14 Oct 2022 11:19:15 +0200 Subject: [PATCH] feat(txpool): add get_all function (#65) --- crates/transaction-pool/src/lib.rs | 7 +++++++ crates/transaction-pool/src/pool/mod.rs | 10 ++++++++++ crates/transaction-pool/src/pool/txpool.rs | 8 ++++++++ crates/transaction-pool/src/traits.rs | 10 ++++++++++ 4 files changed, 35 insertions(+) diff --git a/crates/transaction-pool/src/lib.rs b/crates/transaction-pool/src/lib.rs index b00f43963..3d9db9bc1 100644 --- a/crates/transaction-pool/src/lib.rs +++ b/crates/transaction-pool/src/lib.rs @@ -212,6 +212,13 @@ where fn get(&self, tx_hash: &TxHash) -> Option>> { self.inner().get(tx_hash) } + + fn get_all( + &self, + txs: impl IntoIterator, + ) -> Vec>> { + self.inner().get_all(txs) + } } impl Clone for Pool { diff --git a/crates/transaction-pool/src/pool/mod.rs b/crates/transaction-pool/src/pool/mod.rs index 20c8697cf..d59f12a25 100644 --- a/crates/transaction-pool/src/pool/mod.rs +++ b/crates/transaction-pool/src/pool/mod.rs @@ -283,6 +283,16 @@ where self.pool.read().get(tx_hash) } + /// Returns all the transactions belonging to the hashes. + /// + /// If no transaction exists, it is skipped. + pub(crate) fn get_all( + &self, + txs: impl IntoIterator, + ) -> Vec>> { + self.pool.read().get_all(txs).collect() + } + /// Number of transactions in the entire pool pub(crate) fn len(&self) -> usize { self.pool.read().len() diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index 11a1a0ce7..10f26565a 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -123,6 +123,14 @@ impl TxPool { self.all_transactions.by_hash.get(tx_hash).cloned() } + /// Returns all transaction for the hashes, if it exis. + pub(crate) fn get_all<'a>( + &'a self, + txs: impl IntoIterator + 'a, + ) -> impl Iterator>> + 'a { + txs.into_iter().filter_map(|tx| self.get(&tx)) + } + /// Adds the transaction into the pool. /// /// This pool consists of two three-pools: `Queued`, `Pending` and `BaseFee`. diff --git a/crates/transaction-pool/src/traits.rs b/crates/transaction-pool/src/traits.rs index 8a680b20c..fa3ac043b 100644 --- a/crates/transaction-pool/src/traits.rs +++ b/crates/transaction-pool/src/traits.rs @@ -67,6 +67,16 @@ pub trait TransactionPool: Send + Sync { /// Returns the transaction for the given hash. fn get(&self, tx_hash: &TxHash) -> Option>>; + + /// Returns all transactions objects for the given hashes. + /// + /// This adheres to the expected behavior of [`GetPooledTransactions`](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getpooledtransactions-0x09): + /// The transactions must be in same order as in the request, but it is OK to skip transactions + /// which are not available. + fn get_all( + &self, + txs: impl IntoIterator, + ) -> Vec>>; } /// Represents a new transaction