Skip to content

Commit c6b1a45

Browse files
authored
feat(txpool): add append_* helpers (#20028)
1 parent b92741a commit c6b1a45

File tree

1 file changed

+52
-18
lines changed
  • crates/transaction-pool/src/pool

1 file changed

+52
-18
lines changed

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

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -284,33 +284,67 @@ where
284284
self.pool.read()
285285
}
286286

287-
/// Returns hashes of transactions in the pool that can be propagated.
288-
pub fn pooled_transactions_hashes(&self) -> Vec<TxHash> {
289-
self.get_pool_data()
290-
.all()
291-
.transactions_iter()
292-
.filter(|tx| tx.propagate)
293-
.map(|tx| *tx.hash())
294-
.collect()
295-
}
296-
297287
/// Returns transactions in the pool that can be propagated
298288
pub fn pooled_transactions(&self) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
299-
self.get_pool_data().all().transactions_iter().filter(|tx| tx.propagate).cloned().collect()
289+
let mut out = Vec::new();
290+
self.append_pooled_transactions(&mut out);
291+
out
292+
}
293+
294+
/// Returns hashes of transactions in the pool that can be propagated.
295+
pub fn pooled_transactions_hashes(&self) -> Vec<TxHash> {
296+
let mut out = Vec::new();
297+
self.append_pooled_transactions_hashes(&mut out);
298+
out
300299
}
301300

302301
/// Returns only the first `max` transactions in the pool that can be propagated.
303302
pub fn pooled_transactions_max(
304303
&self,
305304
max: usize,
306305
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
307-
self.get_pool_data()
308-
.all()
309-
.transactions_iter()
310-
.filter(|tx| tx.propagate)
311-
.take(max)
312-
.cloned()
313-
.collect()
306+
let mut out = Vec::new();
307+
self.append_pooled_transactions_max(max, &mut out);
308+
out
309+
}
310+
311+
/// Extends the given vector with all transactions in the pool that can be propagated.
312+
pub fn append_pooled_transactions(
313+
&self,
314+
out: &mut Vec<Arc<ValidPoolTransaction<T::Transaction>>>,
315+
) {
316+
out.extend(
317+
self.get_pool_data().all().transactions_iter().filter(|tx| tx.propagate).cloned(),
318+
);
319+
}
320+
321+
/// Extends the given vector with the hashes of all transactions in the pool that can be
322+
/// propagated.
323+
pub fn append_pooled_transactions_hashes(&self, out: &mut Vec<TxHash>) {
324+
out.extend(
325+
self.get_pool_data()
326+
.all()
327+
.transactions_iter()
328+
.filter(|tx| tx.propagate)
329+
.map(|tx| *tx.hash()),
330+
);
331+
}
332+
333+
/// Extends the given vector with only the first `max` transactions in the pool that can be
334+
/// propagated.
335+
pub fn append_pooled_transactions_max(
336+
&self,
337+
max: usize,
338+
out: &mut Vec<Arc<ValidPoolTransaction<T::Transaction>>>,
339+
) {
340+
out.extend(
341+
self.get_pool_data()
342+
.all()
343+
.transactions_iter()
344+
.filter(|tx| tx.propagate)
345+
.take(max)
346+
.cloned(),
347+
);
314348
}
315349

316350
/// Returns only the first `max` hashes of transactions in the pool that can be propagated.

0 commit comments

Comments
 (0)