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:
nk_ysg
2024-08-05 00:22:14 +08:00
committed by GitHub
parent 9c80cc4a57
commit b592607394
22 changed files with 252 additions and 254 deletions

2
Cargo.lock generated
View File

@ -7562,7 +7562,7 @@ dependencies = [
"auto_impl",
"enr",
"futures",
"reth-eth-wire",
"reth-eth-wire-types",
"reth-network-p2p",
"reth-network-peers",
"reth-network-types",

View 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,
})
}
}

View File

@ -12,7 +12,7 @@ mod status;
pub use status::{Status, StatusBuilder};
pub mod version;
pub use version::EthVersion;
pub use version::{EthVersion, ProtocolVersion};
pub mod message;
pub use message::{EthMessage, EthMessageID, ProtocolMessage};
@ -37,3 +37,6 @@ pub use receipts::*;
pub mod disconnect_reason;
pub use disconnect_reason::*;
pub mod capability;
pub use capability::*;

View File

@ -1,8 +1,11 @@
//! 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 reth_codecs_derive::derive_arbitrary;
/// Error thrown when failed to parse a valid [`EthVersion`].
#[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)]
mod tests {
use super::{EthVersion, ParseVersionError};

View File

@ -5,18 +5,15 @@ use crate::{
p2pstream::MAX_RESERVED_MESSAGE_ID,
protocol::{ProtoVersion, Protocol},
version::ParseVersionError,
EthMessage, EthMessageID, EthVersion,
Capability, EthMessage, EthMessageID, EthVersion,
};
use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};
use derive_more::{Deref, DerefMut};
use reth_codecs::add_arbitrary_tests;
use reth_primitives::bytes::{BufMut, Bytes};
use reth_primitives::bytes::Bytes;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{
borrow::Cow,
collections::{BTreeSet, HashMap},
fmt,
};
/// A Capability message consisting of the message-id and the payload
@ -40,173 +37,6 @@ pub enum CapabilityMessage {
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.
///
/// The [offset](SharedCapability::message_id_offset) is the message ID offset for this shared
@ -549,6 +379,7 @@ pub struct UnsupportedCapabilityError {
#[cfg(test)]
mod tests {
use super::*;
use crate::{Capabilities, Capability};
#[test]
fn from_eth_68() {

View File

@ -349,8 +349,9 @@ mod tests {
broadcast::BlockHashNumber,
errors::{EthHandshakeError, EthStreamError},
hello::DEFAULT_TCP_PORT,
p2pstream::{ProtocolVersion, UnauthedP2PStream},
EthMessage, EthStream, EthVersion, HelloMessageWithProtocols, PassthroughCodec, Status,
p2pstream::UnauthedP2PStream,
EthMessage, EthStream, EthVersion, HelloMessageWithProtocols, PassthroughCodec,
ProtocolVersion, Status,
};
use futures::{SinkExt, StreamExt};
use reth_chainspec::NamedChain;

View File

@ -1,4 +1,4 @@
use crate::{capability::Capability, EthVersion, ProtocolVersion};
use crate::{Capability, EthVersion, ProtocolVersion};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use reth_codecs::derive_arbitrary;
use reth_network_peers::PeerId;
@ -214,9 +214,7 @@ impl HelloMessageBuilder {
#[cfg(test)]
mod tests {
use crate::{
capability::Capability, p2pstream::P2PMessage, EthVersion, HelloMessage, ProtocolVersion,
};
use crate::{p2pstream::P2PMessage, Capability, EthVersion, HelloMessage, ProtocolVersion};
use alloy_rlp::{Decodable, Encodable, EMPTY_STRING_CODE};
use reth_network_peers::pk2id;
use secp256k1::{SecretKey, SECP256K1};

View File

@ -32,14 +32,14 @@ pub use tokio_util::codec::{
};
pub use crate::{
capability::Capability,
disconnect::CanDisconnect,
ethstream::{EthStream, UnauthedEthStream, MAX_MESSAGE_SIZE},
hello::{HelloMessage, HelloMessageBuilder, HelloMessageWithProtocols},
p2pstream::{
DisconnectP2P, P2PMessage, P2PMessageID, P2PStream, ProtocolVersion, UnauthedP2PStream,
DisconnectP2P, P2PMessage, P2PMessageID, P2PStream, UnauthedP2PStream,
MAX_RESERVED_MESSAGE_ID,
},
Capability, ProtocolVersion,
};
// Re-export wire types

View File

@ -17,10 +17,10 @@ use std::{
};
use crate::{
capability::{Capability, SharedCapabilities, SharedCapability, UnsupportedCapabilityError},
capability::{SharedCapabilities, SharedCapability, UnsupportedCapabilityError},
errors::{EthStreamError, P2PStreamError},
p2pstream::DisconnectP2P,
CanDisconnect, DisconnectReason, EthStream, P2PStream, Status, UnauthedEthStream,
CanDisconnect, Capability, DisconnectReason, EthStream, P2PStream, Status, UnauthedEthStream,
};
use bytes::{Bytes, BytesMut};
use futures::{Sink, SinkExt, Stream, StreamExt, TryStream, TryStreamExt};

View File

@ -16,7 +16,7 @@ use reth_primitives::{
};
use std::{
collections::VecDeque,
fmt, io,
io,
pin::Pin,
task::{ready, Context, Poll},
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)]
mod tests {
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_util::codec::Decoder;

View File

@ -1,6 +1,6 @@
//! 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.
///

View File

@ -60,7 +60,7 @@ pub async fn connect_passthrough(
/// A Rplx subprotocol for testing
pub mod proto {
use super::*;
use crate::{capability::Capability, protocol::Protocol};
use crate::{protocol::Protocol, Capability};
use bytes::{Buf, BufMut, BytesMut};
/// Returns a new testing `HelloMessage` with eth and the test protocol

View File

@ -13,7 +13,7 @@ workspace = true
[dependencies]
# reth
reth-eth-wire.workspace = true
reth-eth-wire-types.workspace = true
alloy-rpc-types-admin.workspace = true
reth-network-peers.workspace = true
reth-network-types.workspace = true

View File

@ -28,7 +28,7 @@ pub use error::NetworkError;
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;
/// The `PeerId` type.

View File

@ -7,7 +7,7 @@ use std::net::{IpAddr, SocketAddr};
use alloy_rpc_types_admin::EthProtocolInfo;
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_types::{PeerKind, Reputation, ReputationChangeKind};

View File

@ -30,8 +30,7 @@ use std::{
use futures::{Future, StreamExt};
use parking_lot::Mutex;
use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
DisconnectReason, EthVersion, Status,
capability::CapabilityMessage, Capabilities, DisconnectReason, EthVersion, Status,
};
use reth_fs_util::{self as fs, FsPathError};
use reth_metrics::common::mpsc::UnboundedMeteredSender;

View File

@ -13,10 +13,9 @@ use std::{
use futures::{stream::Fuse, SinkExt, StreamExt};
use reth_eth_wire::{
capability::Capabilities,
errors::{EthHandshakeError, EthStreamError, P2PStreamError},
message::{EthBroadcastMessage, RequestPair},
DisconnectP2P, DisconnectReason, EthMessage,
Capabilities, DisconnectP2P, DisconnectReason, EthMessage,
};
use reth_metrics::common::mpsc::MeteredPollSender;
use reth_network_p2p::error::RequestError;

View File

@ -4,9 +4,8 @@ use std::{io, net::SocketAddr, sync::Arc, time::Instant};
use reth_ecies::ECIESError;
use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
errors::EthStreamError,
DisconnectReason, EthVersion, Status,
capability::CapabilityMessage, errors::EthStreamError, Capabilities, DisconnectReason,
EthVersion, Status,
};
use reth_network_api::PeerInfo;
use reth_network_peers::{NodeRecord, PeerId};

View File

@ -28,11 +28,9 @@ use counter::SessionCounter;
use futures::{future::Either, io, FutureExt, StreamExt};
use reth_ecies::{stream::ECIESStream, ECIESError};
use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
errors::EthStreamError,
multiplex::RlpxProtocolMultiplexer,
DisconnectReason, EthVersion, HelloMessageWithProtocols, Status, UnauthedEthStream,
UnauthedP2PStream,
capability::CapabilityMessage, errors::EthStreamError, multiplex::RlpxProtocolMultiplexer,
Capabilities, DisconnectReason, EthVersion, HelloMessageWithProtocols, Status,
UnauthedEthStream, UnauthedP2PStream,
};
use reth_metrics::common::mpsc::MeteredPollSender;
use reth_network_peers::PeerId;

View File

@ -13,9 +13,7 @@ use std::{
};
use rand::seq::SliceRandom;
use reth_eth_wire::{
capability::Capabilities, BlockHashNumber, DisconnectReason, NewBlockHashes, Status,
};
use reth_eth_wire::{BlockHashNumber, Capabilities, DisconnectReason, NewBlockHashes, Status};
use reth_network_peers::PeerId;
use reth_network_types::{PeerAddr, PeerKind};
use reth_primitives::{ForkId, B256};
@ -553,10 +551,7 @@ mod tests {
state::{BlockNumReader, NetworkState},
PeerRequest,
};
use reth_eth_wire::{
capability::{Capabilities, Capability},
BlockBodies, EthVersion,
};
use reth_eth_wire::{BlockBodies, Capabilities, Capability, EthVersion};
use reth_network_p2p::{bodies::client::BodiesClient, error::RequestError};
use reth_network_peers::PeerId;
use reth_primitives::{BlockBody, Header, B256};

View File

@ -8,9 +8,7 @@ use std::{
use futures::Stream;
use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
errors::EthStreamError,
EthVersion, Status,
capability::CapabilityMessage, errors::EthStreamError, Capabilities, EthVersion, Status,
};
use reth_network_peers::PeerId;
use tracing::trace;

View File

@ -26,7 +26,7 @@ use crate::multiplex::proto::{PingPongProtoMessage, PingPongProtoMessageKind};
/// A simple Rlpx subprotocol that sends pings and pongs
mod proto {
use super::*;
use reth_eth_wire::capability::Capability;
use reth_eth_wire::Capability;
use reth_primitives::{Buf, BufMut};
#[repr(u8)]