1use crate::{
4 builder::ETH_REQUEST_CHANNEL_CAPACITY,
5 error::NetworkError,
6 eth_requests::EthRequestHandler,
7 protocol::IntoRlpxSubProtocol,
8 transactions::{
9 config::{StrictEthAnnouncementFilter, TransactionPropagationKind},
10 policy::NetworkPolicies,
11 TransactionsHandle, TransactionsManager, TransactionsManagerConfig,
12 },
13 NetworkConfig, NetworkConfigBuilder, NetworkHandle, NetworkManager,
14};
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::{PooledTransactionVariant, 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<
250 Consensus = TransactionSigned,
251 Pooled = PooledTransactionVariant,
252 >,
253 > + Unpin
254 + 'static,
255{
256 pub fn spawn(self) -> TestnetHandle<C, Pool> {
258 let (tx, rx) = oneshot::channel::<oneshot::Sender<Self>>();
259 let peers = self.peers.iter().map(|peer| peer.peer_handle()).collect::<Vec<_>>();
260 let mut net = self;
261 let handle = tokio::task::spawn(async move {
262 let mut tx = None;
263 tokio::select! {
264 _ = &mut net => {}
265 inc = rx => {
266 tx = inc.ok();
267 }
268 }
269 if let Some(tx) = tx {
270 let _ = tx.send(net);
271 }
272 });
273
274 TestnetHandle { _handle: handle, peers, terminate: tx }
275 }
276}
277
278impl Testnet<NoopProvider, TestPool> {
279 pub async fn create(num_peers: usize) -> Self {
281 Self::try_create(num_peers).await.unwrap()
282 }
283
284 pub async fn try_create(num_peers: usize) -> Result<Self, NetworkError> {
286 let mut this = Self::default();
287
288 this.extend_peer_with_config((0..num_peers).map(|_| Default::default())).await?;
289 Ok(this)
290 }
291
292 pub async fn add_peer(&mut self) -> Result<(), NetworkError> {
294 self.add_peer_with_config(Default::default()).await
295 }
296}
297
298impl<C, Pool> Default for Testnet<C, Pool> {
299 fn default() -> Self {
300 Self { peers: Vec::new() }
301 }
302}
303
304impl<C, Pool> fmt::Debug for Testnet<C, Pool> {
305 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
306 f.debug_struct("Testnet {{}}").finish_non_exhaustive()
307 }
308}
309
310impl<C, Pool> Future for Testnet<C, Pool>
311where
312 C: BlockReader<
313 Block = reth_ethereum_primitives::Block,
314 Receipt = reth_ethereum_primitives::Receipt,
315 Header = alloy_consensus::Header,
316 > + HeaderProvider
317 + Unpin
318 + 'static,
319 Pool: TransactionPool<
320 Transaction: PoolTransaction<
321 Consensus = TransactionSigned,
322 Pooled = PooledTransactionVariant,
323 >,
324 > + Unpin
325 + 'static,
326{
327 type Output = ();
328
329 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
330 let this = self.get_mut();
331 for peer in &mut this.peers {
332 let _ = peer.poll_unpin(cx);
333 }
334 Poll::Pending
335 }
336}
337
338#[derive(Debug)]
340pub struct TestnetHandle<C, Pool> {
341 _handle: JoinHandle<()>,
342 peers: Vec<PeerHandle<Pool>>,
343 terminate: oneshot::Sender<oneshot::Sender<Testnet<C, Pool>>>,
344}
345
346impl<C, Pool> TestnetHandle<C, Pool> {
349 pub async fn terminate(self) -> Testnet<C, Pool> {
351 let (tx, rx) = oneshot::channel();
352 self.terminate.send(tx).unwrap();
353 rx.await.unwrap()
354 }
355
356 pub fn peers(&self) -> &[PeerHandle<Pool>] {
358 &self.peers
359 }
360
361 pub async fn connect_peers(&self) {
367 if self.peers.len() < 2 {
368 return
369 }
370
371 let streams =
373 self.peers.iter().map(|handle| NetworkEventStream::new(handle.event_listener()));
374
375 for (idx, handle) in self.peers.iter().enumerate().take(self.peers.len() - 1) {
377 for idx in (idx + 1)..self.peers.len() {
378 let neighbour = &self.peers[idx];
379 handle.network.add_peer(*neighbour.peer_id(), neighbour.local_addr());
380 }
381 }
382
383 let num_sessions_per_peer = self.peers.len() - 1;
385 let fut = streams.into_iter().map(|mut stream| async move {
386 stream.take_session_established(num_sessions_per_peer).await
387 });
388
389 futures::future::join_all(fut).await;
390 }
391}
392
393#[pin_project]
395#[derive(Debug)]
396pub struct Peer<C, Pool = TestPool> {
397 #[pin]
398 network: NetworkManager<EthNetworkPrimitives>,
399 #[pin]
400 request_handler: Option<EthRequestHandler<C, EthNetworkPrimitives>>,
401 #[pin]
402 transactions_manager: Option<TransactionsManager<Pool, EthNetworkPrimitives>>,
403 pool: Option<Pool>,
404 client: C,
405 secret_key: SecretKey,
406}
407
408impl<C, Pool> Peer<C, Pool>
411where
412 C: BlockReader + HeaderProvider + Clone + 'static,
413 Pool: TransactionPool,
414{
415 pub fn num_peers(&self) -> usize {
417 self.network.num_connected_peers()
418 }
419
420 pub fn add_rlpx_sub_protocol(&mut self, protocol: impl IntoRlpxSubProtocol) {
422 self.network.add_rlpx_sub_protocol(protocol);
423 }
424
425 pub fn peer_handle(&self) -> PeerHandle<Pool> {
427 PeerHandle {
428 network: self.network.handle().clone(),
429 pool: self.pool.clone(),
430 transactions: self.transactions_manager.as_ref().map(|mgr| mgr.handle()),
431 }
432 }
433
434 pub const fn local_addr(&self) -> SocketAddr {
436 self.network.local_addr()
437 }
438
439 pub fn peer_id(&self) -> PeerId {
441 *self.network.peer_id()
442 }
443
444 pub const fn network_mut(&mut self) -> &mut NetworkManager<EthNetworkPrimitives> {
446 &mut self.network
447 }
448
449 pub fn handle(&self) -> NetworkHandle<EthNetworkPrimitives> {
451 self.network.handle().clone()
452 }
453
454 pub const fn pool(&self) -> Option<&Pool> {
456 self.pool.as_ref()
457 }
458
459 pub fn install_request_handler(&mut self) {
461 let (tx, rx) = channel(ETH_REQUEST_CHANNEL_CAPACITY);
462 self.network.set_eth_request_handler(tx);
463 let peers = self.network.peers_handle();
464 let request_handler = EthRequestHandler::new(self.client.clone(), peers, rx);
465 self.request_handler = Some(request_handler);
466 }
467
468 pub fn install_transactions_manager(&mut self, pool: Pool) {
470 let (tx, rx) = unbounded_channel();
471 self.network.set_transactions(tx);
472 let transactions_manager = TransactionsManager::new(
473 self.handle(),
474 pool.clone(),
475 rx,
476 TransactionsManagerConfig::default(),
477 );
478 self.transactions_manager = Some(transactions_manager);
479 self.pool = Some(pool);
480 }
481
482 pub fn map_transactions_manager<P>(self, pool: P) -> Peer<C, P>
484 where
485 P: TransactionPool,
486 {
487 let Self { mut network, request_handler, client, secret_key, .. } = self;
488 let (tx, rx) = unbounded_channel();
489 network.set_transactions(tx);
490 let transactions_manager = TransactionsManager::new(
491 network.handle().clone(),
492 pool.clone(),
493 rx,
494 TransactionsManagerConfig::default(),
495 );
496 Peer {
497 network,
498 request_handler,
499 transactions_manager: Some(transactions_manager),
500 pool: Some(pool),
501 client,
502 secret_key,
503 }
504 }
505
506 pub fn map_transactions_manager_with_config<P>(
508 self,
509 pool: P,
510 config: TransactionsManagerConfig,
511 ) -> Peer<C, P>
512 where
513 P: TransactionPool,
514 {
515 self.map_transactions_manager_with(pool, config, Default::default())
516 }
517
518 pub fn map_transactions_manager_with<P>(
520 self,
521 pool: P,
522 config: TransactionsManagerConfig,
523 policy: TransactionPropagationKind,
524 ) -> Peer<C, P>
525 where
526 P: TransactionPool,
527 {
528 let Self { mut network, request_handler, client, secret_key, .. } = self;
529 let (tx, rx) = unbounded_channel();
530 network.set_transactions(tx);
531
532 let announcement_policy = StrictEthAnnouncementFilter::default();
533 let policies = NetworkPolicies::new(policy, announcement_policy);
534
535 let transactions_manager = TransactionsManager::with_policy(
536 network.handle().clone(),
537 pool.clone(),
538 rx,
539 config,
540 policies,
541 );
542
543 Peer {
544 network,
545 request_handler,
546 transactions_manager: Some(transactions_manager),
547 pool: Some(pool),
548 client,
549 secret_key,
550 }
551 }
552}
553
554impl<C> Peer<C>
555where
556 C: BlockReader + HeaderProvider + Clone + 'static,
557{
558 pub fn install_test_pool(&mut self) {
560 self.install_transactions_manager(TestPoolBuilder::default().into())
561 }
562}
563
564impl<C, Pool> Future for Peer<C, Pool>
565where
566 C: BlockReader<
567 Block = reth_ethereum_primitives::Block,
568 Receipt = reth_ethereum_primitives::Receipt,
569 Header = alloy_consensus::Header,
570 > + HeaderProvider
571 + Unpin
572 + 'static,
573 Pool: TransactionPool<
574 Transaction: PoolTransaction<
575 Consensus = TransactionSigned,
576 Pooled = PooledTransactionVariant,
577 >,
578 > + Unpin
579 + 'static,
580{
581 type Output = ();
582
583 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
584 let this = self.project();
585
586 if let Some(request) = this.request_handler.as_pin_mut() {
587 let _ = request.poll(cx);
588 }
589
590 if let Some(tx_manager) = this.transactions_manager.as_pin_mut() {
591 let _ = tx_manager.poll(cx);
592 }
593
594 this.network.poll(cx)
595 }
596}
597
598#[derive(Debug)]
600pub struct PeerConfig<C = NoopProvider> {
601 config: NetworkConfig<C>,
602 client: C,
603 secret_key: SecretKey,
604}
605
606#[derive(Debug)]
608pub struct PeerHandle<Pool> {
609 network: NetworkHandle<EthNetworkPrimitives>,
610 transactions: Option<TransactionsHandle<EthNetworkPrimitives>>,
611 pool: Option<Pool>,
612}
613
614impl<Pool> PeerHandle<Pool> {
617 pub fn peer_id(&self) -> &PeerId {
619 self.network.peer_id()
620 }
621
622 pub fn peer_handle(&self) -> &PeersHandle {
624 self.network.peers_handle()
625 }
626
627 pub fn local_addr(&self) -> SocketAddr {
629 self.network.local_addr()
630 }
631
632 pub fn event_listener(&self) -> EventStream<NetworkEvent> {
634 self.network.event_listener()
635 }
636
637 pub const fn transactions(&self) -> Option<&TransactionsHandle> {
639 self.transactions.as_ref()
640 }
641
642 pub const fn pool(&self) -> Option<&Pool> {
644 self.pool.as_ref()
645 }
646
647 pub const fn network(&self) -> &NetworkHandle<EthNetworkPrimitives> {
649 &self.network
650 }
651}
652
653impl<C> PeerConfig<C>
656where
657 C: BlockReader + HeaderProvider + Clone + 'static,
658{
659 pub async fn launch(self) -> Result<Peer<C>, NetworkError> {
661 let Self { config, client, secret_key } = self;
662 let network = NetworkManager::new(config).await?;
663 let peer = Peer {
664 network,
665 client,
666 secret_key,
667 request_handler: None,
668 transactions_manager: None,
669 pool: None,
670 };
671 Ok(peer)
672 }
673
674 pub fn new(client: C) -> Self
677 where
678 C: ChainSpecProvider<ChainSpec: Hardforks>,
679 {
680 let secret_key = SecretKey::new(&mut rand_08::thread_rng());
681 let config = Self::network_config_builder(secret_key).build(client.clone());
682 Self { config, client, secret_key }
683 }
684
685 pub fn with_secret_key(client: C, secret_key: SecretKey) -> Self
688 where
689 C: ChainSpecProvider<ChainSpec: Hardforks>,
690 {
691 let config = Self::network_config_builder(secret_key).build(client.clone());
692 Self { config, client, secret_key }
693 }
694
695 pub fn with_protocols(client: C, protocols: impl IntoIterator<Item = Protocol>) -> Self
697 where
698 C: ChainSpecProvider<ChainSpec: Hardforks>,
699 {
700 let secret_key = SecretKey::new(&mut rand_08::thread_rng());
701
702 let builder = Self::network_config_builder(secret_key);
703 let hello_message =
704 HelloMessageWithProtocols::builder(builder.get_peer_id()).protocols(protocols).build();
705 let config = builder.hello_message(hello_message).build(client.clone());
706
707 Self { config, client, secret_key }
708 }
709
710 fn network_config_builder(secret_key: SecretKey) -> NetworkConfigBuilder {
711 NetworkConfigBuilder::new(secret_key)
712 .listener_addr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)))
713 .discovery_addr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0)))
714 .disable_dns_discovery()
715 .disable_discv4_discovery()
716 }
717}
718
719impl Default for PeerConfig {
720 fn default() -> Self {
721 Self::new(NoopProvider::default())
722 }
723}
724
725#[derive(Debug)]
729pub struct NetworkEventStream {
730 inner: EventStream<NetworkEvent>,
731}
732
733impl NetworkEventStream {
736 pub const fn new(inner: EventStream<NetworkEvent>) -> Self {
738 Self { inner }
739 }
740
741 pub async fn next_session_closed(&mut self) -> Option<(PeerId, Option<DisconnectReason>)> {
743 while let Some(ev) = self.inner.next().await {
744 if let NetworkEvent::Peer(PeerEvent::SessionClosed { peer_id, reason }) = ev {
745 return Some((peer_id, reason))
746 }
747 }
748 None
749 }
750
751 pub async fn next_session_established(&mut self) -> Option<PeerId> {
753 while let Some(ev) = self.inner.next().await {
754 match ev {
755 NetworkEvent::ActivePeerSession { info, .. } |
756 NetworkEvent::Peer(PeerEvent::SessionEstablished(info)) => {
757 return Some(info.peer_id)
758 }
759 _ => {}
760 }
761 }
762 None
763 }
764
765 pub async fn take_session_established(&mut self, mut num: usize) -> Vec<PeerId> {
767 if num == 0 {
768 return Vec::new();
769 }
770 let mut peers = Vec::with_capacity(num);
771 while let Some(ev) = self.inner.next().await {
772 if let NetworkEvent::ActivePeerSession { info: SessionInfo { peer_id, .. }, .. } = ev {
773 peers.push(peer_id);
774 num -= 1;
775 if num == 0 {
776 return peers;
777 }
778 }
779 }
780 peers
781 }
782
783 pub async fn peer_added_and_established(&mut self) -> Option<PeerId> {
787 let peer_id = match self.inner.next().await {
788 Some(NetworkEvent::Peer(PeerEvent::PeerAdded(peer_id))) => peer_id,
789 _ => return None,
790 };
791
792 match self.inner.next().await {
793 Some(NetworkEvent::ActivePeerSession {
794 info: SessionInfo { peer_id: peer_id2, .. },
795 ..
796 }) => {
797 debug_assert_eq!(
798 peer_id, peer_id2,
799 "PeerAdded peer_id {peer_id} does not match SessionEstablished peer_id {peer_id2}"
800 );
801 Some(peer_id)
802 }
803 _ => None,
804 }
805 }
806
807 pub async fn peer_added(&mut self) -> Option<PeerId> {
809 let peer_id = match self.inner.next().await {
810 Some(NetworkEvent::Peer(PeerEvent::PeerAdded(peer_id))) => peer_id,
811 _ => return None,
812 };
813
814 Some(peer_id)
815 }
816
817 pub async fn peer_removed(&mut self) -> Option<PeerId> {
819 let peer_id = match self.inner.next().await {
820 Some(NetworkEvent::Peer(PeerEvent::PeerRemoved(peer_id))) => peer_id,
821 _ => return None,
822 };
823
824 Some(peer_id)
825 }
826}