Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/find_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 5 additions & 8 deletions src/discv5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn kbucket::Filter<Enr>>),
Some(Box::new(kbucket::IpBucketFilter) as Box<dyn kbucket::Filter<Enr>>),
Some(Box::new(kbucket::filter::IpTableFilter)
as Box<dyn kbucket::filter::Filter<Enr>>),
Some(Box::new(kbucket::filter::IpBucketFilter)
as Box<dyn kbucket::filter::Filter<Enr>>),
)
} else {
(None, None)
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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, .. } => {
Expand Down Expand Up @@ -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,
};
Expand Down
4 changes: 2 additions & 2 deletions src/kbucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

mod bucket;
mod entry;
mod filter;
pub(crate) mod filter;
mod key;

pub use entry::*;
Expand All @@ -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},
Expand Down
1 change: 1 addition & 0 deletions src/kbucket/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/kbucket/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Enr> for IpTableFilter {
fn filter(
Expand All @@ -51,7 +51,7 @@ impl Filter<Enr> for IpTableFilter {
}

#[derive(Clone)]
pub struct IpBucketFilter;
pub(crate) struct IpBucketFilter;

impl Filter<Enr> for IpBucketFilter {
fn filter(
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/metrics.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Expand Down
22 changes: 7 additions & 15 deletions src/packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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<NodeId> {
Expand Down
8 changes: 4 additions & 4 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<Result<Vec<Enr>, RequestError>>),
/// A response from a TALK request
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/service/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ async fn build_service_with(

let (table_filter, bucket_filter) = if filters {
(
Some(Box::new(kbucket::IpTableFilter) as Box<dyn kbucket::Filter<Enr>>),
Some(Box::new(kbucket::IpBucketFilter) as Box<dyn kbucket::Filter<Enr>>),
Some(Box::new(kbucket::filter::IpTableFilter) as Box<dyn kbucket::filter::Filter<Enr>>),
Some(Box::new(kbucket::filter::IpBucketFilter) as Box<dyn kbucket::filter::Filter<Enr>>),
)
} else {
(None, None)
Expand Down Expand Up @@ -154,8 +154,8 @@ fn build_non_handler_service(

let (table_filter, bucket_filter) = if filters {
(
Some(Box::new(kbucket::IpTableFilter) as Box<dyn kbucket::Filter<Enr>>),
Some(Box::new(kbucket::IpBucketFilter) as Box<dyn kbucket::Filter<Enr>>),
Some(Box::new(kbucket::filter::IpTableFilter) as Box<dyn kbucket::filter::Filter<Enr>>),
Some(Box::new(kbucket::filter::IpBucketFilter) as Box<dyn kbucket::filter::Filter<Enr>>),
)
} else {
(None, None)
Expand Down
11 changes: 5 additions & 6 deletions src/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<dyn Executor + Send + Sync>,
/// Configuration details for the packet filter.
Expand All @@ -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<OutboundPacket>,
pub recv: mpsc::Receiver<RecvPacket>,
sender_exit: Option<oneshot::Sender<()>>,
Expand Down
6 changes: 3 additions & 3 deletions src/socket/recv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<Duration>,
Expand Down
2 changes: 1 addition & 1 deletion src/socket/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading