mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 22:51:13 +00:00
Bump libp2p to 0.40.0 (#10035)
* Bump libp2p to 0.40.0-rc.1 * Fix PingFailure import * Reduce the number of compilation errors (this is a FIXME commit) * Bump libp2p to 0.40.0-rc.2 * Fix sc-network::Behaviour to inject events into fields * Fix some NetworkBehaviourAction types * More fixes * More fixes * More fixes * Fix DiscoveryBehaviour * Fix PeerInfoBehaviour * Fix RequestResponsesBehaviour * Fix RequestResponsesBehaviour * Fix Notifications * Fix NetworkWorker * Fix Behaviour * Please borrowchk * Please borrowchk * Please borrowchk * Fix fmt * Cover all cases in matches * Fix some clippy warnings * Fix into_peer_id -> to_peer_id * Fix some warnings * Fix some inject_dial_failure FIXMEs * Fix DiscoveryBehaviour::inject_dial_failure * Fix RequestResponsesBehaviour::inject_dial_failure * Fix the order of inject_connection_closed PeerInfoBehaviour events * Make KademliaEvent with filtering unreachable * Fix Notifications::inject_dial_failure * Use concurrent_dial_errors in NetworkWorker * Remove commented-out RequestResponsesBehaviour::inject_addr_reach_failure * Fix tests * Dont report new PendingConnectionError and DialError variants to metrics * Bump libp2p to 0.40.0 * Add fn inject_listen_failure and inject_address_change * Review fixes
This commit is contained in:
@@ -67,8 +67,8 @@ use libp2p::{
|
||||
mdns::{Mdns, MdnsConfig, MdnsEvent},
|
||||
multiaddr::Protocol,
|
||||
swarm::{
|
||||
protocols_handler::multi::IntoMultiHandler, IntoProtocolsHandler, NetworkBehaviour,
|
||||
NetworkBehaviourAction, PollParameters, ProtocolsHandler,
|
||||
protocols_handler::multi::IntoMultiHandler, DialError, IntoProtocolsHandler,
|
||||
NetworkBehaviour, NetworkBehaviourAction, PollParameters, ProtocolsHandler,
|
||||
},
|
||||
};
|
||||
use log::{debug, error, info, trace, warn};
|
||||
@@ -107,7 +107,7 @@ impl DiscoveryConfig {
|
||||
/// Create a default configuration with the given public key.
|
||||
pub fn new(local_public_key: PublicKey) -> Self {
|
||||
Self {
|
||||
local_peer_id: local_public_key.into_peer_id(),
|
||||
local_peer_id: local_public_key.to_peer_id(),
|
||||
permanent_addresses: Vec::new(),
|
||||
dht_random_walk: true,
|
||||
allow_private_ipv4: true,
|
||||
@@ -428,6 +428,29 @@ impl DiscoveryBehaviour {
|
||||
};
|
||||
ip.is_global()
|
||||
}
|
||||
|
||||
fn new_handler_with_replacement(
|
||||
&mut self,
|
||||
pid: ProtocolId,
|
||||
handler: KademliaHandlerProto<QueryId>,
|
||||
) -> <DiscoveryBehaviour as NetworkBehaviour>::ProtocolsHandler {
|
||||
let mut handlers: HashMap<_, _> = self
|
||||
.kademlias
|
||||
.iter_mut()
|
||||
.map(|(p, k)| (p.clone(), NetworkBehaviour::new_handler(k)))
|
||||
.collect();
|
||||
|
||||
if let Some(h) = handlers.get_mut(&pid) {
|
||||
*h = handler
|
||||
}
|
||||
|
||||
IntoMultiHandler::try_from_iter(handlers).expect(
|
||||
"There can be at most one handler per `ProtocolId` and protocol names contain the \
|
||||
`ProtocolId` so no two protocol names in `self.kademlias` can be equal which is the \
|
||||
only error `try_from_iter` can return, therefore this call is guaranteed to succeed; \
|
||||
qed",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Event generated by the `DiscoveryBehaviour`.
|
||||
@@ -527,15 +550,34 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
list
|
||||
}
|
||||
|
||||
fn inject_address_change(
|
||||
&mut self,
|
||||
peer_id: &PeerId,
|
||||
connection_id: &ConnectionId,
|
||||
old: &ConnectedPoint,
|
||||
new: &ConnectedPoint,
|
||||
) {
|
||||
for k in self.kademlias.values_mut() {
|
||||
NetworkBehaviour::inject_address_change(k, peer_id, connection_id, old, new);
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_connection_established(
|
||||
&mut self,
|
||||
peer_id: &PeerId,
|
||||
conn: &ConnectionId,
|
||||
endpoint: &ConnectedPoint,
|
||||
failed_addresses: Option<&Vec<Multiaddr>>,
|
||||
) {
|
||||
self.num_connections += 1;
|
||||
for k in self.kademlias.values_mut() {
|
||||
NetworkBehaviour::inject_connection_established(k, peer_id, conn, endpoint)
|
||||
NetworkBehaviour::inject_connection_established(
|
||||
k,
|
||||
peer_id,
|
||||
conn,
|
||||
endpoint,
|
||||
failed_addresses,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -547,14 +589,13 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
|
||||
fn inject_connection_closed(
|
||||
&mut self,
|
||||
peer_id: &PeerId,
|
||||
conn: &ConnectionId,
|
||||
endpoint: &ConnectedPoint,
|
||||
_peer_id: &PeerId,
|
||||
_conn: &ConnectionId,
|
||||
_endpoint: &ConnectedPoint,
|
||||
_handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
|
||||
) {
|
||||
self.num_connections -= 1;
|
||||
for k in self.kademlias.values_mut() {
|
||||
NetworkBehaviour::inject_connection_closed(k, peer_id, conn, endpoint)
|
||||
}
|
||||
// NetworkBehaviour::inject_connection_closed on Kademlia<MemoryStore> does nothing.
|
||||
}
|
||||
|
||||
fn inject_disconnected(&mut self, peer_id: &PeerId) {
|
||||
@@ -563,20 +604,25 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_addr_reach_failure(
|
||||
fn inject_dial_failure(
|
||||
&mut self,
|
||||
peer_id: Option<&PeerId>,
|
||||
addr: &Multiaddr,
|
||||
error: &dyn std::error::Error,
|
||||
peer_id: Option<PeerId>,
|
||||
_: Self::ProtocolsHandler,
|
||||
error: &DialError,
|
||||
) {
|
||||
if let Some(peer_id) = peer_id {
|
||||
if let Some(list) = self.ephemeral_addresses.get_mut(peer_id) {
|
||||
list.retain(|a| a != addr);
|
||||
if let DialError::Transport(errors) = error {
|
||||
if let Some(list) = self.ephemeral_addresses.get_mut(&peer_id) {
|
||||
for (addr, _error) in errors {
|
||||
list.retain(|a| a != addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for k in self.kademlias.values_mut() {
|
||||
NetworkBehaviour::inject_addr_reach_failure(k, peer_id, addr, error)
|
||||
let handler = k.new_handler();
|
||||
NetworkBehaviour::inject_dial_failure(k, peer_id, handler, error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -631,12 +677,6 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_dial_failure(&mut self, peer_id: &PeerId) {
|
||||
for k in self.kademlias.values_mut() {
|
||||
NetworkBehaviour::inject_dial_failure(k, peer_id)
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_new_listener(&mut self, id: ListenerId) {
|
||||
for k in self.kademlias.values_mut() {
|
||||
NetworkBehaviour::inject_new_listener(k, id)
|
||||
@@ -649,6 +689,10 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
fn inject_listen_failure(&mut self, _: &Multiaddr, _: &Multiaddr, _: Self::ProtocolsHandler) {
|
||||
// NetworkBehaviour::inject_listen_failure on Kademlia<MemoryStore> does nothing.
|
||||
}
|
||||
|
||||
fn inject_listener_error(&mut self, id: ListenerId, err: &(dyn std::error::Error + 'static)) {
|
||||
for k in self.kademlias.values_mut() {
|
||||
NetworkBehaviour::inject_listener_error(k, id, err)
|
||||
@@ -665,12 +709,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<
|
||||
NetworkBehaviourAction<
|
||||
<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent,
|
||||
Self::OutEvent,
|
||||
>,
|
||||
>{
|
||||
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
|
||||
// Immediately process the content of `discovered`.
|
||||
if let Some(ev) = self.pending_events.pop_front() {
|
||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev))
|
||||
@@ -731,6 +770,10 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
let ev = DiscoveryOut::Discovered(peer);
|
||||
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev))
|
||||
},
|
||||
KademliaEvent::InboundPutRecordRequest { .. } |
|
||||
KademliaEvent::InboundAddProviderRequest { .. } => {
|
||||
debug_assert!(false, "We don't use kad filtering at the moment");
|
||||
},
|
||||
KademliaEvent::PendingRoutablePeer { .. } |
|
||||
KademliaEvent::InboundRequestServed { .. } => {
|
||||
// We are not interested in this event at the moment.
|
||||
@@ -847,10 +890,20 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
warn!(target: "sub-libp2p", "Libp2p => Unhandled Kademlia event: {:?}", e)
|
||||
},
|
||||
},
|
||||
NetworkBehaviourAction::DialAddress { address } =>
|
||||
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
|
||||
NetworkBehaviourAction::DialPeer { peer_id, condition } =>
|
||||
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }),
|
||||
NetworkBehaviourAction::DialAddress { address, handler } => {
|
||||
let pid = pid.clone();
|
||||
let handler = self.new_handler_with_replacement(pid, handler);
|
||||
return Poll::Ready(NetworkBehaviourAction::DialAddress { address, handler })
|
||||
},
|
||||
NetworkBehaviourAction::DialPeer { peer_id, condition, handler } => {
|
||||
let pid = pid.clone();
|
||||
let handler = self.new_handler_with_replacement(pid, handler);
|
||||
return Poll::Ready(NetworkBehaviourAction::DialPeer {
|
||||
peer_id,
|
||||
condition,
|
||||
handler,
|
||||
})
|
||||
},
|
||||
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
|
||||
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
|
||||
peer_id,
|
||||
@@ -888,10 +941,12 @@ impl NetworkBehaviour for DiscoveryBehaviour {
|
||||
},
|
||||
MdnsEvent::Expired(_) => {},
|
||||
},
|
||||
NetworkBehaviourAction::DialAddress { address } =>
|
||||
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
|
||||
NetworkBehaviourAction::DialPeer { peer_id, condition } =>
|
||||
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }),
|
||||
NetworkBehaviourAction::DialAddress { .. } => {
|
||||
unreachable!("mDNS never dials!");
|
||||
},
|
||||
NetworkBehaviourAction::DialPeer { .. } => {
|
||||
unreachable!("mDNS never dials!");
|
||||
},
|
||||
NetworkBehaviourAction::NotifyHandler { event, .. } => match event {}, /* `event` is an enum with no variant */
|
||||
NetworkBehaviourAction::ReportObservedAddr { address, score } =>
|
||||
return Poll::Ready(NetworkBehaviourAction::ReportObservedAddr {
|
||||
@@ -940,7 +995,7 @@ impl MdnsWrapper {
|
||||
&mut self,
|
||||
cx: &mut Context<'_>,
|
||||
params: &mut impl PollParameters,
|
||||
) -> Poll<NetworkBehaviourAction<void::Void, MdnsEvent>> {
|
||||
) -> Poll<NetworkBehaviourAction<MdnsEvent, <Mdns as NetworkBehaviour>::ProtocolsHandler>> {
|
||||
loop {
|
||||
match self {
|
||||
Self::Instantiating(fut) =>
|
||||
@@ -1007,13 +1062,13 @@ mod tests {
|
||||
config.finish()
|
||||
};
|
||||
|
||||
let mut swarm = Swarm::new(transport, behaviour, keypair.public().into_peer_id());
|
||||
let mut swarm = Swarm::new(transport, behaviour, keypair.public().to_peer_id());
|
||||
let listen_addr: Multiaddr =
|
||||
format!("/memory/{}", rand::random::<u64>()).parse().unwrap();
|
||||
|
||||
if i == 0 {
|
||||
first_swarm_peer_id_and_addr =
|
||||
Some((keypair.public().into_peer_id(), listen_addr.clone()))
|
||||
Some((keypair.public().to_peer_id(), listen_addr.clone()))
|
||||
}
|
||||
|
||||
swarm.listen_on(listen_addr.clone()).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user