chore(net): extract NetworkHandle methods for launching node to traits (#9966)

This commit is contained in:
Emilia Hane
2024-08-01 18:44:23 +02:00
committed by GitHub
parent f3fac56fd9
commit b10517b3bf
56 changed files with 985 additions and 776 deletions

View File

@ -13,24 +13,23 @@
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};
/// Network Error
pub mod error;
/// Implementation of network traits for that does nothing.
pub mod noop;
pub use alloy_rpc_types_admin::EthProtocolInfo;
pub use error::NetworkError;
pub use reth_network_types::{PeerKind, PeersHandle, Reputation, ReputationChangeKind};
use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};
use reth_eth_wire::{capability::Capabilities, DisconnectReason, EthVersion, Status};
use reth_network_peers::NodeRecord;
pub use reth_network_types::{Reputation, ReputationChangeKind};
/// The `PeerId` type.
pub type PeerId = alloy_primitives::B512;
/// Network Error
pub mod error;
/// Implementation of network traits for that does nothing.
pub mod noop;
/// Provides general purpose information about the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait NetworkInfo: Send + Sync {
@ -51,6 +50,7 @@ pub trait NetworkInfo: Send + Sync {
}
/// Provides general purpose information about Peers in the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait PeersInfo: Send + Sync {
/// Returns how many peers the network is currently connected to.
///
@ -65,6 +65,7 @@ pub trait PeersInfo: Send + Sync {
}
/// Provides an API for managing the peers of the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait Peers: PeersInfo {
/// Adds a peer to the peer set with UDP `SocketAddr`.
fn add_peer(&self, peer: PeerId, tcp_addr: SocketAddr) {
@ -157,33 +158,13 @@ pub trait Peers: PeersInfo {
) -> impl Future<Output = Result<Option<Reputation>, NetworkError>> + Send;
}
/// Represents the kind of peer
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub enum PeerKind {
/// Basic peer kind.
#[default]
Basic,
/// Static peer, added via JSON-RPC.
Static,
/// Trusted peer.
Trusted,
}
impl PeerKind {
/// Returns `true` if the peer is trusted.
pub const fn is_trusted(&self) -> bool {
matches!(self, Self::Trusted)
}
/// Returns `true` if the peer is static.
pub const fn is_static(&self) -> bool {
matches!(self, Self::Static)
}
/// Returns `true` if the peer is basic.
pub const fn is_basic(&self) -> bool {
matches!(self, Self::Basic)
}
/// Provides an API for managing the peers of the network.
#[auto_impl::auto_impl(&, Arc)]
pub trait PeersHandleProvider {
/// Returns the [`PeersHandle`] that can be cloned and shared.
///
/// The [`PeersHandle`] can be used to interact with the network's peer set.
fn peers_handle(&self) -> &PeersHandle;
}
/// Info about an active peer session.

View File

@ -3,15 +3,15 @@
//! This is useful for wiring components together that don't require network but still need to be
//! generic over it.
use crate::{
NetworkError, NetworkInfo, NetworkStatus, PeerId, PeerInfo, PeerKind, Peers, PeersInfo,
Reputation, ReputationChangeKind,
};
use std::net::{IpAddr, SocketAddr};
use alloy_rpc_types_admin::EthProtocolInfo;
use enr::{secp256k1::SecretKey, Enr};
use reth_eth_wire::{DisconnectReason, ProtocolVersion};
use reth_network_peers::NodeRecord;
use std::net::{IpAddr, SocketAddr};
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.
///