Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
*: Update to libp2p v0.32.0 (#7696)
Browse files Browse the repository at this point in the history
* *: Update to libp2p v0.32.0

* Cargo.lock: Update async-tls to 0.10.2

* client/network/request_response: Adjust to new request response events

* client/network/request_response.rs: Clean up silently failing responses

* client/network/discovery: Lazily instantiate mdns

* client/network/discovery: Exclude MdnsWrapper for target_os unknown

* client/network/discovery: Fix indentation

* client/network/request-response: Use LruCache to track pending resp time

* client/network/request_responses: Fix early connection closed error

* client/network/request-response: Replace debug_assert with debug

* client/network/request-response: Fix typo

* client/network/request-response: Don't emit event on send_response fail

* client/network/request-response: Revert waker.wake_by_ref()

* client/network/request-resp: Make duration in InboundRequest optional

* client/network/req-resp: Don't emit two events for busy builder

When a response builder is busy incoming requests are dropped.
Previously this was reported both via a `ResponseFailure::Busy` and a
`ReponseFailure::Network(InboundFailure::Omisssion)` event.

With this commit the former is removed, leaving only the latter in
place.
  • Loading branch information
mxinden authored Dec 9, 2020
1 parent 9d843c3 commit 6b600cd
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 131 deletions.
93 changes: 56 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/node/browser-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "Apache-2.0"

[dependencies]
futures-timer = "3.0.2"
libp2p = { version = "0.31.1", default-features = false }
libp2p = { version = "0.32.0", default-features = false }
jsonrpc-core = "15.0.0"
serde = "1.0.106"
serde_json = "1.0.48"
Expand Down
2 changes: 1 addition & 1 deletion client/authority-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ derive_more = "0.99.2"
either = "1.5.3"
futures = "0.3.4"
futures-timer = "3.0.1"
libp2p = { version = "0.31.2", default-features = false, features = ["kad"] }
libp2p = { version = "0.32.0", default-features = false, features = ["kad"] }
log = "0.4.8"
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0"}
prost = "0.6.1"
Expand Down
2 changes: 1 addition & 1 deletion client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ regex = "1.4.2"
tokio = { version = "0.2.21", features = [ "signal", "rt-core", "rt-threaded", "blocking" ] }
futures = "0.3.4"
fdlimit = "0.2.1"
libp2p = "0.31.2"
libp2p = "0.32.0"
parity-scale-codec = "1.3.0"
hex = "0.4.2"
rand = "0.7.3"
Expand Down
2 changes: 1 addition & 1 deletion client/network-gossip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
futures = "0.3.4"
futures-timer = "3.0.1"
libp2p = { version = "0.31.2", default-features = false }
libp2p = { version = "0.32.0", default-features = false }
log = "0.4.8"
lru = "0.6.1"
sc-network = { version = "0.8.0", path = "../network" }
Expand Down
6 changes: 3 additions & 3 deletions client/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ wasm-timer = "0.2"
zeroize = "1.0.0"

[dependencies.libp2p]
version = "0.31.2"
version = "0.32.0"
default-features = false
features = ["identify", "kad", "mdns-async-std", "mplex", "noise", "ping", "request-response", "tcp-async-std", "websocket", "yamux"]
features = ["identify", "kad", "mdns", "mplex", "noise", "ping", "request-response", "tcp-async-std", "websocket", "yamux"]

[dev-dependencies]
assert_matches = "1.3"
libp2p = { version = "0.31.2", default-features = false }
libp2p = { version = "0.32.0", default-features = false }
quickcheck = "0.9.0"
rand = "0.7.2"
sp-keyring = { version = "2.0.0", path = "../../primitives/keyring" }
Expand Down
4 changes: 2 additions & 2 deletions client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub enum BehaviourOut<B: BlockT> {
protocol: Cow<'static, str>,
/// If `Ok`, contains the time elapsed between when we received the request and when we
/// sent back the response. If `Err`, the error that happened.
result: Result<Duration, ResponseFailure>,
result: Result<Option<Duration>, ResponseFailure>,
},

/// A request initiated using [`Behaviour::send_request`] has succeeded or failed.
Expand Down Expand Up @@ -417,7 +417,7 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviourEventProcess<block_requests::Event<B
self.events.push_back(BehaviourOut::InboundRequest {
peer,
protocol: self.block_requests.protocol_name().to_owned().into(),
result: Ok(total_handling_time),
result: Ok(Some(total_handling_time)),
});
},
block_requests::Event::Response { peer, response, request_duration } => {
Expand Down
56 changes: 45 additions & 11 deletions client/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ use libp2p::kad::handler::KademliaHandlerProto;
use libp2p::kad::QueryId;
use libp2p::kad::record::{self, store::{MemoryStore, RecordStore}};
#[cfg(not(target_os = "unknown"))]
use libp2p::swarm::toggle::Toggle;
#[cfg(not(target_os = "unknown"))]
use libp2p::mdns::{Mdns, MdnsEvent};
use libp2p::multiaddr::Protocol;
use log::{debug, info, trace, warn};
Expand Down Expand Up @@ -206,15 +204,9 @@ impl DiscoveryConfig {
discovery_only_if_under_num,
#[cfg(not(target_os = "unknown"))]
mdns: if enable_mdns {
match Mdns::new() {
Ok(mdns) => Some(mdns).into(),
Err(err) => {
warn!(target: "sub-libp2p", "Failed to initialize mDNS: {:?}", err);
None.into()
}
}
MdnsWrapper::Instantiating(Mdns::new().boxed())
} else {
None.into()
MdnsWrapper::Disabled
},
allow_non_globals_in_dht,
known_external_addresses: LruHashSet::new(
Expand All @@ -234,7 +226,7 @@ pub struct DiscoveryBehaviour {
kademlias: HashMap<ProtocolId, Kademlia<MemoryStore>>,
/// Discovers nodes on the local network.
#[cfg(not(target_os = "unknown"))]
mdns: Toggle<Mdns>,
mdns: MdnsWrapper,
/// Stream that fires when we need to perform the next random Kademlia query.
next_kad_random_query: Delay,
/// After `next_kad_random_query` triggers, the next one triggers after this duration.
Expand Down Expand Up @@ -785,6 +777,48 @@ fn protocol_name_from_protocol_id(id: &ProtocolId) -> Vec<u8> {
v
}

/// [`Mdns::new`] returns a future. Instead of forcing [`DiscoveryConfig::finish`] and all its
/// callers to be async, lazily instantiate [`Mdns`].
#[cfg(not(target_os = "unknown"))]
enum MdnsWrapper {
Instantiating(futures::future::BoxFuture<'static, std::io::Result<Mdns>>),
Ready(Mdns),
Disabled,
}

#[cfg(not(target_os = "unknown"))]
impl MdnsWrapper {
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr> {
match self {
MdnsWrapper::Instantiating(_) => Vec::new(),
MdnsWrapper::Ready(mdns) => mdns.addresses_of_peer(peer_id),
MdnsWrapper::Disabled => Vec::new(),
}
}

fn poll(
&mut self,
cx: &mut Context<'_>,
params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<void::Void, MdnsEvent>> {
loop {
match self {
MdnsWrapper::Instantiating(fut) => {
*self = match futures::ready!(fut.as_mut().poll(cx)) {
Ok(mdns) => MdnsWrapper::Ready(mdns),
Err(err) => {
warn!(target: "sub-libp2p", "Failed to initialize mDNS: {:?}", err);
MdnsWrapper::Disabled
},
}
}
MdnsWrapper::Ready(mdns) => return mdns.poll(cx, params),
MdnsWrapper::Disabled => return Poll::Pending,
}
}
}
}

#[cfg(test)]
mod tests {
use crate::config::ProtocolId;
Expand Down
Loading

0 comments on commit 6b600cd

Please sign in to comment.