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

Commit 309c75c

Browse files
author
Roman S. Borschel
committed
Update to libp2p-0.17
1 parent 5104f88 commit 309c75c

File tree

21 files changed

+437
-305
lines changed

21 files changed

+437
-305
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,3 @@ members = [
174174
# Substrate runtime requires unwinding.
175175
panic = "unwind"
176176

177-
[patch.crates-io]
178-
# libp2p = { path = "../rust-libp2p" }
179-
libp2p = { git = "https://github.com/libp2p/rust-libp2p", branch = "master" }
180-
libp2p-wasm-ext = { git = "https://github.com/libp2p/rust-libp2p", branch = "master" }
181-
182-

bin/utils/subkey/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ derive_more = { version = "0.99.2" }
2929
sc-rpc = { version = "2.0.0-alpha.5", path = "../../../client/rpc" }
3030
jsonrpc-core-client = { version = "14.0.3", features = ["http"] }
3131
hyper = "0.12.35"
32-
libp2p = "0.16.2"
32+
libp2p = "0.17.0"
3333
serde_json = "1.0"
3434

3535
[features]

client/authority-discovery/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ codec = { package = "parity-scale-codec", default-features = false, version = "1
1818
derive_more = "0.99.2"
1919
futures = "0.3.4"
2020
futures-timer = "3.0.1"
21-
libp2p = { version = "0.16.2", default-features = false, features = ["secp256k1", "libp2p-websocket"] }
21+
libp2p = { version = "0.17.0", default-features = false, features = ["secp256k1", "libp2p-websocket"] }
2222
log = "0.4.8"
2323
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../utils/prometheus", version = "0.8.0-alpha.5"}
2424
prost = "0.6.1"

client/network-gossip/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ documentation = "https://docs.rs/sc-network-gossip"
1313
[dependencies]
1414
futures = "0.3.4"
1515
futures-timer = "3.0.1"
16-
libp2p = { version = "0.16.2", default-features = false, features = ["websocket"] }
16+
libp2p = { version = "0.17.0", default-features = false, features = ["websocket"] }
1717
log = "0.4.8"
1818
lru = "0.4.3"
1919
sc-network = { version = "0.8.0-alpha.5", path = "../network" }

client/network/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ void = "1.0.2"
5858
zeroize = "1.0.0"
5959

6060
[dependencies.libp2p]
61-
version = "0.16.2"
61+
version = "0.17.0"
6262
default-features = false
6363
features = ["websocket", "kad", "mdns", "ping", "identify", "mplex", "yamux", "noise"]
6464

6565
[dev-dependencies]
6666
async-std = "1.5"
6767
assert_matches = "1.3"
6868
env_logger = "0.7.0"
69-
libp2p = { version = "0.16.2", default-features = false, features = ["secio"] }
69+
libp2p = { version = "0.17.0", default-features = false, features = ["secio"] }
7070
quickcheck = "0.9.0"
7171
rand = "0.7.2"
7272
sp-keyring = { version = "2.0.0-alpha.5", path = "../../primitives/keyring" }

client/network/src/debug_info.rs

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ use libp2p::swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
2424
use libp2p::identify::{Identify, IdentifyEvent, IdentifyInfo};
2525
use libp2p::ping::{Ping, PingConfig, PingEvent, PingSuccess};
2626
use log::{debug, trace, error};
27-
use std::error;
27+
use smallvec::SmallVec;
28+
use std::{error, io};
2829
use std::collections::hash_map::Entry;
2930
use std::pin::Pin;
3031
use std::task::{Context, Poll};
@@ -57,7 +58,7 @@ struct NodeInfo {
5758
/// to the node.
5859
info_expire: Option<Instant>,
5960
/// How we're connected to the node.
60-
endpoint: ConnectedPoint,
61+
endpoints: SmallVec<[ConnectedPoint; 2]>,
6162
/// Version reported by the remote, or `None` if unknown.
6263
client_version: Option<String>,
6364
/// Latest ping time with this node.
@@ -123,7 +124,7 @@ pub struct Node<'a>(&'a NodeInfo);
123124
impl<'a> Node<'a> {
124125
/// Returns the endpoint we are connected to or were last connected to.
125126
pub fn endpoint(&self) -> &'a ConnectedPoint {
126-
&self.0.endpoint
127+
&self.0.endpoints[0] // TODO: Multiple?
127128
}
128129

129130
/// Returns the latest version information we know of.
@@ -168,15 +169,21 @@ impl NetworkBehaviour for DebugInfoBehaviour {
168169
list
169170
}
170171

171-
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint) {
172-
self.ping.inject_connected(peer_id.clone(), endpoint.clone());
173-
self.identify.inject_connected(peer_id.clone(), endpoint.clone());
172+
fn inject_connected(&mut self, peer_id: &PeerId) {
173+
self.ping.inject_connected(peer_id);
174+
self.identify.inject_connected(peer_id);
175+
}
174176

175-
match self.nodes_info.entry(peer_id) {
177+
fn inject_connection_established(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
178+
self.ping.inject_connection_established(peer_id, conn, endpoint);
179+
self.identify.inject_connection_established(peer_id, conn, endpoint);
180+
match self.nodes_info.entry(peer_id.clone()) {
176181
Entry::Vacant(e) => {
182+
let mut endpoints = SmallVec::new();
183+
endpoints.push(endpoint.clone());
177184
e.insert(NodeInfo {
178185
info_expire: None,
179-
endpoint,
186+
endpoints,
180187
client_version: None,
181188
latest_ping: None,
182189
});
@@ -188,14 +195,26 @@ impl NetworkBehaviour for DebugInfoBehaviour {
188195
e.latest_ping = None;
189196
}
190197
e.info_expire = None;
191-
e.endpoint = endpoint;
198+
e.endpoints.push(endpoint.clone());
192199
}
193200
}
194201
}
195202

196-
fn inject_disconnected(&mut self, peer_id: &PeerId, endpoint: ConnectedPoint) {
197-
self.ping.inject_disconnected(peer_id, endpoint.clone());
198-
self.identify.inject_disconnected(peer_id, endpoint);
203+
fn inject_connection_closed(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
204+
self.ping.inject_connection_closed(peer_id, conn, endpoint);
205+
self.identify.inject_connection_closed(peer_id, conn, endpoint);
206+
207+
if let Some(entry) = self.nodes_info.get_mut(peer_id) {
208+
entry.endpoints.retain(|ep| ep != endpoint)
209+
} else {
210+
error!(target: "sub-libp2p",
211+
"Unknown connection to {:?} closed: {:?}", peer_id, endpoint);
212+
}
213+
}
214+
215+
fn inject_disconnected(&mut self, peer_id: &PeerId) {
216+
self.ping.inject_disconnected(peer_id);
217+
self.identify.inject_disconnected(peer_id);
199218

200219
if let Some(entry) = self.nodes_info.get_mut(peer_id) {
201220
entry.info_expire = Some(Instant::now() + CACHE_EXPIRE);
@@ -247,9 +266,9 @@ impl NetworkBehaviour for DebugInfoBehaviour {
247266
self.identify.inject_listener_error(id, err);
248267
}
249268

250-
fn inject_listener_closed(&mut self, id: ListenerId) {
251-
self.ping.inject_listener_closed(id);
252-
self.identify.inject_listener_closed(id);
269+
fn inject_listener_closed(&mut self, id: ListenerId, reason: Result<(), &io::Error>) {
270+
self.ping.inject_listener_closed(id, reason);
271+
self.identify.inject_listener_closed(id, reason);
253272
}
254273

255274
fn poll(
@@ -272,8 +291,8 @@ impl NetworkBehaviour for DebugInfoBehaviour {
272291
},
273292
Poll::Ready(NetworkBehaviourAction::DialAddress { address }) =>
274293
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
275-
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }) =>
276-
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
294+
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }) =>
295+
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }),
277296
Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }) =>
278297
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
279298
peer_id,
@@ -302,8 +321,8 @@ impl NetworkBehaviour for DebugInfoBehaviour {
302321
},
303322
Poll::Ready(NetworkBehaviourAction::DialAddress { address }) =>
304323
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
305-
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }) =>
306-
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
324+
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }) =>
325+
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }),
307326
Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }) =>
308327
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
309328
peer_id,

client/network/src/discovery.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use libp2p::{swarm::toggle::Toggle};
5858
use libp2p::mdns::{Mdns, MdnsEvent};
5959
use libp2p::multiaddr::Protocol;
6060
use log::{debug, info, trace, warn, error};
61-
use std::{cmp, collections::VecDeque, time::Duration};
61+
use std::{cmp, collections::VecDeque, io, time::Duration};
6262
use std::task::{Context, Poll};
6363
use sp_core::hexdisplay::HexDisplay;
6464

@@ -260,14 +260,22 @@ impl NetworkBehaviour for DiscoveryBehaviour {
260260
list
261261
}
262262

263-
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint) {
263+
fn inject_connection_established(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
264264
self.num_connections += 1;
265-
NetworkBehaviour::inject_connected(&mut self.kademlia, peer_id, endpoint)
265+
NetworkBehaviour::inject_connection_established(&mut self.kademlia, peer_id, conn, endpoint)
266266
}
267267

268-
fn inject_disconnected(&mut self, peer_id: &PeerId, endpoint: ConnectedPoint) {
268+
fn inject_connected(&mut self, peer_id: &PeerId) {
269+
NetworkBehaviour::inject_connected(&mut self.kademlia, peer_id)
270+
}
271+
272+
fn inject_connection_closed(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
269273
self.num_connections -= 1;
270-
NetworkBehaviour::inject_disconnected(&mut self.kademlia, peer_id, endpoint)
274+
NetworkBehaviour::inject_connection_closed(&mut self.kademlia, peer_id, conn, endpoint)
275+
}
276+
277+
fn inject_disconnected(&mut self, peer_id: &PeerId) {
278+
NetworkBehaviour::inject_disconnected(&mut self.kademlia, peer_id)
271279
}
272280

273281
fn inject_addr_reach_failure(
@@ -313,9 +321,9 @@ impl NetworkBehaviour for DiscoveryBehaviour {
313321
NetworkBehaviour::inject_listener_error(&mut self.kademlia, id, err);
314322
}
315323

316-
fn inject_listener_closed(&mut self, id: ListenerId) {
317-
error!(target: "sub-libp2p", "Libp2p listener {:?} closed", id);
318-
NetworkBehaviour::inject_listener_closed(&mut self.kademlia, id);
324+
fn inject_listener_closed(&mut self, id: ListenerId, reason: Result<(), &io::Error>) {
325+
error!(target: "sub-libp2p", "Libp2p listener {:?} closed: {:?}", id, reason);
326+
NetworkBehaviour::inject_listener_closed(&mut self.kademlia, id, reason);
319327
}
320328

321329
fn poll(
@@ -450,8 +458,8 @@ impl NetworkBehaviour for DiscoveryBehaviour {
450458
},
451459
NetworkBehaviourAction::DialAddress { address } =>
452460
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
453-
NetworkBehaviourAction::DialPeer { peer_id } =>
454-
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
461+
NetworkBehaviourAction::DialPeer { peer_id, condition } =>
462+
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }),
455463
NetworkBehaviourAction::NotifyHandler { peer_id, handler, event } =>
456464
return Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }),
457465
NetworkBehaviourAction::ReportObservedAddr { address } =>
@@ -481,8 +489,8 @@ impl NetworkBehaviour for DiscoveryBehaviour {
481489
},
482490
NetworkBehaviourAction::DialAddress { address } =>
483491
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
484-
NetworkBehaviourAction::DialPeer { peer_id } =>
485-
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
492+
NetworkBehaviourAction::DialPeer { peer_id, condition } =>
493+
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }),
486494
NetworkBehaviourAction::NotifyHandler { event, .. } =>
487495
match event {}, // `event` is an enum with no variant
488496
NetworkBehaviourAction::ReportObservedAddr { address } =>

client/network/src/protocol.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use std::borrow::Cow;
4848
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
4949
use std::sync::Arc;
5050
use std::fmt::Write;
51-
use std::{cmp, num::NonZeroUsize, pin::Pin, task::Poll, time};
51+
use std::{cmp, io, num::NonZeroUsize, pin::Pin, task::Poll, time};
5252
use log::{log, Level, trace, debug, warn, error};
5353
use crate::chain::{Client, FinalityProofProvider};
5454
use sc_client_api::{ChangesProof, StorageProof};
@@ -1836,12 +1836,20 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviour for Protocol<B, H> {
18361836
self.behaviour.addresses_of_peer(peer_id)
18371837
}
18381838

1839-
fn inject_connected(&mut self, peer_id: PeerId, endpoint: ConnectedPoint) {
1840-
self.behaviour.inject_connected(peer_id, endpoint)
1839+
fn inject_connection_established(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
1840+
self.behaviour.inject_connection_established(peer_id, conn, endpoint)
18411841
}
18421842

1843-
fn inject_disconnected(&mut self, peer_id: &PeerId, endpoint: ConnectedPoint) {
1844-
self.behaviour.inject_disconnected(peer_id, endpoint)
1843+
fn inject_connection_closed(&mut self, peer_id: &PeerId, conn: &ConnectionId, endpoint: &ConnectedPoint) {
1844+
self.behaviour.inject_connection_closed(peer_id, conn, endpoint)
1845+
}
1846+
1847+
fn inject_connected(&mut self, peer_id: &PeerId) {
1848+
self.behaviour.inject_connected(peer_id)
1849+
}
1850+
1851+
fn inject_disconnected(&mut self, peer_id: &PeerId) {
1852+
self.behaviour.inject_disconnected(peer_id)
18451853
}
18461854

18471855
fn inject_event(
@@ -1907,8 +1915,8 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviour for Protocol<B, H> {
19071915
Poll::Ready(NetworkBehaviourAction::GenerateEvent(ev)) => ev,
19081916
Poll::Ready(NetworkBehaviourAction::DialAddress { address }) =>
19091917
return Poll::Ready(NetworkBehaviourAction::DialAddress { address }),
1910-
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }) =>
1911-
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id }),
1918+
Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }) =>
1919+
return Poll::Ready(NetworkBehaviourAction::DialPeer { peer_id, condition }),
19121920
Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }) =>
19131921
return Poll::Ready(NetworkBehaviourAction::NotifyHandler { peer_id, handler, event }),
19141922
Poll::Ready(NetworkBehaviourAction::ReportObservedAddr { address }) =>
@@ -2003,8 +2011,8 @@ impl<B: BlockT, H: ExHashT> NetworkBehaviour for Protocol<B, H> {
20032011
self.behaviour.inject_listener_error(id, err);
20042012
}
20052013

2006-
fn inject_listener_closed(&mut self, id: ListenerId) {
2007-
self.behaviour.inject_listener_closed(id);
2014+
fn inject_listener_closed(&mut self, id: ListenerId, reason: Result<(), &io::Error>) {
2015+
self.behaviour.inject_listener_closed(id, reason);
20082016
}
20092017
}
20102018

client/network/src/protocol/block_requests.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use libp2p::{
4444
NetworkBehaviour,
4545
NetworkBehaviourAction,
4646
OneShotHandler,
47+
OneShotHandlerConfig,
4748
PollParameters,
4849
SubstreamProtocol
4950
}
@@ -258,17 +259,19 @@ where
258259
max_request_len: self.config.max_request_len,
259260
protocol: self.config.protocol.clone(),
260261
};
261-
OneShotHandler::new(SubstreamProtocol::new(p), self.config.inactivity_timeout)
262+
let mut cfg = OneShotHandlerConfig::default();
263+
cfg.inactive_timeout = self.config.inactivity_timeout;
264+
OneShotHandler::new(SubstreamProtocol::new(p), cfg)
262265
}
263266

264267
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
265268
Vec::new()
266269
}
267270

268-
fn inject_connected(&mut self, _peer: PeerId, _info: ConnectedPoint) {
271+
fn inject_connected(&mut self, _peer: &PeerId) {
269272
}
270273

271-
fn inject_disconnected(&mut self, _peer: &PeerId, _info: ConnectedPoint) {
274+
fn inject_disconnected(&mut self, _peer: &PeerId) {
272275
}
273276

274277
fn inject_event(

0 commit comments

Comments
 (0)