Skip to main content

reth_network/test_utils/
transactions.rs

1//! Test helper impls for transactions
2
3#![allow(dead_code)]
4
5use crate::{
6    transactions::{
7        announcement::{AnnouncedTransaction, TransactionMetadata},
8        constants::tx_manager::DEFAULT_MAX_COUNT_TRANSACTIONS_SEEN_BY_PEER,
9        fetcher::TransactionFetcher,
10        PeerMetadata, TransactionsManager, TransactionsManagerConfig,
11    },
12    NetworkConfigBuilder, NetworkManager,
13};
14use alloy_primitives::TxHash;
15use reth_eth_wire::EthVersion;
16use reth_eth_wire_types::EthNetworkPrimitives;
17use reth_network_api::{PeerKind, PeerRequest, PeerRequestSender};
18use reth_network_peers::PeerId;
19use reth_storage_api::noop::NoopProvider;
20use reth_tasks::Runtime;
21use reth_transaction_pool::test_utils::{testing_pool, TestPool};
22use secp256k1::SecretKey;
23use std::sync::Arc;
24use tokio::sync::mpsc;
25
26/// A new tx manager for testing.
27pub async fn new_tx_manager(
28) -> (TransactionsManager<TestPool, EthNetworkPrimitives>, NetworkManager<EthNetworkPrimitives>) {
29    new_tx_manager_with_config(TransactionsManagerConfig::default()).await
30}
31
32/// A new tx manager for testing with the given config.
33pub async fn new_tx_manager_with_config(
34    transactions_manager_config: TransactionsManagerConfig,
35) -> (TransactionsManager<TestPool, EthNetworkPrimitives>, NetworkManager<EthNetworkPrimitives>) {
36    let secret_key = SecretKey::new(&mut rand_08::thread_rng());
37    let client = NoopProvider::default();
38
39    let config = NetworkConfigBuilder::new(secret_key, Runtime::test())
40        // let OS choose port
41        .listener_port(0)
42        .disable_discovery()
43        .build(client);
44
45    let pool = testing_pool();
46
47    let (_network_handle, network, transactions, _) = NetworkManager::new(config)
48        .await
49        .unwrap()
50        .into_builder()
51        .transactions(pool.clone(), transactions_manager_config)
52        .split_with_handle();
53
54    (transactions, network)
55}
56
57/// Announces a hash to the tx fetcher on behalf of the given peer, without sending a request.
58pub fn buffer_hash_to_tx_fetcher(
59    tx_fetcher: &mut TransactionFetcher,
60    hash: TxHash,
61    peer_id: PeerId,
62    tx_encoded_length: Option<usize>,
63) {
64    tx_fetcher.on_announcement(
65        peer_id,
66        [AnnouncedTransaction {
67            hash,
68            metadata: tx_encoded_length.map(|size| TransactionMetadata { tx_type: 0, size }),
69        }],
70    );
71}
72
73/// Mock a new session, returns (peer, channel-to-send-get-pooled-tx-response-on).
74pub fn new_mock_session(
75    peer_id: PeerId,
76    version: EthVersion,
77) -> (PeerMetadata<EthNetworkPrimitives>, mpsc::Receiver<PeerRequest>) {
78    new_mock_session_with_capacity(peer_id, version, 1)
79}
80
81/// Mock a new session whose request channel buffers up to `capacity` requests, returns (peer,
82/// channel-to-send-get-pooled-tx-response-on).
83pub fn new_mock_session_with_capacity(
84    peer_id: PeerId,
85    version: EthVersion,
86    capacity: usize,
87) -> (PeerMetadata<EthNetworkPrimitives>, mpsc::Receiver<PeerRequest>) {
88    let (to_mock_session_tx, to_mock_session_rx) = mpsc::channel(capacity);
89
90    (
91        PeerMetadata::new(
92            PeerRequestSender::new(peer_id, to_mock_session_tx),
93            version,
94            Arc::from(""),
95            DEFAULT_MAX_COUNT_TRANSACTIONS_SEEN_BY_PEER,
96            PeerKind::Trusted,
97        ),
98        to_mock_session_rx,
99    )
100}