Skip to content

Commit

Permalink
Upgrade to libp2p 0.44.0 (paritytech#11009)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kpp authored Apr 29, 2022
1 parent 39a634d commit 95067cc
Show file tree
Hide file tree
Showing 24 changed files with 758 additions and 541 deletions.
711 changes: 465 additions & 246 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 @@ thiserror = "1.0"
futures = "0.3.21"
futures-timer = "3.0.1"
ip_network = "0.4.1"
libp2p = { version = "0.40.0", default-features = false, features = ["kad"] }
libp2p = { version = "0.44.0", default-features = false, features = ["kad"] }
log = "0.4.16"
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.10.0-dev" }
prost = "0.9"
Expand Down
4 changes: 2 additions & 2 deletions client/authority-discovery/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use codec::Decode;
use ip_network::IpNetwork;
use libp2p::{
core::multiaddr,
multihash::{Hasher, Multihash},
multihash::{Multihash, MultihashDigest},
};
use log::{debug, error, log_enabled};
use prometheus_endpoint::{register, Counter, CounterVec, Gauge, Opts, U64};
Expand Down Expand Up @@ -638,7 +638,7 @@ where
}

fn hash_authority_id(id: &[u8]) -> sc_network::KademliaKey {
sc_network::KademliaKey::new(&libp2p::multihash::Sha2_256::digest(id))
sc_network::KademliaKey::new(&libp2p::multihash::Code::Sha2_256.digest(id).digest())
}

// Makes sure all values are the same and returns it
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 @@ clap = { version = "3.1.6", features = ["derive"] }
fdlimit = "0.2.1"
futures = "0.3.21"
hex = "0.4.2"
libp2p = "0.40.0"
libp2p = "0.44.0"
log = "0.4.16"
names = { version = "0.13.0", default-features = false }
rand = "0.7.3"
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.40.0", default-features = false }
libp2p = { version = "0.44.0", default-features = false }
log = "0.4.16"
futures = { version = "0.3.21", 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.21"
futures-timer = "3.0.1"
libp2p = { version = "0.40.0", default-features = false }
libp2p = { version = "0.44.0", default-features = false }
log = "0.4.16"
lru = "0.7.5"
ahash = "0.7.6"
Expand Down
6 changes: 3 additions & 3 deletions client/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ unsigned-varint = { version = "0.6.0", features = [
"asynchronous_codec",
] }
void = "1.0.2"
zeroize = "1.5.4"
libp2p = "0.40.0"
zeroize = "1.4.3"
libp2p = "0.44.0"

[dev-dependencies]
assert_matches = "1.3"
libp2p = { version = "0.40.0", default-features = false }
libp2p = { version = "0.44.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
8 changes: 4 additions & 4 deletions client/network/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use libp2p::{
identify::IdentifyInfo,
kad::record,
swarm::{
toggle::Toggle, NetworkBehaviour, NetworkBehaviourAction, NetworkBehaviourEventProcess,
PollParameters,
behaviour::toggle::Toggle, NetworkBehaviour, NetworkBehaviourAction,
NetworkBehaviourEventProcess, PollParameters,
},
NetworkBehaviour,
};
Expand Down Expand Up @@ -304,7 +304,7 @@ impl<B: BlockT> Behaviour<B> {

/// Start querying a record from the DHT. Will later produce either a `ValueFound` or a
/// `ValueNotFound` event.
pub fn get_value(&mut self, key: &record::Key) {
pub fn get_value(&mut self, key: record::Key) {
self.discovery.get_value(key);
}

Expand Down Expand Up @@ -519,7 +519,7 @@ impl<B: BlockT> Behaviour<B> {
&mut self,
_cx: &mut Context,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<BehaviourOut<B>, <Self as NetworkBehaviour>::ProtocolsHandler>>
) -> Poll<NetworkBehaviourAction<BehaviourOut<B>, <Self as NetworkBehaviour>::ConnectionHandler>>
{
if let Some(event) = self.events.pop_front() {
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event))
Expand Down
10 changes: 3 additions & 7 deletions client/network/src/bitswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,17 @@ impl<B: BlockT> Bitswap<B> {
}

impl<B: BlockT> NetworkBehaviour for Bitswap<B> {
type ProtocolsHandler = OneShotHandler<BitswapConfig, BitswapMessage, HandlerEvent>;
type ConnectionHandler = OneShotHandler<BitswapConfig, BitswapMessage, HandlerEvent>;
type OutEvent = void::Void;

fn new_handler(&mut self) -> Self::ProtocolsHandler {
fn new_handler(&mut self) -> Self::ConnectionHandler {
Default::default()
}

fn addresses_of_peer(&mut self, _peer: &PeerId) -> Vec<Multiaddr> {
Vec::new()
}

fn inject_connected(&mut self, _peer: &PeerId) {}

fn inject_disconnected(&mut self, _peer: &PeerId) {}

fn inject_event(&mut self, peer: PeerId, _connection: ConnectionId, message: HandlerEvent) {
let request = match message {
HandlerEvent::ResponseSent => return,
Expand Down Expand Up @@ -300,7 +296,7 @@ impl<B: BlockT> NetworkBehaviour for Bitswap<B> {
&mut self,
_ctx: &mut Context,
_: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
if let Some((peer_id, message)) = self.ready_blocks.pop_front() {
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
peer_id,
Expand Down
81 changes: 33 additions & 48 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, DialError, IntoProtocolsHandler,
NetworkBehaviour, NetworkBehaviourAction, PollParameters, ProtocolsHandler,
handler::multi::IntoMultiHandler, ConnectionHandler, DialError, IntoConnectionHandler,
NetworkBehaviour, NetworkBehaviourAction, PollParameters,
},
};
use log::{debug, error, info, trace, warn};
Expand Down Expand Up @@ -355,9 +355,9 @@ impl DiscoveryBehaviour {
/// Start fetching a record from the DHT.
///
/// A corresponding `ValueFound` or `ValueNotFound` event will later be generated.
pub fn get_value(&mut self, key: &record::Key) {
pub fn get_value(&mut self, key: record::Key) {
for k in self.kademlias.values_mut() {
k.get_record(key, Quorum::One);
k.get_record(key.clone(), Quorum::One);
}
}

Expand Down Expand Up @@ -433,7 +433,7 @@ impl DiscoveryBehaviour {
&mut self,
pid: ProtocolId,
handler: KademliaHandlerProto<QueryId>,
) -> <DiscoveryBehaviour as NetworkBehaviour>::ProtocolsHandler {
) -> <DiscoveryBehaviour as NetworkBehaviour>::ConnectionHandler {
let mut handlers: HashMap<_, _> = self
.kademlias
.iter_mut()
Expand Down Expand Up @@ -498,10 +498,10 @@ pub enum DiscoveryOut {
}

impl NetworkBehaviour for DiscoveryBehaviour {
type ProtocolsHandler = IntoMultiHandler<ProtocolId, KademliaHandlerProto<QueryId>>;
type ConnectionHandler = IntoMultiHandler<ProtocolId, KademliaHandlerProto<QueryId>>;
type OutEvent = DiscoveryOut;

fn new_handler(&mut self) -> Self::ProtocolsHandler {
fn new_handler(&mut self) -> Self::ConnectionHandler {
let iter = self
.kademlias
.iter_mut()
Expand Down Expand Up @@ -568,6 +568,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
conn: &ConnectionId,
endpoint: &ConnectedPoint,
failed_addresses: Option<&Vec<Multiaddr>>,
other_established: usize,
) {
self.num_connections += 1;
for k in self.kademlias.values_mut() {
Expand All @@ -577,37 +578,37 @@ impl NetworkBehaviour for DiscoveryBehaviour {
conn,
endpoint,
failed_addresses,
other_established,
)
}
}

fn inject_connected(&mut self, peer_id: &PeerId) {
for k in self.kademlias.values_mut() {
NetworkBehaviour::inject_connected(k, peer_id)
}
}

fn inject_connection_closed(
&mut self,
_peer_id: &PeerId,
_conn: &ConnectionId,
_endpoint: &ConnectedPoint,
_handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
peer_id: &PeerId,
conn: &ConnectionId,
endpoint: &ConnectedPoint,
handler: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
remaining_established: usize,
) {
self.num_connections -= 1;
// NetworkBehaviour::inject_connection_closed on Kademlia<MemoryStore> does nothing.
}

fn inject_disconnected(&mut self, peer_id: &PeerId) {
for k in self.kademlias.values_mut() {
NetworkBehaviour::inject_disconnected(k, peer_id)
for (pid, event) in handler.into_iter() {
if let Some(kad) = self.kademlias.get_mut(&pid) {
kad.inject_connection_closed(peer_id, conn, endpoint, event, remaining_established)
} else {
error!(
target: "sub-libp2p",
"inject_connection_closed: no kademlia instance registered for protocol {:?}",
pid,
)
}
}
}

fn inject_dial_failure(
&mut self,
peer_id: Option<PeerId>,
_: Self::ProtocolsHandler,
_: Self::ConnectionHandler,
error: &DialError,
) {
if let Some(peer_id) = peer_id {
Expand All @@ -630,7 +631,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
&mut self,
peer_id: PeerId,
connection: ConnectionId,
(pid, event): <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
(pid, event): <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
) {
if let Some(kad) = self.kademlias.get_mut(&pid) {
return kad.inject_event(peer_id, connection, event)
Expand Down Expand Up @@ -689,7 +690,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
}
}

fn inject_listen_failure(&mut self, _: &Multiaddr, _: &Multiaddr, _: Self::ProtocolsHandler) {
fn inject_listen_failure(&mut self, _: &Multiaddr, _: &Multiaddr, _: Self::ConnectionHandler) {
// NetworkBehaviour::inject_listen_failure on Kademlia<MemoryStore> does nothing.
}

Expand All @@ -709,7 +710,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
&mut self,
cx: &mut Context,
params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
// 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 @@ -770,12 +771,8 @@ 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 { .. } => {
KademliaEvent::InboundRequest { .. } => {
// We are not interested in this event at the moment.
},
KademliaEvent::OutboundQueryCompleted {
Expand Down Expand Up @@ -890,19 +887,10 @@ impl NetworkBehaviour for DiscoveryBehaviour {
warn!(target: "sub-libp2p", "Libp2p => Unhandled Kademlia event: {:?}", e)
},
},
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 } => {
NetworkBehaviourAction::Dial { opts, handler } => {
let pid = pid.clone();
let handler = self.new_handler_with_replacement(pid, handler);
return Poll::Ready(NetworkBehaviourAction::DialPeer {
peer_id,
condition,
handler,
})
return Poll::Ready(NetworkBehaviourAction::Dial { opts, handler })
},
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
Expand Down Expand Up @@ -941,10 +929,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
},
MdnsEvent::Expired(_) => {},
},
NetworkBehaviourAction::DialAddress { .. } => {
unreachable!("mDNS never dials!");
},
NetworkBehaviourAction::DialPeer { .. } => {
NetworkBehaviourAction::Dial { .. } => {
unreachable!("mDNS never dials!");
},
NetworkBehaviourAction::NotifyHandler { event, .. } => match event {}, /* `event` is an enum with no variant */
Expand Down Expand Up @@ -995,7 +980,7 @@ impl MdnsWrapper {
&mut self,
cx: &mut Context<'_>,
params: &mut impl PollParameters,
) -> Poll<NetworkBehaviourAction<MdnsEvent, <Mdns as NetworkBehaviour>::ProtocolsHandler>> {
) -> Poll<NetworkBehaviourAction<MdnsEvent, <Mdns as NetworkBehaviour>::ConnectionHandler>> {
loop {
match self {
Self::Instantiating(fut) =>
Expand Down
29 changes: 26 additions & 3 deletions client/network/src/network_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
//!
//! **Warning**: These APIs are not stable.

use libp2p::{core::ConnectedPoint, Multiaddr};
use libp2p::{
core::{ConnectedPoint, Endpoint as CoreEndpoint},
Multiaddr,
};
use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, HashSet},
Expand Down Expand Up @@ -80,7 +83,7 @@ pub struct NotConnectedPeer {
#[serde(rename_all = "camelCase")]
pub enum PeerEndpoint {
/// We are dialing the given address.
Dialing(Multiaddr),
Dialing(Multiaddr, Endpoint),
/// We are listening.
Listening {
/// Local address of the connection.
Expand All @@ -90,12 +93,32 @@ pub enum PeerEndpoint {
},
}

/// Part of the `NetworkState` struct. Unstable.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Endpoint {
/// The socket comes from a dialer.
Dialer,
/// The socket comes from a listener.
Listener,
}

impl From<ConnectedPoint> for PeerEndpoint {
fn from(endpoint: ConnectedPoint) -> Self {
match endpoint {
ConnectedPoint::Dialer { address } => Self::Dialing(address),
ConnectedPoint::Dialer { address, role_override } =>
Self::Dialing(address, role_override.into()),
ConnectedPoint::Listener { local_addr, send_back_addr } =>
Self::Listening { local_addr, send_back_addr },
}
}
}

impl From<CoreEndpoint> for Endpoint {
fn from(endpoint: CoreEndpoint) -> Self {
match endpoint {
CoreEndpoint::Dialer => Self::Dialer,
CoreEndpoint::Listener => Self::Listener,
}
}
}
Loading

0 comments on commit 95067cc

Please sign in to comment.