Skip to content

Commit 2eca38c

Browse files
core/upgrade/: Add ReadyUpgrade (#2855)
1 parent 8644c65 commit 2eca38c

File tree

5 files changed

+91
-41
lines changed

5 files changed

+91
-41
lines changed

core/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
- Make RSA keypair support optional. To enable RSA support, `rsa` feature should be enabled.
44
See [PR 2860].
55

6+
- Add `ReadyUpgrade`. See [PR 2855].
7+
8+
[PR 2855]: https://github.com/libp2p/rust-libp2p/pull/2855
69
[PR 2860]: https://github.com/libp2p/rust-libp2p/pull/2860/
710

811
# 0.35.1

core/src/upgrade.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ mod from_fn;
6565
mod map;
6666
mod optional;
6767
mod pending;
68+
mod ready;
6869
mod select;
6970
mod transfer;
7071

@@ -79,6 +80,7 @@ pub use self::{
7980
map::{MapInboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgrade, MapOutboundUpgradeErr},
8081
optional::OptionalUpgrade,
8182
pending::PendingUpgrade,
83+
ready::ReadyUpgrade,
8284
select::SelectUpgrade,
8385
transfer::{read_length_prefixed, read_varint, write_length_prefixed, write_varint},
8486
};

core/src/upgrade/ready.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2022 Protocol Labs.
2+
// Copyright 2017-2018 Parity Technologies (UK) Ltd.
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a
5+
// copy of this software and associated documentation files (the "Software"),
6+
// to deal in the Software without restriction, including without limitation
7+
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
// and/or sell copies of the Software, and to permit persons to whom the
9+
// Software is furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20+
// DEALINGS IN THE SOFTWARE.
21+
22+
use crate::upgrade::{InboundUpgrade, OutboundUpgrade, ProtocolName, UpgradeInfo};
23+
use futures::future;
24+
use std::iter;
25+
use void::Void;
26+
27+
/// Implementation of [`UpgradeInfo`], [`InboundUpgrade`] and [`OutboundUpgrade`] that directly yields the substream.
28+
#[derive(Debug, Copy, Clone)]
29+
pub struct ReadyUpgrade<P> {
30+
protocol_name: P,
31+
}
32+
33+
impl<P> ReadyUpgrade<P> {
34+
pub fn new(protocol_name: P) -> Self {
35+
Self { protocol_name }
36+
}
37+
}
38+
39+
impl<P> UpgradeInfo for ReadyUpgrade<P>
40+
where
41+
P: ProtocolName + Clone,
42+
{
43+
type Info = P;
44+
type InfoIter = iter::Once<P>;
45+
46+
fn protocol_info(&self) -> Self::InfoIter {
47+
iter::once(self.protocol_name.clone())
48+
}
49+
}
50+
51+
impl<C, P> InboundUpgrade<C> for ReadyUpgrade<P>
52+
where
53+
P: ProtocolName + Clone,
54+
{
55+
type Output = C;
56+
type Error = Void;
57+
type Future = future::Ready<Result<Self::Output, Self::Error>>;
58+
59+
fn upgrade_inbound(self, stream: C, _: Self::Info) -> Self::Future {
60+
future::ready(Ok(stream))
61+
}
62+
}
63+
64+
impl<C, P> OutboundUpgrade<C> for ReadyUpgrade<P>
65+
where
66+
P: ProtocolName + Clone,
67+
{
68+
type Output = C;
69+
type Error = Void;
70+
type Future = future::Ready<Result<Self::Output, Self::Error>>;
71+
72+
fn upgrade_outbound(self, stream: C, _: Self::Info) -> Self::Future {
73+
future::ready(Ok(stream))
74+
}
75+
}

protocols/ping/src/handler.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use crate::protocol;
21+
use crate::{protocol, PROTOCOL_NAME};
2222
use futures::future::BoxFuture;
2323
use futures::prelude::*;
2424
use futures_timer::Delay;
25+
use libp2p_core::upgrade::ReadyUpgrade;
2526
use libp2p_core::{upgrade::NegotiationError, UpgradeError};
2627
use libp2p_swarm::{
2728
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
@@ -225,13 +226,13 @@ impl ConnectionHandler for Handler {
225226
type InEvent = Void;
226227
type OutEvent = crate::Result;
227228
type Error = Failure;
228-
type InboundProtocol = protocol::Ping;
229-
type OutboundProtocol = protocol::Ping;
229+
type InboundProtocol = ReadyUpgrade<&'static [u8]>;
230+
type OutboundProtocol = ReadyUpgrade<&'static [u8]>;
230231
type OutboundOpenInfo = ();
231232
type InboundOpenInfo = ();
232233

233-
fn listen_protocol(&self) -> SubstreamProtocol<protocol::Ping, ()> {
234-
SubstreamProtocol::new(protocol::Ping, ())
234+
fn listen_protocol(&self) -> SubstreamProtocol<ReadyUpgrade<&'static [u8]>, ()> {
235+
SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ())
235236
}
236237

237238
fn inject_fully_negotiated_inbound(&mut self, stream: NegotiatedSubstream, (): ()) {
@@ -274,7 +275,8 @@ impl ConnectionHandler for Handler {
274275
fn poll(
275276
&mut self,
276277
cx: &mut Context<'_>,
277-
) -> Poll<ConnectionHandlerEvent<protocol::Ping, (), crate::Result, Self::Error>> {
278+
) -> Poll<ConnectionHandlerEvent<ReadyUpgrade<&'static [u8]>, (), crate::Result, Self::Error>>
279+
{
278280
match self.state {
279281
State::Inactive { reported: true } => {
280282
return Poll::Pending; // nothing to do on this connection
@@ -366,7 +368,7 @@ impl ConnectionHandler for Handler {
366368
}
367369
None => {
368370
self.outbound = Some(PingState::OpenStream);
369-
let protocol = SubstreamProtocol::new(protocol::Ping, ())
371+
let protocol = SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ())
370372
.with_timeout(self.config.timeout);
371373
return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest {
372374
protocol,

protocols/ping/src/protocol.rs

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@
2020

2121
use futures::prelude::*;
2222
use instant::Instant;
23-
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
24-
use libp2p_swarm::NegotiatedSubstream;
2523
use rand::{distributions, prelude::*};
26-
use std::{io, iter, time::Duration};
27-
use void::Void;
24+
use std::{io, time::Duration};
2825

29-
pub const PROTOCOL_NAME: &[u8; 16] = b"/ipfs/ping/1.0.0";
26+
pub const PROTOCOL_NAME: &[u8] = b"/ipfs/ping/1.0.0";
3027

3128
/// The `Ping` protocol upgrade.
3229
///
@@ -52,35 +49,6 @@ pub struct Ping;
5249

5350
const PING_SIZE: usize = 32;
5451

55-
impl UpgradeInfo for Ping {
56-
type Info = &'static [u8];
57-
type InfoIter = iter::Once<Self::Info>;
58-
59-
fn protocol_info(&self) -> Self::InfoIter {
60-
iter::once(PROTOCOL_NAME)
61-
}
62-
}
63-
64-
impl InboundUpgrade<NegotiatedSubstream> for Ping {
65-
type Output = NegotiatedSubstream;
66-
type Error = Void;
67-
type Future = future::Ready<Result<Self::Output, Self::Error>>;
68-
69-
fn upgrade_inbound(self, stream: NegotiatedSubstream, _: Self::Info) -> Self::Future {
70-
future::ok(stream)
71-
}
72-
}
73-
74-
impl OutboundUpgrade<NegotiatedSubstream> for Ping {
75-
type Output = NegotiatedSubstream;
76-
type Error = Void;
77-
type Future = future::Ready<Result<Self::Output, Self::Error>>;
78-
79-
fn upgrade_outbound(self, stream: NegotiatedSubstream, _: Self::Info) -> Self::Future {
80-
future::ok(stream)
81-
}
82-
}
83-
8452
/// Sends a ping and waits for the pong.
8553
pub async fn send_ping<S>(mut stream: S) -> io::Result<(S, Duration)>
8654
where

0 commit comments

Comments
 (0)