diff --git a/examples/find_nodes.rs b/examples/find_nodes.rs index f996ac34..1f76a8e4 100644 --- a/examples/find_nodes.rs +++ b/examples/find_nodes.rs @@ -169,7 +169,7 @@ async fn main() { // pick a random node target let target_random_node_id = enr::NodeId::random(); // get metrics - let metrics = discv5.metrics(); + let metrics = Discv5::metrics(); let connected_peers = discv5.connected_peers(); info!( connected_peers, diff --git a/src/discv5.rs b/src/discv5.rs index 4250d0cf..cd91be94 100644 --- a/src/discv5.rs +++ b/src/discv5.rs @@ -122,8 +122,10 @@ impl Discv5 { // may expose this functionality to the users if there is demand for it. let (table_filter, bucket_filter) = if config.ip_limit { ( - Some(Box::new(kbucket::IpTableFilter) as Box>), - Some(Box::new(kbucket::IpBucketFilter) as Box>), + Some(Box::new(kbucket::filter::IpTableFilter) + as Box>), + Some(Box::new(kbucket::filter::IpBucketFilter) + as Box>), ) } else { (None, None) @@ -293,15 +295,10 @@ impl Discv5 { } /// Gets the metrics associated with the Server - pub fn metrics(&self) -> Metrics { + pub fn metrics() -> Metrics { Metrics::from(&METRICS) } - /// Exposes the raw reference to the underlying internal metrics. - pub fn raw_metrics() -> &'static METRICS { - &METRICS - } - /// Returns the local ENR of the node. pub fn local_enr(&self) -> Enr { self.local_enr.read().clone() diff --git a/src/handler/mod.rs b/src/handler/mod.rs index 7c088df1..cfe1c70b 100644 --- a/src/handler/mod.rs +++ b/src/handler/mod.rs @@ -353,10 +353,10 @@ impl Handler { } Some(incoming_packet) = self.socket.recv.recv() => { match incoming_packet { - socket::RecvPacket::Inbound(inbound_packet) => { + socket::recv::RecvPacket::Inbound(inbound_packet) => { self.process_inbound_packet(inbound_packet).await; } - socket::RecvPacket::UnrecognizedFrame(frame) => { + socket::recv::RecvPacket::UnrecognizedFrame(frame) => { if let Err(e) = self .service_send .send(HandlerOut::UnrecognizedFrame(frame)) @@ -384,7 +384,7 @@ impl Handler { } /// Processes an inbound decoded packet. - async fn process_inbound_packet(&mut self, inbound_packet: socket::InboundPacket) { + async fn process_inbound_packet(&mut self, inbound_packet: socket::recv::InboundPacket) { let message_nonce = inbound_packet.header.message_nonce; match inbound_packet.header.kind { PacketKind::WhoAreYou { enr_seq, .. } => { @@ -1363,7 +1363,7 @@ impl Handler { /// Sends a packet to the send handler to be encoded and sent. async fn send(&mut self, node_address: NodeAddress, packet: Packet) { - let outbound_packet = socket::OutboundPacket { + let outbound_packet = socket::send::OutboundPacket { node_address, packet, }; diff --git a/src/kbucket.rs b/src/kbucket.rs index 8d9a98d6..e07b0130 100644 --- a/src/kbucket.rs +++ b/src/kbucket.rs @@ -69,7 +69,7 @@ mod bucket; mod entry; -mod filter; +pub(crate) mod filter; mod key; pub use entry::*; @@ -81,7 +81,7 @@ pub use bucket::{ ConnectionState, FailureReason, InsertResult as BucketInsertResult, UpdateResult, MAX_NODES_PER_BUCKET, }; -pub use filter::{Filter, IpBucketFilter, IpTableFilter}; +use filter::Filter; use std::{ collections::VecDeque, time::{Duration, Instant}, diff --git a/src/kbucket/bucket.rs b/src/kbucket/bucket.rs index d620fcab..1b586496 100644 --- a/src/kbucket/bucket.rs +++ b/src/kbucket/bucket.rs @@ -31,6 +31,7 @@ #![allow(dead_code)] use super::*; +use filter::Filter; use tracing::{debug, error}; /// Maximum number of nodes in a bucket, i.e. the (fixed) `k` parameter. diff --git a/src/kbucket/filter.rs b/src/kbucket/filter.rs index bc6b23f6..1a3fc421 100644 --- a/src/kbucket/filter.rs +++ b/src/kbucket/filter.rs @@ -38,7 +38,7 @@ const MAX_NODES_PER_SUBNET_TABLE: usize = 10; const MAX_NODES_PER_SUBNET_BUCKET: usize = 2; #[derive(Clone)] -pub struct IpTableFilter; +pub(crate) struct IpTableFilter; impl Filter for IpTableFilter { fn filter( @@ -51,7 +51,7 @@ impl Filter for IpTableFilter { } #[derive(Clone)] -pub struct IpBucketFilter; +pub(crate) struct IpBucketFilter; impl Filter for IpBucketFilter { fn filter( diff --git a/src/lib.rs b/src/lib.rs index 2d02c252..0c70ed6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -98,7 +98,7 @@ mod config; mod discv5; mod error; mod executor; -pub mod handler; +pub(crate) mod handler; mod ipmode; pub mod kbucket; mod lru_time_cache; @@ -107,7 +107,7 @@ mod node_info; pub mod packet; pub mod permit_ban; mod query_pool; -pub mod rpc; +pub(crate) mod rpc; pub mod service; pub mod socket; @@ -122,8 +122,10 @@ pub use error::{Error, QueryError, RequestError, ResponseError}; pub use executor::{Executor, TokioExecutor}; pub use ipmode::IpMode; pub use kbucket::{ConnectionDirection, ConnectionState, Key}; +pub use node_info::{NodeAddress, NodeContact}; pub use packet::ProtocolIdentity; pub use permit_ban::PermitBanList; +pub use rpc::RequestId; pub use service::TalkRequest; pub use socket::{ListenConfig, RateLimiter, RateLimiterBuilder}; // Re-export the ENR crate diff --git a/src/metrics.rs b/src/metrics.rs index ac48128a..47d8e175 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -1,11 +1,11 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; lazy_static! { - pub static ref METRICS: InternalMetrics = InternalMetrics::default(); + pub(crate) static ref METRICS: InternalMetrics = InternalMetrics::default(); } /// A collection of metrics used throughout the server. -pub struct InternalMetrics { +pub(crate) struct InternalMetrics { /// The number of active UDP sessions that are currently established. pub active_sessions: AtomicUsize, /// The number of seconds to store received packets to taking a moving average over. @@ -37,13 +37,13 @@ impl Default for InternalMetrics { } impl InternalMetrics { - pub fn add_recv_bytes(&self, bytes: usize) { + pub(crate) fn add_recv_bytes(&self, bytes: usize) { let current_bytes_recv = self.bytes_recv.load(Ordering::Relaxed); self.bytes_recv .store(current_bytes_recv.saturating_add(bytes), Ordering::Relaxed); } - pub fn add_sent_bytes(&self, bytes: usize) { + pub(crate) fn add_sent_bytes(&self, bytes: usize) { let current_bytes_sent = self.bytes_sent.load(Ordering::Relaxed); self.bytes_sent .store(current_bytes_sent.saturating_add(bytes), Ordering::Relaxed); diff --git a/src/packet/mod.rs b/src/packet/mod.rs index dc679602..39d52c26 100644 --- a/src/packet/mod.rs +++ b/src/packet/mod.rs @@ -21,14 +21,14 @@ use std::convert::TryInto; use zeroize::Zeroize; /// The packet IV length (u128). -pub const IV_LENGTH: usize = 16; +pub(crate) const IV_LENGTH: usize = 16; /// The length of the static header. (6 byte protocol id, 2 bytes version, 1 byte kind, 12 byte /// message nonce and a 2 byte authdata-size). -pub const STATIC_HEADER_LENGTH: usize = 23; +pub(crate) const STATIC_HEADER_LENGTH: usize = 23; /// The message nonce length (in bytes). -pub const MESSAGE_NONCE_LENGTH: usize = 12; +pub(crate) const MESSAGE_NONCE_LENGTH: usize = 12; /// The Id nonce length (in bytes). -pub const ID_NONCE_LENGTH: usize = 16; +pub(crate) const ID_NONCE_LENGTH: usize = 16; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct ProtocolIdentity { @@ -56,7 +56,7 @@ pub type MessageNonce = [u8; MESSAGE_NONCE_LENGTH]; pub type IdNonce = [u8; ID_NONCE_LENGTH]; // This is the WHOAREYOU authenticated data. -pub struct ChallengeData([u8; 63]); +pub(crate) struct ChallengeData([u8; 63]); impl std::fmt::Debug for ChallengeData { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -84,7 +84,7 @@ impl AsRef<[u8]> for ChallengeData { } #[derive(Debug, Clone, PartialEq, Eq)] -pub struct Packet { +pub(crate) struct Packet { /// Random data unique to the packet. pub iv: u128, /// Protocol header. @@ -94,7 +94,7 @@ pub struct Packet { } #[derive(Debug, Clone, PartialEq, Eq)] -pub struct PacketHeader { +pub(crate) struct PacketHeader { /// The nonce of the associated message pub message_nonce: MessageNonce, /// The protocol identity used @@ -379,14 +379,6 @@ impl Packet { )) } - /// Returns true if the packet is a WHOAREYOU packet. - pub fn is_whoareyou(&self) -> bool { - match &self.header.kind { - PacketKind::WhoAreYou { .. } => true, - PacketKind::Message { .. } | PacketKind::Handshake { .. } => false, - } - } - /// Non-challenge (WHOAREYOU) packets contain the src_id of the node. This function returns the /// src_id in this case. pub fn src_id(&self) -> Option { diff --git a/src/service.rs b/src/service.rs index 94372693..2f194409 100644 --- a/src/service.rs +++ b/src/service.rs @@ -142,7 +142,7 @@ impl TalkRequest { } /// The types of requests to send to the Discv5 service. -pub enum ServiceRequest { +pub(crate) enum ServiceRequest { /// A request to start a query. There are two types of queries: /// - A FindNode Query - Searches for peers using a random target. /// - A Predicate Query - Searches for peers closest to a random target that match a specified @@ -171,7 +171,7 @@ pub enum ServiceRequest { use crate::discv5::PERMIT_BAN_LIST; -pub struct Service { +pub(crate) struct Service { /// Configuration parameters. config: Config, /// The local ENR of the server. @@ -234,7 +234,7 @@ pub struct Pong { } /// The kinds of responses we can send back to the discv5 layer. -pub enum CallbackResponse { +pub(crate) enum CallbackResponse { /// A response to a requested Nodes. Nodes(oneshot::Sender, RequestError>>), /// A response from a TALK request @@ -1682,7 +1682,7 @@ enum QueryEvent { } /// The types of queries that can be made. -pub enum QueryKind { +pub(crate) enum QueryKind { /// A FindNode query. Searches for peers that are closest to a particular target. FindNode { target_node: NodeId }, /// A predicate query. Searches for peers that are close to a target but filtered by a specific diff --git a/src/service/test.rs b/src/service/test.rs index f87551b8..5861a4d8 100644 --- a/src/service/test.rs +++ b/src/service/test.rs @@ -71,8 +71,8 @@ async fn build_service_with( let (table_filter, bucket_filter) = if filters { ( - Some(Box::new(kbucket::IpTableFilter) as Box>), - Some(Box::new(kbucket::IpBucketFilter) as Box>), + Some(Box::new(kbucket::filter::IpTableFilter) as Box>), + Some(Box::new(kbucket::filter::IpBucketFilter) as Box>), ) } else { (None, None) @@ -154,8 +154,8 @@ fn build_non_handler_service( let (table_filter, bucket_filter) = if filters { ( - Some(Box::new(kbucket::IpTableFilter) as Box>), - Some(Box::new(kbucket::IpBucketFilter) as Box>), + Some(Box::new(kbucket::filter::IpTableFilter) as Box>), + Some(Box::new(kbucket::filter::IpBucketFilter) as Box>), ) } else { (None, None) diff --git a/src/socket/mod.rs b/src/socket/mod.rs index fa145617..c2bfcb03 100644 --- a/src/socket/mod.rs +++ b/src/socket/mod.rs @@ -16,15 +16,14 @@ use tokio::{ }; mod filter; -mod recv; -mod send; +pub(crate) mod recv; +pub(crate) mod send; pub use filter::{ rate_limiter::{RateLimiter, RateLimiterBuilder}, FilterConfig, }; -pub use recv::{InboundPacket, RecvPacket, UnrecognizedFrame}; -pub use send::OutboundPacket; +pub use recv::UnrecognizedFrame; /// Configuration for the sockets to listen on. /// @@ -53,7 +52,7 @@ pub enum ListenConfig { } /// Convenience objects for setting up the recv handler. -pub struct SocketConfig { +pub(crate) struct SocketConfig { /// The executor to spawn the tasks. pub executor: Box, /// Configuration details for the packet filter. @@ -71,7 +70,7 @@ pub struct SocketConfig { } /// Creates the UDP socket and handles the exit futures for the send/recv UDP handlers. -pub struct Socket { +pub(crate) struct Socket { pub send: mpsc::Sender, pub recv: mpsc::Receiver, sender_exit: Option>, diff --git a/src/socket/recv.rs b/src/socket/recv.rs index e6eb2364..3196d044 100644 --- a/src/socket/recv.rs +++ b/src/socket/recv.rs @@ -15,7 +15,7 @@ use tracing::{debug, trace, warn}; /// The object sent back by the Recv handler. #[derive(Debug)] -pub struct InboundPacket { +pub(crate) struct InboundPacket { /// The originating socket addr. pub src_address: SocketAddr, /// The packet header. @@ -36,13 +36,13 @@ pub struct UnrecognizedFrame { /// Packet output produced by the recv handler. #[allow(clippy::large_enum_variant)] #[derive(Debug)] -pub enum RecvPacket { +pub(crate) enum RecvPacket { Inbound(InboundPacket), UnrecognizedFrame(UnrecognizedFrame), } /// Convenience objects for setting up the recv handler. -pub struct RecvHandlerConfig { +pub(crate) struct RecvHandlerConfig { pub filter_config: FilterConfig, /// If the filter is enabled this sets the default timeout for bans enacted by the filter. pub ban_duration: Option, diff --git a/src/socket/send.rs b/src/socket/send.rs index 0a1b22d8..839f02f9 100644 --- a/src/socket/send.rs +++ b/src/socket/send.rs @@ -7,7 +7,7 @@ use tokio::{ }; use tracing::{debug, error, trace, warn}; -pub struct OutboundPacket { +pub(crate) struct OutboundPacket { /// The destination node address pub node_address: NodeAddress, /// The packet to be encoded.