Skip to content

Commit 95067cc

Browse files
authored
Upgrade to libp2p 0.44.0 (paritytech#11009)
* 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
1 parent 39a634d commit 95067cc

24 files changed

Lines changed: 758 additions & 541 deletions

File tree

Cargo.lock

Lines changed: 465 additions & 246 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/authority-discovery/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ thiserror = "1.0"
2323
futures = "0.3.21"
2424
futures-timer = "3.0.1"
2525
ip_network = "0.4.1"
26-
libp2p = { version = "0.40.0", default-features = false, features = ["kad"] }
26+
libp2p = { version = "0.44.0", default-features = false, features = ["kad"] }
2727
log = "0.4.16"
2828
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.10.0-dev" }
2929
prost = "0.9"

client/authority-discovery/src/worker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use codec::Decode;
3737
use ip_network::IpNetwork;
3838
use libp2p::{
3939
core::multiaddr,
40-
multihash::{Hasher, Multihash},
40+
multihash::{Multihash, MultihashDigest},
4141
};
4242
use log::{debug, error, log_enabled};
4343
use prometheus_endpoint::{register, Counter, CounterVec, Gauge, Opts, U64};
@@ -638,7 +638,7 @@ where
638638
}
639639

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

644644
// Makes sure all values are the same and returns it

client/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ clap = { version = "3.1.6", features = ["derive"] }
1818
fdlimit = "0.2.1"
1919
futures = "0.3.21"
2020
hex = "0.4.2"
21-
libp2p = "0.40.0"
21+
libp2p = "0.44.0"
2222
log = "0.4.16"
2323
names = { version = "0.13.0", default-features = false }
2424
rand = "0.7.3"

client/consensus/common/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1414

1515
[dependencies]
1616
thiserror = "1.0.30"
17-
libp2p = { version = "0.40.0", default-features = false }
17+
libp2p = { version = "0.44.0", default-features = false }
1818
log = "0.4.16"
1919
futures = { version = "0.3.21", features = ["thread-pool"] }
2020
futures-timer = "3.0.1"

client/network-gossip/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1717
[dependencies]
1818
futures = "0.3.21"
1919
futures-timer = "3.0.1"
20-
libp2p = { version = "0.40.0", default-features = false }
20+
libp2p = { version = "0.44.0", default-features = false }
2121
log = "0.4.16"
2222
lru = "0.7.5"
2323
ahash = "0.7.6"

client/network/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ unsigned-varint = { version = "0.6.0", features = [
6161
"asynchronous_codec",
6262
] }
6363
void = "1.0.2"
64-
zeroize = "1.5.4"
65-
libp2p = "0.40.0"
64+
zeroize = "1.4.3"
65+
libp2p = "0.44.0"
6666

6767
[dev-dependencies]
6868
assert_matches = "1.3"
69-
libp2p = { version = "0.40.0", default-features = false }
69+
libp2p = { version = "0.44.0", default-features = false }
7070
quickcheck = "1.0.3"
7171
rand = "0.7.2"
7272
sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" }

client/network/src/behaviour.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use libp2p::{
3333
identify::IdentifyInfo,
3434
kad::record,
3535
swarm::{
36-
toggle::Toggle, NetworkBehaviour, NetworkBehaviourAction, NetworkBehaviourEventProcess,
37-
PollParameters,
36+
behaviour::toggle::Toggle, NetworkBehaviour, NetworkBehaviourAction,
37+
NetworkBehaviourEventProcess, PollParameters,
3838
},
3939
NetworkBehaviour,
4040
};
@@ -304,7 +304,7 @@ impl<B: BlockT> Behaviour<B> {
304304

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

@@ -519,7 +519,7 @@ impl<B: BlockT> Behaviour<B> {
519519
&mut self,
520520
_cx: &mut Context,
521521
_: &mut impl PollParameters,
522-
) -> Poll<NetworkBehaviourAction<BehaviourOut<B>, <Self as NetworkBehaviour>::ProtocolsHandler>>
522+
) -> Poll<NetworkBehaviourAction<BehaviourOut<B>, <Self as NetworkBehaviour>::ConnectionHandler>>
523523
{
524524
if let Some(event) = self.events.pop_front() {
525525
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event))

client/network/src/bitswap.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,17 @@ impl<B: BlockT> Bitswap<B> {
194194
}
195195

196196
impl<B: BlockT> NetworkBehaviour for Bitswap<B> {
197-
type ProtocolsHandler = OneShotHandler<BitswapConfig, BitswapMessage, HandlerEvent>;
197+
type ConnectionHandler = OneShotHandler<BitswapConfig, BitswapMessage, HandlerEvent>;
198198
type OutEvent = void::Void;
199199

200-
fn new_handler(&mut self) -> Self::ProtocolsHandler {
200+
fn new_handler(&mut self) -> Self::ConnectionHandler {
201201
Default::default()
202202
}
203203

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

208-
fn inject_connected(&mut self, _peer: &PeerId) {}
209-
210-
fn inject_disconnected(&mut self, _peer: &PeerId) {}
211-
212208
fn inject_event(&mut self, peer: PeerId, _connection: ConnectionId, message: HandlerEvent) {
213209
let request = match message {
214210
HandlerEvent::ResponseSent => return,
@@ -300,7 +296,7 @@ impl<B: BlockT> NetworkBehaviour for Bitswap<B> {
300296
&mut self,
301297
_ctx: &mut Context,
302298
_: &mut impl PollParameters,
303-
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
299+
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
304300
if let Some((peer_id, message)) = self.ready_blocks.pop_front() {
305301
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
306302
peer_id,

client/network/src/discovery.rs

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ use libp2p::{
6767
mdns::{Mdns, MdnsConfig, MdnsEvent},
6868
multiaddr::Protocol,
6969
swarm::{
70-
protocols_handler::multi::IntoMultiHandler, DialError, IntoProtocolsHandler,
71-
NetworkBehaviour, NetworkBehaviourAction, PollParameters, ProtocolsHandler,
70+
handler::multi::IntoMultiHandler, ConnectionHandler, DialError, IntoConnectionHandler,
71+
NetworkBehaviour, NetworkBehaviourAction, PollParameters,
7272
},
7373
};
7474
use log::{debug, error, info, trace, warn};
@@ -355,9 +355,9 @@ impl DiscoveryBehaviour {
355355
/// Start fetching a record from the DHT.
356356
///
357357
/// A corresponding `ValueFound` or `ValueNotFound` event will later be generated.
358-
pub fn get_value(&mut self, key: &record::Key) {
358+
pub fn get_value(&mut self, key: record::Key) {
359359
for k in self.kademlias.values_mut() {
360-
k.get_record(key, Quorum::One);
360+
k.get_record(key.clone(), Quorum::One);
361361
}
362362
}
363363

@@ -433,7 +433,7 @@ impl DiscoveryBehaviour {
433433
&mut self,
434434
pid: ProtocolId,
435435
handler: KademliaHandlerProto<QueryId>,
436-
) -> <DiscoveryBehaviour as NetworkBehaviour>::ProtocolsHandler {
436+
) -> <DiscoveryBehaviour as NetworkBehaviour>::ConnectionHandler {
437437
let mut handlers: HashMap<_, _> = self
438438
.kademlias
439439
.iter_mut()
@@ -498,10 +498,10 @@ pub enum DiscoveryOut {
498498
}
499499

500500
impl NetworkBehaviour for DiscoveryBehaviour {
501-
type ProtocolsHandler = IntoMultiHandler<ProtocolId, KademliaHandlerProto<QueryId>>;
501+
type ConnectionHandler = IntoMultiHandler<ProtocolId, KademliaHandlerProto<QueryId>>;
502502
type OutEvent = DiscoveryOut;
503503

504-
fn new_handler(&mut self) -> Self::ProtocolsHandler {
504+
fn new_handler(&mut self) -> Self::ConnectionHandler {
505505
let iter = self
506506
.kademlias
507507
.iter_mut()
@@ -568,6 +568,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
568568
conn: &ConnectionId,
569569
endpoint: &ConnectedPoint,
570570
failed_addresses: Option<&Vec<Multiaddr>>,
571+
other_established: usize,
571572
) {
572573
self.num_connections += 1;
573574
for k in self.kademlias.values_mut() {
@@ -577,37 +578,37 @@ impl NetworkBehaviour for DiscoveryBehaviour {
577578
conn,
578579
endpoint,
579580
failed_addresses,
581+
other_established,
580582
)
581583
}
582584
}
583585

584-
fn inject_connected(&mut self, peer_id: &PeerId) {
585-
for k in self.kademlias.values_mut() {
586-
NetworkBehaviour::inject_connected(k, peer_id)
587-
}
588-
}
589-
590586
fn inject_connection_closed(
591587
&mut self,
592-
_peer_id: &PeerId,
593-
_conn: &ConnectionId,
594-
_endpoint: &ConnectedPoint,
595-
_handler: <Self::ProtocolsHandler as IntoProtocolsHandler>::Handler,
588+
peer_id: &PeerId,
589+
conn: &ConnectionId,
590+
endpoint: &ConnectedPoint,
591+
handler: <Self::ConnectionHandler as IntoConnectionHandler>::Handler,
592+
remaining_established: usize,
596593
) {
597594
self.num_connections -= 1;
598-
// NetworkBehaviour::inject_connection_closed on Kademlia<MemoryStore> does nothing.
599-
}
600-
601-
fn inject_disconnected(&mut self, peer_id: &PeerId) {
602-
for k in self.kademlias.values_mut() {
603-
NetworkBehaviour::inject_disconnected(k, peer_id)
595+
for (pid, event) in handler.into_iter() {
596+
if let Some(kad) = self.kademlias.get_mut(&pid) {
597+
kad.inject_connection_closed(peer_id, conn, endpoint, event, remaining_established)
598+
} else {
599+
error!(
600+
target: "sub-libp2p",
601+
"inject_connection_closed: no kademlia instance registered for protocol {:?}",
602+
pid,
603+
)
604+
}
604605
}
605606
}
606607

607608
fn inject_dial_failure(
608609
&mut self,
609610
peer_id: Option<PeerId>,
610-
_: Self::ProtocolsHandler,
611+
_: Self::ConnectionHandler,
611612
error: &DialError,
612613
) {
613614
if let Some(peer_id) = peer_id {
@@ -630,7 +631,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
630631
&mut self,
631632
peer_id: PeerId,
632633
connection: ConnectionId,
633-
(pid, event): <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent,
634+
(pid, event): <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
634635
) {
635636
if let Some(kad) = self.kademlias.get_mut(&pid) {
636637
return kad.inject_event(peer_id, connection, event)
@@ -689,7 +690,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
689690
}
690691
}
691692

692-
fn inject_listen_failure(&mut self, _: &Multiaddr, _: &Multiaddr, _: Self::ProtocolsHandler) {
693+
fn inject_listen_failure(&mut self, _: &Multiaddr, _: &Multiaddr, _: Self::ConnectionHandler) {
693694
// NetworkBehaviour::inject_listen_failure on Kademlia<MemoryStore> does nothing.
694695
}
695696

@@ -709,7 +710,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
709710
&mut self,
710711
cx: &mut Context,
711712
params: &mut impl PollParameters,
712-
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ProtocolsHandler>> {
713+
) -> Poll<NetworkBehaviourAction<Self::OutEvent, Self::ConnectionHandler>> {
713714
// Immediately process the content of `discovered`.
714715
if let Some(ev) = self.pending_events.pop_front() {
715716
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev))
@@ -770,12 +771,8 @@ impl NetworkBehaviour for DiscoveryBehaviour {
770771
let ev = DiscoveryOut::Discovered(peer);
771772
return Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev))
772773
},
773-
KademliaEvent::InboundPutRecordRequest { .. } |
774-
KademliaEvent::InboundAddProviderRequest { .. } => {
775-
debug_assert!(false, "We don't use kad filtering at the moment");
776-
},
777774
KademliaEvent::PendingRoutablePeer { .. } |
778-
KademliaEvent::InboundRequestServed { .. } => {
775+
KademliaEvent::InboundRequest { .. } => {
779776
// We are not interested in this event at the moment.
780777
},
781778
KademliaEvent::OutboundQueryCompleted {
@@ -890,19 +887,10 @@ impl NetworkBehaviour for DiscoveryBehaviour {
890887
warn!(target: "sub-libp2p", "Libp2p => Unhandled Kademlia event: {:?}", e)
891888
},
892889
},
893-
NetworkBehaviourAction::DialAddress { address, handler } => {
894-
let pid = pid.clone();
895-
let handler = self.new_handler_with_replacement(pid, handler);
896-
return Poll::Ready(NetworkBehaviourAction::DialAddress { address, handler })
897-
},
898-
NetworkBehaviourAction::DialPeer { peer_id, condition, handler } => {
890+
NetworkBehaviourAction::Dial { opts, handler } => {
899891
let pid = pid.clone();
900892
let handler = self.new_handler_with_replacement(pid, handler);
901-
return Poll::Ready(NetworkBehaviourAction::DialPeer {
902-
peer_id,
903-
condition,
904-
handler,
905-
})
893+
return Poll::Ready(NetworkBehaviourAction::Dial { opts, handler })
906894
},
907895
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
908896
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
@@ -941,10 +929,7 @@ impl NetworkBehaviour for DiscoveryBehaviour {
941929
},
942930
MdnsEvent::Expired(_) => {},
943931
},
944-
NetworkBehaviourAction::DialAddress { .. } => {
945-
unreachable!("mDNS never dials!");
946-
},
947-
NetworkBehaviourAction::DialPeer { .. } => {
932+
NetworkBehaviourAction::Dial { .. } => {
948933
unreachable!("mDNS never dials!");
949934
},
950935
NetworkBehaviourAction::NotifyHandler { event, .. } => match event {}, /* `event` is an enum with no variant */
@@ -995,7 +980,7 @@ impl MdnsWrapper {
995980
&mut self,
996981
cx: &mut Context<'_>,
997982
params: &mut impl PollParameters,
998-
) -> Poll<NetworkBehaviourAction<MdnsEvent, <Mdns as NetworkBehaviour>::ProtocolsHandler>> {
983+
) -> Poll<NetworkBehaviourAction<MdnsEvent, <Mdns as NetworkBehaviour>::ConnectionHandler>> {
999984
loop {
1000985
match self {
1001986
Self::Instantiating(fut) =>

0 commit comments

Comments
 (0)