1use crate::{
4 builder::ETH_REQUEST_CHANNEL_CAPACITY,
5 error::NetworkError,
6 eth_requests::EthRequestHandler,
7 protocol::IntoRlpxSubProtocol,
8 transactions::{
9 config::TransactionPropagationKind, TransactionsHandle, TransactionsManager,
10 TransactionsManagerConfig,
11 },
12 NetworkConfig, NetworkConfigBuilder, NetworkHandle, NetworkManager,
13};
14use alloy_consensus::transaction::PooledTransaction;
15use futures::{FutureExt, StreamExt};
16use pin_project::pin_project;
17use reth_chainspec::{ChainSpecProvider, EthereumHardforks, Hardforks};
18use reth_eth_wire::{
19 protocol::Protocol, DisconnectReason, EthNetworkPrimitives, HelloMessageWithProtocols,
20};
21use reth_ethereum_primitives::TransactionSigned;
22use reth_network_api::{
23 events::{PeerEvent, SessionInfo},
24 test_utils::{PeersHandle, PeersHandleProvider},
25 NetworkEvent, NetworkEventListenerProvider, NetworkInfo, Peers,
26};
27use reth_network_peers::PeerId;
28use reth_storage_api::{
29 noop::NoopProvider, BlockReader, BlockReaderIdExt, HeaderProvider, StateProviderFactory,
30};
31use reth_tasks::TokioTaskExecutor;
32use reth_tokio_util::EventStream;
33use reth_transaction_pool::{
34 blobstore::InMemoryBlobStore,
35 test_utils::{TestPool, TestPoolBuilder},
36 EthTransactionPool, PoolTransaction, TransactionPool, TransactionValidationTaskExecutor,
37};
38use secp256k1::SecretKey;
39use std::{
40 fmt,
41 future::Future,
42 net::{Ipv4Addr, SocketAddr, SocketAddrV4},
43 pin::Pin,
44 task::{Context, Poll},
45};
46use tokio::{
47 sync::{
48 mpsc::{channel, unbounded_channel},
49 oneshot,
50 },
51 task::JoinHandle,
52};
53
54pub struct Testnet<C, Pool> {
56 peers: Vec<Peer<C, Pool>>,
58}
59
60impl<C> Testnet<C, TestPool>
63where
64 C: BlockReader + HeaderProvider + Clone + 'static + ChainSpecProvider<ChainSpec: Hardforks>,
65{
66 pub async fn create_with(num_peers: usize, provider: C) -> Self {
68 Self::try_create_with(num_peers, provider).await.unwrap()
69 }
70
71 pub async fn try_create_with(num_peers: usize, provider: C) -> Result<Self, NetworkError> {
73 let mut this = Self { peers: Vec::with_capacity(num_peers) };
74 for _ in 0..num_peers {
75 let config = PeerConfig::new(provider.clone());
76 this.add_peer_with_config(config).await?;
77 }
78 Ok(this)
79 }
80
81 pub async fn extend_peer_with_config(
84 &mut self,
85 configs: impl IntoIterator<Item = PeerConfig<C>>,
86 ) -> Result<(), NetworkError> {
87 let peers = configs.into_iter().map(|c| c.launch()).collect::<Vec<_>>();
88 let peers = futures::future::join_all(peers).await;
89 for peer in peers {
90 self.peers.push(peer?);
91 }
92 Ok(())
93 }
94}
95
96impl<C, Pool> Testnet<C, Pool>
97where
98 C: BlockReader + HeaderProvider + Clone + 'static,
99 Pool: TransactionPool,
100{
101 pub fn peers_mut(&mut self) -> &mut [Peer<C, Pool>] {
103 &mut self.peers
104 }
105
106 pub fn peers(&self) -> &[Peer<C, Pool>] {
108 &self.peers
109 }
110
111 pub fn remove_peer(&mut self, index: usize) -> Peer<C, Pool> {
116 self.peers.remove(index)
117 }
118
119 pub fn peers_iter_mut(&mut self) -> impl Iterator<Item = &mut Peer<C, Pool>> + '_ {
121 self.peers.iter_mut()
122 }
123
124 pub fn peers_iter(&self) -> impl Iterator<Item = &Peer<C, Pool>> + '_ {
126 self.peers.iter()
127 }
128
129 pub async fn add_peer_with_config(
131 &mut self,
132 config: PeerConfig<C>,
133 ) -> Result<(), NetworkError> {
134 let PeerConfig { config, client, secret_key } = config;
135
136 let network = NetworkManager::new(config).await?;
137 let peer = Peer {
138 network,
139 client,
140 secret_key,
141 request_handler: None,
142 transactions_manager: None,
143 pool: None,
144 };
145 self.peers.push(peer);
146 Ok(())
147 }
148
149 pub fn handles(&self) -> impl Iterator<Item = NetworkHandle<EthNetworkPrimitives>> + '_ {
151 self.peers.iter().map(|p| p.handle())
152 }
153
154 pub fn map_pool<F, P>(self, f: F) -> Testnet<C, P>
156 where
157 F: Fn(Peer<C, Pool>) -> Peer<C, P>,
158 P: TransactionPool,
159 {
160 Testnet { peers: self.peers.into_iter().map(f).collect() }
161 }
162
163 pub fn for_each<F>(&self, f: F)
165 where
166 F: Fn(&Peer<C, Pool>),
167 {
168 self.peers.iter().for_each(f)
169 }
170
171 pub fn for_each_mut<F>(&mut self, f: F)
173 where
174 F: FnMut(&mut Peer<C, Pool>),
175 {
176 self.peers.iter_mut().for_each(f)
177 }
178}
179
180impl<C, Pool> Testnet<C, Pool>
181where
182 C: ChainSpecProvider<ChainSpec: EthereumHardforks>
183 + StateProviderFactory
184 + BlockReaderIdExt
185 + HeaderProvider
186 + Clone
187 + 'static,
188 Pool: TransactionPool,
189{
190 pub fn with_eth_pool(self) -> Testnet<C, EthTransactionPool<C, InMemoryBlobStore>> {
192 self.map_pool(|peer| {
193 let blob_store = InMemoryBlobStore::default();
194 let pool = TransactionValidationTaskExecutor::eth(
195 peer.client.clone(),
196 blob_store.clone(),
197 TokioTaskExecutor::default(),
198 );
199 peer.map_transactions_manager(EthTransactionPool::eth_pool(
200 pool,
201 blob_store,
202 Default::default(),
203 ))
204 })
205 }
206
207 pub fn with_eth_pool_config(
209 self,
210 tx_manager_config: TransactionsManagerConfig,
211 ) -> Testnet<C, EthTransactionPool<C, InMemoryBlobStore>> {
212 self.with_eth_pool_config_and_policy(tx_manager_config, Default::default())
213 }
214
215 pub fn with_eth_pool_config_and_policy(
217 self,
218 tx_manager_config: TransactionsManagerConfig,
219 policy: TransactionPropagationKind,
220 ) -> Testnet<C, EthTransactionPool<C, InMemoryBlobStore>> {
221 self.map_pool(|peer| {
222 let blob_store = InMemoryBlobStore::default();
223 let pool = TransactionValidationTaskExecutor::eth(
224 peer.client.clone(),
225 blob_store.clone(),
226 TokioTaskExecutor::default(),
227 );
228
229 peer.map_transactions_manager_with(
230 EthTransactionPool::eth_pool(pool, blob_store, Default::default()),
231 tx_manager_config.clone(),
232 policy,
233 )
234 })
235 }
236}
237
238impl<C, Pool> Testnet<C, Pool>
239where
240 C: BlockReader<
241 Block = reth_ethereum_primitives::Block,
242 Receipt = reth_ethereum_primitives::Receipt,
243 Header = alloy_consensus::Header,
244 > + HeaderProvider
245 + Clone
246 + Unpin
247 + 'static,
248 Pool: TransactionPool<
249 Transaction: PoolTransaction<Consensus = TransactionSigned, Pooled = PooledTransaction>,
250 > + Unpin
251 + 'static,
252{
253 pub fn spawn(self) -> TestnetHandle<C, Pool> {
255 let (tx, rx) = oneshot::channel::<oneshot::Sender<Self>>();
256 let peers = self.peers.iter().map(|peer| peer.peer_handle()).collect::<Vec<_>>();
257 let mut net = self;
258 let handle = tokio::task::spawn(async move {
259 let mut tx = None;
260 tokio::select! {
261 _ = &mut net => {}
262 inc = rx => {
263 tx = inc.ok();
264 }
265 }
266 if let Some(tx) = tx {
267 let _ = tx.send(net);
268 }
269 });
270
271 TestnetHandle { _handle: handle, peers, terminate: tx }
272 }
273}
274
275impl Testnet<NoopProvider, TestPool> {
276 pub async fn create(num_peers: usize) -> Self {
278 Self::try_create(num_peers).await.unwrap()
279 }
280
281 pub async fn try_create(num_peers: usize) -> Result<Self, NetworkError> {
283 let mut this = Self::default();
284
285 this.extend_peer_with_config((0..num_peers).map(|_| Default::default())).await?;
286 Ok(this)
287 }
288
289 pub async fn add_peer(&mut self) -> Result<(), NetworkError> {
291 self.add_peer_with_config(Default::default()).await
292 }
293}
294
295impl<C, Pool> Default for Testnet<C, Pool> {
296 fn default() -> Self {
297 Self { peers: Vec::new() }
298 }
299}
300
301impl<C, Pool> fmt::Debug for Testnet<C, Pool> {
302 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
303 f.debug_struct("Testnet {{}}").finish_non_exhaustive()
304 }
305}
306
307impl<C, Pool> Future for Testnet<C, Pool>
308where
309 C: BlockReader<
310 Block = reth_ethereum_primitives::Block,
311 Receipt = reth_ethereum_primitives::Receipt,
312 Header = alloy_consensus::Header,
313 > + HeaderProvider
314 + Unpin
315 + 'static,
316 Pool: TransactionPool<
317 Transaction: PoolTransaction<Consensus = TransactionSigned, Pooled = PooledTransaction>,
318 > + Unpin
319 + 'static,
320{
321 type Output = ();
322
323 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
324 let this = self.get_mut();
325 for peer in &mut this.peers {
326 let _ = peer.poll_unpin(cx);
327 }
328 Poll::Pending
329 }
330}
331
332#[derive(Debug)]
334pub struct TestnetHandle<C, Pool> {
335 _handle: JoinHandle<()>,
336 peers: Vec<PeerHandle<Pool>>,
337 terminate: oneshot::Sender<oneshot::Sender<Testnet<C, Pool>>>,
338}
339
340impl<C, Pool> TestnetHandle<C, Pool> {
343 pub async fn terminate(self) -> Testnet<C, Pool> {
345 let (tx, rx) = oneshot::channel();
346 self.terminate.send(tx).unwrap();
347 rx.await.unwrap()
348 }
349
350 pub fn peers(&self) -> &[PeerHandle<Pool>] {
352 &self.peers
353 }
354
355 pub async fn connect_peers(&self) {
361 if self.peers.len() < 2 {
362 return
363 }
364
365 let streams =
367 self.peers.iter().map(|handle| NetworkEventStream::new(handle.event_listener()));
368
369 for (idx, handle) in self.peers.iter().enumerate().take(self.peers.len() - 1) {
371 for idx in (idx + 1)..self.peers.len() {
372 let neighbour = &self.peers[idx];
373 handle.network.add_peer(*neighbour.peer_id(), neighbour.local_addr());
374 }
375 }
376
377 let num_sessions_per_peer = self.peers.len() - 1;
379 let fut = streams.into_iter().map(|mut stream| async move {
380 stream.take_session_established(num_sessions_per_peer).await
381 });
382
383 futures::future::join_all(fut).await;
384 }
385}
386
387#[pin_project]
389#[derive(Debug)]
390pub struct Peer<C, Pool = TestPool> {
391 #[pin]
392 network: NetworkManager<EthNetworkPrimitives>,
393 #[pin]
394 request_handler: Option<EthRequestHandler<C, EthNetworkPrimitives>>,
395 #[pin]
396 transactions_manager: Option<TransactionsManager<Pool, EthNetworkPrimitives>>,
397 pool: Option<Pool>,
398 client: C,
399 secret_key: SecretKey,
400}
401
402impl<C, Pool> Peer<C, Pool>
405where
406 C: BlockReader + HeaderProvider + Clone + 'static,
407 Pool: TransactionPool,
408{
409 pub fn num_peers(&self) -> usize {
411 self.network.num_connected_peers()
412 }
413
414 pub fn add_rlpx_sub_protocol(&mut self, protocol: impl IntoRlpxSubProtocol) {
416 self.network.add_rlpx_sub_protocol(protocol);
417 }
418
419 pub fn peer_handle(&self) -> PeerHandle<Pool> {
421 PeerHandle {
422 network: self.network.handle().clone(),
423 pool: self.pool.clone(),
424 transactions: self.transactions_manager.as_ref().map(|mgr| mgr.handle()),
425 }
426 }
427
428 pub const fn local_addr(&self) -> SocketAddr {
430 self.network.local_addr()
431 }
432
433 pub fn peer_id(&self) -> PeerId {
435 *self.network.peer_id()
436 }
437
438 pub const fn network_mut(&mut self) -> &mut NetworkManager<EthNetworkPrimitives> {
440 &mut self.network
441 }
442
443 pub fn handle(&self) -> NetworkHandle<EthNetworkPrimitives> {
445 self.network.handle().clone()
446 }
447
448 pub const fn pool(&self) -> Option<&Pool> {
450 self.pool.as_ref()
451 }
452
453 pub fn install_request_handler(&mut self) {
455 let (tx, rx) = channel(ETH_REQUEST_CHANNEL_CAPACITY);
456 self.network.set_eth_request_handler(tx);
457 let peers = self.network.peers_handle();
458 let request_handler = EthRequestHandler::new(self.client.clone(), peers, rx);
459 self.request_handler = Some(request_handler);
460 }
461
462 pub fn install_transactions_manager(&mut self, pool: Pool) {
464 let (tx, rx) = unbounded_channel();
465 self.network.set_transactions(tx);
466 let transactions_manager = TransactionsManager::new(
467 self.handle(),
468 pool.clone(),
469 rx,
470 TransactionsManagerConfig::default(),
471 );
472 self.transactions_manager = Some(transactions_manager);
473 self.pool = Some(pool);
474 }
475
476 pub fn map_transactions_manager<P>(self, pool: P) -> Peer<C, P>
478 where
479 P: TransactionPool,
480 {
481 let Self { mut network, request_handler, client, secret_key, .. } = self;
482 let (tx, rx) = unbounded_channel();
483 network.set_transactions(tx);
484 let transactions_manager = TransactionsManager::new(
485 network.handle().clone(),
486 pool.clone(),
487 rx,
488 TransactionsManagerConfig::default(),
489 );
490 Peer {
491 network,
492 request_handler,
493 transactions_manager: Some(transactions_manager),
494 pool: Some(pool),
495 client,
496 secret_key,
497 }
498 }
499
500 pub fn map_transactions_manager_with_config<P>(
502 self,
503 pool: P,
504 config: TransactionsManagerConfig,
505 ) -> Peer<C, P>
506 where
507 P: TransactionPool,
508 {
509 self.map_transactions_manager_with(pool, config, Default::default())
510 }
511
512 pub fn map_transactions_manager_with<P>(
514 self,
515 pool: P,
516 config: TransactionsManagerConfig,
517 policy: TransactionPropagationKind,
518 ) -> Peer<C, P>
519 where
520 P: TransactionPool,
521 {
522 let Self { mut network, request_handler, client, secret_key, .. } = self;
523 let (tx, rx) = unbounded_channel();
524 network.set_transactions(tx);
525
526 let transactions_manager = TransactionsManager::with_policy(
527 network.handle().clone(),
528 pool.clone(),
529 rx,
530 config,
531 policy,
532 );
533
534 Peer {
535 network,
536 request_handler,
537 transactions_manager: Some(transactions_manager),
538 pool: Some(pool),
539 client,
540 secret_key,
541 }
542 }
543}
544
545impl<C> Peer<C>
546where
547 C: BlockReader + HeaderProvider + Clone + 'static,
548{
549 pub fn install_test_pool(&mut self) {
551 self.install_transactions_manager(TestPoolBuilder::default().into())
552 }
553}
554
555impl<C, Pool> Future for Peer<C, Pool>
556where
557 C: BlockReader<
558 Block = reth_ethereum_primitives::Block,
559 Receipt = reth_ethereum_primitives::Receipt,
560 Header = alloy_consensus::Header,
561 > + HeaderProvider
562 + Unpin
563 + 'static,
564 Pool: TransactionPool<
565 Transaction: PoolTransaction<Consensus = TransactionSigned, Pooled = PooledTransaction>,
566 > + Unpin
567 + 'static,
568{
569 type Output = ();
570
571 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
572 let this = self.project();
573
574 if let Some(request) = this.request_handler.as_pin_mut() {
575 let _ = request.poll(cx);
576 }
577
578 if let Some(tx_manager) = this.transactions_manager.as_pin_mut() {
579 let _ = tx_manager.poll(cx);
580 }
581
582 this.network.poll(cx)
583 }
584}
585
586#[derive(Debug)]
588pub struct PeerConfig<C = NoopProvider> {
589 config: NetworkConfig<C>,
590 client: C,
591 secret_key: SecretKey,
592}
593
594#[derive(Debug)]
596pub struct PeerHandle<Pool> {
597 network: NetworkHandle<EthNetworkPrimitives>,
598 transactions: Option<TransactionsHandle<EthNetworkPrimitives>>,
599 pool: Option<Pool>,
600}
601
602impl<Pool> PeerHandle<Pool> {
605 pub fn peer_id(&self) -> &PeerId {
607 self.network.peer_id()
608 }
609
610 pub fn peer_handle(&self) -> &PeersHandle {
612 self.network.peers_handle()
613 }
614
615 pub fn local_addr(&self) -> SocketAddr {
617 self.network.local_addr()
618 }
619
620 pub fn event_listener(&self) -> EventStream<NetworkEvent> {
622 self.network.event_listener()
623 }
624
625 pub const fn transactions(&self) -> Option<&TransactionsHandle> {
627 self.transactions.as_ref()
628 }
629
630 pub const fn pool(&self) -> Option<&Pool> {
632 self.pool.as_ref()
633 }
634
635 pub const fn network(&self) -> &NetworkHandle<EthNetworkPrimitives> {
637 &self.network
638 }
639}
640
641impl<C> PeerConfig<C>
644where
645 C: BlockReader + HeaderProvider + Clone + 'static,
646{
647 pub async fn launch(self) -> Result<Peer<C>, NetworkError> {
649 let Self { config, client, secret_key } = self;
650 let network = NetworkManager::new(config).await?;
651 let peer = Peer {
652 network,
653 client,
654 secret_key,
655 request_handler: None,
656 transactions_manager: None,
657 pool: None,
658 };
659 Ok(peer)
660 }
661
662 pub fn new(client: C) -> Self
665 where
666 C: ChainSpecProvider<ChainSpec: Hardforks>,
667 {
668 let secret_key = SecretKey::new(&mut rand_08::thread_rng());
669 let config = Self::network_config_builder(secret_key).build(client.clone());
670 Self { config, client, secret_key }
671 }
672
673 pub fn with_secret_key(client: C, secret_key: SecretKey) -> Self
676 where
677 C: ChainSpecProvider<ChainSpec: Hardforks>,
678 {
679 let config = Self::network_config_builder(secret_key).build(client.clone());
680 Self { config, client, secret_key }
681 }
682
683 pub fn with_protocols(client: C, protocols: impl IntoIterator<Item = Protocol>) -> Self
685 where
686 C: ChainSpecProvider<ChainSpec: Hardforks>,
687 {
688 let secret_key = SecretKey::new(&mut rand_08::thread_rng());
689
690 let builder = Self::network_config_builder(secret_key);
691 let hello_message =
692 HelloMessageWithProtocols::builder(builder.get_peer_id()).protocols(protocols).build();
693 let config = builder.hello_message(hello_message).build(client.clone());
694
695 Self { config, client, secret_key }
696 }
697
698 fn network_config_builder(secret_key: SecretKey) -> NetworkConfigBuilder {
699 NetworkConfigBuilder::new(secret_key)
700 .listener_addr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)))
701 .discovery_addr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)))
702 .disable_dns_discovery()
703 .disable_discv4_discovery()
704 }
705}
706
707impl Default for PeerConfig {
708 fn default() -> Self {
709 Self::new(NoopProvider::default())
710 }
711}
712
713#[derive(Debug)]
717pub struct NetworkEventStream {
718 inner: EventStream<NetworkEvent>,
719}
720
721impl NetworkEventStream {
724 pub const fn new(inner: EventStream<NetworkEvent>) -> Self {
726 Self { inner }
727 }
728
729 pub async fn next_session_closed(&mut self) -> Option<(PeerId, Option<DisconnectReason>)> {
731 while let Some(ev) = self.inner.next().await {
732 if let NetworkEvent::Peer(PeerEvent::SessionClosed { peer_id, reason }) = ev {
733 return Some((peer_id, reason))
734 }
735 }
736 None
737 }
738
739 pub async fn next_session_established(&mut self) -> Option<PeerId> {
741 while let Some(ev) = self.inner.next().await {
742 match ev {
743 NetworkEvent::ActivePeerSession { info, .. } |
744 NetworkEvent::Peer(PeerEvent::SessionEstablished(info)) => {
745 return Some(info.peer_id)
746 }
747 _ => {}
748 }
749 }
750 None
751 }
752
753 pub async fn take_session_established(&mut self, mut num: usize) -> Vec<PeerId> {
755 if num == 0 {
756 return Vec::new();
757 }
758 let mut peers = Vec::with_capacity(num);
759 while let Some(ev) = self.inner.next().await {
760 if let NetworkEvent::ActivePeerSession { info: SessionInfo { peer_id, .. }, .. } = ev {
761 peers.push(peer_id);
762 num -= 1;
763 if num == 0 {
764 return peers;
765 }
766 }
767 }
768 peers
769 }
770
771 pub async fn peer_added_and_established(&mut self) -> Option<PeerId> {
775 let peer_id = match self.inner.next().await {
776 Some(NetworkEvent::Peer(PeerEvent::PeerAdded(peer_id))) => peer_id,
777 _ => return None,
778 };
779
780 match self.inner.next().await {
781 Some(NetworkEvent::ActivePeerSession {
782 info: SessionInfo { peer_id: peer_id2, .. },
783 ..
784 }) => {
785 debug_assert_eq!(
786 peer_id, peer_id2,
787 "PeerAdded peer_id {peer_id} does not match SessionEstablished peer_id {peer_id2}"
788 );
789 Some(peer_id)
790 }
791 _ => None,
792 }
793 }
794
795 pub async fn peer_added(&mut self) -> Option<PeerId> {
797 let peer_id = match self.inner.next().await {
798 Some(NetworkEvent::Peer(PeerEvent::PeerAdded(peer_id))) => peer_id,
799 _ => return None,
800 };
801
802 Some(peer_id)
803 }
804
805 pub async fn peer_removed(&mut self) -> Option<PeerId> {
807 let peer_id = match self.inner.next().await {
808 Some(NetworkEvent::Peer(PeerEvent::PeerRemoved(peer_id))) => peer_id,
809 _ => return None,
810 };
811
812 Some(peer_id)
813 }
814}