mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-28 01:01:34 +00:00
*: Format with rustfmt (#2188)
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
This commit is contained in:
@ -19,32 +19,16 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::protocol::{
|
||||
IdentifyProtocol,
|
||||
IdentifyPushProtocol,
|
||||
IdentifyInfo,
|
||||
InboundPush,
|
||||
OutboundPush,
|
||||
ReplySubstream
|
||||
IdentifyInfo, IdentifyProtocol, IdentifyPushProtocol, InboundPush, OutboundPush, ReplySubstream,
|
||||
};
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::either::{
|
||||
EitherError,
|
||||
EitherOutput,
|
||||
};
|
||||
use libp2p_core::either::{EitherError, EitherOutput};
|
||||
use libp2p_core::upgrade::{
|
||||
EitherUpgrade,
|
||||
InboundUpgrade,
|
||||
OutboundUpgrade,
|
||||
SelectUpgrade,
|
||||
UpgradeError,
|
||||
EitherUpgrade, InboundUpgrade, OutboundUpgrade, SelectUpgrade, UpgradeError,
|
||||
};
|
||||
use libp2p_swarm::{
|
||||
NegotiatedSubstream,
|
||||
KeepAlive,
|
||||
SubstreamProtocol,
|
||||
ProtocolsHandler,
|
||||
ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr
|
||||
KeepAlive, NegotiatedSubstream, ProtocolsHandler, ProtocolsHandlerEvent,
|
||||
ProtocolsHandlerUpgrErr, SubstreamProtocol,
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
use std::{io, pin::Pin, task::Context, task::Poll, time::Duration};
|
||||
@ -57,12 +41,14 @@ use wasm_timer::Delay;
|
||||
/// permitting the underlying connection to be closed.
|
||||
pub struct IdentifyHandler {
|
||||
/// Pending events to yield.
|
||||
events: SmallVec<[ProtocolsHandlerEvent<
|
||||
events: SmallVec<
|
||||
[ProtocolsHandlerEvent<
|
||||
EitherUpgrade<IdentifyProtocol, IdentifyPushProtocol<OutboundPush>>,
|
||||
(),
|
||||
IdentifyHandlerEvent,
|
||||
io::Error,
|
||||
>; 4]>,
|
||||
>; 4],
|
||||
>,
|
||||
|
||||
/// Future that fires when we need to identify the node again.
|
||||
next_id: Delay,
|
||||
@ -114,28 +100,23 @@ impl ProtocolsHandler for IdentifyHandler {
|
||||
|
||||
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
|
||||
SubstreamProtocol::new(
|
||||
SelectUpgrade::new(
|
||||
IdentifyProtocol,
|
||||
IdentifyPushProtocol::inbound(),
|
||||
), ())
|
||||
SelectUpgrade::new(IdentifyProtocol, IdentifyPushProtocol::inbound()),
|
||||
(),
|
||||
)
|
||||
}
|
||||
|
||||
fn inject_fully_negotiated_inbound(
|
||||
&mut self,
|
||||
output: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output,
|
||||
_: Self::InboundOpenInfo
|
||||
_: Self::InboundOpenInfo,
|
||||
) {
|
||||
match output {
|
||||
EitherOutput::First(substream) => {
|
||||
self.events.push(
|
||||
ProtocolsHandlerEvent::Custom(
|
||||
IdentifyHandlerEvent::Identify(substream)))
|
||||
}
|
||||
EitherOutput::Second(info) => {
|
||||
self.events.push(
|
||||
ProtocolsHandlerEvent::Custom(
|
||||
IdentifyHandlerEvent::Identified(info)))
|
||||
}
|
||||
EitherOutput::First(substream) => self.events.push(ProtocolsHandlerEvent::Custom(
|
||||
IdentifyHandlerEvent::Identify(substream),
|
||||
)),
|
||||
EitherOutput::Second(info) => self.events.push(ProtocolsHandlerEvent::Custom(
|
||||
IdentifyHandlerEvent::Identified(info),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
@ -146,39 +127,42 @@ impl ProtocolsHandler for IdentifyHandler {
|
||||
) {
|
||||
match output {
|
||||
EitherOutput::First(remote_info) => {
|
||||
self.events.push(
|
||||
ProtocolsHandlerEvent::Custom(
|
||||
IdentifyHandlerEvent::Identified(remote_info)));
|
||||
self.events.push(ProtocolsHandlerEvent::Custom(
|
||||
IdentifyHandlerEvent::Identified(remote_info),
|
||||
));
|
||||
self.keep_alive = KeepAlive::No;
|
||||
}
|
||||
EitherOutput::Second(()) => self.events.push(
|
||||
ProtocolsHandlerEvent::Custom(IdentifyHandlerEvent::IdentificationPushed))
|
||||
EitherOutput::Second(()) => self.events.push(ProtocolsHandlerEvent::Custom(
|
||||
IdentifyHandlerEvent::IdentificationPushed,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_event(&mut self, IdentifyPush(push): Self::InEvent) {
|
||||
self.events.push(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: SubstreamProtocol::new(
|
||||
EitherUpgrade::B(
|
||||
IdentifyPushProtocol::outbound(push)), ())
|
||||
});
|
||||
self.events
|
||||
.push(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: SubstreamProtocol::new(
|
||||
EitherUpgrade::B(IdentifyPushProtocol::outbound(push)),
|
||||
(),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
fn inject_dial_upgrade_error(
|
||||
&mut self,
|
||||
_info: Self::OutboundOpenInfo,
|
||||
err: ProtocolsHandlerUpgrErr<
|
||||
<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error
|
||||
>
|
||||
<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error,
|
||||
>,
|
||||
) {
|
||||
let err = err.map_upgrade_err(|e| match e {
|
||||
UpgradeError::Select(e) => UpgradeError::Select(e),
|
||||
UpgradeError::Apply(EitherError::A(ioe)) => UpgradeError::Apply(ioe),
|
||||
UpgradeError::Apply(EitherError::B(ioe)) => UpgradeError::Apply(ioe),
|
||||
});
|
||||
self.events.push(
|
||||
ProtocolsHandlerEvent::Custom(
|
||||
IdentifyHandlerEvent::IdentificationError(err)));
|
||||
self.events.push(ProtocolsHandlerEvent::Custom(
|
||||
IdentifyHandlerEvent::IdentificationError(err),
|
||||
));
|
||||
self.keep_alive = KeepAlive::No;
|
||||
self.next_id.reset(self.interval);
|
||||
}
|
||||
@ -187,7 +171,10 @@ impl ProtocolsHandler for IdentifyHandler {
|
||||
self.keep_alive
|
||||
}
|
||||
|
||||
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<
|
||||
fn poll(
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<
|
||||
ProtocolsHandlerEvent<
|
||||
Self::OutboundProtocol,
|
||||
Self::OutboundOpenInfo,
|
||||
@ -196,9 +183,7 @@ impl ProtocolsHandler for IdentifyHandler {
|
||||
>,
|
||||
> {
|
||||
if !self.events.is_empty() {
|
||||
return Poll::Ready(
|
||||
self.events.remove(0),
|
||||
);
|
||||
return Poll::Ready(self.events.remove(0));
|
||||
}
|
||||
|
||||
// Poll the future that fires when we need to identify the node again.
|
||||
@ -207,11 +192,11 @@ impl ProtocolsHandler for IdentifyHandler {
|
||||
Poll::Ready(Ok(())) => {
|
||||
self.next_id.reset(self.interval);
|
||||
let ev = ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
||||
protocol: SubstreamProtocol::new(EitherUpgrade::A(IdentifyProtocol), ())
|
||||
protocol: SubstreamProtocol::new(EitherUpgrade::A(IdentifyProtocol), ()),
|
||||
};
|
||||
Poll::Ready(ev)
|
||||
}
|
||||
Poll::Ready(Err(err)) => Poll::Ready(ProtocolsHandlerEvent::Close(err))
|
||||
Poll::Ready(Err(err)) => Poll::Ready(ProtocolsHandlerEvent::Close(err)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,26 +22,16 @@ use crate::handler::{IdentifyHandler, IdentifyHandlerEvent, IdentifyPush};
|
||||
use crate::protocol::{IdentifyInfo, ReplySubstream};
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::{
|
||||
ConnectedPoint,
|
||||
Multiaddr,
|
||||
PeerId,
|
||||
PublicKey,
|
||||
connection::{ConnectionId, ListenerId},
|
||||
upgrade::UpgradeError
|
||||
upgrade::UpgradeError,
|
||||
ConnectedPoint, Multiaddr, PeerId, PublicKey,
|
||||
};
|
||||
use libp2p_swarm::{
|
||||
AddressScore,
|
||||
DialPeerCondition,
|
||||
NegotiatedSubstream,
|
||||
NetworkBehaviour,
|
||||
NetworkBehaviourAction,
|
||||
NotifyHandler,
|
||||
PollParameters,
|
||||
ProtocolsHandler,
|
||||
ProtocolsHandlerUpgrErr
|
||||
AddressScore, DialPeerCondition, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction,
|
||||
NotifyHandler, PollParameters, ProtocolsHandler, ProtocolsHandlerUpgrErr,
|
||||
};
|
||||
use std::{
|
||||
collections::{HashSet, HashMap, VecDeque},
|
||||
collections::{HashMap, HashSet, VecDeque},
|
||||
io,
|
||||
pin::Pin,
|
||||
task::Context,
|
||||
@ -74,13 +64,13 @@ enum Reply {
|
||||
Queued {
|
||||
peer: PeerId,
|
||||
io: ReplySubstream<NegotiatedSubstream>,
|
||||
observed: Multiaddr
|
||||
observed: Multiaddr,
|
||||
},
|
||||
/// The reply is being sent.
|
||||
Sending {
|
||||
peer: PeerId,
|
||||
io: Pin<Box<dyn Future<Output = Result<(), io::Error>> + Send>>,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
/// Configuration for the [`Identify`] [`NetworkBehaviour`].
|
||||
@ -178,14 +168,14 @@ impl Identify {
|
||||
/// Initiates an active push of the local peer information to the given peers.
|
||||
pub fn push<I>(&mut self, peers: I)
|
||||
where
|
||||
I: IntoIterator<Item = PeerId>
|
||||
I: IntoIterator<Item = PeerId>,
|
||||
{
|
||||
for p in peers {
|
||||
if self.pending_push.insert(p) {
|
||||
if !self.connected.contains_key(&p) {
|
||||
self.events.push_back(NetworkBehaviourAction::DialPeer {
|
||||
peer_id: p,
|
||||
condition: DialPeerCondition::Disconnected
|
||||
condition: DialPeerCondition::Disconnected,
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -201,16 +191,29 @@ impl NetworkBehaviour for Identify {
|
||||
IdentifyHandler::new(self.config.initial_delay, self.config.interval)
|
||||
}
|
||||
|
||||
fn inject_connection_established(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
|
||||
fn inject_connection_established(
|
||||
&mut self,
|
||||
peer_id: &PeerId,
|
||||
conn: &ConnectionId,
|
||||
endpoint: &ConnectedPoint,
|
||||
) {
|
||||
let addr = match endpoint {
|
||||
ConnectedPoint::Dialer { address } => address.clone(),
|
||||
ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr.clone(),
|
||||
};
|
||||
|
||||
self.connected.entry(*peer_id).or_default().insert(*conn, addr);
|
||||
self.connected
|
||||
.entry(*peer_id)
|
||||
.or_default()
|
||||
.insert(*conn, addr);
|
||||
}
|
||||
|
||||
fn inject_connection_closed(&mut self, peer_id: &PeerId, conn: &ConnectionId, _: &ConnectedPoint) {
|
||||
fn inject_connection_closed(
|
||||
&mut self,
|
||||
peer_id: &PeerId,
|
||||
conn: &ConnectionId,
|
||||
_: &ConnectedPoint,
|
||||
) {
|
||||
if let Some(addrs) = self.connected.get_mut(peer_id) {
|
||||
addrs.remove(conn);
|
||||
}
|
||||
@ -248,41 +251,39 @@ impl NetworkBehaviour for Identify {
|
||||
match event {
|
||||
IdentifyHandlerEvent::Identified(info) => {
|
||||
let observed = info.observed_addr.clone();
|
||||
self.events.push_back(
|
||||
NetworkBehaviourAction::GenerateEvent(
|
||||
IdentifyEvent::Received {
|
||||
peer_id,
|
||||
info,
|
||||
}));
|
||||
self.events.push_back(
|
||||
NetworkBehaviourAction::ReportObservedAddr {
|
||||
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
|
||||
IdentifyEvent::Received { peer_id, info },
|
||||
));
|
||||
self.events
|
||||
.push_back(NetworkBehaviourAction::ReportObservedAddr {
|
||||
address: observed,
|
||||
score: AddressScore::Finite(1),
|
||||
});
|
||||
}
|
||||
IdentifyHandlerEvent::IdentificationPushed => {
|
||||
self.events.push_back(
|
||||
NetworkBehaviourAction::GenerateEvent(
|
||||
IdentifyEvent::Pushed {
|
||||
peer_id,
|
||||
}));
|
||||
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
|
||||
IdentifyEvent::Pushed { peer_id },
|
||||
));
|
||||
}
|
||||
IdentifyHandlerEvent::Identify(sender) => {
|
||||
let observed = self.connected.get(&peer_id)
|
||||
let observed = self
|
||||
.connected
|
||||
.get(&peer_id)
|
||||
.and_then(|addrs| addrs.get(&connection))
|
||||
.expect("`inject_event` is only called with an established connection \
|
||||
and `inject_connection_established` ensures there is an entry; qed");
|
||||
self.pending_replies.push_back(
|
||||
Reply::Queued {
|
||||
peer: peer_id,
|
||||
io: sender,
|
||||
observed: observed.clone()
|
||||
});
|
||||
.expect(
|
||||
"`inject_event` is only called with an established connection \
|
||||
and `inject_connection_established` ensures there is an entry; qed",
|
||||
);
|
||||
self.pending_replies.push_back(Reply::Queued {
|
||||
peer: peer_id,
|
||||
io: sender,
|
||||
observed: observed.clone(),
|
||||
});
|
||||
}
|
||||
IdentifyHandlerEvent::IdentificationError(error) => {
|
||||
self.events.push_back(
|
||||
NetworkBehaviourAction::GenerateEvent(
|
||||
IdentifyEvent::Error { peer_id, error }));
|
||||
self.events.push_back(NetworkBehaviourAction::GenerateEvent(
|
||||
IdentifyEvent::Error { peer_id, error },
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -332,7 +333,7 @@ impl NetworkBehaviour for Identify {
|
||||
peer_id,
|
||||
event: push,
|
||||
handler: NotifyHandler::Any,
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
// Check for pending replies to send.
|
||||
@ -360,12 +361,12 @@ impl NetworkBehaviour for Identify {
|
||||
Poll::Ready(Ok(())) => {
|
||||
let event = IdentifyEvent::Sent { peer_id: peer };
|
||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
||||
},
|
||||
}
|
||||
Poll::Pending => {
|
||||
self.pending_replies.push_back(Reply::Sending { peer, io });
|
||||
if sending == to_send {
|
||||
// All remaining futures are NotReady
|
||||
break
|
||||
break;
|
||||
} else {
|
||||
reply = self.pending_replies.pop_front();
|
||||
}
|
||||
@ -373,13 +374,15 @@ impl NetworkBehaviour for Identify {
|
||||
Poll::Ready(Err(err)) => {
|
||||
let event = IdentifyEvent::Error {
|
||||
peer_id: peer,
|
||||
error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(err))
|
||||
error: ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(
|
||||
err,
|
||||
)),
|
||||
};
|
||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event));
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
None => unreachable!()
|
||||
None => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -438,22 +441,20 @@ fn listen_addrs(params: &impl PollParameters) -> Vec<Multiaddr> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use futures::pin_mut;
|
||||
use libp2p_core::{
|
||||
identity,
|
||||
PeerId,
|
||||
muxing::StreamMuxerBox,
|
||||
transport,
|
||||
Transport,
|
||||
upgrade
|
||||
};
|
||||
use libp2p_noise as noise;
|
||||
use libp2p_tcp::TcpConfig;
|
||||
use libp2p_swarm::{Swarm, SwarmEvent};
|
||||
use libp2p_core::{identity, muxing::StreamMuxerBox, transport, upgrade, PeerId, Transport};
|
||||
use libp2p_mplex::MplexConfig;
|
||||
use libp2p_noise as noise;
|
||||
use libp2p_swarm::{Swarm, SwarmEvent};
|
||||
use libp2p_tcp::TcpConfig;
|
||||
|
||||
fn transport() -> (identity::PublicKey, transport::Boxed<(PeerId, StreamMuxerBox)>) {
|
||||
fn transport() -> (
|
||||
identity::PublicKey,
|
||||
transport::Boxed<(PeerId, StreamMuxerBox)>,
|
||||
) {
|
||||
let id_keys = identity::Keypair::generate_ed25519();
|
||||
let noise_keys = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&id_keys).unwrap();
|
||||
let noise_keys = noise::Keypair::<noise::X25519Spec>::new()
|
||||
.into_authentic(&id_keys)
|
||||
.unwrap();
|
||||
let pubkey = id_keys.public();
|
||||
let transport = TcpConfig::new()
|
||||
.nodelay(true)
|
||||
@ -470,7 +471,8 @@ mod tests {
|
||||
let (pubkey, transport) = transport();
|
||||
let protocol = Identify::new(
|
||||
IdentifyConfig::new("a".to_string(), pubkey.clone())
|
||||
.with_agent_version("b".to_string()));
|
||||
.with_agent_version("b".to_string()),
|
||||
);
|
||||
let swarm = Swarm::new(transport, protocol, pubkey.to_peer_id());
|
||||
(swarm, pubkey)
|
||||
};
|
||||
@ -479,12 +481,15 @@ mod tests {
|
||||
let (pubkey, transport) = transport();
|
||||
let protocol = Identify::new(
|
||||
IdentifyConfig::new("c".to_string(), pubkey.clone())
|
||||
.with_agent_version("d".to_string()));
|
||||
.with_agent_version("d".to_string()),
|
||||
);
|
||||
let swarm = Swarm::new(transport, protocol, pubkey.to_peer_id());
|
||||
(swarm, pubkey)
|
||||
};
|
||||
|
||||
swarm1.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
|
||||
swarm1
|
||||
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
||||
.unwrap();
|
||||
|
||||
let listen_addr = async_std::task::block_on(async {
|
||||
loop {
|
||||
@ -509,7 +514,11 @@ mod tests {
|
||||
let swarm2_fut = swarm2.select_next_some();
|
||||
pin_mut!(swarm2_fut);
|
||||
|
||||
match future::select(swarm1_fut, swarm2_fut).await.factor_second().0 {
|
||||
match future::select(swarm1_fut, swarm2_fut)
|
||||
.await
|
||||
.factor_second()
|
||||
.0
|
||||
{
|
||||
future::Either::Left(SwarmEvent::Behaviour(IdentifyEvent::Received {
|
||||
info,
|
||||
..
|
||||
@ -547,7 +556,8 @@ mod tests {
|
||||
let protocol = Identify::new(
|
||||
IdentifyConfig::new("a".to_string(), pubkey.clone())
|
||||
// Delay identification requests so we can test the push protocol.
|
||||
.with_initial_delay(Duration::from_secs(u32::MAX as u64)));
|
||||
.with_initial_delay(Duration::from_secs(u32::MAX as u64)),
|
||||
);
|
||||
let swarm = Swarm::new(transport, protocol, pubkey.to_peer_id());
|
||||
(swarm, pubkey)
|
||||
};
|
||||
@ -558,7 +568,8 @@ mod tests {
|
||||
IdentifyConfig::new("a".to_string(), pubkey.clone())
|
||||
.with_agent_version("b".to_string())
|
||||
// Delay identification requests so we can test the push protocol.
|
||||
.with_initial_delay(Duration::from_secs(u32::MAX as u64)));
|
||||
.with_initial_delay(Duration::from_secs(u32::MAX as u64)),
|
||||
);
|
||||
let swarm = Swarm::new(transport, protocol, pubkey.to_peer_id());
|
||||
(swarm, pubkey)
|
||||
};
|
||||
@ -586,10 +597,15 @@ mod tests {
|
||||
{
|
||||
pin_mut!(swarm1_fut);
|
||||
pin_mut!(swarm2_fut);
|
||||
match future::select(swarm1_fut, swarm2_fut).await.factor_second().0 {
|
||||
future::Either::Left(SwarmEvent::Behaviour(
|
||||
IdentifyEvent::Received { info, .. }
|
||||
)) => {
|
||||
match future::select(swarm1_fut, swarm2_fut)
|
||||
.await
|
||||
.factor_second()
|
||||
.0
|
||||
{
|
||||
future::Either::Left(SwarmEvent::Behaviour(IdentifyEvent::Received {
|
||||
info,
|
||||
..
|
||||
})) => {
|
||||
assert_eq!(info.public_key, pubkey2);
|
||||
assert_eq!(info.protocol_version, "a");
|
||||
assert_eq!(info.agent_version, "b");
|
||||
@ -601,11 +617,13 @@ mod tests {
|
||||
// Once a connection is established, we can initiate an
|
||||
// active push below.
|
||||
}
|
||||
_ => { continue }
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
|
||||
swarm2.behaviour_mut().push(std::iter::once(pubkey1.to_peer_id()));
|
||||
swarm2
|
||||
.behaviour_mut()
|
||||
.push(std::iter::once(pubkey1.to_peer_id()));
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -47,4 +47,3 @@ mod protocol;
|
||||
mod structs_proto {
|
||||
include!(concat!(env!("OUT_DIR"), "/structs.rs"));
|
||||
}
|
||||
|
||||
|
@ -21,9 +21,8 @@
|
||||
use crate::structs_proto;
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::{
|
||||
Multiaddr,
|
||||
PublicKey,
|
||||
upgrade::{self, InboundUpgrade, OutboundUpgrade, UpgradeInfo}
|
||||
upgrade::{self, InboundUpgrade, OutboundUpgrade, UpgradeInfo},
|
||||
Multiaddr, PublicKey,
|
||||
};
|
||||
use log::{debug, trace};
|
||||
use prost::Message;
|
||||
@ -84,7 +83,7 @@ impl<T> fmt::Debug for ReplySubstream<T> {
|
||||
|
||||
impl<T> ReplySubstream<T>
|
||||
where
|
||||
T: AsyncWrite + Unpin
|
||||
T: AsyncWrite + Unpin,
|
||||
{
|
||||
/// Sends back the requested information on the substream.
|
||||
///
|
||||
@ -158,17 +157,18 @@ where
|
||||
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;
|
||||
|
||||
fn upgrade_outbound(self, socket: C, _: Self::Info) -> Self::Future {
|
||||
send(socket, self.0.0).boxed()
|
||||
send(socket, self.0 .0).boxed()
|
||||
}
|
||||
}
|
||||
|
||||
async fn send<T>(mut io: T, info: IdentifyInfo) -> io::Result<()>
|
||||
where
|
||||
T: AsyncWrite + Unpin
|
||||
T: AsyncWrite + Unpin,
|
||||
{
|
||||
trace!("Sending: {:?}", info);
|
||||
|
||||
let listen_addrs = info.listen_addrs
|
||||
let listen_addrs = info
|
||||
.listen_addrs
|
||||
.into_iter()
|
||||
.map(|addr| addr.to_vec())
|
||||
.collect();
|
||||
@ -181,11 +181,13 @@ where
|
||||
public_key: Some(pubkey_bytes),
|
||||
listen_addrs,
|
||||
observed_addr: Some(info.observed_addr.to_vec()),
|
||||
protocols: info.protocols
|
||||
protocols: info.protocols,
|
||||
};
|
||||
|
||||
let mut bytes = Vec::with_capacity(message.encoded_len());
|
||||
message.encode(&mut bytes).expect("Vec<u8> provides capacity as needed");
|
||||
message
|
||||
.encode(&mut bytes)
|
||||
.expect("Vec<u8> provides capacity as needed");
|
||||
|
||||
upgrade::write_length_prefixed(&mut io, bytes).await?;
|
||||
io.close().await?;
|
||||
@ -195,7 +197,7 @@ where
|
||||
|
||||
async fn recv<T>(mut socket: T) -> io::Result<IdentifyInfo>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite + Unpin
|
||||
T: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
socket.close().await?;
|
||||
|
||||
@ -207,7 +209,7 @@ where
|
||||
Ok(v) => v,
|
||||
Err(err) => {
|
||||
debug!("Invalid message: {:?}", err);
|
||||
return Err(err)
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
|
||||
@ -255,14 +257,14 @@ fn parse_proto_msg(msg: impl AsRef<[u8]>) -> Result<IdentifyInfo, io::Error> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use libp2p_tcp::TcpConfig;
|
||||
use futures::{prelude::*, channel::oneshot};
|
||||
use super::*;
|
||||
use futures::{channel::oneshot, prelude::*};
|
||||
use libp2p_core::{
|
||||
identity,
|
||||
upgrade::{self, apply_inbound, apply_outbound},
|
||||
Transport,
|
||||
upgrade::{self, apply_outbound, apply_inbound}
|
||||
};
|
||||
use super::*;
|
||||
use libp2p_tcp::TcpConfig;
|
||||
|
||||
#[test]
|
||||
fn correct_transfer() {
|
||||
@ -280,7 +282,9 @@ mod tests {
|
||||
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
||||
.unwrap();
|
||||
|
||||
let addr = listener.next().await
|
||||
let addr = listener
|
||||
.next()
|
||||
.await
|
||||
.expect("some event")
|
||||
.expect("no error")
|
||||
.into_new_address()
|
||||
@ -288,14 +292,20 @@ mod tests {
|
||||
tx.send(addr).unwrap();
|
||||
|
||||
let socket = listener
|
||||
.next().await.unwrap().unwrap()
|
||||
.into_upgrade().unwrap()
|
||||
.0.await.unwrap();
|
||||
.next()
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.into_upgrade()
|
||||
.unwrap()
|
||||
.0
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let sender = apply_inbound(socket, IdentifyProtocol).await.unwrap();
|
||||
|
||||
sender.send(
|
||||
IdentifyInfo {
|
||||
sender
|
||||
.send(IdentifyInfo {
|
||||
public_key: send_pubkey,
|
||||
protocol_version: "proto_version".to_owned(),
|
||||
agent_version: "agent_version".to_owned(),
|
||||
@ -305,27 +315,36 @@ mod tests {
|
||||
],
|
||||
protocols: vec!["proto1".to_string(), "proto2".to_string()],
|
||||
observed_addr: "/ip4/100.101.102.103/tcp/5000".parse().unwrap(),
|
||||
},
|
||||
).await.unwrap();
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
});
|
||||
|
||||
async_std::task::block_on(async move {
|
||||
let transport = TcpConfig::new();
|
||||
|
||||
let socket = transport.dial(rx.await.unwrap()).unwrap().await.unwrap();
|
||||
let info = apply_outbound(
|
||||
socket,
|
||||
IdentifyProtocol,
|
||||
upgrade::Version::V1
|
||||
).await.unwrap();
|
||||
assert_eq!(info.observed_addr, "/ip4/100.101.102.103/tcp/5000".parse().unwrap());
|
||||
let info = apply_outbound(socket, IdentifyProtocol, upgrade::Version::V1)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
info.observed_addr,
|
||||
"/ip4/100.101.102.103/tcp/5000".parse().unwrap()
|
||||
);
|
||||
assert_eq!(info.public_key, recv_pubkey);
|
||||
assert_eq!(info.protocol_version, "proto_version");
|
||||
assert_eq!(info.agent_version, "agent_version");
|
||||
assert_eq!(info.listen_addrs,
|
||||
&["/ip4/80.81.82.83/tcp/500".parse().unwrap(),
|
||||
"/ip6/::1/udp/1000".parse().unwrap()]);
|
||||
assert_eq!(info.protocols, &["proto1".to_string(), "proto2".to_string()]);
|
||||
assert_eq!(
|
||||
info.listen_addrs,
|
||||
&[
|
||||
"/ip4/80.81.82.83/tcp/500".parse().unwrap(),
|
||||
"/ip6/::1/udp/1000".parse().unwrap()
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
info.protocols,
|
||||
&["proto1".to_string(), "proto2".to_string()]
|
||||
);
|
||||
|
||||
bg_task.await;
|
||||
});
|
||||
|
Reference in New Issue
Block a user