chore: update libp2p to 0.52.1 (#14429)

* update libp2p to 0.52.0

* proto name now must implement `AsRef<str>`

* update libp2p version everywhere

* ToSwarm, FromBehaviour, ToBehaviour

also LocalProtocolsChange and RemoteProtocolsChange

* new NetworkBehaviour invariants

* replace `Vec<u8>` with `StreamProtocol`

* rename ConnectionHandlerEvent::Custom to NotifyBehaviour

* remove DialError & ListenError invariants

also fix pending_events

* use connection_limits::Behaviour

See https://github.com/libp2p/rust-libp2p/pull/3885

* impl `void::Void` for `BehaviourOut`

also use `Behaviour::with_codec`

* KademliaHandler no longer public

* fix StreamProtocol construction

* update libp2p-identify to 0.2.0

* remove non-existing methods from PollParameters

rename ConnectionHandlerUpgrErr to StreamUpgradeError

* `P2p` now contains `PeerId`, not `Multihash`

* use multihash-codetable crate

* update Cargo.lock

* reformat text

* comment out tests for now

* remove `.into()` from P2p

* confirm observed addr manually

See https://github.com/libp2p/rust-libp2p/blob/master/protocols/identify/CHANGELOG.md#0430

* remove SwarmEvent::Banned

since we're not using `ban_peer_id`, this can be safely removed.
we may want to introduce `libp2p::allow_block_list` module in the future.

* fix imports

* replace `libp2p` with smaller deps in network-gossip

* bring back tests

* finish rewriting tests

* uncomment handler tests

* Revert "uncomment handler tests"

This reverts commit 720a06815887f4e10767c62b58864a7ec3a48e50.

* add a fixme

* update Cargo.lock

* remove extra From

* make void uninhabited

* fix discovery test

* use autonat protocols

confirming external addresses manually is unsafe in open networks

* fix SyncNotificationsClogged invariant

* only set server mode manually in tests

doubt that we need to set it on node since we're adding public addresses

* address @dmitry-markin comments

* remove autonat

* removed unused var

* fix EOL

* update smallvec and sha2

in attempt to compile polkadot

* bump k256

in attempt to build cumulus

---------

Co-authored-by: parity-processbot <>
This commit is contained in:
Anton
2023-07-25 15:12:24 +04:00
committed by GitHub
parent ae018a01a4
commit 59d8b86450
44 changed files with 1308 additions and 2198 deletions
+57 -59
View File
@@ -55,17 +55,15 @@ use crate::{
use either::Either;
use futures::{channel::oneshot, prelude::*};
#[allow(deprecated)]
use libp2p::{
connection_limits::Exceeded,
connection_limits::{ConnectionLimits, Exceeded},
core::{upgrade, ConnectedPoint, Endpoint},
identify::Info as IdentifyInfo,
kad::record::Key as KademliaKey,
multiaddr,
ping::Failure as PingFailure,
swarm::{
AddressScore, ConnectionError, ConnectionId, ConnectionLimits, DialError, Executor,
ListenError, NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent, THandlerErr,
ConnectionError, ConnectionId, DialError, Executor, ListenError, NetworkBehaviour, Swarm,
SwarmBuilder, SwarmEvent, THandlerErr,
},
Multiaddr, PeerId,
};
@@ -351,6 +349,11 @@ where
discovery_config,
request_response_protocols,
peerset_handle.clone(),
ConnectionLimits::default()
.with_max_established_per_peer(Some(crate::MAX_CONNECTIONS_PER_PEER as u32))
.with_max_established_incoming(Some(
crate::MAX_CONNECTIONS_ESTABLISHED_INCOMING,
)),
);
match result {
@@ -374,15 +377,7 @@ where
SpawnImpl(params.executor),
)
};
#[allow(deprecated)]
let builder = builder
.connection_limits(
ConnectionLimits::default()
.with_max_established_per_peer(Some(crate::MAX_CONNECTIONS_PER_PEER as u32))
.with_max_established_incoming(Some(
crate::MAX_CONNECTIONS_ESTABLISHED_INCOMING,
)),
)
.substream_upgrade_protocol_override(upgrade::Version::V1Lazy)
.notify_handler_buffer_size(NonZeroUsize::new(32).expect("32 != 0; qed"))
// NOTE: 24 is somewhat arbitrary and should be tuned in the future if necessary.
@@ -414,11 +409,7 @@ where
// Add external addresses.
for addr in &network_config.public_addresses {
Swarm::<Behaviour<B>>::add_external_address(
&mut swarm,
addr.clone(),
AddressScore::Infinite,
);
Swarm::<Behaviour<B>>::add_external_address(&mut swarm, addr.clone());
}
let external_addresses = Arc::new(Mutex::new(Vec::new()));
@@ -602,7 +593,7 @@ where
let peer_id = Swarm::<Behaviour<B>>::local_peer_id(swarm).to_base58();
let listened_addresses = swarm.listeners().cloned().collect();
let external_addresses = swarm.external_addresses().map(|r| &r.addr).cloned().collect();
let external_addresses = swarm.external_addresses().cloned().collect();
NetworkState {
peer_id,
@@ -680,8 +671,7 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkService<B, H> {
.into_iter()
.map(|mut addr| {
let peer = match addr.pop() {
Some(multiaddr::Protocol::P2p(key)) => PeerId::from_multihash(key)
.map_err(|_| "Invalid PeerId format".to_string())?,
Some(multiaddr::Protocol::P2p(peer_id)) => peer_id,
_ => return Err("Missing PeerId from address".to_string()),
};
@@ -1197,8 +1187,7 @@ where
self.network_service.behaviour_mut().user_protocol_mut().num_connected_peers();
self.num_connected.store(num_connected_peers, Ordering::Relaxed);
{
let external_addresses =
self.network_service.external_addresses().map(|r| &r.addr).cloned().collect();
let external_addresses = self.network_service.external_addresses().cloned().collect();
*self.external_addresses.lock() = external_addresses;
let listen_addresses =
@@ -1401,7 +1390,12 @@ where
peer_id,
info:
IdentifyInfo {
protocol_version, agent_version, mut listen_addrs, protocols, ..
protocol_version,
agent_version,
mut listen_addrs,
protocols,
observed_addr,
..
},
}) => {
if listen_addrs.len() > 30 {
@@ -1413,11 +1407,17 @@ where
listen_addrs.truncate(30);
}
for addr in listen_addrs {
self.network_service
.behaviour_mut()
.add_self_reported_address_to_dht(&peer_id, &protocols, addr);
self.network_service.behaviour_mut().add_self_reported_address_to_dht(
&peer_id,
&protocols,
addr.clone(),
);
}
self.network_service.behaviour_mut().user_protocol_mut().add_known_peer(peer_id);
// Confirm the observed address manually since they are no longer trusted by
// default (libp2p >= 0.52)
// TODO: remove this when/if AutoNAT is implemented.
self.network_service.add_external_address(observed_addr);
},
SwarmEvent::Behaviour(BehaviourOut::Discovered(peer_id)) => {
self.network_service.behaviour_mut().user_protocol_mut().add_known_peer(peer_id);
@@ -1562,8 +1562,14 @@ where
}
}
},
SwarmEvent::ConnectionClosed { peer_id, cause, endpoint, num_established } => {
debug!(target: "sub-libp2p", "Libp2p => Disconnected({:?}, {:?})", peer_id, cause);
SwarmEvent::ConnectionClosed {
connection_id,
peer_id,
cause,
endpoint,
num_established,
} => {
debug!(target: "sub-libp2p", "Libp2p => Disconnected({:?} via {:?}: {:?})", peer_id, connection_id, cause);
if let Some(metrics) = self.metrics.as_ref() {
let direction = match endpoint {
ConnectedPoint::Dialer { .. } => "out",
@@ -1572,10 +1578,12 @@ where
let reason = match cause {
Some(ConnectionError::IO(_)) => "transport-error",
Some(ConnectionError::Handler(Either::Left(Either::Left(
Either::Right(Either::Left(PingFailure::Timeout)),
Either::Left(Either::Right(_)),
)))) => "ping-timeout",
Some(ConnectionError::Handler(Either::Left(Either::Left(
Either::Left(NotifsHandlerError::SyncNotificationsClogged),
Either::Left(Either::Left(
NotifsHandlerError::SyncNotificationsClogged,
)),
)))) => "sync-notifications-clogged",
Some(ConnectionError::Handler(_)) => "protocol-error",
Some(ConnectionError::KeepAliveTimeout) => "keep-alive-timeout",
@@ -1601,12 +1609,12 @@ where
metrics.listeners_local_addresses.dec();
}
},
SwarmEvent::OutgoingConnectionError { peer_id, error } => {
SwarmEvent::OutgoingConnectionError { connection_id, peer_id, error } => {
if let Some(peer_id) = peer_id {
trace!(
target: "sub-libp2p",
"Libp2p => Failed to reach {:?}: {}",
peer_id, error,
"Libp2p => Failed to reach {:?} via {:?}: {}",
peer_id, connection_id, error,
);
let not_reported = !self.reported_invalid_boot_nodes.contains(&peer_id);
@@ -1644,12 +1652,9 @@ where
} else {
None
},
DialError::ConnectionLimit(_) => Some("limit-reached"),
DialError::InvalidPeerId(_) |
DialError::WrongPeerId { .. } |
DialError::LocalPeerId { .. } => Some("invalid-peer-id"),
DialError::WrongPeerId { .. } | DialError::LocalPeerId { .. } =>
Some("invalid-peer-id"),
DialError::Transport(_) => Some("transport-error"),
DialError::Banned |
DialError::NoAddresses |
DialError::DialPeerConditionFalse(_) |
DialError::Aborted => None, // ignore them
@@ -1659,21 +1664,26 @@ where
}
}
},
SwarmEvent::Dialing(peer_id) => {
trace!(target: "sub-libp2p", "Libp2p => Dialing({:?})", peer_id)
SwarmEvent::Dialing { peer_id, connection_id } => {
trace!(target: "sub-libp2p", "Libp2p => Dialing({:?} via {:?})", peer_id, connection_id)
},
SwarmEvent::IncomingConnection { local_addr, send_back_addr } => {
trace!(target: "sub-libp2p", "Libp2p => IncomingConnection({},{}))",
local_addr, send_back_addr);
SwarmEvent::IncomingConnection { connection_id, local_addr, send_back_addr } => {
trace!(target: "sub-libp2p", "Libp2p => IncomingConnection({},{} via {:?}))",
local_addr, send_back_addr, connection_id);
if let Some(metrics) = self.metrics.as_ref() {
metrics.incoming_connections_total.inc();
}
},
SwarmEvent::IncomingConnectionError { local_addr, send_back_addr, error } => {
SwarmEvent::IncomingConnectionError {
connection_id,
local_addr,
send_back_addr,
error,
} => {
debug!(
target: "sub-libp2p",
"Libp2p => IncomingConnectionError({},{}): {}",
local_addr, send_back_addr, error,
"Libp2p => IncomingConnectionError({},{} via {:?}): {}",
local_addr, send_back_addr, connection_id, error,
);
if let Some(metrics) = self.metrics.as_ref() {
#[allow(deprecated)]
@@ -1684,7 +1694,6 @@ where
} else {
None
},
ListenError::ConnectionLimit(_) => Some("limit-reached"),
ListenError::WrongPeerId { .. } | ListenError::LocalPeerId { .. } =>
Some("invalid-peer-id"),
ListenError::Transport(_) => Some("transport-error"),
@@ -1699,17 +1708,6 @@ where
}
}
},
#[allow(deprecated)]
SwarmEvent::BannedPeer { peer_id, endpoint } => {
debug!(
target: "sub-libp2p",
"Libp2p => BannedPeer({}). Connected via {:?}.",
peer_id, endpoint,
);
if let Some(metrics) = self.metrics.as_ref() {
metrics.incoming_connections_errors_total.with_label_values(&["banned"]).inc();
}
},
SwarmEvent::ListenerClosed { reason, addresses, .. } => {
if let Some(metrics) = self.metrics.as_ref() {
metrics.listeners_local_addresses.sub(addresses.len() as u64);