From 11404adf6ca1eeb3eb85be87b92c10d2de3bb9f0 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 16 Nov 2022 12:19:30 +0100 Subject: [PATCH] chore(txpool): remove hash generics (#211) --- crates/transaction-pool/src/pool/events.rs | 6 +-- crates/transaction-pool/src/pool/listener.rs | 41 +++++++++----------- crates/transaction-pool/src/pool/mod.rs | 2 +- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/crates/transaction-pool/src/pool/events.rs b/crates/transaction-pool/src/pool/events.rs index 7fc258f450..f36b360784 100644 --- a/crates/transaction-pool/src/pool/events.rs +++ b/crates/transaction-pool/src/pool/events.rs @@ -1,9 +1,9 @@ -use reth_primitives::H256; +use reth_primitives::{TxHash, H256}; use serde::{Deserialize, Serialize}; /// Various events that describe status changes of a transaction. #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -pub enum TransactionEvent { +pub enum TransactionEvent { /// Transaction has been added to the ready queue. Ready, /// Transaction has been added to the pending pool. @@ -15,7 +15,7 @@ pub enum TransactionEvent { /// Transaction has been replaced by the transaction belonging to the hash. /// /// E.g. same (sender + nonce) pair - Replaced(Hash), + Replaced(TxHash), /// Transaction was dropped due to configured limits. Discarded, /// Transaction became invalid indefinitely. diff --git a/crates/transaction-pool/src/pool/listener.rs b/crates/transaction-pool/src/pool/listener.rs index 05c84261f8..220feeccc3 100644 --- a/crates/transaction-pool/src/pool/listener.rs +++ b/crates/transaction-pool/src/pool/listener.rs @@ -1,23 +1,24 @@ //! Listeners for the transaction-pool use crate::pool::events::TransactionEvent; -use reth_primitives::H256; +use reth_primitives::{rpc::TxHash, H256}; use std::{collections::HashMap, hash}; use tokio::sync::mpsc::UnboundedSender; -type EventSink = UnboundedSender>; +type EventSink = UnboundedSender; /// Transaction pool event listeners. -pub(crate) struct PoolEventListener { +#[derive(Default)] +pub(crate) struct PoolEventListener { /// All listeners for certain transaction events. - listeners: HashMap>, + listeners: HashMap, } -impl PoolEventListener { +impl PoolEventListener { /// Calls the notification callback with the `PoolEventListenerSender` that belongs to the hash. - fn notify_with(&mut self, hash: &Hash, callback: F) + fn notify_with(&mut self, hash: &TxHash, callback: F) where - F: FnOnce(&mut PoolEventNotifier), + F: FnOnce(&mut PoolEventNotifier), { let is_done = if let Some(sink) = self.listeners.get_mut(hash) { callback(sink); @@ -32,49 +33,43 @@ impl PoolEventListener { } /// Notify listeners about a transaction that was added to the ready queue. - pub(crate) fn ready(&mut self, tx: &Hash, replaced: Option<&Hash>) { + pub(crate) fn ready(&mut self, tx: &TxHash, replaced: Option<&TxHash>) { self.notify_with(tx, |notifier| notifier.ready()); if let Some(replaced) = replaced { // notify listeners that this transaction was replaced - self.notify_with(replaced, |notifier| notifier.replaced(tx.clone())); + self.notify_with(replaced, |notifier| notifier.replaced(*tx)); } } /// Notify listeners about a transaction that was added to the queued pool. - pub(crate) fn queued(&mut self, tx: &Hash) { + pub(crate) fn queued(&mut self, tx: &TxHash) { self.notify_with(tx, |notifier| notifier.queued()); } /// Notify listeners about a transaction that was discarded. - pub(crate) fn discarded(&mut self, tx: &Hash) { + pub(crate) fn discarded(&mut self, tx: &TxHash) { self.notify_with(tx, |notifier| notifier.discarded()); } /// Notify listeners that the transaction was mined - pub(crate) fn mined(&mut self, tx: &Hash, block_hash: H256) { + pub(crate) fn mined(&mut self, tx: &TxHash, block_hash: H256) { self.notify_with(tx, |notifier| notifier.mined(block_hash)); } } -impl Default for PoolEventListener { - fn default() -> Self { - Self { listeners: Default::default() } - } -} - /// Sender half(s) of the event channels for a specific transaction #[derive(Debug)] -struct PoolEventNotifier { +struct PoolEventNotifier { /// Tracks whether the transaction this notifier can stop because the transaction was /// completed, or removed. is_done: bool, /// Corresponding sender half(s) for event listener channel - senders: Vec>, + senders: Vec, } -impl PoolEventNotifier { - fn notify(&mut self, event: TransactionEvent) { +impl PoolEventNotifier { + fn notify(&mut self, event: TransactionEvent) { self.senders.retain(|sender| sender.send(event.clone()).is_ok()) } @@ -93,7 +88,7 @@ impl PoolEventNotifier { } /// Transaction was replaced with the given transaction - fn replaced(&mut self, hash: Hash) { + fn replaced(&mut self, hash: TxHash) { self.notify(TransactionEvent::Replaced(hash)); self.is_done = true; } diff --git a/crates/transaction-pool/src/pool/mod.rs b/crates/transaction-pool/src/pool/mod.rs index 6d8c3c4074..1393ca8465 100644 --- a/crates/transaction-pool/src/pool/mod.rs +++ b/crates/transaction-pool/src/pool/mod.rs @@ -105,7 +105,7 @@ pub struct PoolInner { /// Pool settings. config: PoolConfig, /// Manages listeners for transaction state change events. - event_listener: RwLock>, + event_listener: RwLock, /// Listeners for new ready transactions. pending_transaction_listener: Mutex>>, /// Listeners for new transactions added to the pool.