diff --git a/crates/net/network/src/test_utils/mod.rs b/crates/net/network/src/test_utils/mod.rs index ead44ae61..5ba8a7c7b 100644 --- a/crates/net/network/src/test_utils/mod.rs +++ b/crates/net/network/src/test_utils/mod.rs @@ -7,4 +7,4 @@ pub use init::{ enr_to_peer_id, unused_port, unused_tcp_addr, unused_tcp_and_udp_port, unused_tcp_udp, unused_udp_addr, unused_udp_port, GETH_TIMEOUT, }; -pub use testnet::{NetworkEventStream, Peer, PeerConfig, Testnet}; +pub use testnet::{NetworkEventStream, Peer, PeerConfig, PeerHandle, Testnet}; diff --git a/crates/net/network/src/test_utils/testnet.rs b/crates/net/network/src/test_utils/testnet.rs index 9999e8fba..9720b7a93 100644 --- a/crates/net/network/src/test_utils/testnet.rs +++ b/crates/net/network/src/test_utils/testnet.rs @@ -491,10 +491,12 @@ impl PeerHandle { self.network.peer_id() } + /// Returns the [`PeersHandle`] from the network. pub fn peer_handle(&self) -> &PeersHandle { self.network.peers_handle() } + /// Returns the local socket as configured for the network. pub fn local_addr(&self) -> SocketAddr { self.network.local_addr() } diff --git a/crates/net/network/src/transactions/mod.rs b/crates/net/network/src/transactions/mod.rs index fc796395d..84fb264fd 100644 --- a/crates/net/network/src/transactions/mod.rs +++ b/crates/net/network/src/transactions/mod.rs @@ -88,14 +88,21 @@ use self::constants::{tx_manager::*, DEFAULT_SOFT_LIMIT_BYTE_SIZE_TRANSACTIONS_B /// The future for inserting a function into the pool pub type PoolImportFuture = Pin>> + Send + 'static>>; -/// Api to interact with [`TransactionsManager`] task. +/// Api to interact with [TransactionsManager] task. +/// +/// This can be obtained via [TransactionsManager::handle] and can be used to manually interact with +/// the [TransactionsManager] task once it is spawned. +/// +/// For example [TransactionsHandle::get_peer_transaction_hashes] returns the transaction hashes +/// known by a specific peer. #[derive(Debug, Clone)] pub struct TransactionsHandle { /// Command channel to the [`TransactionsManager`] manager_tx: mpsc::UnboundedSender, } -/// Implementation of the `TransactionsHandle` API. +/// Implementation of the `TransactionsHandle` API for use in testnet via type +/// [`PeerHandle`](crate::test_utils::PeerHandle). impl TransactionsHandle { fn send(&self, cmd: TransactionsCommand) { let _ = self.manager_tx.send(cmd); @@ -180,8 +187,9 @@ impl TransactionsHandle { /// Manages transactions on top of the p2p network. /// -/// This can be spawned to another task and is supposed to be run as background service while -/// [`TransactionsHandle`] is used as frontend to send commands to. +/// This can be spawned to another task and is supposed to be run as background service. +/// [`TransactionsHandle`] can be used as frontend to programmatically send commands to it and +/// interact with it. /// /// The [`TransactionsManager`] is responsible for: /// - handling incoming eth messages for transactions. @@ -221,8 +229,13 @@ pub struct TransactionsManager { /// All the connected peers. peers: HashMap, /// Send half for the command channel. + /// + /// This is kept so that a new [TransactionsHandle] can be created at any time. command_tx: mpsc::UnboundedSender, /// Incoming commands from [`TransactionsHandle`]. + /// + /// This will only receive commands if a user manually sends a command to the manager through + /// the [TransactionsHandle] to interact with this type directly. command_rx: UnboundedReceiverStream, /// Incoming commands from [`TransactionsHandle`]. pending_transactions: ReceiverStream, @@ -243,6 +256,7 @@ impl TransactionsManager { transactions_manager_config: TransactionsManagerConfig, ) -> Self { let network_events = network.event_listener(); + let (command_tx, command_rx) = mpsc::unbounded_channel(); let transaction_fetcher = TransactionFetcher::default().with_transaction_fetcher_config( diff --git a/examples/network-txpool.rs b/examples/network-txpool.rs index 7c261f5a3..d0e29e490 100644 --- a/examples/network-txpool.rs +++ b/examples/network-txpool.rs @@ -42,6 +42,9 @@ async fn main() -> eyre::Result<()> { .transactions(pool.clone(), transactions_manager_config) .split_with_handle(); + // this can be used to interact with the `txpool` service directly + let _txs_handle = txpool.handle(); + // spawn the network task tokio::task::spawn(network); // spawn the pool task