feat: replace ProtocolName with AsRef<str>

Previously, a protocol could be any sequence of bytes as long as it started with `/`. Now, we directly parse a protocol as `String` which enforces it to be valid UTF8.

To notify users of this change, we delete the `ProtocolName` trait. The new requirement is that users need to provide a type that implements `AsRef<str>`.

We also add a `StreamProtocol` newtype in `libp2p-swarm` which provides an easy way for users to ensure their protocol strings are compliant. The newtype enforces that protocol strings start with `/`. `StreamProtocol` also implements `AsRef<str>`, meaning users can directly use it in their upgrades.

`multistream-select` by itself only changes marginally with this patch. The only thing we enforce in the type-system is that protocols must implement `AsRef<str>`.

Resolves: #2831.

Pull-Request: #3746.
This commit is contained in:
Thomas Eizinger
2023-05-04 05:47:11 +01:00
committed by GitHub
parent 30d0f598ef
commit c93f753018
68 changed files with 539 additions and 561 deletions

View File

@ -26,7 +26,8 @@ use libp2p_identity::PublicKey;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::{
AddressScore, ConnectionDenied, ConnectionHandlerUpgrErr, DialError, ExternalAddresses,
ListenAddresses, NetworkBehaviour, NotifyHandler, PollParameters, THandlerInEvent, ToSwarm,
ListenAddresses, NetworkBehaviour, NotifyHandler, PollParameters, StreamProtocol,
THandlerInEvent, ToSwarm,
};
use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent};
use lru::LruCache;
@ -498,12 +499,14 @@ pub enum Event {
},
}
fn supported_protocols(params: &impl PollParameters) -> Vec<String> {
fn supported_protocols(params: &impl PollParameters) -> Vec<StreamProtocol> {
// The protocol names can be bytes, but the identify protocol except UTF-8 strings.
// There's not much we can do to solve this conflict except strip non-UTF-8 characters.
params
.supported_protocols()
.map(|p| String::from_utf8_lossy(&p).to_string())
.filter_map(|p| {
StreamProtocol::try_from_owned(String::from_utf8_lossy(&p).to_string()).ok()
})
.collect()
}
@ -569,10 +572,7 @@ mod tests {
use libp2p_tcp as tcp;
use std::time::Duration;
fn transport() -> (
identity::PublicKey,
transport::Boxed<(PeerId, StreamMuxerBox)>,
) {
fn transport() -> (PublicKey, transport::Boxed<(PeerId, StreamMuxerBox)>) {
let id_keys = identity::Keypair::generate_ed25519();
let pubkey = id_keys.public();
let transport = tcp::async_io::Transport::new(tcp::Config::default().nodelay(true))

View File

@ -35,7 +35,7 @@ use libp2p_swarm::handler::{
};
use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
NegotiatedSubstream, SubstreamProtocol,
NegotiatedSubstream, StreamProtocol, SubstreamProtocol,
};
use log::warn;
use smallvec::SmallVec;
@ -92,7 +92,7 @@ pub struct InEvent {
pub listen_addrs: Vec<Multiaddr>,
/// The list of protocols supported by the peer, e.g. `/ipfs/ping/1.0.0`.
pub supported_protocols: Vec<String>,
pub supported_protocols: Vec<StreamProtocol>,
/// The protocol w.r.t. the information requested.
pub protocol: Protocol,

View File

@ -28,7 +28,7 @@ use libp2p_core::{
};
use libp2p_identity as identity;
use libp2p_identity::PublicKey;
use libp2p_swarm::ConnectionId;
use libp2p_swarm::{ConnectionId, StreamProtocol};
use log::{debug, trace};
use std::convert::TryFrom;
use std::{io, iter, pin::Pin};
@ -37,9 +37,9 @@ use void::Void;
const MAX_MESSAGE_SIZE_BYTES: usize = 4096;
pub const PROTOCOL_NAME: &[u8; 14] = b"/ipfs/id/1.0.0";
pub const PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/ipfs/id/1.0.0");
pub const PUSH_PROTOCOL_NAME: &[u8; 19] = b"/ipfs/id/push/1.0.0";
pub const PUSH_PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/ipfs/id/push/1.0.0");
/// The type of the Substream protocol.
#[derive(Debug, PartialEq, Eq)]
@ -84,13 +84,13 @@ pub struct Info {
/// The addresses that the peer is listening on.
pub listen_addrs: Vec<Multiaddr>,
/// The list of protocols supported by the peer, e.g. `/ipfs/ping/1.0.0`.
pub protocols: Vec<String>,
pub protocols: Vec<StreamProtocol>,
/// Address observed by or for the remote.
pub observed_addr: Multiaddr,
}
impl UpgradeInfo for Identify {
type Info = &'static [u8];
type Info = StreamProtocol;
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {
@ -122,7 +122,7 @@ where
}
impl<T> UpgradeInfo for Push<T> {
type Info = &'static [u8];
type Info = StreamProtocol;
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {
@ -177,7 +177,7 @@ where
publicKey: Some(pubkey_bytes),
listenAddrs: listen_addrs,
observedAddr: Some(info.observed_addr.to_vec()),
protocols: info.protocols,
protocols: info.protocols.into_iter().map(|p| p.to_string()).collect(),
};
let mut framed_io = FramedWrite::new(
@ -249,7 +249,17 @@ impl TryFrom<proto::Identify> for Info {
protocol_version: msg.protocolVersion.unwrap_or_default(),
agent_version: msg.agentVersion.unwrap_or_default(),
listen_addrs,
protocols: msg.protocols,
protocols: msg
.protocols
.into_iter()
.filter_map(|p| match StreamProtocol::try_from_owned(p) {
Ok(p) => Some(p),
Err(e) => {
debug!("Received invalid protocol from peer: {e}");
None
}
})
.collect(),
observed_addr,
};
@ -328,7 +338,10 @@ mod tests {
"/ip4/80.81.82.83/tcp/500".parse().unwrap(),
"/ip6/::1/udp/1000".parse().unwrap(),
],
protocols: vec!["proto1".to_string(), "proto2".to_string()],
protocols: vec![
StreamProtocol::new("/proto1"),
StreamProtocol::new("/proto2"),
],
observed_addr: "/ip4/100.101.102.103/tcp/5000".parse().unwrap(),
},
)
@ -357,10 +370,7 @@ mod tests {
"/ip6/::1/udp/1000".parse().unwrap()
]
);
assert_eq!(
info.protocols,
&["proto1".to_string(), "proto2".to_string()]
);
assert_eq!(info.protocols, &["/proto1", "/proto2"]);
bg_task.await;
});