Skip to content

Commit

Permalink
refactor(plaintext): rename symbols to follow naming convention
Browse files Browse the repository at this point in the history
Related: #2217.

Pull-Request: #4535.
  • Loading branch information
thomaseizinger authored Sep 27, 2023
1 parent f6b5a13 commit fffd47b
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
16 changes: 7 additions & 9 deletions muxers/mplex/benches/split_send_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()
Expand Down
7 changes: 3 additions & 4 deletions protocols/dcutr/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _;
Expand Down Expand Up @@ -111,16 +111,15 @@ fn build_relay() -> Swarm<relay::Behaviour> {

fn build_client() -> Swarm<Client> {
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 = relay_transport
.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();

Expand Down
17 changes: 7 additions & 10 deletions protocols/relay/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -307,10 +306,9 @@ fn reuse_connection() {

fn build_relay() -> Swarm<Relay> {
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,
Expand All @@ -331,13 +329,12 @@ fn build_relay() -> Swarm<Relay> {

fn build_client() -> Swarm<Client> {
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(
Expand All @@ -353,14 +350,14 @@ fn build_client() -> Swarm<Client> {

fn upgrade_transport<StreamSink>(
transport: Boxed<StreamSink>,
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()
}
Expand Down
10 changes: 4 additions & 6 deletions swarm-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions transports/plaintext/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion transports/plaintext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
50 changes: 25 additions & 25 deletions transports/plaintext/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<IoError> for PlainTextError {
fn from(err: IoError) -> PlainTextError {
PlainTextError::IoError(err)
impl From<IoError> for Error {
fn from(err: IoError) -> Error {
Error::Io(err)
}
}

impl From<DecodeError> for PlainTextError {
fn from(err: DecodeError) -> PlainTextError {
PlainTextError::InvalidPayload(err)
impl From<DecodeError> for Error {
fn from(err: DecodeError) -> Error {
Error::InvalidPayload(err)
}
}

impl From<libp2p_identity::DecodingError> for PlainTextError {
fn from(err: libp2p_identity::DecodingError) -> PlainTextError {
PlainTextError::InvalidPublicKey(err)
impl From<libp2p_identity::DecodingError> for Error {
fn from(err: libp2p_identity::DecodingError) -> Error {
Error::InvalidPublicKey(err)
}
}

impl From<libp2p_identity::ParseError> for PlainTextError {
fn from(err: libp2p_identity::ParseError) -> PlainTextError {
PlainTextError::InvalidPeerId(err)
impl From<libp2p_identity::ParseError> for Error {
fn from(err: libp2p_identity::ParseError) -> Error {
Error::InvalidPeerId(err)
}
}
21 changes: 8 additions & 13 deletions transports/plaintext/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -32,7 +32,7 @@ use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use unsigned_varint::codec::UviBytes;

struct HandshakeContext<T> {
config: PlainText2Config,
config: Config,
state: T,
}

Expand All @@ -50,7 +50,8 @@ pub(crate) struct Remote {
}

impl HandshakeContext<Local> {
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()),
Expand All @@ -69,10 +70,7 @@ impl HandshakeContext<Local> {
}
}

fn with_remote(
self,
exchange_bytes: BytesMut,
) -> Result<HandshakeContext<Remote>, PlainTextError> {
fn with_remote(self, exchange_bytes: BytesMut) -> Result<HandshakeContext<Remote>, Error> {
let mut reader = BytesReader::from_bytes(&exchange_bytes);
let prop = Exchange::from_reader(&mut reader, &exchange_bytes).map_err(DecodeError)?;

Expand All @@ -81,7 +79,7 @@ impl HandshakeContext<Local> {

// 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 {
Expand All @@ -94,10 +92,7 @@ impl HandshakeContext<Local> {
}
}

pub(crate) async fn handshake<S>(
socket: S,
config: PlainText2Config,
) -> Result<(S, Remote, Bytes), PlainTextError>
pub(crate) async fn handshake<S>(socket: S, config: Config) -> Result<(S, Remote, Bytes), Error>
where
S: AsyncRead + AsyncWrite + Send + Unpin,
{
Expand Down
Loading

0 comments on commit fffd47b

Please sign in to comment.