Skip to content

Commit

Permalink
feat(txpool): add get_all function (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 14, 2022
1 parent 794c073 commit 6d0bd90
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/transaction-pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ where
fn get(&self, tx_hash: &TxHash) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>> {
self.inner().get(tx_hash)
}

fn get_all(
&self,
txs: impl IntoIterator<Item = TxHash>,
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
self.inner().get_all(txs)
}
}

impl<V: TransactionValidator, O: TransactionOrdering> Clone for Pool<V, O> {
Expand Down
10 changes: 10 additions & 0 deletions crates/transaction-pool/src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item = TxHash>,
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
self.pool.read().get_all(txs).collect()
}

/// Number of transactions in the entire pool
pub(crate) fn len(&self) -> usize {
self.pool.read().len()
Expand Down
8 changes: 8 additions & 0 deletions crates/transaction-pool/src/pool/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ impl<T: TransactionOrdering> TxPool<T> {
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<Item = TxHash> + 'a,
) -> impl Iterator<Item = Arc<ValidPoolTransaction<T::Transaction>>> + '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`.
Expand Down
10 changes: 10 additions & 0 deletions crates/transaction-pool/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ pub trait TransactionPool: Send + Sync {

/// Returns the transaction for the given hash.
fn get(&self, tx_hash: &TxHash) -> Option<Arc<ValidPoolTransaction<Self::Transaction>>>;

/// 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<Item = TxHash>,
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
}

/// Represents a new transaction
Expand Down

0 comments on commit 6d0bd90

Please sign in to comment.