Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove functions deprecated in earlier versions #2213

Merged
merged 2 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
mxinden marked this conversation as resolved.
Show resolved Hide resolved
[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
Expand Down
2 changes: 0 additions & 2 deletions core/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
110 changes: 1 addition & 109 deletions core/src/upgrade/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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<Vec<u8>, 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<std::io::Error> 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::*;
Expand Down