mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 20:31:13 +00:00
* Revert "chore: update libp2p to 0.52.1 (#14429)"
This reverts commit 59d8b86450.
* Fix dependencies
* Update dependencies
* Update Cargo.lock
This commit is contained in:
@@ -30,8 +30,8 @@ use crate::{
|
||||
use bytes::Bytes;
|
||||
use futures::channel::oneshot;
|
||||
use libp2p::{
|
||||
connection_limits::ConnectionLimits, core::Multiaddr, identify::Info as IdentifyInfo,
|
||||
identity::PublicKey, kad::RecordKey, swarm::NetworkBehaviour, PeerId,
|
||||
core::Multiaddr, identify::Info as IdentifyInfo, identity::PublicKey, kad::RecordKey,
|
||||
swarm::NetworkBehaviour, PeerId,
|
||||
};
|
||||
|
||||
use parking_lot::Mutex;
|
||||
@@ -43,7 +43,7 @@ pub use crate::request_responses::{InboundFailure, OutboundFailure, RequestId, R
|
||||
|
||||
/// General behaviour of the network. Combines all protocols together.
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(to_swarm = "BehaviourOut")]
|
||||
#[behaviour(out_event = "BehaviourOut")]
|
||||
pub struct Behaviour<B: BlockT> {
|
||||
/// All the substrate-specific protocols.
|
||||
substrate: Protocol<B>,
|
||||
@@ -52,8 +52,6 @@ pub struct Behaviour<B: BlockT> {
|
||||
peer_info: peer_info::PeerInfoBehaviour,
|
||||
/// Discovers nodes of the network.
|
||||
discovery: DiscoveryBehaviour,
|
||||
/// Connection limits.
|
||||
connection_limits: libp2p::connection_limits::Behaviour,
|
||||
/// Generic request-response protocols.
|
||||
request_responses: request_responses::RequestResponsesBehaviour,
|
||||
}
|
||||
@@ -174,7 +172,6 @@ impl<B: BlockT> Behaviour<B> {
|
||||
disco_config: DiscoveryConfig,
|
||||
request_response_protocols: Vec<ProtocolConfig>,
|
||||
peer_store_handle: PeerStoreHandle,
|
||||
connection_limits: ConnectionLimits,
|
||||
external_addresses: Arc<Mutex<HashSet<Multiaddr>>>,
|
||||
) -> Result<Self, request_responses::RegisterError> {
|
||||
Ok(Self {
|
||||
@@ -185,7 +182,6 @@ impl<B: BlockT> Behaviour<B> {
|
||||
external_addresses,
|
||||
),
|
||||
discovery: disco_config.finish(),
|
||||
connection_limits: libp2p::connection_limits::Behaviour::new(connection_limits),
|
||||
request_responses: request_responses::RequestResponsesBehaviour::new(
|
||||
request_response_protocols.into_iter(),
|
||||
Box::new(peer_store_handle),
|
||||
@@ -257,7 +253,7 @@ impl<B: BlockT> Behaviour<B> {
|
||||
pub fn add_self_reported_address_to_dht(
|
||||
&mut self,
|
||||
peer_id: &PeerId,
|
||||
supported_protocols: &[impl AsRef<str>],
|
||||
supported_protocols: &[impl AsRef<[u8]>],
|
||||
addr: Multiaddr,
|
||||
) {
|
||||
self.discovery.add_self_reported_address(peer_id, supported_protocols, addr);
|
||||
@@ -361,9 +357,3 @@ impl From<DiscoveryOut> for BehaviourOut {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<void::Void> for BehaviourOut {
|
||||
fn from(e: void::Void) -> Self {
|
||||
void::unreachable(e)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +110,8 @@ pub fn parse_str_addr(addr_str: &str) -> Result<(PeerId, Multiaddr), ParseErr> {
|
||||
/// Splits a Multiaddress into a Multiaddress and PeerId.
|
||||
pub fn parse_addr(mut addr: Multiaddr) -> Result<(PeerId, Multiaddr), ParseErr> {
|
||||
let who = match addr.pop() {
|
||||
Some(multiaddr::Protocol::P2p(peer_id)) => peer_id,
|
||||
Some(multiaddr::Protocol::P2p(key)) =>
|
||||
PeerId::from_multihash(key).map_err(|_| ParseErr::InvalidPeerId)?,
|
||||
_ => return Err(ParseErr::PeerIdMissing),
|
||||
};
|
||||
|
||||
@@ -143,7 +144,7 @@ pub struct MultiaddrWithPeerId {
|
||||
impl MultiaddrWithPeerId {
|
||||
/// Concatenates the multiaddress and peer ID into one multiaddress containing both.
|
||||
pub fn concat(&self) -> Multiaddr {
|
||||
let proto = multiaddr::Protocol::P2p(self.peer_id);
|
||||
let proto = multiaddr::Protocol::P2p(From::from(self.peer_id));
|
||||
self.multiaddr.clone().with(proto)
|
||||
}
|
||||
}
|
||||
@@ -181,6 +182,8 @@ impl TryFrom<String> for MultiaddrWithPeerId {
|
||||
pub enum ParseErr {
|
||||
/// Error while parsing the multiaddress.
|
||||
MultiaddrParse(multiaddr::Error),
|
||||
/// Multihash of the peer ID is invalid.
|
||||
InvalidPeerId,
|
||||
/// The peer ID is missing from the address.
|
||||
PeerIdMissing,
|
||||
}
|
||||
@@ -189,6 +192,7 @@ impl fmt::Display for ParseErr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::MultiaddrParse(err) => write!(f, "{}", err),
|
||||
Self::InvalidPeerId => write!(f, "Peer id at the end of the address is invalid"),
|
||||
Self::PeerIdMissing => write!(f, "Peer id is missing from the address"),
|
||||
}
|
||||
}
|
||||
@@ -198,6 +202,7 @@ impl std::error::Error for ParseErr {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Self::MultiaddrParse(err) => Some(err),
|
||||
Self::InvalidPeerId => None,
|
||||
Self::PeerIdMissing => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ use ip_network::IpNetwork;
|
||||
use libp2p::{
|
||||
core::{Endpoint, Multiaddr},
|
||||
kad::{
|
||||
self,
|
||||
handler::KademliaHandler,
|
||||
record::store::{MemoryStore, RecordStore},
|
||||
GetClosestPeersError, GetRecordOk, Kademlia, KademliaBucketInserts, KademliaConfig,
|
||||
KademliaEvent, QueryId, QueryResult, Quorum, Record, RecordKey,
|
||||
@@ -65,10 +65,10 @@ use libp2p::{
|
||||
swarm::{
|
||||
behaviour::{
|
||||
toggle::{Toggle, ToggleConnectionHandler},
|
||||
DialFailure, ExternalAddrConfirmed, FromSwarm,
|
||||
DialFailure, FromSwarm, NewExternalAddr,
|
||||
},
|
||||
ConnectionDenied, ConnectionId, DialError, NetworkBehaviour, PollParameters,
|
||||
StreamProtocol, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
|
||||
ConnectionDenied, ConnectionId, DialError, NetworkBehaviour, PollParameters, THandler,
|
||||
THandlerInEvent, THandlerOutEvent, ToSwarm,
|
||||
},
|
||||
PeerId,
|
||||
};
|
||||
@@ -104,7 +104,7 @@ pub struct DiscoveryConfig {
|
||||
discovery_only_if_under_num: u64,
|
||||
enable_mdns: bool,
|
||||
kademlia_disjoint_query_paths: bool,
|
||||
kademlia_protocols: Vec<StreamProtocol>,
|
||||
kademlia_protocols: Vec<Vec<u8>>,
|
||||
kademlia_replication_factor: NonZeroUsize,
|
||||
}
|
||||
|
||||
@@ -223,9 +223,6 @@ impl DiscoveryConfig {
|
||||
|
||||
let store = MemoryStore::new(local_peer_id);
|
||||
let mut kad = Kademlia::with_config(local_peer_id, store, config);
|
||||
// Always set the mode to server, so that any node can accept incoming Kademlia
|
||||
// requests. Otherwise, the connectivity degrades significantly.
|
||||
kad.set_mode(Some(kad::Mode::Server));
|
||||
|
||||
for (peer_id, addr) in &permanent_addresses {
|
||||
kad.add_address(peer_id, addr.clone());
|
||||
@@ -356,7 +353,7 @@ impl DiscoveryBehaviour {
|
||||
pub fn add_self_reported_address(
|
||||
&mut self,
|
||||
peer_id: &PeerId,
|
||||
supported_protocols: &[impl AsRef<str>],
|
||||
supported_protocols: &[impl AsRef<[u8]>],
|
||||
addr: Multiaddr,
|
||||
) {
|
||||
if let Some(kademlia) = self.kademlia.as_mut() {
|
||||
@@ -375,7 +372,7 @@ impl DiscoveryBehaviour {
|
||||
trace!(
|
||||
target: "sub-libp2p",
|
||||
"Adding self-reported address {} from {} to Kademlia DHT {}.",
|
||||
addr, peer_id, matching_protocol.as_ref(),
|
||||
addr, peer_id, String::from_utf8_lossy(matching_protocol.as_ref()),
|
||||
);
|
||||
kademlia.add_address(peer_id, addr.clone());
|
||||
} else {
|
||||
@@ -501,9 +498,8 @@ pub enum DiscoveryOut {
|
||||
}
|
||||
|
||||
impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
type ConnectionHandler =
|
||||
ToggleConnectionHandler<<Kademlia<MemoryStore> as NetworkBehaviour>::ConnectionHandler>;
|
||||
type ToSwarm = DiscoveryOut;
|
||||
type ConnectionHandler = ToggleConnectionHandler<KademliaHandler<QueryId>>;
|
||||
type OutEvent = DiscoveryOut;
|
||||
|
||||
fn handle_established_inbound_connection(
|
||||
&mut self,
|
||||
@@ -631,11 +627,11 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
FromSwarm::ListenerError(e) => {
|
||||
self.kademlia.on_swarm_event(FromSwarm::ListenerError(e));
|
||||
},
|
||||
FromSwarm::ExternalAddrExpired(e) => {
|
||||
FromSwarm::ExpiredExternalAddr(e) => {
|
||||
// We intentionally don't remove the element from `known_external_addresses` in
|
||||
// order to not print the log line again.
|
||||
|
||||
self.kademlia.on_swarm_event(FromSwarm::ExternalAddrExpired(e));
|
||||
self.kademlia.on_swarm_event(FromSwarm::ExpiredExternalAddr(e));
|
||||
},
|
||||
FromSwarm::NewListener(e) => {
|
||||
self.kademlia.on_swarm_event(FromSwarm::NewListener(e));
|
||||
@@ -643,19 +639,8 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
FromSwarm::ExpiredListenAddr(e) => {
|
||||
self.kademlia.on_swarm_event(FromSwarm::ExpiredListenAddr(e));
|
||||
},
|
||||
FromSwarm::NewExternalAddrCandidate(e) => {
|
||||
self.kademlia.on_swarm_event(FromSwarm::NewExternalAddrCandidate(e));
|
||||
},
|
||||
FromSwarm::AddressChange(e) => {
|
||||
self.kademlia.on_swarm_event(FromSwarm::AddressChange(e));
|
||||
},
|
||||
FromSwarm::NewListenAddr(e) => {
|
||||
self.kademlia.on_swarm_event(FromSwarm::NewListenAddr(e));
|
||||
|
||||
self.mdns.on_swarm_event(FromSwarm::NewListenAddr(e));
|
||||
},
|
||||
FromSwarm::ExternalAddrConfirmed(e @ ExternalAddrConfirmed { addr }) => {
|
||||
let new_addr = addr.clone().with(Protocol::P2p(self.local_peer_id));
|
||||
FromSwarm::NewExternalAddr(e @ NewExternalAddr { addr }) => {
|
||||
let new_addr = addr.clone().with(Protocol::P2p(self.local_peer_id.into()));
|
||||
|
||||
if Self::can_add_to_dht(addr) {
|
||||
// NOTE: we might re-discover the same address multiple times
|
||||
@@ -669,7 +654,14 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
self.kademlia.on_swarm_event(FromSwarm::ExternalAddrConfirmed(e));
|
||||
self.kademlia.on_swarm_event(FromSwarm::NewExternalAddr(e));
|
||||
},
|
||||
FromSwarm::AddressChange(e) => {
|
||||
self.kademlia.on_swarm_event(FromSwarm::AddressChange(e));
|
||||
},
|
||||
FromSwarm::NewListenAddr(e) => {
|
||||
self.kademlia.on_swarm_event(FromSwarm::NewListenAddr(e));
|
||||
self.mdns.on_swarm_event(FromSwarm::NewListenAddr(e));
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -687,7 +679,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
// Immediately process the content of `discovered`.
|
||||
if let Some(ev) = self.pending_events.pop_front() {
|
||||
return Poll::Ready(ToSwarm::GenerateEvent(ev))
|
||||
@@ -898,17 +890,10 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
ToSwarm::Dial { opts } => return Poll::Ready(ToSwarm::Dial { opts }),
|
||||
ToSwarm::NotifyHandler { peer_id, handler, event } =>
|
||||
return Poll::Ready(ToSwarm::NotifyHandler { peer_id, handler, event }),
|
||||
ToSwarm::ReportObservedAddr { address, score } =>
|
||||
return Poll::Ready(ToSwarm::ReportObservedAddr { address, score }),
|
||||
ToSwarm::CloseConnection { peer_id, connection } =>
|
||||
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }),
|
||||
ToSwarm::NewExternalAddrCandidate(observed) =>
|
||||
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)),
|
||||
ToSwarm::ExternalAddrConfirmed(addr) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)),
|
||||
ToSwarm::ExternalAddrExpired(addr) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)),
|
||||
ToSwarm::ListenOn { opts } => return Poll::Ready(ToSwarm::ListenOn { opts }),
|
||||
ToSwarm::RemoveListener { id } =>
|
||||
return Poll::Ready(ToSwarm::RemoveListener { id }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -921,9 +906,8 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
continue
|
||||
}
|
||||
|
||||
self.pending_events.extend(
|
||||
list.into_iter().map(|(peer_id, _)| DiscoveryOut::Discovered(peer_id)),
|
||||
);
|
||||
self.pending_events
|
||||
.extend(list.map(|(peer_id, _)| DiscoveryOut::Discovered(peer_id)));
|
||||
if let Some(ev) = self.pending_events.pop_front() {
|
||||
return Poll::Ready(ToSwarm::GenerateEvent(ev))
|
||||
}
|
||||
@@ -936,17 +920,10 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
ToSwarm::NotifyHandler { event, .. } => match event {}, /* `event` is an */
|
||||
// enum with no
|
||||
// variant
|
||||
ToSwarm::ReportObservedAddr { address, score } =>
|
||||
return Poll::Ready(ToSwarm::ReportObservedAddr { address, score }),
|
||||
ToSwarm::CloseConnection { peer_id, connection } =>
|
||||
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }),
|
||||
ToSwarm::NewExternalAddrCandidate(observed) =>
|
||||
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)),
|
||||
ToSwarm::ExternalAddrConfirmed(addr) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)),
|
||||
ToSwarm::ExternalAddrExpired(addr) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)),
|
||||
ToSwarm::ListenOn { opts } => return Poll::Ready(ToSwarm::ListenOn { opts }),
|
||||
ToSwarm::RemoveListener { id } =>
|
||||
return Poll::Ready(ToSwarm::RemoveListener { id }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -955,23 +932,21 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
}
|
||||
|
||||
/// Legacy (fallback) Kademlia protocol name based on `protocol_id`.
|
||||
fn legacy_kademlia_protocol_name(id: &ProtocolId) -> StreamProtocol {
|
||||
let name = format!("/{}/kad", id.as_ref());
|
||||
StreamProtocol::try_from_owned(name).expect("protocol name is valid. qed")
|
||||
fn legacy_kademlia_protocol_name(id: &ProtocolId) -> Vec<u8> {
|
||||
let mut v = vec![b'/'];
|
||||
v.extend_from_slice(id.as_ref().as_bytes());
|
||||
v.extend_from_slice(b"/kad");
|
||||
v
|
||||
}
|
||||
|
||||
/// Kademlia protocol name based on `genesis_hash` and `fork_id`.
|
||||
fn kademlia_protocol_name<Hash: AsRef<[u8]>>(
|
||||
genesis_hash: Hash,
|
||||
fork_id: Option<&str>,
|
||||
) -> StreamProtocol {
|
||||
fn kademlia_protocol_name<Hash: AsRef<[u8]>>(genesis_hash: Hash, fork_id: Option<&str>) -> Vec<u8> {
|
||||
let genesis_hash_hex = bytes2hex("", genesis_hash.as_ref());
|
||||
let name = if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}/kad", genesis_hash_hex, fork_id)
|
||||
if let Some(fork_id) = fork_id {
|
||||
format!("/{}/{}/kad", genesis_hash_hex, fork_id).as_bytes().into()
|
||||
} else {
|
||||
format!("/{}/kad", genesis_hash_hex)
|
||||
};
|
||||
StreamProtocol::try_from_owned(name).expect("protocol name is valid. qed")
|
||||
format!("/{}/kad", genesis_hash_hex).as_bytes().into()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -1102,7 +1077,7 @@ mod tests {
|
||||
.add_self_reported_address(
|
||||
&other,
|
||||
&[protocol_name],
|
||||
addr.clone(),
|
||||
addr,
|
||||
);
|
||||
|
||||
to_discover[swarm_n].remove(&other);
|
||||
|
||||
@@ -31,13 +31,13 @@ use libp2p::{
|
||||
Info as IdentifyInfo,
|
||||
},
|
||||
identity::PublicKey,
|
||||
ping::{Behaviour as Ping, Config as PingConfig, Event as PingEvent},
|
||||
ping::{Behaviour as Ping, Config as PingConfig, Event as PingEvent, Success as PingSuccess},
|
||||
swarm::{
|
||||
behaviour::{
|
||||
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm,
|
||||
ListenFailure,
|
||||
},
|
||||
ConnectionDenied, ConnectionHandler, ConnectionHandlerSelect, ConnectionId,
|
||||
ConnectionDenied, ConnectionHandler, ConnectionId, IntoConnectionHandlerSelect,
|
||||
NetworkBehaviour, PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm,
|
||||
},
|
||||
Multiaddr, PeerId,
|
||||
@@ -148,18 +148,13 @@ impl PeerInfoBehaviour {
|
||||
|
||||
/// Inserts a ping time in the cache. Has no effect if we don't have any entry for that node,
|
||||
/// which shouldn't happen.
|
||||
fn handle_ping_report(
|
||||
&mut self,
|
||||
peer_id: &PeerId,
|
||||
ping_time: Duration,
|
||||
connection: ConnectionId,
|
||||
) {
|
||||
trace!(target: "sub-libp2p", "Ping time with {:?} via {:?}: {:?}", peer_id, connection, ping_time);
|
||||
fn handle_ping_report(&mut self, peer_id: &PeerId, ping_time: Duration) {
|
||||
trace!(target: "sub-libp2p", "Ping time with {:?}: {:?}", peer_id, ping_time);
|
||||
if let Some(entry) = self.nodes_info.get_mut(peer_id) {
|
||||
entry.latest_ping = Some(ping_time);
|
||||
} else {
|
||||
error!(target: "sub-libp2p",
|
||||
"Received ping from node we're not connected to {:?} via {:?}", peer_id, connection);
|
||||
"Received ping from node we're not connected to {:?}", peer_id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,11 +208,11 @@ pub enum PeerInfoEvent {
|
||||
}
|
||||
|
||||
impl NetworkBehaviour for PeerInfoBehaviour {
|
||||
type ConnectionHandler = ConnectionHandlerSelect<
|
||||
type ConnectionHandler = IntoConnectionHandlerSelect<
|
||||
<Ping as NetworkBehaviour>::ConnectionHandler,
|
||||
<Identify as NetworkBehaviour>::ConnectionHandler,
|
||||
>;
|
||||
type ToSwarm = PeerInfoEvent;
|
||||
type OutEvent = PeerInfoEvent;
|
||||
|
||||
fn handle_pending_inbound_connection(
|
||||
&mut self,
|
||||
@@ -383,9 +378,9 @@ impl NetworkBehaviour for PeerInfoBehaviour {
|
||||
self.ping.on_swarm_event(FromSwarm::ListenerError(e));
|
||||
self.identify.on_swarm_event(FromSwarm::ListenerError(e));
|
||||
},
|
||||
FromSwarm::ExternalAddrExpired(e) => {
|
||||
self.ping.on_swarm_event(FromSwarm::ExternalAddrExpired(e));
|
||||
self.identify.on_swarm_event(FromSwarm::ExternalAddrExpired(e));
|
||||
FromSwarm::ExpiredExternalAddr(e) => {
|
||||
self.ping.on_swarm_event(FromSwarm::ExpiredExternalAddr(e));
|
||||
self.identify.on_swarm_event(FromSwarm::ExpiredExternalAddr(e));
|
||||
},
|
||||
FromSwarm::NewListener(e) => {
|
||||
self.ping.on_swarm_event(FromSwarm::NewListener(e));
|
||||
@@ -396,13 +391,9 @@ impl NetworkBehaviour for PeerInfoBehaviour {
|
||||
self.identify.on_swarm_event(FromSwarm::ExpiredListenAddr(e));
|
||||
self.external_addresses.remove(e.addr);
|
||||
},
|
||||
FromSwarm::NewExternalAddrCandidate(e) => {
|
||||
self.ping.on_swarm_event(FromSwarm::NewExternalAddrCandidate(e));
|
||||
self.identify.on_swarm_event(FromSwarm::NewExternalAddrCandidate(e));
|
||||
},
|
||||
FromSwarm::ExternalAddrConfirmed(e) => {
|
||||
self.ping.on_swarm_event(FromSwarm::ExternalAddrConfirmed(e));
|
||||
self.identify.on_swarm_event(FromSwarm::ExternalAddrConfirmed(e));
|
||||
FromSwarm::NewExternalAddr(e) => {
|
||||
self.ping.on_swarm_event(FromSwarm::NewExternalAddr(e));
|
||||
self.identify.on_swarm_event(FromSwarm::NewExternalAddr(e));
|
||||
self.external_addresses.add(e.addr.clone());
|
||||
},
|
||||
FromSwarm::AddressChange(e @ AddressChange { peer_id, old, new, .. }) => {
|
||||
@@ -446,13 +437,13 @@ impl NetworkBehaviour for PeerInfoBehaviour {
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
loop {
|
||||
match self.ping.poll(cx, params) {
|
||||
Poll::Pending => break,
|
||||
Poll::Ready(ToSwarm::GenerateEvent(ev)) => {
|
||||
if let PingEvent { peer, result: Ok(rtt), connection } = ev {
|
||||
self.handle_ping_report(&peer, rtt, connection)
|
||||
if let PingEvent { peer, result: Ok(PingSuccess::Ping { rtt }) } = ev {
|
||||
self.handle_ping_report(&peer, rtt)
|
||||
}
|
||||
},
|
||||
Poll::Ready(ToSwarm::Dial { opts }) => return Poll::Ready(ToSwarm::Dial { opts }),
|
||||
@@ -462,18 +453,10 @@ impl NetworkBehaviour for PeerInfoBehaviour {
|
||||
handler,
|
||||
event: Either::Left(event),
|
||||
}),
|
||||
Poll::Ready(ToSwarm::ReportObservedAddr { address, score }) =>
|
||||
return Poll::Ready(ToSwarm::ReportObservedAddr { address, score }),
|
||||
Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }) =>
|
||||
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }),
|
||||
Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)) =>
|
||||
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)),
|
||||
Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)),
|
||||
Poll::Ready(ToSwarm::ExternalAddrExpired(addr)) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)),
|
||||
Poll::Ready(ToSwarm::ListenOn { opts }) =>
|
||||
return Poll::Ready(ToSwarm::ListenOn { opts }),
|
||||
Poll::Ready(ToSwarm::RemoveListener { id }) =>
|
||||
return Poll::Ready(ToSwarm::RemoveListener { id }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -499,18 +482,10 @@ impl NetworkBehaviour for PeerInfoBehaviour {
|
||||
handler,
|
||||
event: Either::Right(event),
|
||||
}),
|
||||
Poll::Ready(ToSwarm::ReportObservedAddr { address, score }) =>
|
||||
return Poll::Ready(ToSwarm::ReportObservedAddr { address, score }),
|
||||
Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }) =>
|
||||
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }),
|
||||
Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)) =>
|
||||
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)),
|
||||
Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)),
|
||||
Poll::Ready(ToSwarm::ExternalAddrExpired(addr)) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)),
|
||||
Poll::Ready(ToSwarm::ListenOn { opts }) =>
|
||||
return Poll::Ready(ToSwarm::ListenOn { opts }),
|
||||
Poll::Ready(ToSwarm::RemoveListener { id }) =>
|
||||
return Poll::Ready(ToSwarm::RemoveListener { id }),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -212,7 +212,7 @@ pub enum CustomMessageOutcome {
|
||||
|
||||
impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
type ConnectionHandler = <Notifications as NetworkBehaviour>::ConnectionHandler;
|
||||
type ToSwarm = CustomMessageOutcome;
|
||||
type OutEvent = CustomMessageOutcome;
|
||||
|
||||
fn handle_established_inbound_connection(
|
||||
&mut self,
|
||||
@@ -273,7 +273,7 @@ impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
&mut self,
|
||||
cx: &mut std::task::Context,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
while let Poll::Ready(Some(validation_result)) =
|
||||
self.sync_substream_validations.poll_next_unpin(cx)
|
||||
{
|
||||
@@ -297,18 +297,10 @@ impl<B: BlockT> NetworkBehaviour for Protocol<B> {
|
||||
Poll::Ready(ToSwarm::Dial { opts }) => return Poll::Ready(ToSwarm::Dial { opts }),
|
||||
Poll::Ready(ToSwarm::NotifyHandler { peer_id, handler, event }) =>
|
||||
return Poll::Ready(ToSwarm::NotifyHandler { peer_id, handler, event }),
|
||||
Poll::Ready(ToSwarm::ReportObservedAddr { address, score }) =>
|
||||
return Poll::Ready(ToSwarm::ReportObservedAddr { address, score }),
|
||||
Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }) =>
|
||||
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }),
|
||||
Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)) =>
|
||||
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)),
|
||||
Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)),
|
||||
Poll::Ready(ToSwarm::ExternalAddrExpired(addr)) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)),
|
||||
Poll::Ready(ToSwarm::ListenOn { opts }) =>
|
||||
return Poll::Ready(ToSwarm::ListenOn { opts }),
|
||||
Poll::Ready(ToSwarm::RemoveListener { id }) =>
|
||||
return Poll::Ready(ToSwarm::RemoveListener { id }),
|
||||
};
|
||||
|
||||
let outcome = match event {
|
||||
|
||||
@@ -986,7 +986,7 @@ impl Notifications {
|
||||
|
||||
impl NetworkBehaviour for Notifications {
|
||||
type ConnectionHandler = NotifsHandler;
|
||||
type ToSwarm = NotificationsOut;
|
||||
type OutEvent = NotificationsOut;
|
||||
|
||||
fn handle_pending_inbound_connection(
|
||||
&mut self,
|
||||
@@ -1459,11 +1459,10 @@ impl NetworkBehaviour for Notifications {
|
||||
FromSwarm::ListenerClosed(_) => {},
|
||||
FromSwarm::ListenFailure(_) => {},
|
||||
FromSwarm::ListenerError(_) => {},
|
||||
FromSwarm::ExternalAddrExpired(_) => {},
|
||||
FromSwarm::ExpiredExternalAddr(_) => {},
|
||||
FromSwarm::NewListener(_) => {},
|
||||
FromSwarm::ExpiredListenAddr(_) => {},
|
||||
FromSwarm::NewExternalAddrCandidate(_) => {},
|
||||
FromSwarm::ExternalAddrConfirmed(_) => {},
|
||||
FromSwarm::NewExternalAddr(_) => {},
|
||||
FromSwarm::AddressChange(_) => {},
|
||||
FromSwarm::NewListenAddr(_) => {},
|
||||
}
|
||||
@@ -2002,7 +2001,7 @@ impl NetworkBehaviour for Notifications {
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
_params: &mut impl PollParameters,
|
||||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
if let Some(event) = self.events.pop_front() {
|
||||
return Poll::Ready(event)
|
||||
}
|
||||
@@ -2108,6 +2107,7 @@ mod tests {
|
||||
protocol::notifications::handler::tests::*,
|
||||
protocol_controller::{IncomingIndex, ProtoSetConfig, ProtocolController},
|
||||
};
|
||||
use libp2p::swarm::AddressRecord;
|
||||
use sc_utils::mpsc::tracing_unbounded;
|
||||
use std::{collections::HashSet, iter};
|
||||
|
||||
@@ -2127,14 +2127,31 @@ mod tests {
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct MockPollParams {}
|
||||
struct MockPollParams {
|
||||
peer_id: PeerId,
|
||||
addr: Multiaddr,
|
||||
}
|
||||
|
||||
impl PollParameters for MockPollParams {
|
||||
type SupportedProtocolsIter = std::vec::IntoIter<Vec<u8>>;
|
||||
type ListenedAddressesIter = std::vec::IntoIter<Multiaddr>;
|
||||
type ExternalAddressesIter = std::vec::IntoIter<AddressRecord>;
|
||||
|
||||
fn supported_protocols(&self) -> Self::SupportedProtocolsIter {
|
||||
vec![].into_iter()
|
||||
}
|
||||
|
||||
fn listened_addresses(&self) -> Self::ListenedAddressesIter {
|
||||
vec![self.addr.clone()].into_iter()
|
||||
}
|
||||
|
||||
fn external_addresses(&self) -> Self::ExternalAddressesIter {
|
||||
vec![].into_iter()
|
||||
}
|
||||
|
||||
fn local_peer_id(&self) -> &PeerId {
|
||||
&self.peer_id
|
||||
}
|
||||
}
|
||||
|
||||
fn development_notifs() -> (Notifications, ProtocolController) {
|
||||
@@ -2999,7 +3016,7 @@ mod tests {
|
||||
|
||||
notif.on_swarm_event(FromSwarm::DialFailure(libp2p::swarm::behaviour::DialFailure {
|
||||
peer_id: Some(peer),
|
||||
error: &libp2p::swarm::DialError::Aborted,
|
||||
error: &libp2p::swarm::DialError::Banned,
|
||||
connection_id: ConnectionId::new_unchecked(1337),
|
||||
}));
|
||||
|
||||
@@ -3536,7 +3553,7 @@ mod tests {
|
||||
let now = Instant::now();
|
||||
notif.on_swarm_event(FromSwarm::DialFailure(libp2p::swarm::behaviour::DialFailure {
|
||||
peer_id: Some(peer),
|
||||
error: &libp2p::swarm::DialError::Aborted,
|
||||
error: &libp2p::swarm::DialError::Banned,
|
||||
connection_id: ConnectionId::new_unchecked(0),
|
||||
}));
|
||||
|
||||
@@ -3656,7 +3673,7 @@ mod tests {
|
||||
assert!(notif.peers.get(&(peer, set_id)).is_some());
|
||||
|
||||
if tokio::time::timeout(Duration::from_secs(5), async {
|
||||
let mut params = MockPollParams {};
|
||||
let mut params = MockPollParams { peer_id: PeerId::random(), addr: Multiaddr::empty() };
|
||||
|
||||
loop {
|
||||
futures::future::poll_fn(|cx| {
|
||||
@@ -3765,7 +3782,7 @@ mod tests {
|
||||
// verify that the code continues to keep the peer disabled by resetting the timer
|
||||
// after the first one expired.
|
||||
if tokio::time::timeout(Duration::from_secs(5), async {
|
||||
let mut params = MockPollParams {};
|
||||
let mut params = MockPollParams { peer_id: PeerId::random(), addr: Multiaddr::empty() };
|
||||
|
||||
loop {
|
||||
futures::future::poll_fn(|cx| {
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -160,7 +160,7 @@ impl std::ops::DerefMut for CustomProtoWithAddr {
|
||||
|
||||
impl NetworkBehaviour for CustomProtoWithAddr {
|
||||
type ConnectionHandler = <Notifications as NetworkBehaviour>::ConnectionHandler;
|
||||
type ToSwarm = <Notifications as NetworkBehaviour>::ToSwarm;
|
||||
type OutEvent = <Notifications as NetworkBehaviour>::OutEvent;
|
||||
|
||||
fn handle_pending_inbound_connection(
|
||||
&mut self,
|
||||
@@ -238,10 +238,9 @@ impl NetworkBehaviour for CustomProtoWithAddr {
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
let _ = self.peer_store_future.poll_unpin(cx);
|
||||
let _ = self.protocol_controller_future.poll_unpin(cx);
|
||||
|
||||
self.inner.poll(cx, params)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use futures::prelude::*;
|
||||
use libp2p::core::upgrade::{InboundUpgrade, UpgradeInfo};
|
||||
use libp2p::core::upgrade::{InboundUpgrade, ProtocolName, UpgradeInfo};
|
||||
use std::{
|
||||
iter::FromIterator,
|
||||
pin::Pin,
|
||||
@@ -76,9 +76,9 @@ where
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct ProtoNameWithUsize<T>(T, usize);
|
||||
|
||||
impl<T: AsRef<str>> AsRef<str> for ProtoNameWithUsize<T> {
|
||||
fn as_ref(&self) -> &str {
|
||||
self.0.as_ref()
|
||||
impl<T: ProtocolName> ProtocolName for ProtoNameWithUsize<T> {
|
||||
fn protocol_name(&self) -> &[u8] {
|
||||
self.0.protocol_name()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,13 +104,13 @@ impl<T: Future<Output = Result<O, E>>, O, E> Future for FutWithUsize<T> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::types::ProtocolName as ProtoName;
|
||||
use libp2p::core::upgrade::UpgradeInfo;
|
||||
use libp2p::core::upgrade::{ProtocolName, UpgradeInfo};
|
||||
|
||||
// TODO: move to mocks
|
||||
mockall::mock! {
|
||||
pub ProtocolUpgrade<T> {}
|
||||
|
||||
impl<T: Clone + AsRef<str>> UpgradeInfo for ProtocolUpgrade<T> {
|
||||
impl<T: Clone + ProtocolName> UpgradeInfo for ProtocolUpgrade<T> {
|
||||
type Info = T;
|
||||
type InfoIter = vec::IntoIter<T>;
|
||||
fn protocol_info(&self) -> vec::IntoIter<T>;
|
||||
|
||||
@@ -114,6 +114,13 @@ pub struct NotificationsOutSubstream<TSubstream> {
|
||||
socket: Framed<TSubstream, UviBytes<io::Cursor<Vec<u8>>>>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl<TSubstream> NotificationsOutSubstream<TSubstream> {
|
||||
pub fn new(socket: Framed<TSubstream, UviBytes<io::Cursor<Vec<u8>>>>) -> Self {
|
||||
Self { socket }
|
||||
}
|
||||
}
|
||||
|
||||
impl NotificationsIn {
|
||||
/// Builds a new potential upgrade.
|
||||
pub fn new(
|
||||
@@ -196,13 +203,13 @@ impl<TSubstream> NotificationsInSubstream<TSubstream>
|
||||
where
|
||||
TSubstream: AsyncRead + AsyncWrite + Unpin,
|
||||
{
|
||||
// #[cfg(test)]
|
||||
// pub fn new(
|
||||
// socket: Framed<TSubstream, UviBytes<io::Cursor<Vec<u8>>>>,
|
||||
// handshake: NotificationsInSubstreamHandshake,
|
||||
// ) -> Self {
|
||||
// Self { socket, handshake }
|
||||
// }
|
||||
#[cfg(test)]
|
||||
pub fn new(
|
||||
socket: Framed<TSubstream, UviBytes<io::Cursor<Vec<u8>>>>,
|
||||
handshake: NotificationsInSubstreamHandshake,
|
||||
) -> Self {
|
||||
Self { socket, handshake }
|
||||
}
|
||||
|
||||
/// Sends the handshake in order to inform the remote that we accept the substream.
|
||||
pub fn send_handshake(&mut self, message: impl Into<Vec<u8>>) {
|
||||
@@ -491,92 +498,41 @@ pub enum NotificationsOutError {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use futures::channel::oneshot;
|
||||
use libp2p::core::{upgrade, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use super::{NotificationsIn, NotificationsInOpen, NotificationsOut, NotificationsOutOpen};
|
||||
use futures::{channel::oneshot, prelude::*};
|
||||
use libp2p::core::upgrade;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio_util::compat::TokioAsyncReadCompatExt;
|
||||
|
||||
/// Opens a substream to the given address, negotiates the protocol, and returns the substream
|
||||
/// along with the handshake message.
|
||||
async fn dial(
|
||||
addr: std::net::SocketAddr,
|
||||
handshake: impl Into<Vec<u8>>,
|
||||
) -> Result<
|
||||
(
|
||||
Vec<u8>,
|
||||
NotificationsOutSubstream<
|
||||
multistream_select::Negotiated<tokio_util::compat::Compat<TcpStream>>,
|
||||
>,
|
||||
),
|
||||
NotificationsHandshakeError,
|
||||
> {
|
||||
let socket = TcpStream::connect(addr).await.unwrap();
|
||||
let notifs_out = NotificationsOut::new("/test/proto/1", Vec::new(), handshake, 1024 * 1024);
|
||||
let (_, substream) = multistream_select::dialer_select_proto(
|
||||
socket.compat(),
|
||||
notifs_out.protocol_info().into_iter(),
|
||||
upgrade::Version::V1,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let NotificationsOutOpen { handshake, substream, .. } =
|
||||
<NotificationsOut as OutboundUpgrade<_>>::upgrade_outbound(
|
||||
notifs_out,
|
||||
substream,
|
||||
"/test/proto/1".into(),
|
||||
)
|
||||
.await?;
|
||||
Ok((handshake, substream))
|
||||
}
|
||||
|
||||
/// Listens on a localhost, negotiates the protocol, and returns the substream along with the
|
||||
/// handshake message.
|
||||
///
|
||||
/// Also sends the listener address through the given channel.
|
||||
async fn listen_on_localhost(
|
||||
listener_addr_tx: oneshot::Sender<std::net::SocketAddr>,
|
||||
) -> Result<
|
||||
(
|
||||
Vec<u8>,
|
||||
NotificationsInSubstream<
|
||||
multistream_select::Negotiated<tokio_util::compat::Compat<TcpStream>>,
|
||||
>,
|
||||
),
|
||||
NotificationsHandshakeError,
|
||||
> {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
|
||||
|
||||
let (socket, _) = listener.accept().await.unwrap();
|
||||
let notifs_in = NotificationsIn::new("/test/proto/1", Vec::new(), 1024 * 1024);
|
||||
let (_, substream) =
|
||||
multistream_select::listener_select_proto(socket.compat(), notifs_in.protocol_info())
|
||||
.await
|
||||
.unwrap();
|
||||
let NotificationsInOpen { handshake, substream, .. } =
|
||||
<NotificationsIn as InboundUpgrade<_>>::upgrade_inbound(
|
||||
notifs_in,
|
||||
substream,
|
||||
"/test/proto/1".into(),
|
||||
)
|
||||
.await?;
|
||||
Ok((handshake, substream))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn basic_works() {
|
||||
const PROTO_NAME: &str = "/test/proto/1";
|
||||
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
|
||||
|
||||
let client = tokio::spawn(async move {
|
||||
let (handshake, mut substream) =
|
||||
dial(listener_addr_rx.await.unwrap(), &b"initial message"[..]).await.unwrap();
|
||||
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
|
||||
let NotificationsOutOpen { handshake, mut substream, .. } = upgrade::apply_outbound(
|
||||
socket.compat(),
|
||||
NotificationsOut::new(PROTO_NAME, Vec::new(), &b"initial message"[..], 1024 * 1024),
|
||||
upgrade::Version::V1,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(handshake, b"hello world");
|
||||
substream.send(b"test message".to_vec()).await.unwrap();
|
||||
});
|
||||
|
||||
let (handshake, mut substream) = listen_on_localhost(listener_addr_tx).await.unwrap();
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
|
||||
|
||||
let (socket, _) = listener.accept().await.unwrap();
|
||||
let NotificationsInOpen { handshake, mut substream, .. } = upgrade::apply_inbound(
|
||||
socket.compat(),
|
||||
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(handshake, b"initial message");
|
||||
substream.send_handshake(&b"hello world"[..]);
|
||||
@@ -591,17 +547,33 @@ mod tests {
|
||||
async fn empty_handshake() {
|
||||
// Check that everything still works when the handshake messages are empty.
|
||||
|
||||
const PROTO_NAME: &str = "/test/proto/1";
|
||||
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
|
||||
|
||||
let client = tokio::spawn(async move {
|
||||
let (handshake, mut substream) =
|
||||
dial(listener_addr_rx.await.unwrap(), vec![]).await.unwrap();
|
||||
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
|
||||
let NotificationsOutOpen { handshake, mut substream, .. } = upgrade::apply_outbound(
|
||||
socket.compat(),
|
||||
NotificationsOut::new(PROTO_NAME, Vec::new(), vec![], 1024 * 1024),
|
||||
upgrade::Version::V1,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(handshake.is_empty());
|
||||
substream.send(Default::default()).await.unwrap();
|
||||
});
|
||||
|
||||
let (handshake, mut substream) = listen_on_localhost(listener_addr_tx).await.unwrap();
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
|
||||
|
||||
let (socket, _) = listener.accept().await.unwrap();
|
||||
let NotificationsInOpen { handshake, mut substream, .. } = upgrade::apply_inbound(
|
||||
socket.compat(),
|
||||
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(handshake.is_empty());
|
||||
substream.send_handshake(vec![]);
|
||||
@@ -614,10 +586,17 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn refused() {
|
||||
const PROTO_NAME: &str = "/test/proto/1";
|
||||
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
|
||||
|
||||
let client = tokio::spawn(async move {
|
||||
let outcome = dial(listener_addr_rx.await.unwrap(), &b"hello"[..]).await;
|
||||
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
|
||||
let outcome = upgrade::apply_outbound(
|
||||
socket.compat(),
|
||||
NotificationsOut::new(PROTO_NAME, Vec::new(), &b"hello"[..], 1024 * 1024),
|
||||
upgrade::Version::V1,
|
||||
)
|
||||
.await;
|
||||
|
||||
// Despite the protocol negotiation being successfully conducted on the listener
|
||||
// side, we have to receive an error here because the listener didn't send the
|
||||
@@ -625,7 +604,16 @@ mod tests {
|
||||
assert!(outcome.is_err());
|
||||
});
|
||||
|
||||
let (handshake, substream) = listen_on_localhost(listener_addr_tx).await.unwrap();
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
|
||||
|
||||
let (socket, _) = listener.accept().await.unwrap();
|
||||
let NotificationsInOpen { handshake, substream, .. } = upgrade::apply_inbound(
|
||||
socket.compat(),
|
||||
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(handshake, b"hello");
|
||||
|
||||
@@ -637,16 +625,35 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn large_initial_message_refused() {
|
||||
const PROTO_NAME: &str = "/test/proto/1";
|
||||
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
|
||||
|
||||
let client = tokio::spawn(async move {
|
||||
let ret =
|
||||
dial(listener_addr_rx.await.unwrap(), (0..32768).map(|_| 0).collect::<Vec<_>>())
|
||||
.await;
|
||||
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
|
||||
let ret = upgrade::apply_outbound(
|
||||
socket.compat(),
|
||||
// We check that an initial message that is too large gets refused.
|
||||
NotificationsOut::new(
|
||||
PROTO_NAME,
|
||||
Vec::new(),
|
||||
(0..32768).map(|_| 0).collect::<Vec<_>>(),
|
||||
1024 * 1024,
|
||||
),
|
||||
upgrade::Version::V1,
|
||||
)
|
||||
.await;
|
||||
assert!(ret.is_err());
|
||||
});
|
||||
|
||||
let ret = listen_on_localhost(listener_addr_tx).await;
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
|
||||
|
||||
let (socket, _) = listener.accept().await.unwrap();
|
||||
let ret = upgrade::apply_inbound(
|
||||
socket.compat(),
|
||||
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
|
||||
)
|
||||
.await;
|
||||
assert!(ret.is_err());
|
||||
|
||||
client.await.unwrap();
|
||||
@@ -654,14 +661,30 @@ mod tests {
|
||||
|
||||
#[tokio::test]
|
||||
async fn large_handshake_refused() {
|
||||
const PROTO_NAME: &str = "/test/proto/1";
|
||||
let (listener_addr_tx, listener_addr_rx) = oneshot::channel();
|
||||
|
||||
let client = tokio::spawn(async move {
|
||||
let ret = dial(listener_addr_rx.await.unwrap(), &b"initial message"[..]).await;
|
||||
let socket = TcpStream::connect(listener_addr_rx.await.unwrap()).await.unwrap();
|
||||
let ret = upgrade::apply_outbound(
|
||||
socket.compat(),
|
||||
NotificationsOut::new(PROTO_NAME, Vec::new(), &b"initial message"[..], 1024 * 1024),
|
||||
upgrade::Version::V1,
|
||||
)
|
||||
.await;
|
||||
assert!(ret.is_err());
|
||||
});
|
||||
|
||||
let (handshake, mut substream) = listen_on_localhost(listener_addr_tx).await.unwrap();
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
listener_addr_tx.send(listener.local_addr().unwrap()).unwrap();
|
||||
|
||||
let (socket, _) = listener.accept().await.unwrap();
|
||||
let NotificationsInOpen { handshake, mut substream, .. } = upgrade::apply_inbound(
|
||||
socket.compat(),
|
||||
NotificationsIn::new(PROTO_NAME, Vec::new(), 1024 * 1024),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(handshake, b"initial message");
|
||||
|
||||
// We check that a handshake that is too large gets refused.
|
||||
|
||||
@@ -312,13 +312,13 @@ impl RequestResponsesBehaviour {
|
||||
ProtocolSupport::Outbound
|
||||
};
|
||||
|
||||
let rq_rp = Behaviour::with_codec(
|
||||
let rq_rp = Behaviour::new(
|
||||
GenericCodec {
|
||||
max_request_size: protocol.max_request_size,
|
||||
max_response_size: protocol.max_response_size,
|
||||
},
|
||||
iter::once(protocol.name.clone())
|
||||
.chain(protocol.fallback_names)
|
||||
iter::once(protocol.name.as_bytes().to_vec())
|
||||
.chain(protocol.fallback_names.iter().map(|name| name.as_bytes().to_vec()))
|
||||
.zip(iter::repeat(protocol_support)),
|
||||
cfg,
|
||||
);
|
||||
@@ -385,7 +385,7 @@ impl RequestResponsesBehaviour {
|
||||
impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
type ConnectionHandler =
|
||||
MultiHandler<String, <Behaviour<GenericCodec> as NetworkBehaviour>::ConnectionHandler>;
|
||||
type ToSwarm = Event;
|
||||
type OutEvent = Event;
|
||||
|
||||
fn handle_pending_inbound_connection(
|
||||
&mut self,
|
||||
@@ -501,9 +501,9 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
NetworkBehaviour::on_swarm_event(p, FromSwarm::ListenerError(e));
|
||||
},
|
||||
FromSwarm::ExternalAddrExpired(e) =>
|
||||
FromSwarm::ExpiredExternalAddr(e) =>
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
NetworkBehaviour::on_swarm_event(p, FromSwarm::ExternalAddrExpired(e));
|
||||
NetworkBehaviour::on_swarm_event(p, FromSwarm::ExpiredExternalAddr(e));
|
||||
},
|
||||
FromSwarm::NewListener(e) =>
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
@@ -513,13 +513,9 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
NetworkBehaviour::on_swarm_event(p, FromSwarm::ExpiredListenAddr(e));
|
||||
},
|
||||
FromSwarm::NewExternalAddrCandidate(e) =>
|
||||
FromSwarm::NewExternalAddr(e) =>
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
NetworkBehaviour::on_swarm_event(p, FromSwarm::NewExternalAddrCandidate(e));
|
||||
},
|
||||
FromSwarm::ExternalAddrConfirmed(e) =>
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
NetworkBehaviour::on_swarm_event(p, FromSwarm::ExternalAddrConfirmed(e));
|
||||
NetworkBehaviour::on_swarm_event(p, FromSwarm::NewExternalAddr(e));
|
||||
},
|
||||
FromSwarm::AddressChange(e) =>
|
||||
for (p, _) in self.protocols.values_mut() {
|
||||
@@ -554,7 +550,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
|
||||
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
|
||||
'poll_all: loop {
|
||||
// Poll to see if any response is ready to be sent back.
|
||||
while let Poll::Ready(Some(outcome)) = self.pending_responses.poll_next_unpin(cx) {
|
||||
@@ -623,18 +619,10 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
|
||||
handler,
|
||||
event: ((*protocol).to_string(), event),
|
||||
}),
|
||||
ToSwarm::ReportObservedAddr { address, score } =>
|
||||
return Poll::Ready(ToSwarm::ReportObservedAddr { address, score }),
|
||||
ToSwarm::CloseConnection { peer_id, connection } =>
|
||||
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }),
|
||||
ToSwarm::NewExternalAddrCandidate(observed) =>
|
||||
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)),
|
||||
ToSwarm::ExternalAddrConfirmed(addr) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)),
|
||||
ToSwarm::ExternalAddrExpired(addr) =>
|
||||
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)),
|
||||
ToSwarm::ListenOn { opts } =>
|
||||
return Poll::Ready(ToSwarm::ListenOn { opts }),
|
||||
ToSwarm::RemoveListener { id } =>
|
||||
return Poll::Ready(ToSwarm::RemoveListener { id }),
|
||||
};
|
||||
|
||||
match ev {
|
||||
@@ -869,7 +857,7 @@ pub struct GenericCodec {
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl Codec for GenericCodec {
|
||||
type Protocol = ProtocolName;
|
||||
type Protocol = Vec<u8>;
|
||||
type Request = Vec<u8>;
|
||||
type Response = Result<Vec<u8>, ()>;
|
||||
|
||||
|
||||
@@ -56,15 +56,17 @@ use crate::{
|
||||
|
||||
use either::Either;
|
||||
use futures::{channel::oneshot, prelude::*};
|
||||
#[allow(deprecated)]
|
||||
use libp2p::{
|
||||
connection_limits::{ConnectionLimits, Exceeded},
|
||||
connection_limits::Exceeded,
|
||||
core::{upgrade, ConnectedPoint, Endpoint},
|
||||
identify::Info as IdentifyInfo,
|
||||
kad::record::Key as KademliaKey,
|
||||
multiaddr,
|
||||
ping::Failure as PingFailure,
|
||||
swarm::{
|
||||
ConnectionError, ConnectionId, DialError, Executor, ListenError, NetworkBehaviour, Swarm,
|
||||
SwarmBuilder, SwarmEvent, THandlerErr,
|
||||
AddressScore, ConnectionError, ConnectionId, ConnectionLimits, DialError, Executor,
|
||||
ListenError, NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent, THandlerErr,
|
||||
},
|
||||
Multiaddr, PeerId,
|
||||
};
|
||||
@@ -434,11 +436,6 @@ where
|
||||
discovery_config,
|
||||
request_response_protocols,
|
||||
params.peer_store.clone(),
|
||||
ConnectionLimits::default()
|
||||
.with_max_established_per_peer(Some(crate::MAX_CONNECTIONS_PER_PEER as u32))
|
||||
.with_max_established_incoming(Some(
|
||||
crate::MAX_CONNECTIONS_ESTABLISHED_INCOMING,
|
||||
)),
|
||||
external_addresses.clone(),
|
||||
);
|
||||
|
||||
@@ -463,7 +460,15 @@ where
|
||||
SpawnImpl(params.executor),
|
||||
)
|
||||
};
|
||||
#[allow(deprecated)]
|
||||
let builder = builder
|
||||
.connection_limits(
|
||||
ConnectionLimits::default()
|
||||
.with_max_established_per_peer(Some(crate::MAX_CONNECTIONS_PER_PEER as u32))
|
||||
.with_max_established_incoming(Some(
|
||||
crate::MAX_CONNECTIONS_ESTABLISHED_INCOMING,
|
||||
)),
|
||||
)
|
||||
.substream_upgrade_protocol_override(upgrade::Version::V1Lazy)
|
||||
.notify_handler_buffer_size(NonZeroUsize::new(32).expect("32 != 0; qed"))
|
||||
// NOTE: 24 is somewhat arbitrary and should be tuned in the future if necessary.
|
||||
@@ -495,7 +500,11 @@ where
|
||||
|
||||
// Add external addresses.
|
||||
for addr in &network_config.public_addresses {
|
||||
Swarm::<Behaviour<B>>::add_external_address(&mut swarm, addr.clone());
|
||||
Swarm::<Behaviour<B>>::add_external_address(
|
||||
&mut swarm,
|
||||
addr.clone(),
|
||||
AddressScore::Infinite,
|
||||
);
|
||||
}
|
||||
|
||||
let listen_addresses = Arc::new(Mutex::new(HashSet::new()));
|
||||
@@ -680,7 +689,7 @@ where
|
||||
|
||||
let peer_id = Swarm::<Behaviour<B>>::local_peer_id(swarm).to_base58();
|
||||
let listened_addresses = swarm.listeners().cloned().collect();
|
||||
let external_addresses = swarm.external_addresses().cloned().collect();
|
||||
let external_addresses = swarm.external_addresses().map(|r| &r.addr).cloned().collect();
|
||||
|
||||
NetworkState {
|
||||
peer_id,
|
||||
@@ -752,7 +761,8 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
|
||||
.into_iter()
|
||||
.map(|mut addr| {
|
||||
let peer = match addr.pop() {
|
||||
Some(multiaddr::Protocol::P2p(peer_id)) => peer_id,
|
||||
Some(multiaddr::Protocol::P2p(key)) => PeerId::from_multihash(key)
|
||||
.map_err(|_| "Invalid PeerId format".to_string())?,
|
||||
_ => return Err("Missing PeerId from address".to_string()),
|
||||
};
|
||||
|
||||
@@ -1432,12 +1442,7 @@ where
|
||||
peer_id,
|
||||
info:
|
||||
IdentifyInfo {
|
||||
protocol_version,
|
||||
agent_version,
|
||||
mut listen_addrs,
|
||||
protocols,
|
||||
observed_addr,
|
||||
..
|
||||
protocol_version, agent_version, mut listen_addrs, protocols, ..
|
||||
},
|
||||
}) => {
|
||||
if listen_addrs.len() > 30 {
|
||||
@@ -1449,17 +1454,11 @@ where
|
||||
listen_addrs.truncate(30);
|
||||
}
|
||||
for addr in listen_addrs {
|
||||
self.network_service.behaviour_mut().add_self_reported_address_to_dht(
|
||||
&peer_id,
|
||||
&protocols,
|
||||
addr.clone(),
|
||||
);
|
||||
self.network_service
|
||||
.behaviour_mut()
|
||||
.add_self_reported_address_to_dht(&peer_id, &protocols, addr);
|
||||
}
|
||||
self.peer_store_handle.add_known_peer(peer_id);
|
||||
// Confirm the observed address manually since they are no longer trusted by
|
||||
// default (libp2p >= 0.52)
|
||||
// TODO: remove this when/if AutoNAT is implemented.
|
||||
self.network_service.add_external_address(observed_addr);
|
||||
},
|
||||
SwarmEvent::Behaviour(BehaviourOut::Discovered(peer_id)) => {
|
||||
self.peer_store_handle.add_known_peer(peer_id);
|
||||
@@ -1604,14 +1603,8 @@ where
|
||||
}
|
||||
}
|
||||
},
|
||||
SwarmEvent::ConnectionClosed {
|
||||
connection_id,
|
||||
peer_id,
|
||||
cause,
|
||||
endpoint,
|
||||
num_established,
|
||||
} => {
|
||||
debug!(target: "sub-libp2p", "Libp2p => Disconnected({:?} via {:?}: {:?})", peer_id, connection_id, cause);
|
||||
SwarmEvent::ConnectionClosed { peer_id, cause, endpoint, num_established } => {
|
||||
debug!(target: "sub-libp2p", "Libp2p => Disconnected({:?}, {:?})", peer_id, cause);
|
||||
if let Some(metrics) = self.metrics.as_ref() {
|
||||
let direction = match endpoint {
|
||||
ConnectedPoint::Dialer { .. } => "out",
|
||||
@@ -1620,12 +1613,10 @@ where
|
||||
let reason = match cause {
|
||||
Some(ConnectionError::IO(_)) => "transport-error",
|
||||
Some(ConnectionError::Handler(Either::Left(Either::Left(
|
||||
Either::Left(Either::Right(_)),
|
||||
Either::Right(Either::Left(PingFailure::Timeout)),
|
||||
)))) => "ping-timeout",
|
||||
Some(ConnectionError::Handler(Either::Left(Either::Left(
|
||||
Either::Left(Either::Left(
|
||||
NotifsHandlerError::SyncNotificationsClogged,
|
||||
)),
|
||||
Either::Left(NotifsHandlerError::SyncNotificationsClogged),
|
||||
)))) => "sync-notifications-clogged",
|
||||
Some(ConnectionError::Handler(_)) => "protocol-error",
|
||||
Some(ConnectionError::KeepAliveTimeout) => "keep-alive-timeout",
|
||||
@@ -1653,12 +1644,12 @@ where
|
||||
}
|
||||
self.listen_addresses.lock().remove(&address);
|
||||
},
|
||||
SwarmEvent::OutgoingConnectionError { connection_id, peer_id, error } => {
|
||||
SwarmEvent::OutgoingConnectionError { peer_id, error } => {
|
||||
if let Some(peer_id) = peer_id {
|
||||
trace!(
|
||||
target: "sub-libp2p",
|
||||
"Libp2p => Failed to reach {:?} via {:?}: {}",
|
||||
peer_id, connection_id, error,
|
||||
"Libp2p => Failed to reach {:?}: {}",
|
||||
peer_id, error,
|
||||
);
|
||||
|
||||
let not_reported = !self.reported_invalid_boot_nodes.contains(&peer_id);
|
||||
@@ -1696,9 +1687,12 @@ where
|
||||
} else {
|
||||
None
|
||||
},
|
||||
DialError::WrongPeerId { .. } | DialError::LocalPeerId { .. } =>
|
||||
Some("invalid-peer-id"),
|
||||
DialError::ConnectionLimit(_) => Some("limit-reached"),
|
||||
DialError::InvalidPeerId(_) |
|
||||
DialError::WrongPeerId { .. } |
|
||||
DialError::LocalPeerId { .. } => Some("invalid-peer-id"),
|
||||
DialError::Transport(_) => Some("transport-error"),
|
||||
DialError::Banned |
|
||||
DialError::NoAddresses |
|
||||
DialError::DialPeerConditionFalse(_) |
|
||||
DialError::Aborted => None, // ignore them
|
||||
@@ -1708,26 +1702,21 @@ where
|
||||
}
|
||||
}
|
||||
},
|
||||
SwarmEvent::Dialing { peer_id, connection_id } => {
|
||||
trace!(target: "sub-libp2p", "Libp2p => Dialing({:?} via {:?})", peer_id, connection_id)
|
||||
SwarmEvent::Dialing(peer_id) => {
|
||||
trace!(target: "sub-libp2p", "Libp2p => Dialing({:?})", peer_id)
|
||||
},
|
||||
SwarmEvent::IncomingConnection { connection_id, local_addr, send_back_addr } => {
|
||||
trace!(target: "sub-libp2p", "Libp2p => IncomingConnection({},{} via {:?}))",
|
||||
local_addr, send_back_addr, connection_id);
|
||||
SwarmEvent::IncomingConnection { local_addr, send_back_addr } => {
|
||||
trace!(target: "sub-libp2p", "Libp2p => IncomingConnection({},{}))",
|
||||
local_addr, send_back_addr);
|
||||
if let Some(metrics) = self.metrics.as_ref() {
|
||||
metrics.incoming_connections_total.inc();
|
||||
}
|
||||
},
|
||||
SwarmEvent::IncomingConnectionError {
|
||||
connection_id,
|
||||
local_addr,
|
||||
send_back_addr,
|
||||
error,
|
||||
} => {
|
||||
SwarmEvent::IncomingConnectionError { local_addr, send_back_addr, error } => {
|
||||
debug!(
|
||||
target: "sub-libp2p",
|
||||
"Libp2p => IncomingConnectionError({},{} via {:?}): {}",
|
||||
local_addr, send_back_addr, connection_id, error,
|
||||
"Libp2p => IncomingConnectionError({},{}): {}",
|
||||
local_addr, send_back_addr, error,
|
||||
);
|
||||
if let Some(metrics) = self.metrics.as_ref() {
|
||||
#[allow(deprecated)]
|
||||
@@ -1738,6 +1727,7 @@ where
|
||||
} else {
|
||||
None
|
||||
},
|
||||
ListenError::ConnectionLimit(_) => Some("limit-reached"),
|
||||
ListenError::WrongPeerId { .. } | ListenError::LocalPeerId { .. } =>
|
||||
Some("invalid-peer-id"),
|
||||
ListenError::Transport(_) => Some("transport-error"),
|
||||
@@ -1752,6 +1742,17 @@ where
|
||||
}
|
||||
}
|
||||
},
|
||||
#[allow(deprecated)]
|
||||
SwarmEvent::BannedPeer { peer_id, endpoint } => {
|
||||
debug!(
|
||||
target: "sub-libp2p",
|
||||
"Libp2p => BannedPeer({}). Connected via {:?}.",
|
||||
peer_id, endpoint,
|
||||
);
|
||||
if let Some(metrics) = self.metrics.as_ref() {
|
||||
metrics.incoming_connections_errors_total.with_label_values(&["banned"]).inc();
|
||||
}
|
||||
},
|
||||
SwarmEvent::ListenerClosed { reason, addresses, .. } => {
|
||||
if let Some(metrics) = self.metrics.as_ref() {
|
||||
metrics.listeners_local_addresses.sub(addresses.len() as u64);
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
|
||||
//! `sc-network` type definitions
|
||||
|
||||
use libp2p::core::upgrade;
|
||||
|
||||
use std::{
|
||||
borrow::Borrow,
|
||||
fmt,
|
||||
@@ -90,9 +92,9 @@ impl fmt::Display for ProtocolName {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<str> for ProtocolName {
|
||||
fn as_ref(&self) -> &str {
|
||||
self as &str
|
||||
impl upgrade::ProtocolName for ProtocolName {
|
||||
fn protocol_name(&self) -> &[u8] {
|
||||
(self as &str).as_bytes()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user