reth_network/test_utils/
testnet.rs

1//! A network implementation for testing purposes.
2
3use 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
54/// A test network consisting of multiple peers.
55pub struct Testnet<C, Pool> {
56    /// All running peers in the network.
57    peers: Vec<Peer<C, Pool>>,
58}
59
60// === impl Testnet ===
61
62impl<C> Testnet<C, TestPool>
63where
64    C: BlockReader + HeaderProvider + Clone + 'static + ChainSpecProvider<ChainSpec: Hardforks>,
65{
66    /// Same as [`Self::try_create_with`] but panics on error
67    pub async fn create_with(num_peers: usize, provider: C) -> Self {
68        Self::try_create_with(num_peers, provider).await.unwrap()
69    }
70
71    /// Creates a new [`Testnet`] with the given number of peers and the provider.
72    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    /// Extend the list of peers with new peers that are configured with each of the given
82    /// [`PeerConfig`]s.
83    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    /// Return a mutable slice of all peers.
102    pub fn peers_mut(&mut self) -> &mut [Peer<C, Pool>] {
103        &mut self.peers
104    }
105
106    /// Return a slice of all peers.
107    pub fn peers(&self) -> &[Peer<C, Pool>] {
108        &self.peers
109    }
110
111    /// Remove a peer from the [`Testnet`] and return it.
112    ///
113    /// # Panics
114    /// If the index is out of bounds.
115    pub fn remove_peer(&mut self, index: usize) -> Peer<C, Pool> {
116        self.peers.remove(index)
117    }
118
119    /// Return a mutable iterator over all peers.
120    pub fn peers_iter_mut(&mut self) -> impl Iterator<Item = &mut Peer<C, Pool>> + '_ {
121        self.peers.iter_mut()
122    }
123
124    /// Return an iterator over all peers.
125    pub fn peers_iter(&self) -> impl Iterator<Item = &Peer<C, Pool>> + '_ {
126        self.peers.iter()
127    }
128
129    /// Add a peer to the [`Testnet`] with the given [`PeerConfig`].
130    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    /// Returns all handles to the networks
150    pub fn handles(&self) -> impl Iterator<Item = NetworkHandle<EthNetworkPrimitives>> + '_ {
151        self.peers.iter().map(|p| p.handle())
152    }
153
154    /// Maps the pool of each peer with the given closure
155    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    /// Apply a closure on each peer
164    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    /// Apply a closure on each peer
172    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    /// Installs an eth pool on each peer
191    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    /// Installs an eth pool on each peer with custom transaction manager config
208    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    /// Installs an eth pool on each peer with custom transaction manager config and policy.
216    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    /// Spawns the testnet to a separate task
257    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    /// Same as [`Self::try_create`] but panics on error
280    pub async fn create(num_peers: usize) -> Self {
281        Self::try_create(num_peers).await.unwrap()
282    }
283
284    /// Creates a new [`Testnet`] with the given number of peers
285    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    /// Add a peer to the [`Testnet`]
293    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/// A handle to a [`Testnet`] that can be shared.
339#[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
346// === impl TestnetHandle ===
347
348impl<C, Pool> TestnetHandle<C, Pool> {
349    /// Terminates the task and returns the [`Testnet`] back.
350    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    /// Returns the [`PeerHandle`]s of this [`Testnet`].
357    pub fn peers(&self) -> &[PeerHandle<Pool>] {
358        &self.peers
359    }
360
361    /// Connects all peers with each other.
362    ///
363    /// This establishes sessions concurrently between all peers.
364    ///
365    /// Returns once all sessions are established.
366    pub async fn connect_peers(&self) {
367        if self.peers.len() < 2 {
368            return
369        }
370
371        // add an event stream for _each_ peer
372        let streams =
373            self.peers.iter().map(|handle| NetworkEventStream::new(handle.event_listener()));
374
375        // add all peers to each other
376        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        // await all sessions to be established
384        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/// A peer in the [`Testnet`].
394#[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
408// === impl Peer ===
409
410impl<C, Pool> Peer<C, Pool>
411where
412    C: BlockReader + HeaderProvider + Clone + 'static,
413    Pool: TransactionPool,
414{
415    /// Returns the number of connected peers.
416    pub fn num_peers(&self) -> usize {
417        self.network.num_connected_peers()
418    }
419
420    /// Adds an additional protocol handler to the peer.
421    pub fn add_rlpx_sub_protocol(&mut self, protocol: impl IntoRlpxSubProtocol) {
422        self.network.add_rlpx_sub_protocol(protocol);
423    }
424
425    /// Returns a handle to the peer's network.
426    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    /// The address that listens for incoming connections.
435    pub const fn local_addr(&self) -> SocketAddr {
436        self.network.local_addr()
437    }
438
439    /// The [`PeerId`] of this peer.
440    pub fn peer_id(&self) -> PeerId {
441        *self.network.peer_id()
442    }
443
444    /// Returns mutable access to the network.
445    pub const fn network_mut(&mut self) -> &mut NetworkManager<EthNetworkPrimitives> {
446        &mut self.network
447    }
448
449    /// Returns the [`NetworkHandle`] of this peer.
450    pub fn handle(&self) -> NetworkHandle<EthNetworkPrimitives> {
451        self.network.handle().clone()
452    }
453
454    /// Returns the [`TestPool`] of this peer.
455    pub const fn pool(&self) -> Option<&Pool> {
456        self.pool.as_ref()
457    }
458
459    /// Set a new request handler that's connected to the peer's network
460    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    /// Set a new transactions manager that's connected to the peer's network
469    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    /// Set a new transactions manager that's connected to the peer's network
483    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    /// Map transactions manager with custom config
507    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    /// Map transactions manager with custom config and the given policy.
519    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    /// Installs a new [`TestPool`]
559    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/// A helper config for setting up the reth networking stack.
599#[derive(Debug)]
600pub struct PeerConfig<C = NoopProvider> {
601    config: NetworkConfig<C>,
602    client: C,
603    secret_key: SecretKey,
604}
605
606/// A handle to a peer in the [`Testnet`].
607#[derive(Debug)]
608pub struct PeerHandle<Pool> {
609    network: NetworkHandle<EthNetworkPrimitives>,
610    transactions: Option<TransactionsHandle<EthNetworkPrimitives>>,
611    pool: Option<Pool>,
612}
613
614// === impl PeerHandle ===
615
616impl<Pool> PeerHandle<Pool> {
617    /// Returns the [`PeerId`] used in the network.
618    pub fn peer_id(&self) -> &PeerId {
619        self.network.peer_id()
620    }
621
622    /// Returns the [`PeersHandle`] from the network.
623    pub fn peer_handle(&self) -> &PeersHandle {
624        self.network.peers_handle()
625    }
626
627    /// Returns the local socket as configured for the network.
628    pub fn local_addr(&self) -> SocketAddr {
629        self.network.local_addr()
630    }
631
632    /// Creates a new [`NetworkEvent`] listener channel.
633    pub fn event_listener(&self) -> EventStream<NetworkEvent> {
634        self.network.event_listener()
635    }
636
637    /// Returns the [`TransactionsHandle`] of this peer.
638    pub const fn transactions(&self) -> Option<&TransactionsHandle> {
639        self.transactions.as_ref()
640    }
641
642    /// Returns the [`TestPool`] of this peer.
643    pub const fn pool(&self) -> Option<&Pool> {
644        self.pool.as_ref()
645    }
646
647    /// Returns the [`NetworkHandle`] of this peer.
648    pub const fn network(&self) -> &NetworkHandle<EthNetworkPrimitives> {
649        &self.network
650    }
651}
652
653// === impl PeerConfig ===
654
655impl<C> PeerConfig<C>
656where
657    C: BlockReader + HeaderProvider + Clone + 'static,
658{
659    /// Launches the network and returns the [Peer] that manages it
660    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    /// Initialize the network with a random secret key, allowing the devp2p and discovery to bind
675    /// to any available IP and port.
676    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    /// Initialize the network with a given secret key, allowing devp2p and discovery to bind any
686    /// available IP and port.
687    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    /// Initialize the network with a given capabilities.
696    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/// A helper type to await network events
726///
727/// This makes it easier to await established connections
728#[derive(Debug)]
729pub struct NetworkEventStream {
730    inner: EventStream<NetworkEvent>,
731}
732
733// === impl NetworkEventStream ===
734
735impl NetworkEventStream {
736    /// Create a new [`NetworkEventStream`] from the given network event receiver stream.
737    pub const fn new(inner: EventStream<NetworkEvent>) -> Self {
738        Self { inner }
739    }
740
741    /// Awaits the next event for a session to be closed
742    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    /// Awaits the next event for an established session
752    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    /// Awaits the next `num` events for an established session
766    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    /// Ensures that the first two events are a [`NetworkEvent::Peer`] and
784    /// [`PeerEvent::PeerAdded`][`NetworkEvent::ActivePeerSession`], returning the [`PeerId`] of the
785    /// established session.
786    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    /// Awaits the next event for a peer added.
808    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    /// Awaits the next event for a peer removed.
818    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}