-
Notifications
You must be signed in to change notification settings - Fork 710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade libp2p from 0.52.4 to 0.54.1 #6248
Open
nazar-pc
wants to merge
17
commits into
paritytech:master
Choose a base branch
from
nazar-pc:libp2p-0.53.x
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+846
−1,068
Open
Changes from 8 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
de76b66
Initial upgrade libp2p to 0.53.2 (some deprecations are not fixed yet)
nazar-pc 99432cc
Resolve many libp2p deprecations, suppress others
nazar-pc aa14790
Add prdoc
nazar-pc 7903b11
Upgrade libp2p to 0.54.1
nazar-pc 46e722b
Address review feedback
nazar-pc 5591612
Merge remote-tracking branch 'upstream/master' into libp2p-0.53.x
nazar-pc 614bb83
Increase test timeout further to make sure it passes in CI
nazar-pc 56e40c6
Merge remote-tracking branch 'upstream/master' into libp2p-0.53.x
nazar-pc 6c2b051
minor: revert string literal formatting changes
dmitry-markin 28f1849
Add TODO
nazar-pc ba7a246
Merge remote-tracking branch 'upstream/master' into libp2p-0.53.x
nazar-pc 9291511
Merge remote-tracking branch 'upstream/master' into libp2p-0.53.x
nazar-pc 3c6b5ec
Handle kademlia `QueryResult::Bootstrap` event
dmitry-markin f5820d1
Merge remote-tracking branch 'origin/master' into libp2p-0.53.x
dmitry-markin 234a80d
Fix prdoc
dmitry-markin 8e6354d
Add `sc-network-types` to prdoc
dmitry-markin 8d2b703
Fix prdoc
dmitry-markin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
title: Upgrade libp2p to 0.54.1 | ||
|
||
doc: | ||
- audience: [Node Dev, Node Operator] | ||
description: | | ||
Upgrade libp2p from 0.52.4 to 0.54.1 | ||
|
||
crates: | ||
- name: sc-authority-discovery | ||
bump: major | ||
- name: sc-network | ||
bump: major | ||
- name: sc-network-sync | ||
bump: major | ||
- name: sc-telemetry | ||
bump: major |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,12 +53,12 @@ use futures::prelude::*; | |
use futures_timer::Delay; | ||
use ip_network::IpNetwork; | ||
use libp2p::{ | ||
core::{Endpoint, Multiaddr}, | ||
core::{transport::PortUse, Endpoint, Multiaddr}, | ||
kad::{ | ||
self, | ||
record::store::{MemoryStore, RecordStore}, | ||
store::{MemoryStore, RecordStore}, | ||
Behaviour as Kademlia, BucketInserts, Config as KademliaConfig, Event as KademliaEvent, | ||
GetClosestPeersError, GetRecordOk, PeerRecord, QueryId, QueryResult, Quorum, Record, | ||
Event, GetClosestPeersError, GetRecordOk, PeerRecord, QueryId, QueryResult, Quorum, Record, | ||
RecordKey, | ||
}, | ||
mdns::{self, tokio::Behaviour as TokioMdns}, | ||
|
@@ -68,8 +68,8 @@ use libp2p::{ | |
toggle::{Toggle, ToggleConnectionHandler}, | ||
DialFailure, ExternalAddrConfirmed, FromSwarm, | ||
}, | ||
ConnectionDenied, ConnectionId, DialError, NetworkBehaviour, PollParameters, | ||
StreamProtocol, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm, | ||
ConnectionDenied, ConnectionId, DialError, NetworkBehaviour, StreamProtocol, THandler, | ||
THandlerInEvent, THandlerOutEvent, ToSwarm, | ||
}, | ||
PeerId, | ||
}; | ||
|
@@ -214,23 +214,14 @@ impl DiscoveryConfig { | |
enable_mdns, | ||
kademlia_disjoint_query_paths, | ||
kademlia_protocol, | ||
kademlia_legacy_protocol, | ||
kademlia_legacy_protocol: _, | ||
kademlia_replication_factor, | ||
} = self; | ||
|
||
let kademlia = if let Some(ref kademlia_protocol) = kademlia_protocol { | ||
let mut config = KademliaConfig::default(); | ||
let mut config = KademliaConfig::new(kademlia_protocol.clone()); | ||
|
||
config.set_replication_factor(kademlia_replication_factor); | ||
// Populate kad with both the legacy and the new protocol names. | ||
// Remove the legacy protocol: | ||
// https://github.com/paritytech/polkadot-sdk/issues/504 | ||
let kademlia_protocols = if let Some(legacy_protocol) = kademlia_legacy_protocol { | ||
vec![kademlia_protocol.clone(), legacy_protocol] | ||
} else { | ||
vec![kademlia_protocol.clone()] | ||
}; | ||
Comment on lines
-225
to
-232
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good! We should finally do this) |
||
config.set_protocol_names(kademlia_protocols.into_iter().map(Into::into).collect()); | ||
|
||
config.set_record_filtering(libp2p::kad::StoreInserts::FilterBoth); | ||
|
||
|
@@ -613,12 +604,14 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
peer: PeerId, | ||
addr: &Multiaddr, | ||
role_override: Endpoint, | ||
port_use: PortUse, | ||
) -> Result<THandler<Self>, ConnectionDenied> { | ||
self.kademlia.handle_established_outbound_connection( | ||
connection_id, | ||
peer, | ||
addr, | ||
role_override, | ||
port_use, | ||
) | ||
} | ||
|
||
|
@@ -690,7 +683,7 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
Ok(list.into_iter().collect()) | ||
} | ||
|
||
fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>) { | ||
fn on_swarm_event(&mut self, event: FromSwarm) { | ||
match event { | ||
FromSwarm::ConnectionEstablished(e) => { | ||
self.num_connections += 1; | ||
|
@@ -765,6 +758,10 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
|
||
self.kademlia.on_swarm_event(FromSwarm::ExternalAddrConfirmed(e)); | ||
}, | ||
event => { | ||
debug!(target: "sub-libp2p", "New unknown `FromSwarm` libp2p event: {event:?}"); | ||
self.kademlia.on_swarm_event(event); | ||
}, | ||
} | ||
} | ||
|
||
|
@@ -777,11 +774,7 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
self.kademlia.on_connection_handler_event(peer_id, connection_id, event); | ||
} | ||
|
||
fn poll( | ||
&mut self, | ||
cx: &mut Context, | ||
params: &mut impl PollParameters, | ||
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> { | ||
fn poll(&mut self, cx: &mut Context) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> { | ||
// Immediately process the content of `discovered`. | ||
if let Some(ev) = self.pending_events.pop_front() { | ||
return Poll::Ready(ToSwarm::GenerateEvent(ev)) | ||
|
@@ -824,7 +817,7 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
} | ||
} | ||
|
||
while let Poll::Ready(ev) = self.kademlia.poll(cx, params) { | ||
while let Poll::Ready(ev) = self.kademlia.poll(cx) { | ||
match ev { | ||
ToSwarm::GenerateEvent(ev) => match ev { | ||
KademliaEvent::RoutingUpdated { peer, .. } => { | ||
|
@@ -1011,26 +1004,21 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
KademliaEvent::OutboundQueryProgressed { result: e, .. } => { | ||
warn!(target: "sub-libp2p", "Libp2p => Unhandled Kademlia event: {:?}", e) | ||
}, | ||
Event::ModeChanged { new_mode } => { | ||
debug!(target: "sub-libp2p", "Libp2p => Kademlia mode changed: {new_mode}") | ||
}, | ||
}, | ||
ToSwarm::Dial { opts } => return Poll::Ready(ToSwarm::Dial { opts }), | ||
ToSwarm::NotifyHandler { peer_id, handler, event } => | ||
return Poll::Ready(ToSwarm::NotifyHandler { peer_id, handler, event }), | ||
ToSwarm::CloseConnection { peer_id, connection } => | ||
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }), | ||
ToSwarm::NewExternalAddrCandidate(observed) => | ||
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)), | ||
ToSwarm::ExternalAddrConfirmed(addr) => | ||
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)), | ||
ToSwarm::ExternalAddrExpired(addr) => | ||
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)), | ||
ToSwarm::ListenOn { opts } => return Poll::Ready(ToSwarm::ListenOn { opts }), | ||
ToSwarm::RemoveListener { id } => | ||
return Poll::Ready(ToSwarm::RemoveListener { id }), | ||
event => { | ||
return Poll::Ready(event.map_out(|_| { | ||
unreachable!("`GenerateEvent` is handled in a branch above; qed") | ||
})); | ||
}, | ||
} | ||
} | ||
|
||
// Poll mDNS. | ||
while let Poll::Ready(ev) = self.mdns.poll(cx, params) { | ||
while let Poll::Ready(ev) = self.mdns.poll(cx) { | ||
match ev { | ||
ToSwarm::GenerateEvent(event) => match event { | ||
mdns::Event::Discovered(list) => { | ||
|
@@ -1052,17 +1040,17 @@ impl NetworkBehaviour for DiscoveryBehaviour { | |
}, | ||
// `event` is an enum with no variant | ||
ToSwarm::NotifyHandler { event, .. } => match event {}, | ||
ToSwarm::CloseConnection { peer_id, connection } => | ||
return Poll::Ready(ToSwarm::CloseConnection { peer_id, connection }), | ||
ToSwarm::NewExternalAddrCandidate(observed) => | ||
return Poll::Ready(ToSwarm::NewExternalAddrCandidate(observed)), | ||
ToSwarm::ExternalAddrConfirmed(addr) => | ||
return Poll::Ready(ToSwarm::ExternalAddrConfirmed(addr)), | ||
ToSwarm::ExternalAddrExpired(addr) => | ||
return Poll::Ready(ToSwarm::ExternalAddrExpired(addr)), | ||
ToSwarm::ListenOn { opts } => return Poll::Ready(ToSwarm::ListenOn { opts }), | ||
ToSwarm::RemoveListener { id } => | ||
return Poll::Ready(ToSwarm::RemoveListener { id }), | ||
event => { | ||
return Poll::Ready( | ||
event | ||
.map_in(|_| { | ||
unreachable!("`NotifyHandler` is handled in a branch above; qed") | ||
}) | ||
.map_out(|_| { | ||
unreachable!("`GenerateEvent` is handled in a branch above; qed") | ||
}), | ||
); | ||
}, | ||
} | ||
} | ||
|
||
|
@@ -1105,21 +1093,14 @@ mod tests { | |
}, | ||
identity::Keypair, | ||
noise, | ||
swarm::{Executor, Swarm, SwarmEvent}, | ||
swarm::{Swarm, SwarmEvent}, | ||
yamux, Multiaddr, | ||
}; | ||
use sp_core::hash::H256; | ||
use std::{collections::HashSet, pin::Pin, task::Poll}; | ||
use std::{collections::HashSet, task::Poll, time::Duration}; | ||
|
||
struct TokioExecutor(tokio::runtime::Runtime); | ||
impl Executor for TokioExecutor { | ||
fn exec(&self, f: Pin<Box<dyn Future<Output = ()> + Send>>) { | ||
let _ = self.0.spawn(f); | ||
} | ||
} | ||
|
||
#[test] | ||
fn discovery_working() { | ||
#[tokio::test] | ||
async fn discovery_working() { | ||
let mut first_swarm_peer_id_and_addr = None; | ||
|
||
let genesis_hash = H256::from_low_u64_be(1); | ||
|
@@ -1130,42 +1111,40 @@ mod tests { | |
// the first swarm via `with_permanent_addresses`. | ||
let mut swarms = (0..25) | ||
.map(|i| { | ||
let keypair = Keypair::generate_ed25519(); | ||
|
||
let transport = MemoryTransport::new() | ||
.upgrade(upgrade::Version::V1) | ||
.authenticate(noise::Config::new(&keypair).unwrap()) | ||
.multiplex(yamux::Config::default()) | ||
.boxed(); | ||
|
||
let behaviour = { | ||
let mut config = DiscoveryConfig::new(keypair.public().to_peer_id()); | ||
config | ||
.with_permanent_addresses(first_swarm_peer_id_and_addr.clone()) | ||
.allow_private_ip(true) | ||
.allow_non_globals_in_dht(true) | ||
.discovery_limit(50) | ||
.with_kademlia(genesis_hash, fork_id, &protocol_id); | ||
|
||
config.finish() | ||
}; | ||
|
||
let runtime = tokio::runtime::Runtime::new().unwrap(); | ||
#[allow(deprecated)] | ||
let mut swarm = libp2p::swarm::SwarmBuilder::with_executor( | ||
transport, | ||
behaviour, | ||
keypair.public().to_peer_id(), | ||
TokioExecutor(runtime), | ||
) | ||
.build(); | ||
let mut swarm = libp2p::SwarmBuilder::with_new_identity() | ||
.with_tokio() | ||
.with_other_transport(|keypair| { | ||
MemoryTransport::new() | ||
.upgrade(upgrade::Version::V1) | ||
.authenticate(noise::Config::new(&keypair).unwrap()) | ||
.multiplex(yamux::Config::default()) | ||
.boxed() | ||
}) | ||
.unwrap() | ||
.with_behaviour(|keypair| { | ||
let mut config = DiscoveryConfig::new(keypair.public().to_peer_id()); | ||
config | ||
.with_permanent_addresses(first_swarm_peer_id_and_addr.clone()) | ||
.allow_private_ip(true) | ||
.allow_non_globals_in_dht(true) | ||
.discovery_limit(50) | ||
.with_kademlia(genesis_hash, fork_id, &protocol_id); | ||
|
||
config.finish() | ||
}) | ||
.unwrap() | ||
.with_swarm_config(|config| { | ||
// This is taken care of by notification protocols in non-test environment | ||
config.with_idle_connection_timeout(Duration::from_secs(10)) | ||
}) | ||
.build(); | ||
|
||
let listen_addr: Multiaddr = | ||
format!("/memory/{}", rand::random::<u64>()).parse().unwrap(); | ||
|
||
if i == 0 { | ||
first_swarm_peer_id_and_addr = | ||
Some((keypair.public().to_peer_id(), listen_addr.clone())) | ||
Some((*swarm.local_peer_id(), listen_addr.clone())) | ||
} | ||
|
||
swarm.listen_on(listen_addr.clone()).unwrap(); | ||
|
@@ -1252,7 +1231,7 @@ mod tests { | |
} | ||
}); | ||
|
||
futures::executor::block_on(fut); | ||
fut.await | ||
} | ||
|
||
#[test] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should wait a bit and scrape the network. I believe everything should be fine, but let's double check first.
I guess we can merge #6109 before this one as it also handles legacy_protocol removal, but shouldn't matter too much.
I'll have a look this week and investigate versions, generally I think its safe for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I specifically used
kademlia_legacy_protocol: _,
above instead of full removal of the property to reduce the inevitable conflict with #6109 whichever ends up being first.