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
+62 -58
View File
@@ -28,8 +28,8 @@ use libp2p::{
identify::{Identify, IdentifyConfig, IdentifyEvent, IdentifyInfo},
ping::{Ping, PingConfig, PingEvent, PingSuccess},
swarm::{
IntoProtocolsHandler, IntoProtocolsHandlerSelect, NetworkBehaviour, NetworkBehaviourAction,
PollParameters, ProtocolsHandler,
ConnectionHandler, IntoConnectionHandler, IntoConnectionHandlerSelect, NetworkBehaviour,
NetworkBehaviourAction, PollParameters,
},
Multiaddr,
};
@@ -170,14 +170,14 @@ pub enum PeerInfoEvent {
}
impl NetworkBehaviour for PeerInfoBehaviour {
type ProtocolsHandler = IntoProtocolsHandlerSelect<
<Ping as NetworkBehaviour>::ProtocolsHandler,
<Identify as NetworkBehaviour>::ProtocolsHandler,
type ConnectionHandler = IntoConnectionHandlerSelect<
<Ping as NetworkBehaviour>::ConnectionHandler,
<Identify as NetworkBehaviour>::ConnectionHandler,
>;
type OutEvent = PeerInfoEvent;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
IntoProtocolsHandler::select(self.ping.new_handler(), self.identify.new_handler())
fn new_handler(&mut self) -> Self::ConnectionHandler {
IntoConnectionHandler::select(self.ping.new_handler(), self.identify.new_handler())
}
fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr> {
@@ -195,11 +195,18 @@ impl NetworkBehaviour for PeerInfoBehaviour {
) {
self.ping.inject_address_change(peer_id, conn, old, new);
self.identify.inject_address_change(peer_id, conn, old, new);
}
fn inject_connected(&mut self, peer_id: &PeerId) {
self.ping.inject_connected(peer_id);
self.identify.inject_connected(peer_id);
if let Some(entry) = self.nodes_info.get_mut(peer_id) {
if let Some(endpoint) = entry.endpoints.iter_mut().find(|e| e == &old) {
*endpoint = new.clone();
} else {
error!(target: "sub-libp2p",
"Unknown address change for peer {:?} from {:?} to {:?}", peer_id, old, new);
}
} else {
error!(target: "sub-libp2p",
"Unknown peer {:?} to change address from {:?} to {:?}", peer_id, old, new);
}
}
fn inject_connection_established(
@@ -208,11 +215,22 @@ impl NetworkBehaviour for PeerInfoBehaviour {
conn: &ConnectionId,
endpoint: &ConnectedPoint,
failed_addresses: Option<&Vec<Multiaddr>>,
other_established: usize,
) {
self.ping
.inject_connection_established(peer_id, conn, endpoint, failed_addresses);
self.identify
.inject_connection_established(peer_id, conn, endpoint, failed_addresses);
self.ping.inject_connection_established(
peer_id,
conn,
endpoint,
failed_addresses,
other_established,
);
self.identify.inject_connection_established(
peer_id,
conn,
endpoint,
failed_addresses,
other_established,
);
match self.nodes_info.entry(*peer_id) {
Entry::Vacant(e) => {
e.insert(NodeInfo::new(endpoint.clone()));
@@ -234,14 +252,29 @@ impl NetworkBehaviour for PeerInfoBehaviour {
peer_id: &PeerId,
conn: &ConnectionId,
endpoint: &ConnectedPoint,
handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
handler: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
remaining_established: usize,
) {
let (ping_handler, identity_handler) = handler.into_inner();
self.identify
.inject_connection_closed(peer_id, conn, endpoint, identity_handler);
self.ping.inject_connection_closed(peer_id, conn, endpoint, ping_handler);
self.identify.inject_connection_closed(
peer_id,
conn,
endpoint,
identity_handler,
remaining_established,
);
self.ping.inject_connection_closed(
peer_id,
conn,
endpoint,
ping_handler,
remaining_established,
);
if let Some(entry) = self.nodes_info.get_mut(peer_id) {
if remaining_established == 0 {
entry.info_expire = Some(Instant::now() + CACHE_EXPIRE);
}
entry.endpoints.retain(|ep| ep != endpoint)
} else {
error!(target: "sub-libp2p",
@@ -249,23 +282,11 @@ impl NetworkBehaviour for PeerInfoBehaviour {
}
}
fn inject_disconnected(&mut self, peer_id: &PeerId) {
self.ping.inject_disconnected(peer_id);
self.identify.inject_disconnected(peer_id);
if let Some(entry) = self.nodes_info.get_mut(peer_id) {
entry.info_expire = Some(Instant::now() + CACHE_EXPIRE);
} else {
error!(target: "sub-libp2p",
"Disconnected from node we were not connected to {:?}", peer_id);
}
}
fn inject_event(
&mut self,
peer_id: PeerId,
connection: ConnectionId,
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
) {
match event {
EitherOutput::First(event) => self.ping.inject_event(peer_id, connection, event),
@@ -276,7 +297,7 @@ impl NetworkBehaviour for PeerInfoBehaviour {
fn inject_dial_failure(
&mut self,
peer_id: Option<PeerId>,
handler: Self::ProtocolsHandler,
handler: Self::ConnectionHandler,
error: &libp2p::swarm::DialError,
) {
let (ping_handler, identity_handler) = handler.into_inner();
@@ -313,7 +334,7 @@ impl NetworkBehaviour for PeerInfoBehaviour {
&mut self,
local_addr: &Multiaddr,
send_back_addr: &Multiaddr,
handler: Self::ProtocolsHandler,
handler: Self::ConnectionHandler,
) {
let (ping_handler, identity_handler) = handler.into_inner();
self.identify
@@ -335,7 +356,7 @@ impl NetworkBehaviour for PeerInfoBehaviour {
&mut self,
cx: &mut Context,
params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
loop {
match self.ping.poll(cx, params) {
Poll::Pending => break,
@@ -344,19 +365,10 @@ impl NetworkBehaviour for PeerInfoBehaviour {
self.handle_ping_report(&peer, rtt)
}
},
Poll::Ready(NetworkBehaviourAction::DialAddress { address, handler }) => {
Poll::Ready(NetworkBehaviourAction::Dial { opts, handler }) => {
let handler =
IntoProtocolsHandler::select(handler, self.identify.new_handler());
return Poll::Ready(NetworkBehaviourAction::DialAddress { address, handler })
},
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition, handler }) => {
let handler =
IntoProtocolsHandler::select(handler, self.identify.new_handler());
return Poll::Ready(NetworkBehaviourAction::DialPeer {
peer_id,
condition,
handler,
})
IntoConnectionHandler::select(handler, self.identify.new_handler());
return Poll::Ready(NetworkBehaviourAction::Dial { opts, handler })
},
Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }) =>
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
@@ -392,17 +404,9 @@ impl NetworkBehaviour for PeerInfoBehaviour {
IdentifyEvent::Pushed { .. } => {},
IdentifyEvent::Sent { .. } => {},
},
Poll::Ready(NetworkBehaviourAction::DialAddress { address, handler }) => {
let handler = IntoProtocolsHandler::select(self.ping.new_handler(), handler);
return Poll::Ready(NetworkBehaviourAction::DialAddress { address, handler })
},
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition, handler }) => {
let handler = IntoProtocolsHandler::select(self.ping.new_handler(), handler);
return Poll::Ready(NetworkBehaviourAction::DialPeer {
peer_id,
condition,
handler,
})
Poll::Ready(NetworkBehaviourAction::Dial { opts, handler }) => {
let handler = IntoConnectionHandler::select(self.ping.new_handler(), handler);
return Poll::Ready(NetworkBehaviourAction::Dial { opts, handler })
},
Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }) =>
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {