Skip to content

Commit 87f3cb8

Browse files
authored
protocols/plaintext: Do not UVI encode goodput (#1765)
* protocols/plaintext: Do not UVI encode goodput Only prefix handshake messages with the message length in bytes as an unsigned varint. Return a plain socket once handshaking succeeded. * *: Bump minor version in changelogs and cargo tomls
1 parent 3c72b07 commit 87f3cb8

File tree

6 files changed

+24
-75
lines changed

6 files changed

+24
-75
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
- [`parity-multiaddr` CHANGELOG](misc/multiaddr/CHANGELOG.md)
2424
- [`libp2p-core-derive` CHANGELOG](misc/core-derive/CHANGELOG.md)
2525

26-
# Version 0.28.2 [unreleased]
26+
# Version 0.29.0 [unreleased]
2727

28-
- Update `libp2p-core`, `libp2p-gossipsub`, `libp2p-mplex`, `libp2p-noise`, `libp2p-websocket` and `parity-multiaddr`.
28+
- Update `libp2p-core`, `libp2p-gossipsub`, `libp2p-mplex`, `libp2p-noise`,
29+
`libp2p-plaintext`, `libp2p-websocket` and `parity-multiaddr`.
2930

3031
# Version 0.28.1 [2020-09-10]
3132

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "libp2p"
33
edition = "2018"
44
description = "Peer-to-peer networking library"
5-
version = "0.28.2"
5+
version = "0.29.0"
66
authors = ["Parity Technologies <[email protected]>"]
77
license = "MIT"
88
repository = "https://github.com/libp2p/rust-libp2p"
@@ -71,7 +71,7 @@ libp2p-kad = { version = "0.23.1", path = "protocols/kad", optional = true }
7171
libp2p-mplex = { version = "0.22.1", path = "muxers/mplex", optional = true }
7272
libp2p-noise = { version = "0.24.1", path = "protocols/noise", optional = true }
7373
libp2p-ping = { version = "0.22.0", path = "protocols/ping", optional = true }
74-
libp2p-plaintext = { version = "0.22.1", path = "protocols/plaintext", optional = true }
74+
libp2p-plaintext = { version = "0.23.0", path = "protocols/plaintext", optional = true }
7575
libp2p-pnet = { version = "0.19.1", path = "protocols/pnet", optional = true }
7676
libp2p-request-response = { version = "0.3.0", path = "protocols/request-response", optional = true }
7777
libp2p-swarm = { version = "0.22.0", path = "swarm" }

protocols/plaintext/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
# 0.22.1 [unreleased]
1+
# 0.23.0 [unreleased]
22

33
- Improve error logging
44
[PR 1759](https://github.com/libp2p/rust-libp2p/pull/1759).
55

66
- Update dependencies.
77

8+
- Only prefix handshake messages with the message length in bytes as an unsigned
9+
varint. Return a plain socket once handshaking succeeded. See [issue
10+
1760](https://github.com/libp2p/rust-libp2p/issues/1760) for details.
11+
812
# 0.22.0 [2020-09-09]
913

1014
- Bump `libp2p-core` dependency.

protocols/plaintext/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "libp2p-plaintext"
33
edition = "2018"
44
description = "Plaintext encryption dummy protocol for libp2p"
5-
version = "0.22.1"
5+
version = "0.23.0"
66
authors = ["Parity Technologies <[email protected]>"]
77
license = "MIT"
88
repository = "https://github.com/libp2p/rust-libp2p"
@@ -16,7 +16,6 @@ futures_codec = "0.4.0"
1616
libp2p-core = { version = "0.22.0", path = "../../core" }
1717
log = "0.4.8"
1818
prost = "0.6.1"
19-
rw-stream-sink = "0.2.0"
2019
unsigned-varint = { version = "0.5.1", features = ["futures-codec"] }
2120
void = "1.0.2"
2221

protocols/plaintext/src/handshake.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,21 @@ impl HandshakeContext<Local> {
111111
}
112112

113113
pub async fn handshake<S>(socket: S, config: PlainText2Config)
114-
-> Result<(Framed<S, UviBytes<BytesMut>>, Remote), PlainTextError>
114+
-> Result<(S, Remote), PlainTextError>
115115
where
116116
S: AsyncRead + AsyncWrite + Send + Unpin,
117117
{
118118
// The handshake messages all start with a variable-length integer indicating the size.
119-
let mut socket = Framed::new(socket, UviBytes::default());
119+
let mut framed_socket = Framed::new(socket, UviBytes::default());
120120

121121
trace!("starting handshake");
122122
let context = HandshakeContext::new(config)?;
123123

124124
trace!("sending exchange to remote");
125-
socket.send(BytesMut::from(&context.state.exchange_bytes[..])).await?;
125+
framed_socket.send(BytesMut::from(&context.state.exchange_bytes[..])).await?;
126126

127127
trace!("receiving the remote's exchange");
128-
let context = match socket.next().await {
128+
let context = match framed_socket.next().await {
129129
Some(p) => context.with_remote(p?)?,
130130
None => {
131131
debug!("unexpected eof while waiting for remote's exchange");
@@ -135,5 +135,5 @@ where
135135
};
136136

137137
trace!("received exchange from remote; pubkey = {:?}", context.state.public_key);
138-
Ok((socket, context.state))
138+
Ok((framed_socket.into_inner(), context.state))
139139
}

protocols/plaintext/src/lib.rs

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,10 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
use crate::error::PlainTextError;
22-
use crate::handshake::Remote;
2322

24-
use bytes::BytesMut;
2523
use futures::future::{self, Ready};
2624
use futures::prelude::*;
27-
use futures::{future::BoxFuture, Sink, Stream};
28-
use futures_codec::Framed;
25+
use futures::future::BoxFuture;
2926
use libp2p_core::{
3027
identity,
3128
InboundUpgrade,
@@ -35,9 +32,7 @@ use libp2p_core::{
3532
PublicKey,
3633
};
3734
use log::debug;
38-
use rw_stream_sink::RwStreamSink;
3935
use std::{io, iter, pin::Pin, task::{Context, Poll}};
40-
use unsigned_varint::codec::UviBytes;
4136
use void::Void;
4237

4338
mod error;
@@ -153,76 +148,26 @@ impl PlainText2Config {
153148
T: AsyncRead + AsyncWrite + Send + Unpin + 'static
154149
{
155150
debug!("Starting plaintext handshake.");
156-
let (stream_sink, remote) = PlainTextMiddleware::handshake(socket, self).await?;
151+
let (socket, remote) = handshake::handshake(socket, self).await?;
157152
debug!("Finished plaintext handshake.");
158153

159154
Ok((
160155
remote.peer_id,
161156
PlainTextOutput {
162-
stream: RwStreamSink::new(stream_sink),
157+
socket,
163158
remote_key: remote.public_key,
164159
}
165160
))
166161
}
167162
}
168163

169-
pub struct PlainTextMiddleware<S> {
170-
inner: Framed<S, UviBytes<BytesMut>>,
171-
}
172-
173-
impl<S> PlainTextMiddleware<S>
174-
where
175-
S: AsyncRead + AsyncWrite + Send + Unpin,
176-
{
177-
async fn handshake(socket: S, config: PlainText2Config)
178-
-> Result<(PlainTextMiddleware<S>, Remote), PlainTextError>
179-
{
180-
let (inner, remote) = handshake::handshake(socket, config).await?;
181-
Ok((PlainTextMiddleware { inner }, remote))
182-
}
183-
}
184-
185-
impl<S> Sink<BytesMut> for PlainTextMiddleware<S>
186-
where
187-
S: AsyncRead + AsyncWrite + Unpin,
188-
{
189-
type Error = io::Error;
190-
191-
fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
192-
Sink::poll_ready(Pin::new(&mut self.inner), cx)
193-
}
194-
195-
fn start_send(mut self: Pin<&mut Self>, item: BytesMut) -> Result<(), Self::Error> {
196-
Sink::start_send(Pin::new(&mut self.inner), item)
197-
}
198-
199-
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
200-
Sink::poll_flush(Pin::new(&mut self.inner), cx)
201-
}
202-
203-
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
204-
Sink::poll_close(Pin::new(&mut self.inner), cx)
205-
}
206-
}
207-
208-
impl<S> Stream for PlainTextMiddleware<S>
209-
where
210-
S: AsyncRead + AsyncWrite + Unpin,
211-
{
212-
type Item = Result<BytesMut, io::Error>;
213-
214-
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
215-
Stream::poll_next(Pin::new(&mut self.inner), cx)
216-
}
217-
}
218-
219164
/// Output of the plaintext protocol.
220165
pub struct PlainTextOutput<S>
221166
where
222167
S: AsyncRead + AsyncWrite + Unpin,
223168
{
224169
/// The plaintext stream.
225-
pub stream: RwStreamSink<PlainTextMiddleware<S>>,
170+
pub socket: S,
226171
/// The public key of the remote.
227172
pub remote_key: PublicKey,
228173
}
@@ -231,26 +176,26 @@ impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for PlainTextOutput<S> {
231176
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8])
232177
-> Poll<Result<usize, io::Error>>
233178
{
234-
AsyncRead::poll_read(Pin::new(&mut self.stream), cx, buf)
179+
AsyncRead::poll_read(Pin::new(&mut self.socket), cx, buf)
235180
}
236181
}
237182

238183
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for PlainTextOutput<S> {
239184
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8])
240185
-> Poll<Result<usize, io::Error>>
241186
{
242-
AsyncWrite::poll_write(Pin::new(&mut self.stream), cx, buf)
187+
AsyncWrite::poll_write(Pin::new(&mut self.socket), cx, buf)
243188
}
244189

245190
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>)
246191
-> Poll<Result<(), io::Error>>
247192
{
248-
AsyncWrite::poll_flush(Pin::new(&mut self.stream), cx)
193+
AsyncWrite::poll_flush(Pin::new(&mut self.socket), cx)
249194
}
250195

251196
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>)
252197
-> Poll<Result<(), io::Error>>
253198
{
254-
AsyncWrite::poll_close(Pin::new(&mut self.stream), cx)
199+
AsyncWrite::poll_close(Pin::new(&mut self.socket), cx)
255200
}
256201
}

0 commit comments

Comments
 (0)