reth_network/
network.rs

1use crate::{
2    config::NetworkMode, message::PeerMessage, protocol::RlpxSubProtocol,
3    swarm::NetworkConnectionState, transactions::TransactionsHandle, FetchClient,
4};
5use alloy_primitives::B256;
6use enr::Enr;
7use futures::StreamExt;
8use parking_lot::Mutex;
9use reth_discv4::{Discv4, NatResolver};
10use reth_discv5::Discv5;
11use reth_eth_wire::{
12    BlockRangeUpdate, DisconnectReason, EthNetworkPrimitives, NetworkPrimitives,
13    NewPooledTransactionHashes, SharedTransactions,
14};
15use reth_ethereum_forks::Head;
16use reth_network_api::{
17    events::{NetworkPeersEvents, PeerEvent, PeerEventStream},
18    test_utils::{PeersHandle, PeersHandleProvider},
19    BlockDownloaderProvider, DiscoveryEvent, NetworkError, NetworkEvent,
20    NetworkEventListenerProvider, NetworkInfo, NetworkStatus, PeerInfo, PeerRequest, Peers,
21    PeersInfo,
22};
23use reth_network_p2p::sync::{NetworkSyncUpdater, SyncState, SyncStateProvider};
24use reth_network_peers::{NodeRecord, PeerId};
25use reth_network_types::{PeerAddr, PeerKind, Reputation, ReputationChangeKind};
26use reth_tokio_util::{EventSender, EventStream};
27use secp256k1::SecretKey;
28use std::{
29    net::SocketAddr,
30    sync::{
31        atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering},
32        Arc,
33    },
34};
35use tokio::sync::{
36    mpsc::{self, UnboundedSender},
37    oneshot,
38};
39use tokio_stream::wrappers::UnboundedReceiverStream;
40
41/// A _shareable_ network frontend. Used to interact with the network.
42///
43/// See also [`NetworkManager`](crate::NetworkManager).
44#[derive(Clone, Debug)]
45pub struct NetworkHandle<N: NetworkPrimitives = EthNetworkPrimitives> {
46    /// The Arc'ed delegate that contains the state.
47    inner: Arc<NetworkInner<N>>,
48}
49
50// === impl NetworkHandle ===
51
52impl<N: NetworkPrimitives> NetworkHandle<N> {
53    /// Creates a single new instance.
54    #[expect(clippy::too_many_arguments)]
55    pub(crate) fn new(
56        num_active_peers: Arc<AtomicUsize>,
57        listener_address: Arc<Mutex<SocketAddr>>,
58        to_manager_tx: UnboundedSender<NetworkHandleMessage<N>>,
59        secret_key: SecretKey,
60        local_peer_id: PeerId,
61        peers: PeersHandle,
62        network_mode: NetworkMode,
63        chain_id: Arc<AtomicU64>,
64        tx_gossip_disabled: bool,
65        discv4: Option<Discv4>,
66        discv5: Option<Discv5>,
67        event_sender: EventSender<NetworkEvent<PeerRequest<N>>>,
68        nat: Option<NatResolver>,
69    ) -> Self {
70        let inner = NetworkInner {
71            num_active_peers,
72            to_manager_tx,
73            listener_address,
74            secret_key,
75            local_peer_id,
76            peers,
77            network_mode,
78            is_syncing: Arc::new(AtomicBool::new(false)),
79            initial_sync_done: Arc::new(AtomicBool::new(false)),
80            chain_id,
81            tx_gossip_disabled,
82            discv4,
83            discv5,
84            event_sender,
85            nat,
86        };
87        Self { inner: Arc::new(inner) }
88    }
89
90    /// Returns the [`PeerId`] used in the network.
91    pub fn peer_id(&self) -> &PeerId {
92        &self.inner.local_peer_id
93    }
94
95    fn manager(&self) -> &UnboundedSender<NetworkHandleMessage<N>> {
96        &self.inner.to_manager_tx
97    }
98
99    /// Returns the mode of the network, either pow, or pos
100    pub fn mode(&self) -> &NetworkMode {
101        &self.inner.network_mode
102    }
103
104    /// Sends a [`NetworkHandleMessage`] to the manager
105    pub(crate) fn send_message(&self, msg: NetworkHandleMessage<N>) {
106        let _ = self.inner.to_manager_tx.send(msg);
107    }
108
109    /// Update the status of the node.
110    pub fn update_status(&self, head: Head) {
111        self.send_message(NetworkHandleMessage::StatusUpdate { head });
112    }
113
114    /// Announce a block over devp2p
115    ///
116    /// Caution: in `PoS` this is a noop because new blocks are no longer announced over devp2p.
117    /// Instead they are sent to the node by CL and can be requested over devp2p.
118    /// Broadcasting new blocks is considered a protocol violation.
119    pub fn announce_block(&self, block: N::NewBlockPayload, hash: B256) {
120        self.send_message(NetworkHandleMessage::AnnounceBlock(block, hash))
121    }
122
123    /// Sends a [`PeerRequest`] to the given peer's session.
124    pub fn send_request(&self, peer_id: PeerId, request: PeerRequest<N>) {
125        self.send_message(NetworkHandleMessage::EthRequest { peer_id, request })
126    }
127
128    /// Send transactions hashes to the peer.
129    pub fn send_transactions_hashes(&self, peer_id: PeerId, msg: NewPooledTransactionHashes) {
130        self.send_message(NetworkHandleMessage::SendPooledTransactionHashes { peer_id, msg })
131    }
132
133    /// Send full transactions to the peer
134    pub fn send_transactions(&self, peer_id: PeerId, msg: Vec<Arc<N::BroadcastedTransaction>>) {
135        self.send_message(NetworkHandleMessage::SendTransaction {
136            peer_id,
137            msg: SharedTransactions(msg),
138        })
139    }
140
141    /// Send eth message to the peer.
142    pub fn send_eth_message(&self, peer_id: PeerId, message: PeerMessage<N>) {
143        self.send_message(NetworkHandleMessage::EthMessage { peer_id, message })
144    }
145
146    /// Send message to get the [`TransactionsHandle`].
147    ///
148    /// Returns `None` if no transaction task is installed.
149    pub async fn transactions_handle(&self) -> Option<TransactionsHandle<N>> {
150        let (tx, rx) = oneshot::channel();
151        let _ = self.manager().send(NetworkHandleMessage::GetTransactionsHandle(tx));
152        rx.await.unwrap()
153    }
154
155    /// Send message to gracefully shutdown node.
156    ///
157    /// This will disconnect all active and pending sessions and prevent
158    /// new connections to be established.
159    pub async fn shutdown(&self) -> Result<(), oneshot::error::RecvError> {
160        let (tx, rx) = oneshot::channel();
161        self.send_message(NetworkHandleMessage::Shutdown(tx));
162        rx.await
163    }
164
165    /// Set network connection state to Active.
166    ///
167    /// New outbound connections will be established if there's capacity.
168    pub fn set_network_active(&self) {
169        self.set_network_conn(NetworkConnectionState::Active);
170    }
171
172    /// Set network connection state to Hibernate.
173    ///
174    /// No new outbound connections will be established.
175    pub fn set_network_hibernate(&self) {
176        self.set_network_conn(NetworkConnectionState::Hibernate);
177    }
178
179    /// Set network connection state.
180    fn set_network_conn(&self, network_conn: NetworkConnectionState) {
181        self.send_message(NetworkHandleMessage::SetNetworkState(network_conn));
182    }
183
184    /// Whether tx gossip is disabled
185    pub fn tx_gossip_disabled(&self) -> bool {
186        self.inner.tx_gossip_disabled
187    }
188
189    /// Returns the secret key used for authenticating sessions.
190    pub fn secret_key(&self) -> &SecretKey {
191        &self.inner.secret_key
192    }
193}
194
195// === API Implementations ===
196
197impl<N: NetworkPrimitives> NetworkPeersEvents for NetworkHandle<N> {
198    /// Returns an event stream of peer-specific network events.
199    fn peer_events(&self) -> PeerEventStream {
200        let peer_events = self.inner.event_sender.new_listener().map(|event| match event {
201            NetworkEvent::Peer(peer_event) => peer_event,
202            NetworkEvent::ActivePeerSession { info, .. } => PeerEvent::SessionEstablished(info),
203        });
204        PeerEventStream::new(peer_events)
205    }
206}
207
208impl<N: NetworkPrimitives> NetworkEventListenerProvider for NetworkHandle<N> {
209    type Primitives = N;
210
211    fn event_listener(&self) -> EventStream<NetworkEvent<PeerRequest<Self::Primitives>>> {
212        self.inner.event_sender.new_listener()
213    }
214
215    fn discovery_listener(&self) -> UnboundedReceiverStream<DiscoveryEvent> {
216        let (tx, rx) = mpsc::unbounded_channel();
217        let _ = self.manager().send(NetworkHandleMessage::DiscoveryListener(tx));
218        UnboundedReceiverStream::new(rx)
219    }
220}
221
222impl<N: NetworkPrimitives> NetworkProtocols for NetworkHandle<N> {
223    fn add_rlpx_sub_protocol(&self, protocol: RlpxSubProtocol) {
224        self.send_message(NetworkHandleMessage::AddRlpxSubProtocol(protocol))
225    }
226}
227
228impl<N: NetworkPrimitives> PeersInfo for NetworkHandle<N> {
229    fn num_connected_peers(&self) -> usize {
230        self.inner.num_active_peers.load(Ordering::Relaxed)
231    }
232
233    fn local_node_record(&self) -> NodeRecord {
234        if let Some(discv4) = &self.inner.discv4 {
235            // Note: the discv4 services uses the same `nat` so we can directly return the node
236            // record here
237            discv4.node_record()
238        } else if let Some(discv5) = self.inner.discv5.as_ref() {
239            // for disv5 we must check if we have an external ip configured
240            if let Some(external) = self.inner.nat.and_then(|nat| nat.as_external_ip()) {
241                NodeRecord::new((external, discv5.local_port()).into(), *self.peer_id())
242            } else {
243                // use the node record that discv5 tracks or use localhost
244                self.inner.discv5.as_ref().and_then(|d| d.node_record()).unwrap_or_else(|| {
245                    NodeRecord::new(
246                        (std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST), discv5.local_port())
247                            .into(),
248                        *self.peer_id(),
249                    )
250                })
251            }
252            // also use the tcp port
253            .with_tcp_port(self.inner.listener_address.lock().port())
254        } else {
255            let external_ip = self.inner.nat.and_then(|nat| nat.as_external_ip());
256
257            let mut socket_addr = *self.inner.listener_address.lock();
258            if let Some(ip) = external_ip {
259                // if able to resolve external ip, use it instead and also set the local address
260                socket_addr.set_ip(ip)
261            } else if socket_addr.ip().is_unspecified() {
262                // zero address is invalid
263                if socket_addr.ip().is_ipv4() {
264                    socket_addr.set_ip(std::net::IpAddr::V4(std::net::Ipv4Addr::LOCALHOST));
265                } else {
266                    socket_addr.set_ip(std::net::IpAddr::V6(std::net::Ipv6Addr::LOCALHOST));
267                }
268            }
269
270            NodeRecord::new(socket_addr, *self.peer_id())
271        }
272    }
273
274    fn local_enr(&self) -> Enr<SecretKey> {
275        let local_node_record = self.local_node_record();
276        let mut builder = Enr::builder();
277        builder.ip(local_node_record.address);
278        if local_node_record.address.is_ipv4() {
279            builder.udp4(local_node_record.udp_port);
280            builder.tcp4(local_node_record.tcp_port);
281        } else {
282            builder.udp6(local_node_record.udp_port);
283            builder.tcp6(local_node_record.tcp_port);
284        }
285        builder.build(&self.inner.secret_key).expect("valid enr")
286    }
287}
288
289impl<N: NetworkPrimitives> Peers for NetworkHandle<N> {
290    fn add_trusted_peer_id(&self, peer: PeerId) {
291        self.send_message(NetworkHandleMessage::AddTrustedPeerId(peer));
292    }
293
294    /// Sends a message to the [`NetworkManager`](crate::NetworkManager) to add a peer to the known
295    /// set, with the given kind.
296    fn add_peer_kind(
297        &self,
298        peer: PeerId,
299        kind: PeerKind,
300        tcp_addr: SocketAddr,
301        udp_addr: Option<SocketAddr>,
302    ) {
303        let addr = PeerAddr::new(tcp_addr, udp_addr);
304        self.send_message(NetworkHandleMessage::AddPeerAddress(peer, kind, addr));
305    }
306
307    async fn get_peers_by_kind(&self, kind: PeerKind) -> Result<Vec<PeerInfo>, NetworkError> {
308        let (tx, rx) = oneshot::channel();
309        let _ = self.manager().send(NetworkHandleMessage::GetPeerInfosByPeerKind(kind, tx));
310        Ok(rx.await?)
311    }
312
313    async fn get_all_peers(&self) -> Result<Vec<PeerInfo>, NetworkError> {
314        let (tx, rx) = oneshot::channel();
315        let _ = self.manager().send(NetworkHandleMessage::GetPeerInfos(tx));
316        Ok(rx.await?)
317    }
318
319    async fn get_peer_by_id(&self, peer_id: PeerId) -> Result<Option<PeerInfo>, NetworkError> {
320        let (tx, rx) = oneshot::channel();
321        let _ = self.manager().send(NetworkHandleMessage::GetPeerInfoById(peer_id, tx));
322        Ok(rx.await?)
323    }
324
325    async fn get_peers_by_id(&self, peer_ids: Vec<PeerId>) -> Result<Vec<PeerInfo>, NetworkError> {
326        let (tx, rx) = oneshot::channel();
327        let _ = self.manager().send(NetworkHandleMessage::GetPeerInfosByIds(peer_ids, tx));
328        Ok(rx.await?)
329    }
330
331    /// Sends a message to the [`NetworkManager`](crate::NetworkManager) to remove a peer from the
332    /// set corresponding to given kind.
333    fn remove_peer(&self, peer: PeerId, kind: PeerKind) {
334        self.send_message(NetworkHandleMessage::RemovePeer(peer, kind))
335    }
336
337    /// Sends a message to the [`NetworkManager`](crate::NetworkManager)  to disconnect an existing
338    /// connection to the given peer.
339    fn disconnect_peer(&self, peer: PeerId) {
340        self.send_message(NetworkHandleMessage::DisconnectPeer(peer, None))
341    }
342
343    /// Sends a message to the [`NetworkManager`](crate::NetworkManager)  to disconnect an existing
344    /// connection to the given peer using the provided reason
345    fn disconnect_peer_with_reason(&self, peer: PeerId, reason: DisconnectReason) {
346        self.send_message(NetworkHandleMessage::DisconnectPeer(peer, Some(reason)))
347    }
348
349    /// Sends a message to the [`NetworkManager`](crate::NetworkManager) to connect to the given
350    /// peer.
351    ///
352    /// This will add a new entry for the given peer if it isn't tracked yet.
353    /// If it is tracked then the peer is updated with the given information.
354    fn connect_peer_kind(
355        &self,
356        peer_id: PeerId,
357        kind: PeerKind,
358        tcp_addr: SocketAddr,
359        udp_addr: Option<SocketAddr>,
360    ) {
361        self.send_message(NetworkHandleMessage::ConnectPeer(
362            peer_id,
363            kind,
364            PeerAddr::new(tcp_addr, udp_addr),
365        ))
366    }
367
368    /// Send a reputation change for the given peer.
369    fn reputation_change(&self, peer_id: PeerId, kind: ReputationChangeKind) {
370        self.send_message(NetworkHandleMessage::ReputationChange(peer_id, kind));
371    }
372
373    async fn reputation_by_id(&self, peer_id: PeerId) -> Result<Option<Reputation>, NetworkError> {
374        let (tx, rx) = oneshot::channel();
375        let _ = self.manager().send(NetworkHandleMessage::GetReputationById(peer_id, tx));
376        Ok(rx.await?)
377    }
378}
379
380impl<N: NetworkPrimitives> PeersHandleProvider for NetworkHandle<N> {
381    fn peers_handle(&self) -> &PeersHandle {
382        &self.inner.peers
383    }
384}
385
386impl<N: NetworkPrimitives> NetworkInfo for NetworkHandle<N> {
387    fn local_addr(&self) -> SocketAddr {
388        *self.inner.listener_address.lock()
389    }
390
391    async fn network_status(&self) -> Result<NetworkStatus, NetworkError> {
392        let (tx, rx) = oneshot::channel();
393        let _ = self.manager().send(NetworkHandleMessage::GetStatus(tx));
394        rx.await.map_err(Into::into)
395    }
396
397    fn chain_id(&self) -> u64 {
398        self.inner.chain_id.load(Ordering::Relaxed)
399    }
400
401    fn is_syncing(&self) -> bool {
402        SyncStateProvider::is_syncing(self)
403    }
404
405    fn is_initially_syncing(&self) -> bool {
406        SyncStateProvider::is_initially_syncing(self)
407    }
408}
409
410impl<N: NetworkPrimitives> SyncStateProvider for NetworkHandle<N> {
411    fn is_syncing(&self) -> bool {
412        self.inner.is_syncing.load(Ordering::Relaxed)
413    }
414    // used to guard the txpool
415    fn is_initially_syncing(&self) -> bool {
416        if self.inner.initial_sync_done.load(Ordering::Relaxed) {
417            return false
418        }
419        self.inner.is_syncing.load(Ordering::Relaxed)
420    }
421}
422
423impl<N: NetworkPrimitives> NetworkSyncUpdater for NetworkHandle<N> {
424    fn update_sync_state(&self, state: SyncState) {
425        let future_state = state.is_syncing();
426        let prev_state = self.inner.is_syncing.swap(future_state, Ordering::Relaxed);
427        let syncing_to_idle_state_transition = prev_state && !future_state;
428        if syncing_to_idle_state_transition {
429            self.inner.initial_sync_done.store(true, Ordering::Relaxed);
430        }
431    }
432
433    /// Update the status of the node.
434    fn update_status(&self, head: Head) {
435        self.send_message(NetworkHandleMessage::StatusUpdate { head });
436    }
437
438    /// Updates the advertised block range.
439    fn update_block_range(&self, update: reth_eth_wire::BlockRangeUpdate) {
440        self.send_message(NetworkHandleMessage::InternalBlockRangeUpdate(update));
441    }
442}
443
444impl<N: NetworkPrimitives> BlockDownloaderProvider for NetworkHandle<N> {
445    type Client = FetchClient<N>;
446
447    async fn fetch_client(&self) -> Result<Self::Client, oneshot::error::RecvError> {
448        let (tx, rx) = oneshot::channel();
449        let _ = self.manager().send(NetworkHandleMessage::FetchClient(tx));
450        rx.await
451    }
452}
453
454#[derive(Debug)]
455struct NetworkInner<N: NetworkPrimitives = EthNetworkPrimitives> {
456    /// Number of active peer sessions the node's currently handling.
457    num_active_peers: Arc<AtomicUsize>,
458    /// Sender half of the message channel to the [`crate::NetworkManager`].
459    to_manager_tx: UnboundedSender<NetworkHandleMessage<N>>,
460    /// The local address that accepts incoming connections.
461    listener_address: Arc<Mutex<SocketAddr>>,
462    /// The secret key used for authenticating sessions.
463    secret_key: SecretKey,
464    /// The identifier used by this node.
465    local_peer_id: PeerId,
466    /// Access to all the nodes.
467    peers: PeersHandle,
468    /// The mode of the network
469    network_mode: NetworkMode,
470    /// Represents if the network is currently syncing.
471    is_syncing: Arc<AtomicBool>,
472    /// Used to differentiate between an initial pipeline sync or a live sync
473    initial_sync_done: Arc<AtomicBool>,
474    /// The chain id
475    chain_id: Arc<AtomicU64>,
476    /// Whether to disable transaction gossip
477    tx_gossip_disabled: bool,
478    /// The instance of the discv4 service
479    discv4: Option<Discv4>,
480    /// The instance of the discv5 service
481    discv5: Option<Discv5>,
482    /// Sender for high level network events.
483    event_sender: EventSender<NetworkEvent<PeerRequest<N>>>,
484    /// The NAT resolver
485    nat: Option<NatResolver>,
486}
487
488/// Provides access to modify the network's additional protocol handlers.
489pub trait NetworkProtocols: Send + Sync {
490    /// Adds an additional protocol handler to the `RLPx` sub-protocol list.
491    fn add_rlpx_sub_protocol(&self, protocol: RlpxSubProtocol);
492}
493
494/// Internal messages that can be passed to the  [`NetworkManager`](crate::NetworkManager).
495#[derive(Debug)]
496pub(crate) enum NetworkHandleMessage<N: NetworkPrimitives = EthNetworkPrimitives> {
497    /// Marks a peer as trusted.
498    AddTrustedPeerId(PeerId),
499    /// Adds an address for a peer, including its ID, kind, and socket address.
500    AddPeerAddress(PeerId, PeerKind, PeerAddr),
501    /// Removes a peer from the peerset corresponding to the given kind.
502    RemovePeer(PeerId, PeerKind),
503    /// Disconnects a connection to a peer if it exists, optionally providing a disconnect reason.
504    DisconnectPeer(PeerId, Option<DisconnectReason>),
505    /// Broadcasts an event to announce a new block to all nodes.
506    AnnounceBlock(N::NewBlockPayload, B256),
507    /// Sends a list of transactions to the given peer.
508    SendTransaction {
509        /// The ID of the peer to which the transactions are sent.
510        peer_id: PeerId,
511        /// The shared transactions to send.
512        msg: SharedTransactions<N::BroadcastedTransaction>,
513    },
514    /// Sends a list of transaction hashes to the given peer.
515    SendPooledTransactionHashes {
516        /// The ID of the peer to which the transaction hashes are sent.
517        peer_id: PeerId,
518        /// The new pooled transaction hashes to send.
519        msg: NewPooledTransactionHashes,
520    },
521    /// Sends an `eth` protocol request to the peer.
522    EthRequest {
523        /// The peer to send the request to.
524        peer_id: PeerId,
525        /// The request to send to the peer's sessions.
526        request: PeerRequest<N>,
527    },
528    /// Sends an `eth` protocol message to the peer.
529    EthMessage {
530        /// The peer to send the message to.
531        peer_id: PeerId,
532        /// The `eth` protocol message to send to the peer's session.
533        message: PeerMessage<N>,
534    },
535    /// Applies a reputation change to the given peer.
536    ReputationChange(PeerId, ReputationChangeKind),
537    /// Returns the client that can be used to interact with the network.
538    FetchClient(oneshot::Sender<FetchClient<N>>),
539    /// Applies a status update.
540    StatusUpdate {
541        /// The head status to apply.
542        head: Head,
543    },
544    /// Retrieves the current status via a oneshot sender.
545    GetStatus(oneshot::Sender<NetworkStatus>),
546    /// Gets `PeerInfo` for the specified peer IDs.
547    GetPeerInfosByIds(Vec<PeerId>, oneshot::Sender<Vec<PeerInfo>>),
548    /// Gets `PeerInfo` from all the peers via a oneshot sender.
549    GetPeerInfos(oneshot::Sender<Vec<PeerInfo>>),
550    /// Gets `PeerInfo` for a specific peer via a oneshot sender.
551    GetPeerInfoById(PeerId, oneshot::Sender<Option<PeerInfo>>),
552    /// Gets `PeerInfo` for a specific peer kind via a oneshot sender.
553    GetPeerInfosByPeerKind(PeerKind, oneshot::Sender<Vec<PeerInfo>>),
554    /// Gets the reputation for a specific peer via a oneshot sender.
555    GetReputationById(PeerId, oneshot::Sender<Option<Reputation>>),
556    /// Retrieves the `TransactionsHandle` via a oneshot sender.
557    GetTransactionsHandle(oneshot::Sender<Option<TransactionsHandle<N>>>),
558    /// Initiates a graceful shutdown of the network via a oneshot sender.
559    Shutdown(oneshot::Sender<()>),
560    /// Sets the network state between hibernation and active.
561    SetNetworkState(NetworkConnectionState),
562    /// Adds a new listener for `DiscoveryEvent`.
563    DiscoveryListener(UnboundedSender<DiscoveryEvent>),
564    /// Adds an additional `RlpxSubProtocol`.
565    AddRlpxSubProtocol(RlpxSubProtocol),
566    /// Connect to the given peer.
567    ConnectPeer(PeerId, PeerKind, PeerAddr),
568    /// Message to update the node's advertised block range information.
569    InternalBlockRangeUpdate(BlockRangeUpdate),
570}