mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
Move reth_eth_wire::ProtocolVersion and reth_eth_wire::Capabilities to reth-eth-wire-types (#10071)
Co-authored-by: Emilia Hane <emiliaha95@gmail.com>
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -7562,7 +7562,7 @@ dependencies = [
|
|||||||
"auto_impl",
|
"auto_impl",
|
||||||
"enr",
|
"enr",
|
||||||
"futures",
|
"futures",
|
||||||
"reth-eth-wire",
|
"reth-eth-wire-types",
|
||||||
"reth-network-p2p",
|
"reth-network-p2p",
|
||||||
"reth-network-peers",
|
"reth-network-peers",
|
||||||
"reth-network-types",
|
"reth-network-types",
|
||||||
|
|||||||
174
crates/net/eth-wire-types/src/capability.rs
Normal file
174
crates/net/eth-wire-types/src/capability.rs
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
//! All capability related types
|
||||||
|
|
||||||
|
use crate::EthVersion;
|
||||||
|
use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};
|
||||||
|
use bytes::BufMut;
|
||||||
|
use reth_codecs_derive::add_arbitrary_tests;
|
||||||
|
use std::{borrow::Cow, fmt};
|
||||||
|
|
||||||
|
/// A message indicating a supported capability and capability version.
|
||||||
|
#[add_arbitrary_tests(rlp)]
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable, Default, Hash)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
pub struct Capability {
|
||||||
|
/// The name of the subprotocol
|
||||||
|
pub name: Cow<'static, str>,
|
||||||
|
/// The version of the subprotocol
|
||||||
|
pub version: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Capability {
|
||||||
|
/// Create a new `Capability` with the given name and version.
|
||||||
|
pub const fn new(name: String, version: usize) -> Self {
|
||||||
|
Self { name: Cow::Owned(name), version }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new `Capability` with the given static name and version.
|
||||||
|
pub const fn new_static(name: &'static str, version: usize) -> Self {
|
||||||
|
Self { name: Cow::Borrowed(name), version }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the corresponding eth capability for the given version.
|
||||||
|
pub const fn eth(version: EthVersion) -> Self {
|
||||||
|
Self::new_static("eth", version as usize)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the [`EthVersion::Eth66`] capability.
|
||||||
|
pub const fn eth_66() -> Self {
|
||||||
|
Self::eth(EthVersion::Eth66)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the [`EthVersion::Eth67`] capability.
|
||||||
|
pub const fn eth_67() -> Self {
|
||||||
|
Self::eth(EthVersion::Eth67)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the [`EthVersion::Eth68`] capability.
|
||||||
|
pub const fn eth_68() -> Self {
|
||||||
|
Self::eth(EthVersion::Eth68)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether this is eth v66 protocol.
|
||||||
|
#[inline]
|
||||||
|
pub fn is_eth_v66(&self) -> bool {
|
||||||
|
self.name == "eth" && self.version == 66
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether this is eth v67.
|
||||||
|
#[inline]
|
||||||
|
pub fn is_eth_v67(&self) -> bool {
|
||||||
|
self.name == "eth" && self.version == 67
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether this is eth v68.
|
||||||
|
#[inline]
|
||||||
|
pub fn is_eth_v68(&self) -> bool {
|
||||||
|
self.name == "eth" && self.version == 68
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether this is any eth version.
|
||||||
|
#[inline]
|
||||||
|
pub fn is_eth(&self) -> bool {
|
||||||
|
self.is_eth_v66() || self.is_eth_v67() || self.is_eth_v68()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Capability {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "{}/{}", self.name, self.version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<EthVersion> for Capability {
|
||||||
|
#[inline]
|
||||||
|
fn from(value: EthVersion) -> Self {
|
||||||
|
Self::eth(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(test, feature = "arbitrary"))]
|
||||||
|
impl<'a> arbitrary::Arbitrary<'a> for Capability {
|
||||||
|
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
|
||||||
|
let version = u.int_in_range(0..=32)?; // TODO: What's the max?
|
||||||
|
let name = String::arbitrary(u)?; // TODO: what possible values?
|
||||||
|
Ok(Self::new(name, version))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Represents all capabilities of a node.
|
||||||
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||||
|
pub struct Capabilities {
|
||||||
|
/// All Capabilities and their versions
|
||||||
|
inner: Vec<Capability>,
|
||||||
|
eth_66: bool,
|
||||||
|
eth_67: bool,
|
||||||
|
eth_68: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Capabilities {
|
||||||
|
/// Returns all capabilities.
|
||||||
|
#[inline]
|
||||||
|
pub fn capabilities(&self) -> &[Capability] {
|
||||||
|
&self.inner
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Consumes the type and returns the all capabilities.
|
||||||
|
#[inline]
|
||||||
|
pub fn into_inner(self) -> Vec<Capability> {
|
||||||
|
self.inner
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether the peer supports `eth` sub-protocol.
|
||||||
|
#[inline]
|
||||||
|
pub const fn supports_eth(&self) -> bool {
|
||||||
|
self.eth_68 || self.eth_67 || self.eth_66
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether this peer supports eth v66 protocol.
|
||||||
|
#[inline]
|
||||||
|
pub const fn supports_eth_v66(&self) -> bool {
|
||||||
|
self.eth_66
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether this peer supports eth v67 protocol.
|
||||||
|
#[inline]
|
||||||
|
pub const fn supports_eth_v67(&self) -> bool {
|
||||||
|
self.eth_67
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether this peer supports eth v68 protocol.
|
||||||
|
#[inline]
|
||||||
|
pub const fn supports_eth_v68(&self) -> bool {
|
||||||
|
self.eth_68
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Vec<Capability>> for Capabilities {
|
||||||
|
fn from(value: Vec<Capability>) -> Self {
|
||||||
|
Self {
|
||||||
|
eth_66: value.iter().any(Capability::is_eth_v66),
|
||||||
|
eth_67: value.iter().any(Capability::is_eth_v67),
|
||||||
|
eth_68: value.iter().any(Capability::is_eth_v68),
|
||||||
|
inner: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encodable for Capabilities {
|
||||||
|
fn encode(&self, out: &mut dyn BufMut) {
|
||||||
|
self.inner.encode(out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decodable for Capabilities {
|
||||||
|
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
|
||||||
|
let inner = Vec::<Capability>::decode(buf)?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
eth_66: inner.iter().any(Capability::is_eth_v66),
|
||||||
|
eth_67: inner.iter().any(Capability::is_eth_v67),
|
||||||
|
eth_68: inner.iter().any(Capability::is_eth_v68),
|
||||||
|
inner,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -12,7 +12,7 @@ mod status;
|
|||||||
pub use status::{Status, StatusBuilder};
|
pub use status::{Status, StatusBuilder};
|
||||||
|
|
||||||
pub mod version;
|
pub mod version;
|
||||||
pub use version::EthVersion;
|
pub use version::{EthVersion, ProtocolVersion};
|
||||||
|
|
||||||
pub mod message;
|
pub mod message;
|
||||||
pub use message::{EthMessage, EthMessageID, ProtocolMessage};
|
pub use message::{EthMessage, EthMessageID, ProtocolMessage};
|
||||||
@ -37,3 +37,6 @@ pub use receipts::*;
|
|||||||
|
|
||||||
pub mod disconnect_reason;
|
pub mod disconnect_reason;
|
||||||
pub use disconnect_reason::*;
|
pub use disconnect_reason::*;
|
||||||
|
|
||||||
|
pub mod capability;
|
||||||
|
pub use capability::*;
|
||||||
|
|||||||
@ -1,8 +1,11 @@
|
|||||||
//! Support for representing the version of the `eth`
|
//! Support for representing the version of the `eth`
|
||||||
|
|
||||||
use std::str::FromStr;
|
use std::{fmt, str::FromStr};
|
||||||
|
|
||||||
|
use alloy_rlp::{Decodable, Encodable, Error as RlpError};
|
||||||
|
use bytes::BufMut;
|
||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
|
use reth_codecs_derive::derive_arbitrary;
|
||||||
|
|
||||||
/// Error thrown when failed to parse a valid [`EthVersion`].
|
/// Error thrown when failed to parse a valid [`EthVersion`].
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||||
@ -127,6 +130,45 @@ impl From<EthVersion> for &'static str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// RLPx `p2p` protocol version
|
||||||
|
#[derive_arbitrary(rlp)]
|
||||||
|
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||||
|
pub enum ProtocolVersion {
|
||||||
|
/// `p2p` version 4
|
||||||
|
V4 = 4,
|
||||||
|
/// `p2p` version 5
|
||||||
|
#[default]
|
||||||
|
V5 = 5,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ProtocolVersion {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "v{}", *self as u8)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Encodable for ProtocolVersion {
|
||||||
|
fn encode(&self, out: &mut dyn BufMut) {
|
||||||
|
(*self as u8).encode(out)
|
||||||
|
}
|
||||||
|
fn length(&self) -> usize {
|
||||||
|
// the version should be a single byte
|
||||||
|
(*self as u8).length()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Decodable for ProtocolVersion {
|
||||||
|
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
|
||||||
|
let version = u8::decode(buf)?;
|
||||||
|
match version {
|
||||||
|
4 => Ok(Self::V4),
|
||||||
|
5 => Ok(Self::V5),
|
||||||
|
_ => Err(RlpError::Custom("unknown p2p protocol version")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{EthVersion, ParseVersionError};
|
use super::{EthVersion, ParseVersionError};
|
||||||
|
|||||||
@ -5,18 +5,15 @@ use crate::{
|
|||||||
p2pstream::MAX_RESERVED_MESSAGE_ID,
|
p2pstream::MAX_RESERVED_MESSAGE_ID,
|
||||||
protocol::{ProtoVersion, Protocol},
|
protocol::{ProtoVersion, Protocol},
|
||||||
version::ParseVersionError,
|
version::ParseVersionError,
|
||||||
EthMessage, EthMessageID, EthVersion,
|
Capability, EthMessage, EthMessageID, EthVersion,
|
||||||
};
|
};
|
||||||
use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};
|
|
||||||
use derive_more::{Deref, DerefMut};
|
use derive_more::{Deref, DerefMut};
|
||||||
use reth_codecs::add_arbitrary_tests;
|
use reth_primitives::bytes::Bytes;
|
||||||
use reth_primitives::bytes::{BufMut, Bytes};
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
borrow::Cow,
|
borrow::Cow,
|
||||||
collections::{BTreeSet, HashMap},
|
collections::{BTreeSet, HashMap},
|
||||||
fmt,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A Capability message consisting of the message-id and the payload
|
/// A Capability message consisting of the message-id and the payload
|
||||||
@ -40,173 +37,6 @@ pub enum CapabilityMessage {
|
|||||||
Other(RawCapabilityMessage),
|
Other(RawCapabilityMessage),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A message indicating a supported capability and capability version.
|
|
||||||
#[add_arbitrary_tests(rlp)]
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable, Default, Hash)]
|
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
||||||
pub struct Capability {
|
|
||||||
/// The name of the subprotocol
|
|
||||||
pub name: Cow<'static, str>,
|
|
||||||
/// The version of the subprotocol
|
|
||||||
pub version: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Capability {
|
|
||||||
/// Create a new `Capability` with the given name and version.
|
|
||||||
pub const fn new(name: String, version: usize) -> Self {
|
|
||||||
Self { name: Cow::Owned(name), version }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Create a new `Capability` with the given static name and version.
|
|
||||||
pub const fn new_static(name: &'static str, version: usize) -> Self {
|
|
||||||
Self { name: Cow::Borrowed(name), version }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the corresponding eth capability for the given version.
|
|
||||||
pub const fn eth(version: EthVersion) -> Self {
|
|
||||||
Self::new_static("eth", version as usize)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`EthVersion::Eth66`] capability.
|
|
||||||
pub const fn eth_66() -> Self {
|
|
||||||
Self::eth(EthVersion::Eth66)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`EthVersion::Eth67`] capability.
|
|
||||||
pub const fn eth_67() -> Self {
|
|
||||||
Self::eth(EthVersion::Eth67)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the [`EthVersion::Eth68`] capability.
|
|
||||||
pub const fn eth_68() -> Self {
|
|
||||||
Self::eth(EthVersion::Eth68)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether this is eth v66 protocol.
|
|
||||||
#[inline]
|
|
||||||
pub fn is_eth_v66(&self) -> bool {
|
|
||||||
self.name == "eth" && self.version == 66
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether this is eth v67.
|
|
||||||
#[inline]
|
|
||||||
pub fn is_eth_v67(&self) -> bool {
|
|
||||||
self.name == "eth" && self.version == 67
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether this is eth v68.
|
|
||||||
#[inline]
|
|
||||||
pub fn is_eth_v68(&self) -> bool {
|
|
||||||
self.name == "eth" && self.version == 68
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether this is any eth version.
|
|
||||||
#[inline]
|
|
||||||
pub fn is_eth(&self) -> bool {
|
|
||||||
self.is_eth_v66() || self.is_eth_v67() || self.is_eth_v68()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for Capability {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(f, "{}/{}", self.name, self.version)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<EthVersion> for Capability {
|
|
||||||
#[inline]
|
|
||||||
fn from(value: EthVersion) -> Self {
|
|
||||||
Self::eth(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(any(test, feature = "arbitrary"))]
|
|
||||||
impl<'a> arbitrary::Arbitrary<'a> for Capability {
|
|
||||||
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
|
|
||||||
let version = u.int_in_range(0..=32)?; // TODO: What's the max?
|
|
||||||
let name = String::arbitrary(u)?; // TODO: what possible values?
|
|
||||||
Ok(Self::new(name, version))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Represents all capabilities of a node.
|
|
||||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
|
||||||
pub struct Capabilities {
|
|
||||||
/// All Capabilities and their versions
|
|
||||||
inner: Vec<Capability>,
|
|
||||||
eth_66: bool,
|
|
||||||
eth_67: bool,
|
|
||||||
eth_68: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Capabilities {
|
|
||||||
/// Returns all capabilities.
|
|
||||||
#[inline]
|
|
||||||
pub fn capabilities(&self) -> &[Capability] {
|
|
||||||
&self.inner
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Consumes the type and returns the all capabilities.
|
|
||||||
#[inline]
|
|
||||||
pub fn into_inner(self) -> Vec<Capability> {
|
|
||||||
self.inner
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether the peer supports `eth` sub-protocol.
|
|
||||||
#[inline]
|
|
||||||
pub const fn supports_eth(&self) -> bool {
|
|
||||||
self.eth_68 || self.eth_67 || self.eth_66
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether this peer supports eth v66 protocol.
|
|
||||||
#[inline]
|
|
||||||
pub const fn supports_eth_v66(&self) -> bool {
|
|
||||||
self.eth_66
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether this peer supports eth v67 protocol.
|
|
||||||
#[inline]
|
|
||||||
pub const fn supports_eth_v67(&self) -> bool {
|
|
||||||
self.eth_67
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Whether this peer supports eth v68 protocol.
|
|
||||||
#[inline]
|
|
||||||
pub const fn supports_eth_v68(&self) -> bool {
|
|
||||||
self.eth_68
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Vec<Capability>> for Capabilities {
|
|
||||||
fn from(value: Vec<Capability>) -> Self {
|
|
||||||
Self {
|
|
||||||
eth_66: value.iter().any(Capability::is_eth_v66),
|
|
||||||
eth_67: value.iter().any(Capability::is_eth_v67),
|
|
||||||
eth_68: value.iter().any(Capability::is_eth_v68),
|
|
||||||
inner: value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encodable for Capabilities {
|
|
||||||
fn encode(&self, out: &mut dyn BufMut) {
|
|
||||||
self.inner.encode(out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decodable for Capabilities {
|
|
||||||
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
|
|
||||||
let inner = Vec::<Capability>::decode(buf)?;
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
eth_66: inner.iter().any(Capability::is_eth_v66),
|
|
||||||
eth_67: inner.iter().any(Capability::is_eth_v67),
|
|
||||||
eth_68: inner.iter().any(Capability::is_eth_v68),
|
|
||||||
inner,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// This represents a shared capability, its version, and its message id offset.
|
/// This represents a shared capability, its version, and its message id offset.
|
||||||
///
|
///
|
||||||
/// The [offset](SharedCapability::message_id_offset) is the message ID offset for this shared
|
/// The [offset](SharedCapability::message_id_offset) is the message ID offset for this shared
|
||||||
@ -549,6 +379,7 @@ pub struct UnsupportedCapabilityError {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::{Capabilities, Capability};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_eth_68() {
|
fn from_eth_68() {
|
||||||
|
|||||||
@ -349,8 +349,9 @@ mod tests {
|
|||||||
broadcast::BlockHashNumber,
|
broadcast::BlockHashNumber,
|
||||||
errors::{EthHandshakeError, EthStreamError},
|
errors::{EthHandshakeError, EthStreamError},
|
||||||
hello::DEFAULT_TCP_PORT,
|
hello::DEFAULT_TCP_PORT,
|
||||||
p2pstream::{ProtocolVersion, UnauthedP2PStream},
|
p2pstream::UnauthedP2PStream,
|
||||||
EthMessage, EthStream, EthVersion, HelloMessageWithProtocols, PassthroughCodec, Status,
|
EthMessage, EthStream, EthVersion, HelloMessageWithProtocols, PassthroughCodec,
|
||||||
|
ProtocolVersion, Status,
|
||||||
};
|
};
|
||||||
use futures::{SinkExt, StreamExt};
|
use futures::{SinkExt, StreamExt};
|
||||||
use reth_chainspec::NamedChain;
|
use reth_chainspec::NamedChain;
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use crate::{capability::Capability, EthVersion, ProtocolVersion};
|
use crate::{Capability, EthVersion, ProtocolVersion};
|
||||||
use alloy_rlp::{RlpDecodable, RlpEncodable};
|
use alloy_rlp::{RlpDecodable, RlpEncodable};
|
||||||
use reth_codecs::derive_arbitrary;
|
use reth_codecs::derive_arbitrary;
|
||||||
use reth_network_peers::PeerId;
|
use reth_network_peers::PeerId;
|
||||||
@ -214,9 +214,7 @@ impl HelloMessageBuilder {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
use crate::{p2pstream::P2PMessage, Capability, EthVersion, HelloMessage, ProtocolVersion};
|
||||||
capability::Capability, p2pstream::P2PMessage, EthVersion, HelloMessage, ProtocolVersion,
|
|
||||||
};
|
|
||||||
use alloy_rlp::{Decodable, Encodable, EMPTY_STRING_CODE};
|
use alloy_rlp::{Decodable, Encodable, EMPTY_STRING_CODE};
|
||||||
use reth_network_peers::pk2id;
|
use reth_network_peers::pk2id;
|
||||||
use secp256k1::{SecretKey, SECP256K1};
|
use secp256k1::{SecretKey, SECP256K1};
|
||||||
|
|||||||
@ -32,14 +32,14 @@ pub use tokio_util::codec::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
capability::Capability,
|
|
||||||
disconnect::CanDisconnect,
|
disconnect::CanDisconnect,
|
||||||
ethstream::{EthStream, UnauthedEthStream, MAX_MESSAGE_SIZE},
|
ethstream::{EthStream, UnauthedEthStream, MAX_MESSAGE_SIZE},
|
||||||
hello::{HelloMessage, HelloMessageBuilder, HelloMessageWithProtocols},
|
hello::{HelloMessage, HelloMessageBuilder, HelloMessageWithProtocols},
|
||||||
p2pstream::{
|
p2pstream::{
|
||||||
DisconnectP2P, P2PMessage, P2PMessageID, P2PStream, ProtocolVersion, UnauthedP2PStream,
|
DisconnectP2P, P2PMessage, P2PMessageID, P2PStream, UnauthedP2PStream,
|
||||||
MAX_RESERVED_MESSAGE_ID,
|
MAX_RESERVED_MESSAGE_ID,
|
||||||
},
|
},
|
||||||
|
Capability, ProtocolVersion,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Re-export wire types
|
// Re-export wire types
|
||||||
|
|||||||
@ -17,10 +17,10 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
capability::{Capability, SharedCapabilities, SharedCapability, UnsupportedCapabilityError},
|
capability::{SharedCapabilities, SharedCapability, UnsupportedCapabilityError},
|
||||||
errors::{EthStreamError, P2PStreamError},
|
errors::{EthStreamError, P2PStreamError},
|
||||||
p2pstream::DisconnectP2P,
|
p2pstream::DisconnectP2P,
|
||||||
CanDisconnect, DisconnectReason, EthStream, P2PStream, Status, UnauthedEthStream,
|
CanDisconnect, Capability, DisconnectReason, EthStream, P2PStream, Status, UnauthedEthStream,
|
||||||
};
|
};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use futures::{Sink, SinkExt, Stream, StreamExt, TryStream, TryStreamExt};
|
use futures::{Sink, SinkExt, Stream, StreamExt, TryStream, TryStreamExt};
|
||||||
|
|||||||
@ -16,7 +16,7 @@ use reth_primitives::{
|
|||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
fmt, io,
|
io,
|
||||||
pin::Pin,
|
pin::Pin,
|
||||||
task::{ready, Context, Poll},
|
task::{ready, Context, Poll},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
@ -788,49 +788,10 @@ impl TryFrom<u8> for P2PMessageID {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// RLPx `p2p` protocol version
|
|
||||||
#[derive_arbitrary(rlp)]
|
|
||||||
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
|
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
|
||||||
pub enum ProtocolVersion {
|
|
||||||
/// `p2p` version 4
|
|
||||||
V4 = 4,
|
|
||||||
/// `p2p` version 5
|
|
||||||
#[default]
|
|
||||||
V5 = 5,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for ProtocolVersion {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
write!(f, "v{}", *self as u8)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Encodable for ProtocolVersion {
|
|
||||||
fn encode(&self, out: &mut dyn BufMut) {
|
|
||||||
(*self as u8).encode(out)
|
|
||||||
}
|
|
||||||
fn length(&self) -> usize {
|
|
||||||
// the version should be a single byte
|
|
||||||
(*self as u8).length()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Decodable for ProtocolVersion {
|
|
||||||
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
|
|
||||||
let version = u8::decode(buf)?;
|
|
||||||
match version {
|
|
||||||
4 => Ok(Self::V4),
|
|
||||||
5 => Ok(Self::V5),
|
|
||||||
_ => Err(RlpError::Custom("unknown p2p protocol version")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{capability::SharedCapability, test_utils::eth_hello, EthVersion};
|
use crate::{capability::SharedCapability, test_utils::eth_hello, EthVersion, ProtocolVersion};
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
use tokio::net::{TcpListener, TcpStream};
|
||||||
use tokio_util::codec::Decoder;
|
use tokio_util::codec::Decoder;
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
//! A Protocol defines a P2P subprotocol in a `RLPx` connection
|
//! A Protocol defines a P2P subprotocol in a `RLPx` connection
|
||||||
|
|
||||||
use crate::{capability::Capability, EthMessageID, EthVersion};
|
use crate::{Capability, EthMessageID, EthVersion};
|
||||||
|
|
||||||
/// Type that represents a [Capability] and the number of messages it uses.
|
/// Type that represents a [Capability] and the number of messages it uses.
|
||||||
///
|
///
|
||||||
|
|||||||
@ -60,7 +60,7 @@ pub async fn connect_passthrough(
|
|||||||
/// A Rplx subprotocol for testing
|
/// A Rplx subprotocol for testing
|
||||||
pub mod proto {
|
pub mod proto {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::{capability::Capability, protocol::Protocol};
|
use crate::{protocol::Protocol, Capability};
|
||||||
use bytes::{Buf, BufMut, BytesMut};
|
use bytes::{Buf, BufMut, BytesMut};
|
||||||
|
|
||||||
/// Returns a new testing `HelloMessage` with eth and the test protocol
|
/// Returns a new testing `HelloMessage` with eth and the test protocol
|
||||||
|
|||||||
@ -13,7 +13,7 @@ workspace = true
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# reth
|
# reth
|
||||||
reth-eth-wire.workspace = true
|
reth-eth-wire-types.workspace = true
|
||||||
alloy-rpc-types-admin.workspace = true
|
alloy-rpc-types-admin.workspace = true
|
||||||
reth-network-peers.workspace = true
|
reth-network-peers.workspace = true
|
||||||
reth-network-types.workspace = true
|
reth-network-types.workspace = true
|
||||||
|
|||||||
@ -28,7 +28,7 @@ pub use error::NetworkError;
|
|||||||
|
|
||||||
use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};
|
use std::{future::Future, net::SocketAddr, sync::Arc, time::Instant};
|
||||||
|
|
||||||
use reth_eth_wire::{capability::Capabilities, DisconnectReason, EthVersion, Status};
|
use reth_eth_wire_types::{capability::Capabilities, DisconnectReason, EthVersion, Status};
|
||||||
use reth_network_peers::NodeRecord;
|
use reth_network_peers::NodeRecord;
|
||||||
|
|
||||||
/// The `PeerId` type.
|
/// The `PeerId` type.
|
||||||
|
|||||||
@ -7,7 +7,7 @@ use std::net::{IpAddr, SocketAddr};
|
|||||||
|
|
||||||
use alloy_rpc_types_admin::EthProtocolInfo;
|
use alloy_rpc_types_admin::EthProtocolInfo;
|
||||||
use enr::{secp256k1::SecretKey, Enr};
|
use enr::{secp256k1::SecretKey, Enr};
|
||||||
use reth_eth_wire::{DisconnectReason, ProtocolVersion};
|
use reth_eth_wire_types::{DisconnectReason, ProtocolVersion};
|
||||||
use reth_network_peers::NodeRecord;
|
use reth_network_peers::NodeRecord;
|
||||||
use reth_network_types::{PeerKind, Reputation, ReputationChangeKind};
|
use reth_network_types::{PeerKind, Reputation, ReputationChangeKind};
|
||||||
|
|
||||||
|
|||||||
@ -30,8 +30,7 @@ use std::{
|
|||||||
use futures::{Future, StreamExt};
|
use futures::{Future, StreamExt};
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use reth_eth_wire::{
|
use reth_eth_wire::{
|
||||||
capability::{Capabilities, CapabilityMessage},
|
capability::CapabilityMessage, Capabilities, DisconnectReason, EthVersion, Status,
|
||||||
DisconnectReason, EthVersion, Status,
|
|
||||||
};
|
};
|
||||||
use reth_fs_util::{self as fs, FsPathError};
|
use reth_fs_util::{self as fs, FsPathError};
|
||||||
use reth_metrics::common::mpsc::UnboundedMeteredSender;
|
use reth_metrics::common::mpsc::UnboundedMeteredSender;
|
||||||
|
|||||||
@ -13,10 +13,9 @@ use std::{
|
|||||||
|
|
||||||
use futures::{stream::Fuse, SinkExt, StreamExt};
|
use futures::{stream::Fuse, SinkExt, StreamExt};
|
||||||
use reth_eth_wire::{
|
use reth_eth_wire::{
|
||||||
capability::Capabilities,
|
|
||||||
errors::{EthHandshakeError, EthStreamError, P2PStreamError},
|
errors::{EthHandshakeError, EthStreamError, P2PStreamError},
|
||||||
message::{EthBroadcastMessage, RequestPair},
|
message::{EthBroadcastMessage, RequestPair},
|
||||||
DisconnectP2P, DisconnectReason, EthMessage,
|
Capabilities, DisconnectP2P, DisconnectReason, EthMessage,
|
||||||
};
|
};
|
||||||
use reth_metrics::common::mpsc::MeteredPollSender;
|
use reth_metrics::common::mpsc::MeteredPollSender;
|
||||||
use reth_network_p2p::error::RequestError;
|
use reth_network_p2p::error::RequestError;
|
||||||
|
|||||||
@ -4,9 +4,8 @@ use std::{io, net::SocketAddr, sync::Arc, time::Instant};
|
|||||||
|
|
||||||
use reth_ecies::ECIESError;
|
use reth_ecies::ECIESError;
|
||||||
use reth_eth_wire::{
|
use reth_eth_wire::{
|
||||||
capability::{Capabilities, CapabilityMessage},
|
capability::CapabilityMessage, errors::EthStreamError, Capabilities, DisconnectReason,
|
||||||
errors::EthStreamError,
|
EthVersion, Status,
|
||||||
DisconnectReason, EthVersion, Status,
|
|
||||||
};
|
};
|
||||||
use reth_network_api::PeerInfo;
|
use reth_network_api::PeerInfo;
|
||||||
use reth_network_peers::{NodeRecord, PeerId};
|
use reth_network_peers::{NodeRecord, PeerId};
|
||||||
|
|||||||
@ -28,11 +28,9 @@ use counter::SessionCounter;
|
|||||||
use futures::{future::Either, io, FutureExt, StreamExt};
|
use futures::{future::Either, io, FutureExt, StreamExt};
|
||||||
use reth_ecies::{stream::ECIESStream, ECIESError};
|
use reth_ecies::{stream::ECIESStream, ECIESError};
|
||||||
use reth_eth_wire::{
|
use reth_eth_wire::{
|
||||||
capability::{Capabilities, CapabilityMessage},
|
capability::CapabilityMessage, errors::EthStreamError, multiplex::RlpxProtocolMultiplexer,
|
||||||
errors::EthStreamError,
|
Capabilities, DisconnectReason, EthVersion, HelloMessageWithProtocols, Status,
|
||||||
multiplex::RlpxProtocolMultiplexer,
|
UnauthedEthStream, UnauthedP2PStream,
|
||||||
DisconnectReason, EthVersion, HelloMessageWithProtocols, Status, UnauthedEthStream,
|
|
||||||
UnauthedP2PStream,
|
|
||||||
};
|
};
|
||||||
use reth_metrics::common::mpsc::MeteredPollSender;
|
use reth_metrics::common::mpsc::MeteredPollSender;
|
||||||
use reth_network_peers::PeerId;
|
use reth_network_peers::PeerId;
|
||||||
|
|||||||
@ -13,9 +13,7 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use reth_eth_wire::{
|
use reth_eth_wire::{BlockHashNumber, Capabilities, DisconnectReason, NewBlockHashes, Status};
|
||||||
capability::Capabilities, BlockHashNumber, DisconnectReason, NewBlockHashes, Status,
|
|
||||||
};
|
|
||||||
use reth_network_peers::PeerId;
|
use reth_network_peers::PeerId;
|
||||||
use reth_network_types::{PeerAddr, PeerKind};
|
use reth_network_types::{PeerAddr, PeerKind};
|
||||||
use reth_primitives::{ForkId, B256};
|
use reth_primitives::{ForkId, B256};
|
||||||
@ -553,10 +551,7 @@ mod tests {
|
|||||||
state::{BlockNumReader, NetworkState},
|
state::{BlockNumReader, NetworkState},
|
||||||
PeerRequest,
|
PeerRequest,
|
||||||
};
|
};
|
||||||
use reth_eth_wire::{
|
use reth_eth_wire::{BlockBodies, Capabilities, Capability, EthVersion};
|
||||||
capability::{Capabilities, Capability},
|
|
||||||
BlockBodies, EthVersion,
|
|
||||||
};
|
|
||||||
use reth_network_p2p::{bodies::client::BodiesClient, error::RequestError};
|
use reth_network_p2p::{bodies::client::BodiesClient, error::RequestError};
|
||||||
use reth_network_peers::PeerId;
|
use reth_network_peers::PeerId;
|
||||||
use reth_primitives::{BlockBody, Header, B256};
|
use reth_primitives::{BlockBody, Header, B256};
|
||||||
|
|||||||
@ -8,9 +8,7 @@ use std::{
|
|||||||
|
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
use reth_eth_wire::{
|
use reth_eth_wire::{
|
||||||
capability::{Capabilities, CapabilityMessage},
|
capability::CapabilityMessage, errors::EthStreamError, Capabilities, EthVersion, Status,
|
||||||
errors::EthStreamError,
|
|
||||||
EthVersion, Status,
|
|
||||||
};
|
};
|
||||||
use reth_network_peers::PeerId;
|
use reth_network_peers::PeerId;
|
||||||
use tracing::trace;
|
use tracing::trace;
|
||||||
|
|||||||
@ -26,7 +26,7 @@ use crate::multiplex::proto::{PingPongProtoMessage, PingPongProtoMessageKind};
|
|||||||
/// A simple Rlpx subprotocol that sends pings and pongs
|
/// A simple Rlpx subprotocol that sends pings and pongs
|
||||||
mod proto {
|
mod proto {
|
||||||
use super::*;
|
use super::*;
|
||||||
use reth_eth_wire::capability::Capability;
|
use reth_eth_wire::Capability;
|
||||||
use reth_primitives::{Buf, BufMut};
|
use reth_primitives::{Buf, BufMut};
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
|
|||||||
Reference in New Issue
Block a user