diff --git a/crates/net/eth-wire/src/capability.rs b/crates/net/eth-wire/src/capability.rs index 192afd3a29..ffd96afae0 100644 --- a/crates/net/eth-wire/src/capability.rs +++ b/crates/net/eth-wire/src/capability.rs @@ -1,3 +1,5 @@ +//! All capability related types + use crate::{version::ParseVersionError, EthMessage, EthVersion}; use bytes::{BufMut, Bytes}; use reth_rlp::{Decodable, DecodeError, Encodable, RlpDecodable, RlpEncodable}; @@ -121,6 +123,7 @@ impl Decodable for Capabilities { /// This represents a shared capability, its version, and its offset. #[derive(Debug, Clone, PartialEq, Eq, Hash)] +#[allow(missing_docs)] pub enum SharedCapability { /// The `eth` capability. Eth { version: EthVersion, offset: u8 }, @@ -139,7 +142,7 @@ impl SharedCapability { } /// Returns the name of the capability. - pub(crate) fn name(&self) -> &str { + pub fn name(&self) -> &str { match self { SharedCapability::Eth { .. } => "eth", SharedCapability::UnknownCapability { name, .. } => name, @@ -147,7 +150,7 @@ impl SharedCapability { } /// Returns the version of the capability. - pub(crate) fn version(&self) -> u8 { + pub fn version(&self) -> u8 { match self { SharedCapability::Eth { version, .. } => *version as u8, SharedCapability::UnknownCapability { version, .. } => *version, @@ -155,7 +158,7 @@ impl SharedCapability { } /// Returns the message ID offset of the current capability. - pub(crate) fn offset(&self) -> u8 { + pub fn offset(&self) -> u8 { match self { SharedCapability::Eth { offset, .. } => *offset, SharedCapability::UnknownCapability { offset, .. } => *offset, @@ -163,7 +166,7 @@ impl SharedCapability { } /// Returns the number of protocol messages supported by this capability. - pub(crate) fn num_messages(&self) -> Result { + pub fn num_messages(&self) -> Result { match self { SharedCapability::Eth { version, .. } => Ok(version.total_messages()), _ => Err(SharedCapabilityError::UnknownCapability), diff --git a/crates/net/eth-wire/src/disconnect.rs b/crates/net/eth-wire/src/disconnect.rs index 359f5c052b..b0627c1f00 100644 --- a/crates/net/eth-wire/src/disconnect.rs +++ b/crates/net/eth-wire/src/disconnect.rs @@ -1,7 +1,8 @@ -use std::fmt::Display; +//! Disconnect use bytes::Buf; use reth_rlp::{Decodable, DecodeError, Encodable, EMPTY_LIST_CODE}; +use std::fmt::Display; use thiserror::Error; /// RLPx disconnect reason. @@ -21,8 +22,11 @@ pub enum DisconnectReason { AlreadyConnected = 0x05, /// `p2p` protocol version is incompatible IncompatibleP2PProtocolVersion = 0x06, + /// Received a null node identity. NullNodeIdentity = 0x07, + /// Reason when the client is shutting down. ClientQuitting = 0x08, + /// When the received handshake's identify is different from what is expected. UnexpectedHandshakeIdentity = 0x09, /// The node is connected to itself ConnectedToSelf = 0x0a, @@ -149,9 +153,8 @@ mod tests { }; use reth_rlp::{Decodable, Encodable}; - #[test] - fn disconnect_round_trip() { - let all_reasons = vec![ + fn all_reasons() -> Vec { + vec![ DisconnectReason::DisconnectRequested, DisconnectReason::TcpSubsystemError, DisconnectReason::ProtocolBreach, @@ -165,7 +168,12 @@ mod tests { DisconnectReason::ConnectedToSelf, DisconnectReason::PingTimeout, DisconnectReason::SubprotocolSpecific, - ]; + ] + } + + #[test] + fn disconnect_round_trip() { + let all_reasons = all_reasons(); for reason in all_reasons { let disconnect = P2PMessage::Disconnect(reason); @@ -186,21 +194,7 @@ mod tests { #[test] fn disconnect_encoding_length() { - let all_reasons = vec![ - DisconnectReason::DisconnectRequested, - DisconnectReason::TcpSubsystemError, - DisconnectReason::ProtocolBreach, - DisconnectReason::UselessPeer, - DisconnectReason::TooManyPeers, - DisconnectReason::AlreadyConnected, - DisconnectReason::IncompatibleP2PProtocolVersion, - DisconnectReason::NullNodeIdentity, - DisconnectReason::ClientQuitting, - DisconnectReason::UnexpectedHandshakeIdentity, - DisconnectReason::ConnectedToSelf, - DisconnectReason::PingTimeout, - DisconnectReason::SubprotocolSpecific, - ]; + let all_reasons = all_reasons(); for reason in all_reasons { let disconnect = P2PMessage::Disconnect(reason); @@ -232,8 +226,7 @@ mod tests { // ensure that the two encodings are equal assert_eq!( disconnect_expected, disconnect_encoded, - "left: {:#x?}, right: {:#x?}", - disconnect_expected, disconnect_encoded + "left: {disconnect_expected:#x?}, right: {disconnect_encoded:#x?}" ); // also ensure that the length is correct diff --git a/crates/net/eth-wire/src/error.rs b/crates/net/eth-wire/src/error.rs index f089851a4d..e655e2e481 100644 --- a/crates/net/eth-wire/src/error.rs +++ b/crates/net/eth-wire/src/error.rs @@ -57,6 +57,7 @@ pub enum HandshakeError { /// Errors when sending/receiving p2p messages. These should result in kicking the peer. #[derive(thiserror::Error, Debug)] +#[allow(missing_docs)] pub enum P2PStreamError { #[error(transparent)] Io(#[from] io::Error), @@ -104,7 +105,8 @@ impl P2PStreamError { } /// Errors when conducting a p2p handshake -#[derive(thiserror::Error, Debug)] +#[derive(thiserror::Error, Debug, Clone, Eq, PartialEq)] +#[allow(missing_docs)] pub enum P2PHandshakeError { #[error("hello message can only be recv/sent in handshake")] HelloNotInHandshake, diff --git a/crates/net/eth-wire/src/lib.rs b/crates/net/eth-wire/src/lib.rs index ac3e009205..a09dea90c6 100644 --- a/crates/net/eth-wire/src/lib.rs +++ b/crates/net/eth-wire/src/lib.rs @@ -1,6 +1,4 @@ -#![allow(dead_code)] // TODO: REMOVE once eth-wire is done. -// #![warn(missing_docs, unreachable_pub)] -#![allow(missing_docs, unreachable_pub)] +#![warn(missing_docs, unreachable_pub)] #![deny(unused_must_use, rust_2018_idioms)] #![doc(test( no_crate_inject, diff --git a/crates/net/eth-wire/src/p2pstream.rs b/crates/net/eth-wire/src/p2pstream.rs index 312863af12..64d6d2ebc6 100644 --- a/crates/net/eth-wire/src/p2pstream.rs +++ b/crates/net/eth-wire/src/p2pstream.rs @@ -201,6 +201,11 @@ impl P2PStream { } } + /// Returns the shared capability for this stream. + pub fn shared_capability(&self) -> &SharedCapability { + &self.shared_capability + } + /// Returns `true` if the connection is about to disconnect. pub fn is_disconnecting(&self) -> bool { self.disconnecting @@ -815,8 +820,7 @@ mod tests { // ensure that the two encodings are equal assert_eq!( ping_expected, ping_encoded, - "left: {:#x?}, right: {:#x?}", - ping_expected, ping_encoded + "left: {ping_expected:#x?}, right: {ping_encoded:#x?}" ); // also ensure that the length is correct @@ -852,8 +856,7 @@ mod tests { // ensure that the two encodings are equal assert_eq!( pong_expected, pong_encoded, - "left: {:#x?}, right: {:#x?}", - pong_expected, pong_encoded + "left: {pong_expected:#x?}, right: {pong_encoded:#x?}" ); // also ensure that the length is correct diff --git a/crates/net/eth-wire/src/types/version.rs b/crates/net/eth-wire/src/types/version.rs index 4735e0cd84..8db1759a1c 100644 --- a/crates/net/eth-wire/src/types/version.rs +++ b/crates/net/eth-wire/src/types/version.rs @@ -1,8 +1,10 @@ +//! Support for representing the version of the `eth`. [`Capability`]. + +use crate::capability::Capability; use std::str::FromStr; use thiserror::Error; -use crate::capability::Capability; - +/// Error thrown when failed to parse a valid [`EthVersion`]. #[derive(Debug, Clone, PartialEq, Eq, Error)] #[error("Unknown eth protocol version: {0}")] pub struct ParseVersionError(String);