test: TransactionGenerator and p2p testing support (#5023)

This commit is contained in:
Matthias Seitz
2023-10-14 23:13:36 +02:00
committed by GitHub
parent 3bf2c8865a
commit d58e4a46f8
11 changed files with 650 additions and 68 deletions

View File

@ -1,5 +1,3 @@
use std::sync::Arc;
use reth_eth_wire::{GetPooledTransactions, PooledTransactions};
use reth_interfaces::sync::{NetworkSyncUpdater, SyncState};
use reth_network::{
@ -11,7 +9,7 @@ use reth_primitives::{Signature, TransactionSigned, B256};
use reth_provider::test_utils::MockEthProvider;
use reth_transaction_pool::{
test_utils::{testing_pool, MockTransaction},
TransactionOrigin, TransactionPool,
TransactionPool,
};
use tokio::sync::oneshot;
@ -39,15 +37,14 @@ async fn test_large_tx_req() {
let txs_hashes: Vec<B256> = txs.iter().map(|tx| tx.get_hash()).collect();
// setup testnet
let mock_provider = Arc::new(MockEthProvider::default());
let mut net = Testnet::create_with(2, mock_provider.clone()).await;
let mut net = Testnet::create_with(2, MockEthProvider::default()).await;
// install request handlers
net.for_each_mut(|peer| peer.install_request_handler());
// insert generated txs into responding peer's pool
let pool1 = testing_pool();
pool1.add_transactions(TransactionOrigin::Private, txs).await.unwrap();
pool1.add_external_transactions(txs).await.unwrap();
// install transactions managers
net.peers_mut()[0].install_transactions_manager(testing_pool());

View File

@ -22,6 +22,21 @@ use secp256k1::SecretKey;
use std::{collections::HashSet, net::SocketAddr, time::Duration};
use tokio::task;
#[tokio::test(flavor = "multi_thread")]
async fn test_connect_all() {
reth_tracing::init_test_tracing();
let num_peers = 3;
let net = Testnet::create(num_peers).await;
let handle = net.spawn();
handle.connect_peers().await;
handle.peers().iter().for_each(|peer| {
assert_eq!(peer.network().num_connected_peers(), num_peers - 1);
});
}
#[tokio::test(flavor = "multi_thread")]
async fn test_establish_connections() {
reth_tracing::init_test_tracing();

View File

@ -5,5 +5,6 @@ mod geth;
mod requests;
mod session;
mod startup;
mod txgossip;
fn main() {}

View File

@ -0,0 +1,45 @@
//! Testing gossiping of transactions.
use rand::thread_rng;
use reth_network::test_utils::Testnet;
use reth_primitives::U256;
use reth_provider::test_utils::{ExtendedAccount, MockEthProvider};
use reth_transaction_pool::{test_utils::TransactionGenerator, PoolTransaction, TransactionPool};
#[tokio::test(flavor = "multi_thread")]
async fn test_tx_gossip() {
reth_tracing::init_test_tracing();
let provider = MockEthProvider::default();
let net = Testnet::create_with(2, provider.clone()).await;
// install request handlers
let net = net.with_eth_pool();
let handle = net.spawn();
// connect all the peers
handle.connect_peers().await;
let peer0 = &handle.peers()[0];
let peer1 = &handle.peers()[1];
let peer0_pool = peer0.pool().unwrap();
let mut peer0_tx_listener = peer0.pool().unwrap().pending_transactions_listener();
let mut peer1_tx_listener = peer1.pool().unwrap().pending_transactions_listener();
let mut gen = TransactionGenerator::new(thread_rng());
let tx = gen.gen_eip1559_pooled();
// ensure the sender has balance
let sender = tx.sender();
provider.add_account(sender, ExtendedAccount::new(0, U256::from(100_000_000)));
// insert pending tx in peer0's pool
let hash = peer0_pool.add_external_transaction(tx).await.unwrap();
let inserted = peer0_tx_listener.recv().await.unwrap();
assert_eq!(inserted, hash);
// ensure tx is gossiped to peer1
let received = peer1_tx_listener.recv().await.unwrap();
assert_eq!(received, hash);
}