From 6924e5ef7a61688192ec1925fcb00d0c9da3ddba Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 6 Sep 2021 16:04:30 +1000 Subject: [PATCH] core/: Remove deprecated read/write functions (#2213) Co-authored-by: Max Inden --- core/CHANGELOG.md | 4 ++ core/src/upgrade.rs | 2 - core/src/upgrade/transfer.rs | 110 +---------------------------------- 3 files changed, 5 insertions(+), 111 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 3b1ec3c4..f5227e7c 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -30,7 +30,11 @@ - Report abortion of pending connection through `DialError`, `UnknownPeerDialError` or `IncomingConnectionError` (see [PR 2191]). +- Remove deprecated functions `upgrade::write_one`, `upgrade::write_with_len_prefix` + and `upgrade::read_one` (see [PR 2213]). + [PR 2145]: https://github.com/libp2p/rust-libp2p/pull/2145 +[PR 2213]: https://github.com/libp2p/rust-libp2p/pull/2213 [PR 2142]: https://github.com/libp2p/rust-libp2p/pull/2142 [PR 2137]: https://github.com/libp2p/rust-libp2p/pull/2137 [PR 2183]: https://github.com/libp2p/rust-libp2p/pull/2183 diff --git a/core/src/upgrade.rs b/core/src/upgrade.rs index fbee321a..d9edd649 100644 --- a/core/src/upgrade.rs +++ b/core/src/upgrade.rs @@ -69,8 +69,6 @@ mod transfer; use futures::future::Future; -#[allow(deprecated)] -pub use self::transfer::ReadOneError; pub use self::{ apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply}, denied::DeniedUpgrade, diff --git a/core/src/upgrade/transfer.rs b/core/src/upgrade/transfer.rs index fd812775..baea400a 100644 --- a/core/src/upgrade/transfer.rs +++ b/core/src/upgrade/transfer.rs @@ -21,7 +21,7 @@ //! Contains some helper futures for creating upgrades. use futures::prelude::*; -use std::{error, fmt, io}; +use std::io; // TODO: these methods could be on an Ext trait to AsyncWrite @@ -40,42 +40,6 @@ pub async fn write_length_prefixed( Ok(()) } -/// Send a message to the given socket, then shuts down the writing side. -/// -/// > **Note**: Prepends a variable-length prefix indicate the length of the message. This is -/// > compatible with what `read_one` expects. -/// -#[deprecated( - since = "0.29.0", - note = "Use `write_length_prefixed` instead. You will need to manually close the stream using `socket.close().await`." -)] -#[allow(dead_code)] -pub async fn write_one( - socket: &mut (impl AsyncWrite + Unpin), - data: impl AsRef<[u8]>, -) -> Result<(), io::Error> { - write_varint(socket, data.as_ref().len()).await?; - socket.write_all(data.as_ref()).await?; - socket.close().await?; - Ok(()) -} - -/// Send a message to the given socket with a length prefix appended to it. Also flushes the socket. -/// -/// > **Note**: Prepends a variable-length prefix indicate the length of the message. This is -/// > compatible with what `read_one` expects. -#[deprecated(since = "0.29.0", note = "Use `write_length_prefixed` instead.")] -#[allow(dead_code)] -pub async fn write_with_len_prefix( - socket: &mut (impl AsyncWrite + Unpin), - data: impl AsRef<[u8]>, -) -> Result<(), io::Error> { - write_varint(socket, data.as_ref().len()).await?; - socket.write_all(data.as_ref()).await?; - socket.flush().await?; - Ok(()) -} - /// Writes a variable-length integer to the `socket`. /// /// > **Note**: Does **NOT** flush the socket. @@ -162,78 +126,6 @@ pub async fn read_length_prefixed( Ok(buf) } -/// Reads a length-prefixed message from the given socket. -/// -/// The `max_size` parameter is the maximum size in bytes of the message that we accept. This is -/// necessary in order to avoid DoS attacks where the remote sends us a message of several -/// gigabytes. -/// -/// > **Note**: Assumes that a variable-length prefix indicates the length of the message. This is -/// > compatible with what `write_one` does. -#[deprecated(since = "0.29.0", note = "Use `read_length_prefixed` instead.")] -#[allow(dead_code, deprecated)] -pub async fn read_one( - socket: &mut (impl AsyncRead + Unpin), - max_size: usize, -) -> Result, ReadOneError> { - let len = read_varint(socket).await?; - if len > max_size { - return Err(ReadOneError::TooLarge { - requested: len, - max: max_size, - }); - } - - let mut buf = vec![0; len]; - socket.read_exact(&mut buf).await?; - Ok(buf) -} - -/// Error while reading one message. -#[derive(Debug)] -#[deprecated( - since = "0.29.0", - note = "Use `read_length_prefixed` instead of `read_one` to avoid depending on this type." -)] -pub enum ReadOneError { - /// Error on the socket. - Io(std::io::Error), - /// Requested data is over the maximum allowed size. - TooLarge { - /// Size requested by the remote. - requested: usize, - /// Maximum allowed. - max: usize, - }, -} - -#[allow(deprecated)] -impl From for ReadOneError { - fn from(err: std::io::Error) -> ReadOneError { - ReadOneError::Io(err) - } -} - -#[allow(deprecated)] -impl fmt::Display for ReadOneError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { - ReadOneError::Io(ref err) => write!(f, "{}", err), - ReadOneError::TooLarge { .. } => write!(f, "Received data size over maximum"), - } - } -} - -#[allow(deprecated)] -impl error::Error for ReadOneError { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - match *self { - ReadOneError::Io(ref err) => Some(err), - ReadOneError::TooLarge { .. } => None, - } - } -} - #[cfg(test)] mod tests { use super::*;