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

@@ -19,14 +19,17 @@
// DEALINGS IN THE SOFTWARE.
use crate::proto;
use libp2p_swarm::StreamProtocol;
use std::time::Duration;
pub(crate) mod inbound_hop;
pub(crate) mod inbound_stop;
pub(crate) mod outbound_hop;
pub(crate) mod outbound_stop;
pub const HOP_PROTOCOL_NAME: &[u8; 31] = b"/libp2p/circuit/relay/0.2.0/hop";
pub const STOP_PROTOCOL_NAME: &[u8; 32] = b"/libp2p/circuit/relay/0.2.0/stop";
pub const HOP_PROTOCOL_NAME: StreamProtocol =
StreamProtocol::new("/libp2p/circuit/relay/0.2.0/hop");
pub const STOP_PROTOCOL_NAME: StreamProtocol =
StreamProtocol::new("/libp2p/circuit/relay/0.2.0/stop");
const MAX_MESSAGE_SIZE: usize = 4096;

View File

@@ -26,7 +26,7 @@ use futures::{future::BoxFuture, prelude::*};
use instant::{Duration, SystemTime};
use libp2p_core::{upgrade, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::NegotiatedSubstream;
use libp2p_swarm::{NegotiatedSubstream, StreamProtocol};
use std::convert::TryInto;
use std::iter;
use thiserror::Error;
@@ -38,7 +38,7 @@ pub struct Upgrade {
}
impl upgrade::UpgradeInfo for Upgrade {
type Info = &'static [u8];
type Info = StreamProtocol;
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {

View File

@@ -25,14 +25,14 @@ use bytes::Bytes;
use futures::{future::BoxFuture, prelude::*};
use libp2p_core::upgrade;
use libp2p_identity::PeerId;
use libp2p_swarm::NegotiatedSubstream;
use libp2p_swarm::{NegotiatedSubstream, StreamProtocol};
use std::iter;
use thiserror::Error;
pub struct Upgrade {}
impl upgrade::UpgradeInfo for Upgrade {
type Info = &'static [u8];
type Info = StreamProtocol;
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {

View File

@@ -27,7 +27,7 @@ use futures_timer::Delay;
use instant::{Duration, SystemTime};
use libp2p_core::{upgrade, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::NegotiatedSubstream;
use libp2p_swarm::{NegotiatedSubstream, StreamProtocol};
use std::convert::TryFrom;
use std::iter;
use thiserror::Error;
@@ -38,7 +38,7 @@ pub enum Upgrade {
}
impl upgrade::UpgradeInfo for Upgrade {
type Info = &'static [u8];
type Info = StreamProtocol;
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {

View File

@@ -25,7 +25,7 @@ use bytes::Bytes;
use futures::{future::BoxFuture, prelude::*};
use libp2p_core::upgrade;
use libp2p_identity::PeerId;
use libp2p_swarm::NegotiatedSubstream;
use libp2p_swarm::{NegotiatedSubstream, StreamProtocol};
use std::convert::TryInto;
use std::iter;
use std::time::Duration;
@@ -38,7 +38,7 @@ pub struct Upgrade {
}
impl upgrade::UpgradeInfo for Upgrade {
type Info = &'static [u8];
type Info = StreamProtocol;
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {

View File

@@ -154,18 +154,6 @@ pub mod protocol {
note = "Use libp2p_relay::outbound::stop::FatalUpgradeError instead."
)]
pub type OutboundStopFatalUpgradeError = crate::outbound::stop::FatalUpgradeError;
#[deprecated(
since = "0.15.0",
note = "Use libp2p_relay::HOP_PROTOCOL_NAME instead."
)]
pub const HOP_PROTOCOL_NAME: &[u8; 31] = crate::HOP_PROTOCOL_NAME;
#[deprecated(
since = "0.15.0",
note = "Use libp2p_relay::STOP_PROTOCOL_NAME instead."
)]
pub const STOP_PROTOCOL_NAME: &[u8; 32] = crate::STOP_PROTOCOL_NAME;
}
#[deprecated(