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

protocols/plaintext: Move to stable futures and use unsigned varints #1306

Merged
merged 6 commits into from
Nov 19, 2019

Conversation

mxinden
Copy link
Member

@mxinden mxinden commented Nov 14, 2019

The plaintext 2.0 specification requires to use unsigned varints for
frame length delimiting instead of fixed 4 byte integer frame length
delimiting. This commit aligns the implementation with the
specification.

The plaintext 2.0 specification requires to use unsigned varints for
frame length delimiting instead of fixed 4 byte integer frame length
delimiting. This commit aligns the implementation with the
specification.
Copy link
Contributor

@twittner twittner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me except for the change from BytesMut to Vec<u8>. I would like to know how this was motivated?

use futures::{Sink, Stream};
use futures::prelude::*;
use futures_codec::Framed;
use libp2p_core::{identity, InboundUpgrade, OutboundUpgrade, UpgradeInfo, upgrade::Negotiated, PeerId, PublicKey};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is much longer than 100 characters (cf. .editorconfig)

fn map_err(err: io::Error) -> io::Error {
debug!("error during plaintext handshake {:?}", err);
io::Error::new(io::ErrorKind::InvalidData, err)
}

pub struct PlainTextMiddleware<S> {
inner: Framed<S, BytesMut>,
inner: Framed<S, UviBytes<Vec<u8>>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you decide to move from BytesMut to Vec<u8>?

fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
self.inner.poll()
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
// TODO: Too much of a hack? (BytesMut -> Vec<u8>)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would stick with BytesMut instead of copying the data.

pub fn handshake<S>(socket: S, config: PlainText2Config)
-> impl Future<Item = (Framed<S, BytesMut>, Remote), Error = PlainTextError>
pub async fn handshake<S>(socket: S, config: PlainText2Config)
-> Result<(Framed<S, unsigned_varint::codec::UviBytes<Vec<u8>>>, Remote), PlainTextError>
Copy link
Contributor

@twittner twittner Nov 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would import unsigned_varint::codec::UviBytes and shorten this line. Again, I would keep using BytesMut.

// The handshake messages all start with a variable-length integer indicating the size.
let mut socket = Framed::new(
socket,
unsigned_varint::codec::UviBytes::<Vec<u8>>::default()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can write Framed::new(socket, UviBytes::default()) here.

@mxinden
Copy link
Member Author

mxinden commented Nov 17, 2019

Why did you decide to move from BytesMut to Vec<u8>?

This wasn't too much of a conscious decision, but rather me mechanically comparing the plaintext implementation to the secio implementation of Stream and Sink. Thanks for the catch!

Can you take another look?

Copy link
Contributor

@twittner twittner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I suggested to use BoxFuture as a shortcut to Pin<Box<dyn Future ...>> but feel free to go with the version you have if you prefer the longer type signature.

{
type Output = (PeerId, PlainTextOutput<Negotiated<C>>);
type Error = PlainTextError;
type Future = Box<dyn Future<Item = Self::Output, Error = Self::Error> + Send>;
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;

(assuming an use futures::future::BoxFuture;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, cool. Wasn't aware of this type alias. Thanks.

{
type Output = (PeerId, PlainTextOutput<Negotiated<C>>);
type Error = PlainTextError;
type Future = Box<dyn Future<Item = Self::Output, Error = Self::Error> + Send>;
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like above

Suggested change
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;
type Future = BoxFuture<'static, Result<Self::Output, Self::Error>>;

protocols/secio/src/codec/mod.rs Show resolved Hide resolved
@twittner twittner merged commit be73b90 into libp2p:stable-futures Nov 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants