feat(txpool): add get_all function (#65)

This commit is contained in:
Matthias Seitz
2022-10-14 11:19:15 +02:00
committed by GitHub
parent 794c07391e
commit 6d0bd9037e
4 changed files with 35 additions and 0 deletions

View File

@ -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> {

View File

@ -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()

View File

@ -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`.

View File

@ -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