Skip to content

Commit 6d0bd90

Browse files
authored
feat(txpool): add get_all function (#65)
1 parent 794c073 commit 6d0bd90

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

crates/transaction-pool/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ where
212212
fn get(&self, tx_hash: &TxHash) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>> {
213213
self.inner().get(tx_hash)
214214
}
215+
216+
fn get_all(
217+
&self,
218+
txs: impl IntoIterator<Item = TxHash>,
219+
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
220+
self.inner().get_all(txs)
221+
}
215222
}
216223

217224
impl<V: TransactionValidator, O: TransactionOrdering> Clone for Pool<V, O> {

crates/transaction-pool/src/pool/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ where
283283
self.pool.read().get(tx_hash)
284284
}
285285

286+
/// Returns all the transactions belonging to the hashes.
287+
///
288+
/// If no transaction exists, it is skipped.
289+
pub(crate) fn get_all(
290+
&self,
291+
txs: impl IntoIterator<Item = TxHash>,
292+
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
293+
self.pool.read().get_all(txs).collect()
294+
}
295+
286296
/// Number of transactions in the entire pool
287297
pub(crate) fn len(&self) -> usize {
288298
self.pool.read().len()

crates/transaction-pool/src/pool/txpool.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ impl<T: TransactionOrdering> TxPool<T> {
123123
self.all_transactions.by_hash.get(tx_hash).cloned()
124124
}
125125

126+
/// Returns all transaction for the hashes, if it exis.
127+
pub(crate) fn get_all<'a>(
128+
&'a self,
129+
txs: impl IntoIterator<Item = TxHash> + 'a,
130+
) -> impl Iterator<Item = Arc<ValidPoolTransaction<T::Transaction>>> + 'a {
131+
txs.into_iter().filter_map(|tx| self.get(&tx))
132+
}
133+
126134
/// Adds the transaction into the pool.
127135
///
128136
/// This pool consists of two three-pools: `Queued`, `Pending` and `BaseFee`.

crates/transaction-pool/src/traits.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ pub trait TransactionPool: Send + Sync {
6767

6868
/// Returns the transaction for the given hash.
6969
fn get(&self, tx_hash: &TxHash) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>;
70+
71+
/// Returns all transactions objects for the given hashes.
72+
///
73+
/// This adheres to the expected behavior of [`GetPooledTransactions`](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getpooledtransactions-0x09):
74+
/// The transactions must be in same order as in the request, but it is OK to skip transactions
75+
/// which are not available.
76+
fn get_all(
77+
&self,
78+
txs: impl IntoIterator<Item = TxHash>,
79+
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
7080
}
7181

7282
/// Represents a new transaction

0 commit comments

Comments
 (0)