feat(txpool): Make TransactionPool trait object safe (#4156)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
PatStiles
2023-08-11 08:34:56 -05:00
committed by GitHub
parent 03fc87701a
commit 427a8395f9
7 changed files with 19 additions and 28 deletions

View File

@ -135,7 +135,9 @@ where
{ {
Ok((new_header, post_state)) => { Ok((new_header, post_state)) => {
// clear all transactions from pool // clear all transactions from pool
pool.remove_transactions(transactions.iter().map(|tx| tx.hash())); pool.remove_transactions(
transactions.iter().map(|tx| tx.hash()).collect(),
);
let state = ForkchoiceState { let state = ForkchoiceState {
head_block_hash: new_header.hash, head_block_hash: new_header.hash,

View File

@ -204,7 +204,7 @@ where
/// complete transaction object if it is unknown to them. The dissemination of complete /// complete transaction object if it is unknown to them. The dissemination of complete
/// transactions to a fraction of peers usually ensures that all nodes receive the transaction /// transactions to a fraction of peers usually ensures that all nodes receive the transaction
/// and won't need to request it. /// and won't need to request it.
fn on_new_transactions(&mut self, hashes: impl IntoIterator<Item = TxHash>) { fn on_new_transactions(&mut self, hashes: Vec<TxHash>) {
// Nothing to propagate while initially syncing // Nothing to propagate while initially syncing
if self.network.is_initially_syncing() { if self.network.is_initially_syncing() {
return return
@ -372,9 +372,7 @@ where
/// Handles a command received from a detached [`TransactionsHandle`] /// Handles a command received from a detached [`TransactionsHandle`]
fn on_command(&mut self, cmd: TransactionsCommand) { fn on_command(&mut self, cmd: TransactionsCommand) {
match cmd { match cmd {
TransactionsCommand::PropagateHash(hash) => { TransactionsCommand::PropagateHash(hash) => self.on_new_transactions(vec![hash]),
self.on_new_transactions(std::iter::once(hash))
}
} }
} }

View File

@ -401,7 +401,7 @@ where
fn remove_transactions( fn remove_transactions(
&self, &self,
hashes: impl IntoIterator<Item = TxHash>, hashes: Vec<TxHash>,
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> { ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
self.pool.remove_transactions(hashes) self.pool.remove_transactions(hashes)
} }
@ -414,10 +414,7 @@ where
self.inner().get(tx_hash) self.inner().get(tx_hash)
} }
fn get_all( fn get_all(&self, txs: Vec<TxHash>) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
&self,
txs: impl IntoIterator<Item = TxHash>,
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
self.inner().get_all(txs) self.inner().get_all(txs)
} }

View File

@ -135,7 +135,7 @@ impl TransactionPool for NoopTransactionPool {
fn remove_transactions( fn remove_transactions(
&self, &self,
_hashes: impl IntoIterator<Item = TxHash>, _hashes: Vec<TxHash>,
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> { ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
vec![] vec![]
} }
@ -146,10 +146,7 @@ impl TransactionPool for NoopTransactionPool {
None None
} }
fn get_all( fn get_all(&self, _txs: Vec<TxHash>) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
&self,
_txs: impl IntoIterator<Item = TxHash>,
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>> {
vec![] vec![]
} }

View File

@ -513,7 +513,7 @@ where
/// Removes and returns all matching transactions from the pool. /// Removes and returns all matching transactions from the pool.
pub(crate) fn remove_transactions( pub(crate) fn remove_transactions(
&self, &self,
hashes: impl IntoIterator<Item = TxHash>, hashes: Vec<TxHash>,
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> { ) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
let removed = self.pool.write().remove_transactions(hashes); let removed = self.pool.write().remove_transactions(hashes);
@ -552,7 +552,7 @@ where
/// If no transaction exists, it is skipped. /// If no transaction exists, it is skipped.
pub(crate) fn get_all( pub(crate) fn get_all(
&self, &self,
txs: impl IntoIterator<Item = TxHash>, txs: Vec<TxHash>,
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> { ) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
self.pool.read().get_all(txs).collect() self.pool.read().get_all(txs).collect()
} }

View File

@ -247,10 +247,10 @@ impl<T: TransactionOrdering> TxPool<T> {
} }
/// Returns transactions for the multiple given hashes, if they exist. /// Returns transactions for the multiple given hashes, if they exist.
pub(crate) fn get_all<'a>( pub(crate) fn get_all(
&'a self, &self,
txs: impl IntoIterator<Item = TxHash> + 'a, txs: Vec<TxHash>,
) -> impl Iterator<Item = Arc<ValidPoolTransaction<T::Transaction>>> + 'a { ) -> impl Iterator<Item = Arc<ValidPoolTransaction<T::Transaction>>> + '_ {
txs.into_iter().filter_map(|tx| self.get(&tx)) txs.into_iter().filter_map(|tx| self.get(&tx))
} }
@ -409,7 +409,7 @@ impl<T: TransactionOrdering> TxPool<T> {
/// Maintenance task to apply a series of updates. /// Maintenance task to apply a series of updates.
/// ///
/// This will move/discard the given transaction according to the `PoolUpdate` /// This will move/discard the given transaction according to the `PoolUpdate`
fn process_updates(&mut self, updates: impl IntoIterator<Item = PoolUpdate>) -> UpdateOutcome { fn process_updates(&mut self, updates: Vec<PoolUpdate>) -> UpdateOutcome {
let mut outcome = UpdateOutcome::default(); let mut outcome = UpdateOutcome::default();
for update in updates { for update in updates {
let PoolUpdate { id, hash, current, destination } = update; let PoolUpdate { id, hash, current, destination } = update;
@ -445,7 +445,7 @@ impl<T: TransactionOrdering> TxPool<T> {
/// any additional updates. /// any additional updates.
pub(crate) fn remove_transactions( pub(crate) fn remove_transactions(
&mut self, &mut self,
hashes: impl IntoIterator<Item = TxHash>, hashes: Vec<TxHash>,
) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> { ) -> Vec<Arc<ValidPoolTransaction<T::Transaction>>> {
hashes.into_iter().filter_map(|hash| self.remove_transaction_by_hash(&hash)).collect() hashes.into_iter().filter_map(|hash| self.remove_transaction_by_hash(&hash)).collect()
} }

View File

@ -227,7 +227,7 @@ pub trait TransactionPool: Send + Sync + Clone {
/// Consumer: Block production /// Consumer: Block production
fn remove_transactions( fn remove_transactions(
&self, &self,
hashes: impl IntoIterator<Item = TxHash>, hashes: Vec<TxHash>,
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>; ) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
/// Retains only those hashes that are unknown to the pool. /// Retains only those hashes that are unknown to the pool.
@ -250,10 +250,7 @@ pub trait TransactionPool: Send + Sync + Clone {
/// This adheres to the expected behavior of [`GetPooledTransactions`](https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getpooledtransactions-0x09): /// 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 /// The transactions must be in same order as in the request, but it is OK to skip transactions
/// which are not available. /// which are not available.
fn get_all( fn get_all(&self, txs: Vec<TxHash>) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
&self,
txs: impl IntoIterator<Item = TxHash>,
) -> Vec<Arc<ValidPoolTransaction<Self::Transaction>>>;
/// Notify the pool about transactions that are propagated to peers. /// Notify the pool about transactions that are propagated to peers.
/// ///