Skip to content

Commit

Permalink
feat: replace ProtocolName with AsRef<str>
Browse files Browse the repository at this point in the history
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: libp2p#2831.

Pull-Request: libp2p#3746.
  • Loading branch information
thomaseizinger committed May 4, 2023
1 parent 2242b8b commit dafaee5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
mod as_client;
mod as_server;

use crate::protocol::{AutoNatCodec, AutoNatProtocol, DialRequest, DialResponse, ResponseError};
use crate::protocol::{AutoNatCodec, DialRequest, DialResponse, ResponseError};
use crate::DEFAULT_PROTOCOL_NAME;
use as_client::AsClient;
pub use as_client::{OutboundProbeError, OutboundProbeEvent};
use as_server::AsServer;
Expand Down Expand Up @@ -218,7 +219,7 @@ pub struct Behaviour {

impl Behaviour {
pub fn new(local_peer_id: PeerId, config: Config) -> Self {
let protocols = iter::once((AutoNatProtocol, ProtocolSupport::Full));
let protocols = iter::once((DEFAULT_PROTOCOL_NAME, ProtocolSupport::Full));
let mut cfg = request_response::Config::default();
cfg.set_request_timeout(config.timeout);
let inner = request_response::Behaviour::new(AutoNatCodec, protocols, cfg);
Expand Down
28 changes: 8 additions & 20 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,24 @@ use async_trait::async_trait;
use futures::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use libp2p_core::{upgrade, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_request_response::{self as request_response, ProtocolName};
use libp2p_request_response::{self as request_response};
use libp2p_swarm::StreamProtocol;
use quick_protobuf::{BytesReader, Writer};
use std::{convert::TryFrom, io};

#[derive(Clone, Debug)]
pub struct AutoNatProtocol;

/// The protocol name used for negotiating with multistream-select.
pub const DEFAULT_PROTOCOL_NAME: &[u8] = b"/libp2p/autonat/1.0.0";

impl ProtocolName for AutoNatProtocol {
fn protocol_name(&self) -> &[u8] {
DEFAULT_PROTOCOL_NAME
}
}
pub const DEFAULT_PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/libp2p/autonat/1.0.0");

#[derive(Clone)]
pub struct AutoNatCodec;

#[async_trait]
impl request_response::Codec for AutoNatCodec {
type Protocol = AutoNatProtocol;
type Protocol = StreamProtocol;
type Request = DialRequest;
type Response = DialResponse;

async fn read_request<T>(
&mut self,
_: &AutoNatProtocol,
io: &mut T,
) -> io::Result<Self::Request>
async fn read_request<T>(&mut self, _: &StreamProtocol, io: &mut T) -> io::Result<Self::Request>
where
T: AsyncRead + Send + Unpin,
{
Expand All @@ -63,7 +51,7 @@ impl request_response::Codec for AutoNatCodec {

async fn read_response<T>(
&mut self,
_: &AutoNatProtocol,
_: &StreamProtocol,
io: &mut T,
) -> io::Result<Self::Response>
where
Expand All @@ -76,7 +64,7 @@ impl request_response::Codec for AutoNatCodec {

async fn write_request<T>(
&mut self,
_: &AutoNatProtocol,
_: &StreamProtocol,
io: &mut T,
data: Self::Request,
) -> io::Result<()>
Expand All @@ -89,7 +77,7 @@ impl request_response::Codec for AutoNatCodec {

async fn write_response<T>(
&mut self,
_: &AutoNatProtocol,
_: &StreamProtocol,
io: &mut T,
data: Self::Response,
) -> io::Result<()>
Expand Down

0 comments on commit dafaee5

Please sign in to comment.