diff --git a/src/lib.rs b/src/lib.rs index f27d948..4f5d775 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +//! Bitcoin Peer-to-Peer connections. +#![warn(missing_docs)] use std::{ collections::HashMap, sync::{ @@ -143,8 +145,15 @@ impl ConnectionMetrics { /// The rate at which a peer sends a particular message #[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] pub enum MessageRate { + /// No message of this type has been received. NoneReceived, - Ongoing { count: f64, start: Instant }, + /// The total count of messages along with the first message of this type. + Ongoing { + /// Total count of messages + count: f64, + /// The time of the first message + start: Instant, + }, } impl MessageRate { @@ -257,7 +266,9 @@ enum OutboundPing { LastReceived { then: Instant }, } +/// DNS seed provider pub trait SeedsExt { + /// List DNS seeds fn seeds(&self) -> Vec<&str>; } diff --git a/src/net.rs b/src/net.rs index 9d5b5c2..05d65a8 100644 --- a/src/net.rs +++ b/src/net.rs @@ -24,8 +24,12 @@ use crate::{ ConnectionMetrics, OutboundPing, Preferences, TimedMessage, TimedMessages, }; +/// Maximum amount of time the peer has to seed a message after idling. pub const READ_TIMEOUT: Duration = Duration::from_secs(60); +/// The interval to send a new ping message. pub const PING_INTERVAL: Duration = Duration::from_secs(30); +/// The initial TCP handshake timeout. +pub const TCP_TIMEOUT: Duration = Duration::from_secs(2); /// Open or begin a connection to an inbound or outbound peer. pub trait ConnectionExt: Send + Sync { @@ -58,7 +62,7 @@ impl ConnectionExt for ConnectionConfig { to: impl Into, timeout_params: TimeoutParams, ) -> Result<(ConnectionWriter, ConnectionReader, ConnectionMetrics), Error> { - let tcp_stream = TcpStream::connect(to.into())?; + let tcp_stream = TcpStream::connect_timeout(&to.into(), timeout_params.tcp)?; tcp_stream.set_read_timeout(timeout_params.read)?; tcp_stream.set_write_timeout(timeout_params.write)?; Self::handshake(self, tcp_stream, timeout_params) @@ -150,26 +154,37 @@ impl ConnectionExt for ConnectionConfig { } } +/// Configurations for ending a connection due to inactivity. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct TimeoutParams { read: Option, write: Option, + tcp: Duration, ping_interval: Duration, } impl TimeoutParams { + /// Construct new timeout parameters pub fn new() -> Self { Self::default() } + /// Set the time a peer has until they have must sent a message. pub fn read_timeout(&mut self, timeout: Duration) { self.read = Some(timeout) } + /// Maximum amount of time it should take to write a message. pub fn write_timeout(&mut self, timeout: Duration) { self.write = Some(timeout) } + /// The initial TCP handshake timeout. + pub fn tcp_handshake_timeout(&mut self, timeout: Duration) { + self.tcp = timeout; + } + + /// How often is this peer pinged for activity pub fn ping_interval(&mut self, every: Duration) { self.ping_interval = every } @@ -180,6 +195,7 @@ impl Default for TimeoutParams { Self { read: Some(READ_TIMEOUT), write: None, + tcp: TCP_TIMEOUT, ping_interval: PING_INTERVAL, } }