From fffd47b69f7a974d85fbc7cad9b75e8ba2453668 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 28 Sep 2023 05:42:45 +1000 Subject: [PATCH] refactor(plaintext): rename symbols to follow naming convention Related: #2217. Pull-Request: #4535. --- Cargo.lock | 2 +- Cargo.toml | 2 +- muxers/mplex/benches/split_send_size.rs | 16 ++++---- protocols/dcutr/tests/lib.rs | 7 ++-- protocols/relay/tests/lib.rs | 17 ++++----- swarm-test/src/lib.rs | 10 ++--- swarm/src/lib.rs | 4 +- transports/plaintext/CHANGELOG.md | 5 +++ transports/plaintext/Cargo.toml | 2 +- transports/plaintext/src/error.rs | 50 ++++++++++++------------- transports/plaintext/src/handshake.rs | 21 ++++------- transports/plaintext/src/lib.rs | 49 +++++++++++++++--------- transports/plaintext/tests/smoke.rs | 15 ++------ 13 files changed, 98 insertions(+), 102 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9610316c746..c90cff73674 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2868,7 +2868,7 @@ dependencies = [ [[package]] name = "libp2p-plaintext" -version = "0.40.0" +version = "0.40.1" dependencies = [ "asynchronous-codec", "bytes", diff --git a/Cargo.toml b/Cargo.toml index 75354b74596..9e662abb630 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -92,7 +92,7 @@ libp2p-muxer-test-harness = { path = "muxers/test-harness" } libp2p-noise = { version = "0.43.1", path = "transports/noise" } libp2p-perf = { version = "0.2.0", path = "protocols/perf" } libp2p-ping = { version = "0.43.1", path = "protocols/ping" } -libp2p-plaintext = { version = "0.40.0", path = "transports/plaintext" } +libp2p-plaintext = { version = "0.40.1", path = "transports/plaintext" } libp2p-pnet = { version = "0.23.0", path = "transports/pnet" } libp2p-quic = { version = "0.9.2", path = "transports/quic" } libp2p-relay = { version = "0.16.1", path = "protocols/relay" } diff --git a/muxers/mplex/benches/split_send_size.rs b/muxers/mplex/benches/split_send_size.rs index dfdc619afb1..86f84ceab2c 100644 --- a/muxers/mplex/benches/split_send_size.rs +++ b/muxers/mplex/benches/split_send_size.rs @@ -32,7 +32,7 @@ use libp2p_core::{multiaddr::multiaddr, muxing, transport, upgrade, Multiaddr, T use libp2p_identity as identity; use libp2p_identity::PeerId; use libp2p_mplex as mplex; -use libp2p_plaintext::PlainText2Config; +use libp2p_plaintext as plaintext; use std::pin::Pin; use std::time::Duration; @@ -166,30 +166,28 @@ fn run( } fn tcp_transport(split_send_size: usize) -> BenchTransport { - let key = identity::Keypair::generate_ed25519(); - let local_public_key = key.public(); - let mut mplex = mplex::MplexConfig::default(); mplex.set_split_send_size(split_send_size); libp2p_tcp::async_io::Transport::new(libp2p_tcp::Config::default().nodelay(true)) .upgrade(upgrade::Version::V1) - .authenticate(PlainText2Config { local_public_key }) + .authenticate(plaintext::Config::new( + &identity::Keypair::generate_ed25519(), + )) .multiplex(mplex) .timeout(Duration::from_secs(5)) .boxed() } fn mem_transport(split_send_size: usize) -> BenchTransport { - let key = identity::Keypair::generate_ed25519(); - let local_public_key = key.public(); - let mut mplex = mplex::MplexConfig::default(); mplex.set_split_send_size(split_send_size); transport::MemoryTransport::default() .upgrade(upgrade::Version::V1) - .authenticate(PlainText2Config { local_public_key }) + .authenticate(plaintext::Config::new( + &identity::Keypair::generate_ed25519(), + )) .multiplex(mplex) .timeout(Duration::from_secs(5)) .boxed() diff --git a/protocols/dcutr/tests/lib.rs b/protocols/dcutr/tests/lib.rs index 6888e5914a0..a47f413f268 100644 --- a/protocols/dcutr/tests/lib.rs +++ b/protocols/dcutr/tests/lib.rs @@ -24,7 +24,7 @@ use libp2p_core::transport::{MemoryTransport, Transport}; use libp2p_dcutr as dcutr; use libp2p_identity as identity; use libp2p_identity::PeerId; -use libp2p_plaintext::PlainText2Config; +use libp2p_plaintext as plaintext; use libp2p_relay as relay; use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent}; use libp2p_swarm_test::SwarmExt as _; @@ -111,8 +111,7 @@ fn build_relay() -> Swarm { fn build_client() -> Swarm { let local_key = identity::Keypair::generate_ed25519(); - let local_public_key = local_key.public(); - let local_peer_id = local_public_key.to_peer_id(); + let local_peer_id = local_key.public().to_peer_id(); let (relay_transport, behaviour) = relay::client::new(local_peer_id); @@ -120,7 +119,7 @@ fn build_client() -> Swarm { .or_transport(MemoryTransport::default()) .or_transport(libp2p_tcp::async_io::Transport::default()) .upgrade(Version::V1) - .authenticate(PlainText2Config { local_public_key }) + .authenticate(plaintext::Config::new(&local_key)) .multiplex(libp2p_yamux::Config::default()) .boxed(); diff --git a/protocols/relay/tests/lib.rs b/protocols/relay/tests/lib.rs index fa79ab67f4b..b7784d17b3a 100644 --- a/protocols/relay/tests/lib.rs +++ b/protocols/relay/tests/lib.rs @@ -30,9 +30,8 @@ use libp2p_core::transport::{Boxed, MemoryTransport, Transport}; use libp2p_core::upgrade; use libp2p_identity as identity; use libp2p_identity::PeerId; -use libp2p_identity::PublicKey; use libp2p_ping as ping; -use libp2p_plaintext::PlainText2Config; +use libp2p_plaintext as plaintext; use libp2p_relay as relay; use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent}; use std::time::Duration; @@ -307,10 +306,9 @@ fn reuse_connection() { fn build_relay() -> Swarm { let local_key = identity::Keypair::generate_ed25519(); - let local_public_key = local_key.public(); - let local_peer_id = local_public_key.to_peer_id(); + let local_peer_id = local_key.public().to_peer_id(); - let transport = upgrade_transport(MemoryTransport::default().boxed(), local_public_key); + let transport = upgrade_transport(MemoryTransport::default().boxed(), &local_key); SwarmBuilder::with_async_std_executor( transport, @@ -331,13 +329,12 @@ fn build_relay() -> Swarm { fn build_client() -> Swarm { let local_key = identity::Keypair::generate_ed25519(); - let local_public_key = local_key.public(); - let local_peer_id = local_public_key.to_peer_id(); + let local_peer_id = local_key.public().to_peer_id(); let (relay_transport, behaviour) = relay::client::new(local_peer_id); let transport = upgrade_transport( OrTransport::new(relay_transport, MemoryTransport::default()).boxed(), - local_public_key, + &local_key, ); SwarmBuilder::with_async_std_executor( @@ -353,14 +350,14 @@ fn build_client() -> Swarm { fn upgrade_transport( transport: Boxed, - local_public_key: PublicKey, + identity: &identity::Keypair, ) -> Boxed<(PeerId, StreamMuxerBox)> where StreamSink: AsyncRead + AsyncWrite + Send + Unpin + 'static, { transport .upgrade(upgrade::Version::V1) - .authenticate(PlainText2Config { local_public_key }) + .authenticate(plaintext::Config::new(identity)) .multiplex(libp2p_yamux::Config::default()) .boxed() } diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index 819db33ba88..5cc85728b3a 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -25,7 +25,7 @@ use libp2p_core::{ multiaddr::Protocol, transport::MemoryTransport, upgrade::Version, Multiaddr, Transport, }; use libp2p_identity::{Keypair, PeerId}; -use libp2p_plaintext::PlainText2Config; +use libp2p_plaintext as plaintext; use libp2p_swarm::dial_opts::PeerCondition; use libp2p_swarm::{ dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent, THandlerErr, @@ -41,8 +41,8 @@ pub trait SwarmExt { /// Create a new [`Swarm`] with an ephemeral identity. /// - /// The swarm will use a [`MemoryTransport`] together with [`PlainText2Config`] authentication layer and - /// yamux as the multiplexer. However, these details should not be relied upon by the test + /// The swarm will use a [`MemoryTransport`] together with a [`plaintext::Config`] authentication layer and + /// [`yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test /// and may change at any time. fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self where @@ -211,9 +211,7 @@ where let transport = MemoryTransport::default() .or_transport(libp2p_tcp::async_io::Transport::default()) .upgrade(Version::V1) - .authenticate(PlainText2Config { - local_public_key: identity.public(), - }) + .authenticate(plaintext::Config::new(&identity)) .multiplex(yamux::Config::default()) .timeout(Duration::from_secs(20)) .boxed(); diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index e9f08d4c14c..44dc3c8da66 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1848,9 +1848,7 @@ mod tests { let local_public_key = id_keys.public(); let transport = transport::MemoryTransport::default() .upgrade(upgrade::Version::V1) - .authenticate(plaintext::PlainText2Config { - local_public_key: local_public_key.clone(), - }) + .authenticate(plaintext::Config::new(&id_keys)) .multiplex(yamux::Config::default()) .boxed(); let behaviour = CallTraceBehaviour::new(MockBehaviour::new(dummy::ConnectionHandler)); diff --git a/transports/plaintext/CHANGELOG.md b/transports/plaintext/CHANGELOG.md index 63fd538f6ba..dbbc04b5003 100644 --- a/transports/plaintext/CHANGELOG.md +++ b/transports/plaintext/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.40.1 - unreleased + +- Rename `Plaintext2Config` to `Config` to follow naming conventions across repository. + See [PR 4535](https://github.com/libp2p/rust-libp2p/pull/4535). + ## 0.40.0 - Raise MSRV to 1.65. diff --git a/transports/plaintext/Cargo.toml b/transports/plaintext/Cargo.toml index 1f15004d3f4..573114672da 100644 --- a/transports/plaintext/Cargo.toml +++ b/transports/plaintext/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-plaintext" edition = "2021" rust-version = { workspace = true } description = "Plaintext encryption dummy protocol for libp2p" -version = "0.40.0" +version = "0.40.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/transports/plaintext/src/error.rs b/transports/plaintext/src/error.rs index 23f196f147f..a1e4d8660df 100644 --- a/transports/plaintext/src/error.rs +++ b/transports/plaintext/src/error.rs @@ -23,9 +23,9 @@ use std::fmt; use std::io::Error as IoError; #[derive(Debug)] -pub enum PlainTextError { +pub enum Error { /// I/O error. - IoError(IoError), + Io(IoError), /// Failed to parse the handshake protobuf message. InvalidPayload(DecodeError), @@ -55,52 +55,52 @@ impl error::Error for DecodeError { } } -impl error::Error for PlainTextError { +impl error::Error for Error { fn cause(&self) -> Option<&dyn error::Error> { match *self { - PlainTextError::IoError(ref err) => Some(err), - PlainTextError::InvalidPayload(ref err) => Some(err), - PlainTextError::InvalidPublicKey(ref err) => Some(err), - PlainTextError::InvalidPeerId(ref err) => Some(err), + Error::Io(ref err) => Some(err), + Error::InvalidPayload(ref err) => Some(err), + Error::InvalidPublicKey(ref err) => Some(err), + Error::InvalidPeerId(ref err) => Some(err), _ => None, } } } -impl fmt::Display for PlainTextError { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match self { - PlainTextError::IoError(e) => write!(f, "I/O error: {e}"), - PlainTextError::InvalidPayload(_) => f.write_str("Failed to decode protobuf"), - PlainTextError::PeerIdMismatch => f.write_str( + Error::Io(e) => write!(f, "I/O error: {e}"), + Error::InvalidPayload(_) => f.write_str("Failed to decode protobuf"), + Error::PeerIdMismatch => f.write_str( "The peer id of the exchange isn't consistent with the remote public key", ), - PlainTextError::InvalidPublicKey(_) => f.write_str("Failed to decode public key"), - PlainTextError::InvalidPeerId(_) => f.write_str("Failed to decode PeerId"), + Error::InvalidPublicKey(_) => f.write_str("Failed to decode public key"), + Error::InvalidPeerId(_) => f.write_str("Failed to decode PeerId"), } } } -impl From for PlainTextError { - fn from(err: IoError) -> PlainTextError { - PlainTextError::IoError(err) +impl From for Error { + fn from(err: IoError) -> Error { + Error::Io(err) } } -impl From for PlainTextError { - fn from(err: DecodeError) -> PlainTextError { - PlainTextError::InvalidPayload(err) +impl From for Error { + fn from(err: DecodeError) -> Error { + Error::InvalidPayload(err) } } -impl From for PlainTextError { - fn from(err: libp2p_identity::DecodingError) -> PlainTextError { - PlainTextError::InvalidPublicKey(err) +impl From for Error { + fn from(err: libp2p_identity::DecodingError) -> Error { + Error::InvalidPublicKey(err) } } -impl From for PlainTextError { - fn from(err: libp2p_identity::ParseError) -> PlainTextError { - PlainTextError::InvalidPeerId(err) +impl From for Error { + fn from(err: libp2p_identity::ParseError) -> Error { + Error::InvalidPeerId(err) } } diff --git a/transports/plaintext/src/handshake.rs b/transports/plaintext/src/handshake.rs index 46dd6119d92..05e3b9085a0 100644 --- a/transports/plaintext/src/handshake.rs +++ b/transports/plaintext/src/handshake.rs @@ -18,9 +18,9 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use crate::error::{DecodeError, PlainTextError}; +use crate::error::{DecodeError, Error}; use crate::proto::Exchange; -use crate::PlainText2Config; +use crate::Config; use asynchronous_codec::{Framed, FramedParts}; use bytes::{Bytes, BytesMut}; @@ -32,7 +32,7 @@ use std::io::{Error as IoError, ErrorKind as IoErrorKind}; use unsigned_varint::codec::UviBytes; struct HandshakeContext { - config: PlainText2Config, + config: Config, state: T, } @@ -50,7 +50,8 @@ pub(crate) struct Remote { } impl HandshakeContext { - fn new(config: PlainText2Config) -> Self { + fn new(config: Config) -> Self { + #[allow(deprecated)] let exchange = Exchange { id: Some(config.local_public_key.to_peer_id().to_bytes()), pubkey: Some(config.local_public_key.encode_protobuf()), @@ -69,10 +70,7 @@ impl HandshakeContext { } } - fn with_remote( - self, - exchange_bytes: BytesMut, - ) -> Result, PlainTextError> { + fn with_remote(self, exchange_bytes: BytesMut) -> Result, Error> { let mut reader = BytesReader::from_bytes(&exchange_bytes); let prop = Exchange::from_reader(&mut reader, &exchange_bytes).map_err(DecodeError)?; @@ -81,7 +79,7 @@ impl HandshakeContext { // Check the validity of the remote's `Exchange`. if peer_id != public_key.to_peer_id() { - return Err(PlainTextError::PeerIdMismatch); + return Err(Error::PeerIdMismatch); } Ok(HandshakeContext { @@ -94,10 +92,7 @@ impl HandshakeContext { } } -pub(crate) async fn handshake( - socket: S, - config: PlainText2Config, -) -> Result<(S, Remote, Bytes), PlainTextError> +pub(crate) async fn handshake(socket: S, config: Config) -> Result<(S, Remote, Bytes), Error> where S: AsyncRead + AsyncWrite + Send + Unpin, { diff --git a/transports/plaintext/src/lib.rs b/transports/plaintext/src/lib.rs index 76e70a025b7..fa7cba6b8ff 100644 --- a/transports/plaintext/src/lib.rs +++ b/transports/plaintext/src/lib.rs @@ -22,7 +22,7 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] -use crate::error::PlainTextError; +use crate::error::Error; use bytes::Bytes; use futures::future::BoxFuture; @@ -46,14 +46,29 @@ mod proto { pub(crate) use self::structs::Exchange; } -/// `PlainText2Config` is an insecure connection handshake for testing purposes only, implementing -/// the libp2p plaintext connection handshake specification. +#[deprecated(note = "Has been renamed to `Config`.")] +pub type PlainText2Config = Config; + +#[deprecated(note = "Has been renamed to `Output`.")] +pub type PlainTextOutput = Output; + +/// [`Config`] is an insecure connection handshake for testing purposes only. #[derive(Clone)] -pub struct PlainText2Config { +pub struct Config { + #[deprecated(note = "Will be made private in the future, please use `Config::new` instead!")] pub local_public_key: identity::PublicKey, } -impl UpgradeInfo for PlainText2Config { +impl Config { + #[allow(deprecated)] + pub fn new(identity: &identity::Keypair) -> Self { + Self { + local_public_key: identity.public(), + } + } +} + +impl UpgradeInfo for Config { type Info = &'static str; type InfoIter = iter::Once; @@ -62,12 +77,12 @@ impl UpgradeInfo for PlainText2Config { } } -impl InboundUpgrade for PlainText2Config +impl InboundUpgrade for Config where C: AsyncRead + AsyncWrite + Send + Unpin + 'static, { - type Output = (PeerId, PlainTextOutput); - type Error = PlainTextError; + type Output = (PeerId, Output); + type Error = Error; type Future = BoxFuture<'static, Result>; fn upgrade_inbound(self, socket: C, _: Self::Info) -> Self::Future { @@ -75,12 +90,12 @@ where } } -impl OutboundUpgrade for PlainText2Config +impl OutboundUpgrade for Config where C: AsyncRead + AsyncWrite + Send + Unpin + 'static, { - type Output = (PeerId, PlainTextOutput); - type Error = PlainTextError; + type Output = (PeerId, Output); + type Error = Error; type Future = BoxFuture<'static, Result>; fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future { @@ -88,8 +103,8 @@ where } } -impl PlainText2Config { - async fn handshake(self, socket: T) -> Result<(PeerId, PlainTextOutput), PlainTextError> +impl Config { + async fn handshake(self, socket: T) -> Result<(PeerId, Output), Error> where T: AsyncRead + AsyncWrite + Send + Unpin + 'static, { @@ -99,7 +114,7 @@ impl PlainText2Config { Ok(( remote.peer_id, - PlainTextOutput { + Output { socket, remote_key: remote.public_key, read_buffer, @@ -109,7 +124,7 @@ impl PlainText2Config { } /// Output of the plaintext protocol. -pub struct PlainTextOutput +pub struct Output where S: AsyncRead + AsyncWrite + Unpin, { @@ -123,7 +138,7 @@ where read_buffer: Bytes, } -impl AsyncRead for PlainTextOutput { +impl AsyncRead for Output { fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, @@ -139,7 +154,7 @@ impl AsyncRead for PlainTextOutput { } } -impl AsyncWrite for PlainTextOutput { +impl AsyncWrite for Output { fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, diff --git a/transports/plaintext/tests/smoke.rs b/transports/plaintext/tests/smoke.rs index 7147ed56686..ed18fb44cba 100644 --- a/transports/plaintext/tests/smoke.rs +++ b/transports/plaintext/tests/smoke.rs @@ -21,7 +21,7 @@ use futures::io::{AsyncReadExt, AsyncWriteExt}; use libp2p_core::InboundUpgrade; use libp2p_identity as identity; -use libp2p_plaintext::PlainText2Config; +use libp2p_plaintext as plaintext; use log::debug; use quickcheck::QuickCheck; @@ -34,10 +34,7 @@ fn variable_msg_length() { let msg_to_receive = msg; let server_id = identity::Keypair::generate_ed25519(); - let server_id_public = server_id.public(); - let client_id = identity::Keypair::generate_ed25519(); - let client_id_public = client_id.public(); let (server, client) = futures_ringbuf::Endpoint::pair(100, 100); @@ -46,14 +43,8 @@ fn variable_msg_length() { (received_client_id, mut server_channel), (received_server_id, mut client_channel), ) = futures::future::try_join( - PlainText2Config { - local_public_key: server_id_public, - } - .upgrade_inbound(server, ""), - PlainText2Config { - local_public_key: client_id_public, - } - .upgrade_inbound(client, ""), + plaintext::Config::new(&server_id).upgrade_inbound(server, ""), + plaintext::Config::new(&client_id).upgrade_inbound(client, ""), ) .await .unwrap();