Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Bump libp2p to 0.40.0 (#10035)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kpp authored Nov 25, 2021
1 parent 3009d32 commit 4617267
Show file tree
Hide file tree
Showing 21 changed files with 564 additions and 492 deletions.
388 changes: 163 additions & 225 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/authority-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ derive_more = "0.99.16"
futures = "0.3.9"
futures-timer = "3.0.1"
ip_network = "0.4.0"
libp2p = { version = "0.39.1", default-features = false, features = ["kad"] }
libp2p = { version = "0.40.0", default-features = false, features = ["kad"] }
log = "0.4.8"
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.10.0-dev" }
prost = "0.9"
Expand Down
2 changes: 1 addition & 1 deletion client/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ regex = "1.5.4"
tokio = { version = "1.13", features = [ "signal", "rt-multi-thread" ] }
futures = "0.3.9"
fdlimit = "0.2.1"
libp2p = "0.39.1"
libp2p = "0.40.0"
parity-scale-codec = "2.3.1"
hex = "0.4.2"
rand = "0.7.3"
Expand Down
2 changes: 1 addition & 1 deletion client/cli/src/commands/build_spec_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl BuildSpecCmd {

if spec.boot_nodes().is_empty() && !self.disable_default_bootnode {
let keys = network_config.node_key.into_keypair()?;
let peer_id = keys.public().into_peer_id();
let peer_id = keys.public().to_peer_id();
let addr = MultiaddrWithPeerId {
multiaddr: build_multiaddr![Ip4([127, 0, 0, 1]), Tcp(30333u16)],
peer_id,
Expand Down
2 changes: 1 addition & 1 deletion client/cli/src/commands/generate_node_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl GenerateNodeKeyCmd {
pub fn run(&self) -> Result<(), Error> {
let keypair = libp2p_ed25519::Keypair::generate();
let secret = keypair.secret();
let peer_id = PublicKey::Ed25519(keypair.public()).into_peer_id();
let peer_id = PublicKey::Ed25519(keypair.public()).to_peer_id();
let secret_hex = hex::encode(secret.as_ref());

match &self.file {
Expand Down
2 changes: 1 addition & 1 deletion client/cli/src/commands/inspect_node_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl InspectNodeKeyCmd {
ed25519::SecretKey::from_bytes(&mut file_content).map_err(|_| "Bad node key file")?;

let keypair = ed25519::Keypair::from(secret);
let peer_id = PublicKey::Ed25519(keypair.public()).into_peer_id();
let peer_id = PublicKey::Ed25519(keypair.public()).to_peer_id();

println!("{}", peer_id);

Expand Down
2 changes: 1 addition & 1 deletion client/consensus/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
thiserror = "1.0.30"
libp2p = { version = "0.39.1", default-features = false }
libp2p = { version = "0.40.0", default-features = false }
log = "0.4.8"
futures = { version = "0.3.1", features = ["thread-pool"] }
futures-timer = "3.0.1"
Expand Down
2 changes: 1 addition & 1 deletion client/network-gossip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
futures = "0.3.9"
futures-timer = "3.0.1"
libp2p = { version = "0.39.1", default-features = false }
libp2p = { version = "0.40.0", default-features = false }
log = "0.4.8"
lru = "0.7.0"
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.10.0-dev", path = "../../utils/prometheus" }
Expand Down
4 changes: 2 additions & 2 deletions client/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ unsigned-varint = { version = "0.6.0", features = [
] }
void = "1.0.2"
zeroize = "1.4.2"
libp2p = "0.39.1"
libp2p = "0.40.0"

[dev-dependencies]
assert_matches = "1.3"
libp2p = { version = "0.39.1", default-features = false }
libp2p = { version = "0.40.0", default-features = false }
quickcheck = "1.0.3"
rand = "0.7.2"
sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" }
Expand Down
12 changes: 8 additions & 4 deletions client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use libp2p::{
core::{Multiaddr, PeerId, PublicKey},
identify::IdentifyInfo,
kad::record,
swarm::{toggle::Toggle, NetworkBehaviourAction, NetworkBehaviourEventProcess, PollParameters},
swarm::{
toggle::Toggle, NetworkBehaviour, NetworkBehaviourAction, NetworkBehaviourEventProcess,
PollParameters,
},
NetworkBehaviour,
};
use log::debug;
Expand All @@ -58,7 +61,7 @@ pub use crate::request_responses::{

/// General behaviour of the network. Combines all protocols together.
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "BehaviourOut<B>", poll_method = "poll")]
#[behaviour(out_event = "BehaviourOut<B>", poll_method = "poll", event_process = true)]
pub struct Behaviour<B: BlockT> {
/// All the substrate-specific protocols.
substrate: Protocol<B>,
Expand Down Expand Up @@ -512,11 +515,12 @@ impl<B: BlockT> NetworkBehaviourEventProcess<DiscoveryOut> for Behaviour<B> {
}

impl<B: BlockT> Behaviour<B> {
fn poll<TEv>(
fn poll(
&mut self,
_cx: &mut Context,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<TEv, BehaviourOut<B>>> {
) -> Poll<NetworkBehaviourAction<BehaviourOut<B>, <Self as NetworkBehaviour>::ProtocolsHandler>>
{
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event))
}
Expand Down
14 changes: 6 additions & 8 deletions client/network/src/bitswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ use libp2p::{
UpgradeInfo,
},
swarm::{
IntoProtocolsHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
OneShotHandler, PollParameters, ProtocolsHandler,
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, OneShotHandler, PollParameters,
},
};
use log::{debug, error, trace};
Expand Down Expand Up @@ -297,12 +296,11 @@ impl<B: BlockT> NetworkBehaviour for Bitswap<B> {
self.ready_blocks.push_back((peer, response));
}

fn poll(&mut self, _ctx: &mut Context, _: &mut impl PollParameters) -> Poll<
NetworkBehaviourAction<
<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent,
Self::OutEvent,
>,
>{
fn poll(
&mut self,
_ctx: &mut Context,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
if let Some((peer_id, message)) = self.ready_blocks.pop_front() {
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
peer_id,
Expand Down
135 changes: 95 additions & 40 deletions client/network/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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,
)
}
}

Expand All @@ -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) {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit 4617267

Please sign in to comment.