//! A network implementation that does nothing. //! //! This is useful for wiring components together that don't require network but still need to be //! generic over it. use std::net::{IpAddr, SocketAddr}; use alloy_rpc_types_admin::EthProtocolInfo; use enr::{secp256k1::SecretKey, Enr}; use reth_eth_wire_types::{DisconnectReason, ProtocolVersion}; use reth_network_peers::NodeRecord; use reth_network_types::{PeerKind, Reputation, ReputationChangeKind}; use crate::{NetworkError, NetworkInfo, NetworkStatus, PeerId, PeerInfo, Peers, PeersInfo}; /// A type that implements all network trait that does nothing. /// /// Intended for testing purposes where network is not used. #[derive(Debug, Clone, Default)] #[non_exhaustive] pub struct NoopNetwork; impl NetworkInfo for NoopNetwork { fn local_addr(&self) -> SocketAddr { (IpAddr::from(std::net::Ipv4Addr::UNSPECIFIED), 30303).into() } async fn network_status(&self) -> Result { Ok(NetworkStatus { client_version: "reth-test".to_string(), protocol_version: ProtocolVersion::V5 as u64, eth_protocol_info: EthProtocolInfo { difficulty: Default::default(), network: 1, genesis: Default::default(), config: Default::default(), head: Default::default(), }, }) } fn chain_id(&self) -> u64 { // mainnet 1 } fn is_syncing(&self) -> bool { false } fn is_initially_syncing(&self) -> bool { false } } impl PeersInfo for NoopNetwork { fn num_connected_peers(&self) -> usize { 0 } fn local_node_record(&self) -> NodeRecord { NodeRecord::new(self.local_addr(), PeerId::random()) } fn local_enr(&self) -> Enr { let sk = SecretKey::from_slice(&[0xcd; 32]).unwrap(); Enr::builder().build(&sk).unwrap() } } impl Peers for NoopNetwork { fn add_trusted_peer_id(&self, _peer: PeerId) {} fn add_peer_kind( &self, _peer: PeerId, _kind: PeerKind, _tcp_addr: SocketAddr, _udp_addr: Option, ) { } async fn get_peers_by_kind(&self, _kind: PeerKind) -> Result, NetworkError> { Ok(vec![]) } async fn get_all_peers(&self) -> Result, NetworkError> { Ok(vec![]) } async fn get_peer_by_id(&self, _peer_id: PeerId) -> Result, NetworkError> { Ok(None) } async fn get_peers_by_id(&self, _peer_id: Vec) -> Result, NetworkError> { Ok(vec![]) } fn remove_peer(&self, _peer: PeerId, _kind: PeerKind) {} fn disconnect_peer(&self, _peer: PeerId) {} fn disconnect_peer_with_reason(&self, _peer: PeerId, _reason: DisconnectReason) {} fn reputation_change(&self, _peer_id: PeerId, _kind: ReputationChangeKind) {} async fn reputation_by_id(&self, _peer_id: PeerId) -> Result, NetworkError> { Ok(None) } }