Upgrade to libp2p 0.44.0 (#11009)

* Update libp2p to 0.43.0, lru to 0.7.3

* Fix websoket Incoming::Data

* Rename ProtocolsHandler -> ConnectionHandler, remove inject_dis/connected, minor fixes

* Fix args for inject_connection* callbacks

* Fix DialPeer/DialAddress

* Fix debug fmt

* Add Endpoint to NetworkState

* Fix Kad::get_record by key

* Fix Sha2_256::digest

* Fix IntoConnectionHandler

* Fix borrowchk

* Fix DialError::WrongPeerId

* Remove NodeHandlerWrapperError

* Fix KademliaEvent variants

* Fix impl Add for String

* Fix tabs in network_state

* Apply cargo fmt

* Fix a typo in req/resp

* Fix tests

* Fix peer_info:entry.info_expire

* Fix PeerInfoBehaviour inject_address_change and inject_connection_closed

* Patch libp2p to 0.44.0#6cc3b4e

* Fix inject_connection_closed kad, req/resp

* Apply cargo fmt

* Use libp2p from crates.io

* Fix review notes
This commit is contained in:
Roman
2022-04-29 12:49:05 +04:00
committed by GitHub
parent 51915ebe4a
commit 887acda7d0
24 changed files with 759 additions and 542 deletions
@@ -26,8 +26,8 @@ use futures::prelude::*;
use libp2p::{
core::{connection::ConnectionId, ConnectedPoint, Multiaddr, PeerId},
swarm::{
DialError, DialPeerCondition, IntoProtocolsHandler, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters,
DialError, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters,
},
};
use log::{error, trace, warn};
@@ -620,10 +620,8 @@ impl Notifications {
set_id,
);
trace!(target: "sub-libp2p", "Libp2p <= Dial {}", entry.key().0);
// The `DialPeerCondition` ensures that dial attempts are de-duplicated
self.events.push_back(NetworkBehaviourAction::DialPeer {
peer_id: entry.key().0.clone(),
condition: DialPeerCondition::Disconnected,
self.events.push_back(NetworkBehaviourAction::Dial {
opts: entry.key().0.clone().into(),
handler,
});
entry.insert(PeerState::Requested);
@@ -657,10 +655,8 @@ impl Notifications {
set_id,
);
trace!(target: "sub-libp2p", "Libp2p <= Dial {:?}", occ_entry.key());
// The `DialPeerCondition` ensures that dial attempts are de-duplicated
self.events.push_back(NetworkBehaviourAction::DialPeer {
peer_id: occ_entry.key().0.clone(),
condition: DialPeerCondition::Disconnected,
self.events.push_back(NetworkBehaviourAction::Dial {
opts: occ_entry.key().0.clone().into(),
handler,
});
*occ_entry.into_mut() = PeerState::Requested;
@@ -1059,10 +1055,10 @@ impl Notifications {
}
impl NetworkBehaviour for Notifications {
type ProtocolsHandler = NotifsHandlerProto;
type ConnectionHandler = NotifsHandlerProto;
type OutEvent = NotificationsOut;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
fn new_handler(&mut self) -> Self::ConnectionHandler {
NotifsHandlerProto::new(self.notif_protocols.clone())
}
@@ -1070,14 +1066,13 @@ impl NetworkBehaviour for Notifications {
Vec::new()
}
fn inject_connected(&mut self, _: &PeerId) {}
fn inject_connection_established(
&mut self,
peer_id: &PeerId,
conn: &ConnectionId,
endpoint: &ConnectedPoint,
_failed_addresses: Option<&Vec<Multiaddr>>,
_other_established: usize,
) {
for set_id in (0..self.notif_protocols.len()).map(sc_peerset::SetId::from) {
match self.peers.entry((*peer_id, set_id)).or_insert(PeerState::Poisoned) {
@@ -1136,7 +1131,8 @@ impl NetworkBehaviour for Notifications {
peer_id: &PeerId,
conn: &ConnectionId,
_endpoint: &ConnectedPoint,
_handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
_handler: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
_remaining_established: usize,
) {
for set_id in (0..self.notif_protocols.len()).map(sc_peerset::SetId::from) {
let mut entry = if let Entry::Occupied(entry) = self.peers.entry((*peer_id, set_id)) {
@@ -1394,12 +1390,10 @@ impl NetworkBehaviour for Notifications {
}
}
fn inject_disconnected(&mut self, _peer_id: &PeerId) {}
fn inject_dial_failure(
&mut self,
peer_id: Option<PeerId>,
_: Self::ProtocolsHandler,
_: Self::ConnectionHandler,
error: &DialError,
) {
if let DialError::Transport(errors) = error {
@@ -1989,7 +1983,7 @@ impl NetworkBehaviour for Notifications {
&mut self,
cx: &mut Context,
_params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(event)
}
@@ -2038,12 +2032,8 @@ impl NetworkBehaviour for Notifications {
PeerState::PendingRequest { timer, .. } if *timer == delay_id => {
trace!(target: "sub-libp2p", "Libp2p <= Dial {:?} now that ban has expired", peer_id);
// The `DialPeerCondition` ensures that dial attempts are de-duplicated
self.events.push_back(NetworkBehaviourAction::DialPeer {
peer_id,
condition: DialPeerCondition::Disconnected,
handler,
});
self.events
.push_back(NetworkBehaviourAction::Dial { opts: peer_id.into(), handler });
*peer_state = PeerState::Requested;
},