Skip to content

Commit

Permalink
refactor(networking): Reorganize network module and improve test cove…
Browse files Browse the repository at this point in the history
…rage

This commit introduces a major refactoring of the networking module structure
and enhances test coverage. The changes improve code organization, type safety,
and maintainability.

Key changes:
- Split network module into logical submodules (error, record, types)
- Move network-related types into dedicated modules
- Add comprehensive test suite with mock implementations
- Improve error handling and type definitions
- Add proper documentation for public interfaces

New test coverage includes:
- Network address management and holder tracking
- Error formatting and handling
- Configuration validation
- Metrics recording with thread-safe mock implementation
- Record configuration and verification kinds

The refactoring maintains backward compatibility while providing a more
maintainable and testable codebase.
  • Loading branch information
dirvine committed Dec 4, 2024
1 parent 796ea91 commit af860e7
Show file tree
Hide file tree
Showing 20 changed files with 1,497 additions and 1,424 deletions.
53 changes: 53 additions & 0 deletions ant-networking/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Configuration constants and settings for the networking module.
use std::time::Duration;

/// Maximum allowed size for network packets in bytes (2MB)
pub const MAX_PACKET_SIZE: usize = 2 * 1024 * 1024;

/// Number of nodes to maintain in the close group
pub const CLOSE_GROUP_SIZE: usize = 8;

/// Default timeout duration for network requests in seconds
pub const REQUEST_TIMEOUT_DEFAULT_S: u64 = 60;

/// Duration to keep connections alive
pub const CONNECTION_KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(20);

/// Timeout duration for Kademlia queries in seconds
pub const KAD_QUERY_TIMEOUT_S: u64 = 20;

/// Protocol identifier for Kademlia streams
pub const KAD_STREAM_PROTOCOL_ID: &str = "/safe/kad/1.0.0";

/// Size of the networking channel buffer
pub const NETWORKING_CHANNEL_SIZE: usize = 100;

/// Interval for relay manager reservation checks
pub const RELAY_MANAGER_RESERVATION_INTERVAL: Duration = Duration::from_secs(30);

/// Interval for resending identify messages
pub const RESEND_IDENTIFY_INVERVAL: Duration = Duration::from_secs(300);

/// Configuration for the networking component
#[derive(Debug, Clone)]
pub struct NetworkConfig {
pub max_packet_size: usize,
pub close_group_size: usize,
pub request_timeout: Duration,
pub connection_keep_alive: Duration,
pub kad_query_timeout: Duration,
pub channel_size: usize,
}

impl Default for NetworkConfig {
fn default() -> Self {
Self {
max_packet_size: MAX_PACKET_SIZE,
close_group_size: CLOSE_GROUP_SIZE,
request_timeout: Duration::from_secs(REQUEST_TIMEOUT_DEFAULT_S),
connection_keep_alive: CONNECTION_KEEP_ALIVE_TIMEOUT,
kad_query_timeout: Duration::from_secs(KAD_QUERY_TIMEOUT_S),
channel_size: NETWORKING_CHANNEL_SIZE,
}
}
}
38 changes: 38 additions & 0 deletions ant-networking/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use libp2p::{
identify,
kad,
request_response,
};

use crate::messages::{Request, Response};

#[derive(Debug)]
pub enum NodeEvent {
Identify(identify::Event),
Kademlia(kad::Event),
MsgReceived(request_response::Event<Request, Response>),
}

impl From<identify::Event> for NodeEvent {
fn from(event: identify::Event) -> Self {
NodeEvent::Identify(event)
}
}

impl From<kad::Event> for NodeEvent {
fn from(event: kad::Event) -> Self {
NodeEvent::Kademlia(event)
}
}

impl From<request_response::Event<Request, Response>> for NodeEvent {
fn from(event: request_response::Event<Request, Response>) -> Self {
NodeEvent::MsgReceived(event)
}
}

#[derive(Debug)]
pub enum MsgResponder {
Response(Response),
Error(String),
}
27 changes: 27 additions & 0 deletions ant-networking/src/kad/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use libp2p::kad::{KBucketKey, KBucketDistance, Record};
use libp2p::PeerId;
use ant_protocol::NetworkAddress;

pub use KBucketKey;
pub use KBucketDistance;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RecordKey(pub NetworkAddress);

impl From<NetworkAddress> for RecordKey {
fn from(addr: NetworkAddress) -> Self {
RecordKey(addr)
}
}

impl AsRef<[u8]> for RecordKey {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}

impl From<RecordKey> for KBucketKey<PeerId> {
fn from(key: RecordKey) -> Self {
KBucketKey::new(key.0.into())
}
}
Loading

0 comments on commit af860e7

Please sign in to comment.