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;
},
@@ -16,10 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Implementations of the `IntoProtocolsHandler` and `ProtocolsHandler` traits for both incoming
//! Implementations of the `IntoConnectionHandler` and `ConnectionHandler` traits for both incoming
//! and outgoing substreams for all gossiping protocols.
//!
//! This is the main implementation of `ProtocolsHandler` in this crate, that handles all the
//! This is the main implementation of `ConnectionHandler` in this crate, that handles all the
//! gossiping protocols that are Substrate-related and outside of the scope of libp2p.
//!
//! # Usage
@@ -74,8 +74,8 @@ use libp2p::{
ConnectedPoint, PeerId,
},
swarm::{
IntoProtocolsHandler, KeepAlive, NegotiatedSubstream, ProtocolsHandler,
ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr, SubstreamProtocol,
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, IntoConnectionHandler,
KeepAlive, NegotiatedSubstream, SubstreamProtocol,
},
};
use log::error;
@@ -107,7 +107,7 @@ const OPEN_TIMEOUT: Duration = Duration::from_secs(10);
/// open substreams.
const INITIAL_KEEPALIVE_TIME: Duration = Duration::from_secs(5);
/// Implements the `IntoProtocolsHandler` trait of libp2p.
/// Implements the `IntoConnectionHandler` trait of libp2p.
///
/// Every time a connection with a remote starts, an instance of this struct is created and
/// sent to a background task dedicated to this connection. Once the connection is established,
@@ -138,7 +138,7 @@ pub struct NotifsHandler {
/// Events to return in priority from `poll`.
events_queue: VecDeque<
ProtocolsHandlerEvent<NotificationsOut, usize, NotifsHandlerOut, NotifsHandlerError>,
ConnectionHandlerEvent<NotificationsOut, usize, NotifsHandlerOut, NotifsHandlerError>,
>,
}
@@ -225,7 +225,7 @@ enum State {
},
}
impl IntoProtocolsHandler for NotifsHandlerProto {
impl IntoConnectionHandler for NotifsHandlerProto {
type Handler = NotifsHandler;
fn inbound_protocol(&self) -> UpgradeCollec<NotificationsIn> {
@@ -475,7 +475,7 @@ impl NotifsHandlerProto {
}
}
impl ProtocolsHandler for NotifsHandler {
impl ConnectionHandler for NotifsHandler {
type InEvent = NotifsHandlerIn;
type OutEvent = NotifsHandlerOut;
type Error = NotifsHandlerError;
@@ -505,7 +505,7 @@ impl ProtocolsHandler for NotifsHandler {
let mut protocol_info = &mut self.protocols[protocol_index];
match protocol_info.state {
State::Closed { pending_opening } => {
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(
self.events_queue.push_back(ConnectionHandlerEvent::Custom(
NotifsHandlerOut::OpenDesiredByRemote { protocol_index },
));
@@ -573,7 +573,7 @@ impl ProtocolsHandler for NotifsHandler {
in_substream: in_substream.take(),
};
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(
self.events_queue.push_back(ConnectionHandlerEvent::Custom(
NotifsHandlerOut::OpenResultOk {
protocol_index,
negotiated_fallback: new_open.negotiated_fallback,
@@ -601,7 +601,7 @@ impl ProtocolsHandler for NotifsHandler {
);
self.events_queue.push_back(
ProtocolsHandlerEvent::OutboundSubstreamRequest {
ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(proto, protocol_index)
.with_timeout(OPEN_TIMEOUT),
},
@@ -622,7 +622,7 @@ impl ProtocolsHandler for NotifsHandler {
);
self.events_queue.push_back(
ProtocolsHandlerEvent::OutboundSubstreamRequest {
ConnectionHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(proto, protocol_index)
.with_timeout(OPEN_TIMEOUT),
},
@@ -660,7 +660,7 @@ impl ProtocolsHandler for NotifsHandler {
self.protocols[protocol_index].state =
State::Closed { pending_opening: true };
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(
self.events_queue.push_back(ConnectionHandlerEvent::Custom(
NotifsHandlerOut::OpenResultErr { protocol_index },
));
},
@@ -670,7 +670,7 @@ impl ProtocolsHandler for NotifsHandler {
State::Closed { .. } => {},
}
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(
self.events_queue.push_back(ConnectionHandlerEvent::Custom(
NotifsHandlerOut::CloseResult { protocol_index },
));
},
@@ -680,7 +680,7 @@ impl ProtocolsHandler for NotifsHandler {
fn inject_dial_upgrade_error(
&mut self,
num: usize,
_: ProtocolsHandlerUpgrErr<NotificationsHandshakeError>,
_: ConnectionHandlerUpgrErr<NotificationsHandshakeError>,
) {
match self.protocols[num].state {
State::Closed { ref mut pending_opening } |
@@ -692,7 +692,7 @@ impl ProtocolsHandler for NotifsHandler {
State::Opening { .. } => {
self.protocols[num].state = State::Closed { pending_opening: false };
self.events_queue.push_back(ProtocolsHandlerEvent::Custom(
self.events_queue.push_back(ConnectionHandlerEvent::Custom(
NotifsHandlerOut::OpenResultErr { protocol_index: num },
));
},
@@ -717,7 +717,7 @@ impl ProtocolsHandler for NotifsHandler {
&mut self,
cx: &mut Context,
) -> Poll<
ProtocolsHandlerEvent<
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
@@ -741,7 +741,7 @@ impl ProtocolsHandler for NotifsHandler {
// a substream is ready to send if there isn't actually something to send.
match Pin::new(&mut *notifications_sink_rx).as_mut().poll_peek(cx) {
Poll::Ready(Some(&NotificationsSinkMessage::ForceClose)) =>
return Poll::Ready(ProtocolsHandlerEvent::Close(
return Poll::Ready(ConnectionHandlerEvent::Close(
NotifsHandlerError::SyncNotificationsClogged,
)),
Poll::Ready(Some(&NotificationsSinkMessage::Notification { .. })) => {},
@@ -789,7 +789,7 @@ impl ProtocolsHandler for NotifsHandler {
Poll::Ready(Err(_)) => {
*out_substream = None;
let event = NotifsHandlerOut::CloseDesired { protocol_index };
return Poll::Ready(ProtocolsHandlerEvent::Custom(event))
return Poll::Ready(ConnectionHandlerEvent::Custom(event))
},
};
},
@@ -815,7 +815,7 @@ impl ProtocolsHandler for NotifsHandler {
Poll::Pending => {},
Poll::Ready(Some(Ok(message))) => {
let event = NotifsHandlerOut::Notification { protocol_index, message };
return Poll::Ready(ProtocolsHandlerEvent::Custom(event))
return Poll::Ready(ConnectionHandlerEvent::Custom(event))
},
Poll::Ready(None) | Poll::Ready(Some(Err(_))) => *in_substream = None,
},
@@ -827,7 +827,7 @@ impl ProtocolsHandler for NotifsHandler {
Poll::Ready(Err(_)) => {
self.protocols[protocol_index].state =
State::Closed { pending_opening: *pending_opening };
return Poll::Ready(ProtocolsHandlerEvent::Custom(
return Poll::Ready(ConnectionHandlerEvent::Custom(
NotifsHandlerOut::CloseDesired { protocol_index },
))
},
@@ -29,8 +29,8 @@ use libp2p::{
},
identity, noise,
swarm::{
DialError, IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
ProtocolsHandler, Swarm, SwarmEvent,
ConnectionHandler, DialError, IntoConnectionHandler, NetworkBehaviour,
NetworkBehaviourAction, PollParameters, Swarm, SwarmEvent,
},
yamux, Multiaddr, PeerId, Transport,
};
@@ -133,10 +133,10 @@ impl std::ops::DerefMut for CustomProtoWithAddr {
}
impl NetworkBehaviour for CustomProtoWithAddr {
type ProtocolsHandler = <Notifications as NetworkBehaviour>::ProtocolsHandler;
type ConnectionHandler = <Notifications as NetworkBehaviour>::ConnectionHandler;
type OutEvent = <Notifications as NetworkBehaviour>::OutEvent;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
fn new_handler(&mut self) -> Self::ConnectionHandler {
self.inner.new_handler()
}
@@ -150,23 +150,21 @@ impl NetworkBehaviour for CustomProtoWithAddr {
list
}
fn inject_connected(&mut self, peer_id: &PeerId) {
self.inner.inject_connected(peer_id)
}
fn inject_disconnected(&mut self, peer_id: &PeerId) {
self.inner.inject_disconnected(peer_id)
}
fn inject_connection_established(
&mut self,
peer_id: &PeerId,
conn: &ConnectionId,
endpoint: &ConnectedPoint,
failed_addresses: Option<&Vec<Multiaddr>>,
other_established: usize,
) {
self.inner
.inject_connection_established(peer_id, conn, endpoint, failed_addresses)
self.inner.inject_connection_established(
peer_id,
conn,
endpoint,
failed_addresses,
other_established,
)
}
fn inject_connection_closed(
@@ -174,16 +172,18 @@ impl NetworkBehaviour for CustomProtoWithAddr {
peer_id: &PeerId,
conn: &ConnectionId,
endpoint: &ConnectedPoint,
handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
handler: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
remaining_established: usize,
) {
self.inner.inject_connection_closed(peer_id, conn, endpoint, handler)
self.inner
.inject_connection_closed(peer_id, conn, endpoint, handler, remaining_established)
}
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,
) {
self.inner.inject_event(peer_id, connection, event)
}
@@ -192,14 +192,14 @@ impl NetworkBehaviour for CustomProtoWithAddr {
&mut self,
cx: &mut Context,
params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
self.inner.poll(cx, params)
}
fn inject_dial_failure(
&mut self,
peer_id: Option<PeerId>,
handler: Self::ProtocolsHandler,
handler: Self::ConnectionHandler,
error: &DialError,
) {
self.inner.inject_dial_failure(peer_id, handler, error)