From fc324586449ff4c6a303455f8e09a2096a81d42a Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 8 Nov 2023 20:10:07 +0100 Subject: [PATCH] chore: support static str in capability (#5362) --- crates/net/eth-wire/src/capability.rs | 41 +++++++++++++++++++------- crates/net/eth-wire/src/ethstream.rs | 4 +-- crates/net/eth-wire/src/hello.rs | 6 ++-- crates/net/network/tests/it/session.rs | 2 +- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/crates/net/eth-wire/src/capability.rs b/crates/net/eth-wire/src/capability.rs index f303e238f..1ba92f2b9 100644 --- a/crates/net/eth-wire/src/capability.rs +++ b/crates/net/eth-wire/src/capability.rs @@ -4,7 +4,7 @@ use crate::{version::ParseVersionError, EthMessage, EthVersion}; use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable}; use reth_codecs::add_arbitrary_tests; use reth_primitives::bytes::{BufMut, Bytes}; -use std::fmt; +use std::{borrow::Cow, fmt}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -42,7 +42,7 @@ pub enum CapabilityMessage { #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Capability { /// The name of the subprotocol - pub name: String, + pub name: Cow<'static, str>, /// The version of the subprotocol pub version: usize, } @@ -50,7 +50,27 @@ pub struct Capability { impl Capability { /// Create a new `Capability` with the given name and version. pub fn new(name: String, version: usize) -> Self { - Self { name, version } + 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 [EthVersion::Eth66] capability. + pub const fn eth_66() -> Self { + Self::new_static("eth", EthVersion::Eth66 as usize) + } + + /// Returns the [EthVersion::Eth67] capability. + pub const fn eth_67() -> Self { + Self::new_static("eth", EthVersion::Eth67 as usize) + } + + /// Returns the [EthVersion::Eth68] capability. + pub const fn eth_68() -> Self { + Self::new_static("eth", EthVersion::Eth68 as usize) } /// Whether this is eth v66 protocol. @@ -83,7 +103,7 @@ impl<'a> arbitrary::Arbitrary<'a> for Capability { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { let version = u.int_in_range(0..=32)?; // TODO: What's the max? let name = String::arbitrary(u)?; // TODO: what possible values? - Ok(Self { name, version }) + Ok(Self::new(name, version)) } } @@ -94,7 +114,7 @@ impl proptest::arbitrary::Arbitrary for Capability { any_with::(args) // TODO: what possible values? .prop_flat_map(move |name| { any_with::(()) // TODO: What's the max? - .prop_map(move |version| Capability { name: name.clone(), version }) + .prop_map(move |version| Capability::new(name.clone(), version)) }) .boxed() } @@ -186,9 +206,8 @@ impl Decodable for Capabilities { pub enum SharedCapability { /// The `eth` capability. Eth { version: EthVersion, offset: u8 }, - /// An unknown capability. - UnknownCapability { name: String, version: u8, offset: u8 }, + UnknownCapability { name: Cow<'static, str>, version: u8, offset: u8 }, } impl SharedCapability { @@ -196,7 +215,7 @@ impl SharedCapability { pub(crate) fn new(name: &str, version: u8, offset: u8) -> Result { match name { "eth" => Ok(Self::Eth { version: EthVersion::try_from(version)?, offset }), - _ => Ok(Self::UnknownCapability { name: name.into(), version, offset }), + _ => Ok(Self::UnknownCapability { name: name.to_string().into(), version, offset }), } } @@ -278,9 +297,9 @@ mod tests { #[test] fn capabilities_supports_eth() { let capabilities: Capabilities = vec![ - Capability::new("eth".into(), 66), - Capability::new("eth".into(), 67), - Capability::new("eth".into(), 68), + Capability::new_static("eth", 66), + Capability::new_static("eth", 67), + Capability::new_static("eth", 68), ] .into(); diff --git a/crates/net/eth-wire/src/ethstream.rs b/crates/net/eth-wire/src/ethstream.rs index 413ddb768..9f6e9f3c2 100644 --- a/crates/net/eth-wire/src/ethstream.rs +++ b/crates/net/eth-wire/src/ethstream.rs @@ -595,7 +595,7 @@ mod tests { let server_hello = HelloMessage { protocol_version: ProtocolVersion::V5, client_version: "bitcoind/1.0.0".to_string(), - capabilities: vec![Capability::new("eth".into(), EthVersion::Eth67 as usize)], + capabilities: vec![Capability::new_static("eth", EthVersion::Eth67 as usize)], port: DEFAULT_DISCOVERY_PORT, id: pk2id(&server_key.public_key(SECP256K1)), }; @@ -623,7 +623,7 @@ mod tests { let client_hello = HelloMessage { protocol_version: ProtocolVersion::V5, client_version: "bitcoind/1.0.0".to_string(), - capabilities: vec![Capability::new("eth".into(), EthVersion::Eth67 as usize)], + capabilities: vec![Capability::new_static("eth", EthVersion::Eth67 as usize)], port: DEFAULT_DISCOVERY_PORT, id: pk2id(&client_key.public_key(SECP256K1)), }; diff --git a/crates/net/eth-wire/src/hello.rs b/crates/net/eth-wire/src/hello.rs index 6d8636685..3430d6115 100644 --- a/crates/net/eth-wire/src/hello.rs +++ b/crates/net/eth-wire/src/hello.rs @@ -125,7 +125,7 @@ mod tests { let hello = P2PMessage::Hello(HelloMessage { protocol_version: ProtocolVersion::V5, client_version: "reth/0.1.0".to_string(), - capabilities: vec![Capability::new("eth".into(), EthVersion::Eth67 as usize)], + capabilities: vec![Capability::new_static("eth", EthVersion::Eth67 as usize)], port: DEFAULT_DISCOVERY_PORT, id, }); @@ -145,7 +145,7 @@ mod tests { let hello = P2PMessage::Hello(HelloMessage { protocol_version: ProtocolVersion::V5, client_version: "reth/0.1.0".to_string(), - capabilities: vec![Capability::new("eth".into(), EthVersion::Eth67 as usize)], + capabilities: vec![Capability::new_static("eth", EthVersion::Eth67 as usize)], port: DEFAULT_DISCOVERY_PORT, id, }); @@ -164,7 +164,7 @@ mod tests { let hello = P2PMessage::Hello(HelloMessage { protocol_version: ProtocolVersion::V5, client_version: "reth/0.1.0".to_string(), - capabilities: vec![Capability::new("eth".into(), EthVersion::Eth67 as usize)], + capabilities: vec![Capability::new_static("eth", EthVersion::Eth67 as usize)], port: DEFAULT_DISCOVERY_PORT, id, }); diff --git a/crates/net/network/tests/it/session.rs b/crates/net/network/tests/it/session.rs index c00036b08..b9af22da9 100644 --- a/crates/net/network/tests/it/session.rs +++ b/crates/net/network/tests/it/session.rs @@ -49,7 +49,7 @@ async fn test_session_established_with_different_capability() { let mut net = Testnet::create(1).await; - let capabilities = vec![Capability::new("eth".into(), EthVersion::Eth66 as usize)]; + let capabilities = vec![Capability::new_static("eth", EthVersion::Eth66 as usize)]; let p1 = PeerConfig::with_capabilities(NoopProvider::default(), capabilities); net.add_peer_with_config(p1).await.unwrap();