*: Update to libp2p v0.33.0 (#7759)

* *: Update to libp2p v0.33.0

* client/network: Consistently track request arrival time

With https://github.com/libp2p/rust-libp2p/pull/1886/ one is guaranteed
to receive either a `ResponseSent` or a `InboundFailure` event for each
received inbound request via `RequestResponseEvent::Message`. Given this
guarantee there is no need to track arrival times in a best-effort
manner and thus there is no need to use a LRU cache for arrival times.

* client/offchain: Adjust to PeerId API changes
This commit is contained in:
Max Inden
2021-01-04 12:54:51 +01:00
committed by GitHub
parent 51c37ecc15
commit f0b99dd2f2
15 changed files with 88 additions and 91 deletions
@@ -50,7 +50,6 @@ use libp2p::{
PollParameters, ProtocolsHandler,
},
};
use lru::LruCache;
use std::{
borrow::Cow, collections::{hash_map::Entry, HashMap}, convert::TryFrom as _, io, iter,
pin::Pin, task::{Context, Poll}, time::{Duration, Instant},
@@ -129,12 +128,11 @@ pub enum Event {
peer: PeerId,
/// Name of the protocol in question.
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.
/// Whether handling the request was successful or unsuccessful.
///
/// Note: Given that response time is tracked on a best-effort basis only, `Ok(time)` can be
/// `None`.
result: Result<Option<Duration>, ResponseFailure>,
/// When successful contains the time elapsed between when we received the request and when
/// we sent back the response. When unsuccessful contains the failure reason.
result: Result<Duration, ResponseFailure>,
},
/// A request initiated using [`RequestResponsesBehaviour::send_request`] has succeeded or
@@ -164,7 +162,7 @@ pub struct RequestResponsesBehaviour {
>,
/// Whenever an incoming request arrives, the arrival [`Instant`] is recorded here.
pending_responses_arrival_time: LruCache<RequestId, Instant>,
pending_responses_arrival_time: HashMap<RequestId, Instant>,
}
/// Generated by the response builder and waiting to be processed.
@@ -206,7 +204,7 @@ impl RequestResponsesBehaviour {
Ok(Self {
protocols,
pending_responses: Default::default(),
pending_responses_arrival_time: LruCache::new(1_000),
pending_responses_arrival_time: Default::default(),
})
}
@@ -367,9 +365,8 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
if let Some((protocol, _)) = self.protocols.get_mut(&*protocol_name) {
if let Err(_) = protocol.send_response(inner_channel, Ok(response)) {
// Note: In case this happened due to a timeout, the corresponding
// `RequestResponse` behaviour will emit an `InboundFailure::Timeout` event.
self.pending_responses_arrival_time.pop(&request_id);
// Note: Failure is handled further below when receiving `InboundFailure`
// event from `RequestResponse` behaviour.
log::debug!(
target: "sub-libp2p",
"Failed to send response for {:?} on protocol {:?} due to a \
@@ -425,7 +422,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
peer,
message: RequestResponseMessage::Request { request_id, request, channel, .. },
} => {
self.pending_responses_arrival_time.put(
self.pending_responses_arrival_time.insert(
request_id.clone(),
Instant::now(),
);
@@ -495,7 +492,7 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
// An inbound request failed, either while reading the request or due to failing
// to send a response.
RequestResponseEvent::InboundFailure { request_id, peer, error, .. } => {
self.pending_responses_arrival_time.pop(&request_id);
self.pending_responses_arrival_time.remove(&request_id);
let out = Event::InboundRequest {
peer,
protocol: protocol.clone(),
@@ -504,14 +501,9 @@ impl NetworkBehaviour for RequestResponsesBehaviour {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(out));
}
RequestResponseEvent::ResponseSent { request_id, peer } => {
let arrival_time = self.pending_responses_arrival_time.pop(&request_id)
.map(|t| t.elapsed());
if arrival_time.is_none() {
log::debug!(
"Expected to find arrival time for sent response. Is the LRU \
cache size set too small?",
);
}
let arrival_time = self.pending_responses_arrival_time.remove(&request_id)
.map(|t| t.elapsed())
.expect("To find request arrival time for answered request.");
let out = Event::InboundRequest {
peer,