Skip to content

Commit

Permalink
chore(deps): update libp2p to 0.50.0 [fixes NET-332] (#1419)
Browse files Browse the repository at this point in the history
* chore(deps): update libp2p

* Fix libp2p update

* Fix

* Fix

* Fix

* Fix clap

* FIX

* FIX

* Update keypair

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Nick <[email protected]>
  • Loading branch information
renovate[bot] and gurinderu authored Jan 27, 2023
1 parent 968177a commit 9c3eda8
Show file tree
Hide file tree
Showing 9 changed files with 588 additions and 437 deletions.
873 changes: 477 additions & 396 deletions Cargo.lock

Large diffs are not rendered by default.

31 changes: 24 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ fluence-spell-distro = "0.3.0"
fluence-app-service = "0.23.0"
avm-server = "0.28.1"
air-interpreter-wasm = "=0.35.3"
libp2p = "0.48.0"
libp2p-core = { version = "0.36.0", default-features = false, features = [ "secp256k1" ] }
libp2p-metrics = { version = "0.9.0", features = ["kad"] }
libp2p-noise = "0.39.0"
libp2p-swarm = "0.39.0"
prometheus-client = "0.18.0"
libp2p = { version = "0.50.0", features = ["noise", "tcp", "dns", "websocket", "yamux", "mplex", "async-std", "kad", "ping", "identify", "macros"] }
libp2p-core = { version = "0.38.0", default-features = false, features = ["secp256k1"] }
libp2p-metrics = { version = "0.11.0", features = ["kad"] }
libp2p-noise = "0.41.0"
libp2p-swarm = { version = "0.41.1"}
prometheus-client = "0.18.1"
eyre = "0.6.8"
base64 = "0.21.0"
bs58 = "0.4.0"
fluence-keypair = "0.8.1"
fluence-keypair = "0.9.0"
parking_lot = "0.12.1"
async-std = { version = "1.12.0", features = ["unstable"] }
uuid = { version = "1.2.2", features = ["v4"] }
Expand All @@ -117,3 +117,20 @@ serde = "1.0.152"
toml = "0.5.10"
itertools = "0.10.5"
humantime-serde = "1.1.1"

# Enable a small amount of optimization in debug mode
[profile.dev]
opt-level = 1

# Enable high optimizations for dependencies, but not for our code:
[profile.dev.package."*"]
inherits = "release"
codegen-units = 256

[profile.dev.package.clap]
debug-assertions = false

[profile.release]
strip = true
lto = true
codegen-units = 1 # Reduce number of codegen units to increase optimizations
6 changes: 3 additions & 3 deletions crates/connected-client/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use libp2p::{
either::EitherOutput,
Multiaddr,
},
ping::{Ping, PingConfig, PingResult},
ping::{Behaviour as Ping, Config as PingConfig, Result as PingResult},
swarm::{
IntoConnectionHandler, IntoConnectionHandlerSelect, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, OneShotHandler, PollParameters,
Expand All @@ -52,7 +52,7 @@ pub struct ClientBehaviour {

impl ClientBehaviour {
pub fn new(protocol_config: ProtocolConfig) -> Self {
let ping = Ping::new(PingConfig::new().with_keep_alive(true));
let ping = Ping::new(PingConfig::new());
Self {
protocol_config,
events: VecDeque::default(),
Expand Down Expand Up @@ -185,7 +185,7 @@ impl NetworkBehaviour for ClientBehaviour {
sender: peer_id,
}))
}
Second(ping) => self.ping.inject_event(peer_id, cid, ping),
Second(ping) => self.ping.on_connection_handler_event(peer_id, cid, ping),
First(_) => {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/connected-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Client {
let behaviour = ClientBehaviour::new(protocol_config);

let transport = build_transport(transport, self.key_pair.clone(), transport_timeout);
Swarm::new(transport, behaviour, self.peer_id)
Swarm::with_threadpool_executor(transport, behaviour, self.peer_id)
};

match Swarm::dial(&mut swarm, node.clone()) {
Expand Down
93 changes: 72 additions & 21 deletions crates/kademlia/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ use libp2p::core::connection::ConnectionId;
use libp2p::core::transport::ListenerId;
use libp2p::core::ConnectedPoint;
use libp2p::kad::kbucket::Key;
use libp2p::swarm::behaviour::{
ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr, ExpiredListenAddr,
FromSwarm, ListenFailure, ListenerClosed, ListenerError, NewExternalAddr, NewListenAddr,
NewListener,
};
use libp2p::swarm::derive_prelude::AddressChange;
use libp2p::swarm::{
ConnectionHandler, DialError, IntoConnectionHandler, NetworkBehaviourAction, PollParameters,
};
Expand Down Expand Up @@ -388,7 +394,7 @@ impl Kademlia {
}

match event {
KademliaEvent::OutboundQueryCompleted { id, result, .. } => match result {
KademliaEvent::OutboundQueryProgressed { id, result, .. } => match result {
QueryResult::GetClosestPeers(result) => self.closest_finished(id, result),
QueryResult::Bootstrap(result) => self.bootstrap_finished(id, result),
_ => {}
Expand Down Expand Up @@ -446,13 +452,16 @@ impl NetworkBehaviour for Kademlia {
failed_addresses: Option<&Vec<Multiaddr>>,
other_established: usize,
) {
self.kademlia.inject_connection_established(
peer_id,
connection_id,
endpoint,
failed_addresses,
other_established,
)
self.kademlia
.on_swarm_event(FromSwarm::ConnectionEstablished(ConnectionEstablished {
peer_id: *peer_id,
connection_id: *connection_id,
endpoint,
failed_addresses: failed_addresses
.map(|v| v.as_slice())
.unwrap_or_else(|| &[]),
other_established,
}))
}

fn inject_connection_closed(
Expand All @@ -464,7 +473,13 @@ impl NetworkBehaviour for Kademlia {
remaining_established: usize,
) {
self.kademlia
.inject_connection_closed(peer_id, cid, cp, handler, remaining_established)
.on_swarm_event(FromSwarm::ConnectionClosed(ConnectionClosed {
peer_id: *peer_id,
connection_id: *cid,
endpoint: cp,
handler,
remaining_established,
}))
}

fn inject_address_change(
Expand All @@ -474,7 +489,13 @@ impl NetworkBehaviour for Kademlia {
old: &ConnectedPoint,
new: &ConnectedPoint,
) {
self.kademlia.inject_address_change(peer_id, ci, old, new)
self.kademlia
.on_swarm_event(FromSwarm::AddressChange(AddressChange {
peer_id: *peer_id,
connection_id: *ci,
old,
new,
}))
}

fn inject_event(
Expand All @@ -483,7 +504,8 @@ impl NetworkBehaviour for Kademlia {
connection: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
) {
self.kademlia.inject_event(peer_id, connection, event)
self.kademlia
.on_connection_handler_event(peer_id, connection, event)
}

fn inject_dial_failure(
Expand All @@ -492,7 +514,12 @@ impl NetworkBehaviour for Kademlia {
handler: Self::ConnectionHandler,
error: &DialError,
) {
self.kademlia.inject_dial_failure(peer_id, handler, error)
self.kademlia
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id,
handler,
error,
}))
}

fn inject_listen_failure(
Expand All @@ -502,39 +529,62 @@ impl NetworkBehaviour for Kademlia {
handler: Self::ConnectionHandler,
) {
self.kademlia
.inject_listen_failure(local_addr, send_back_addr, handler)
.on_swarm_event(FromSwarm::ListenFailure(ListenFailure {
local_addr,
send_back_addr,
handler,
}))
}

fn inject_new_listener(&mut self, id: ListenerId) {
self.kademlia.inject_new_listener(id)
self.kademlia
.on_swarm_event(FromSwarm::NewListener(NewListener { listener_id: id }))
}

fn inject_new_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) {
self.kademlia.inject_new_listen_addr(id, addr)
self.kademlia
.on_swarm_event(FromSwarm::NewListenAddr(NewListenAddr {
listener_id: id,
addr,
}))
}

fn inject_expired_listen_addr(&mut self, id: ListenerId, addr: &Multiaddr) {
self.kademlia.inject_expired_listen_addr(id, addr)
self.kademlia
.on_swarm_event(FromSwarm::ExpiredListenAddr(ExpiredListenAddr {
listener_id: id,
addr,
}))
}

fn inject_listener_error(&mut self, id: ListenerId, err: &(dyn Error + 'static)) {
self.kademlia.inject_listener_error(id, err)
self.kademlia
.on_swarm_event(FromSwarm::ListenerError(ListenerError {
listener_id: id,
err,
}))
}

fn inject_listener_closed(
&mut self,
id: ListenerId,
reason: std::result::Result<(), &std::io::Error>,
) {
self.kademlia.inject_listener_closed(id, reason)
self.kademlia
.on_swarm_event(FromSwarm::ListenerClosed(ListenerClosed {
listener_id: id,
reason,
}))
}

fn inject_new_external_addr(&mut self, addr: &Multiaddr) {
self.kademlia.inject_new_external_addr(addr)
self.kademlia
.on_swarm_event(FromSwarm::NewExternalAddr(NewExternalAddr { addr }))
}

fn inject_expired_external_addr(&mut self, addr: &Multiaddr) {
self.kademlia.inject_expired_external_addr(addr)
self.kademlia
.on_swarm_event(FromSwarm::ExpiredExternalAddr(ExpiredExternalAddr { addr }))
}

fn poll(
Expand Down Expand Up @@ -606,7 +656,8 @@ mod tests {
let (kad, _) = Kademlia::new(config, None);
let timeout = Duration::from_secs(20);

let mut swarm = Swarm::new(build_memory_transport(kp, timeout), kad, peer_id);
let mut swarm =
Swarm::with_threadpool_executor(build_memory_transport(kp, timeout), kad, peer_id);

let mut maddr = create_memory_maddr();
maddr.push(Protocol::P2p(peer_id.into()));
Expand Down
5 changes: 3 additions & 2 deletions crates/libp2p/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use libp2p::core::muxing::StreamMuxerBox;
use libp2p::core::transport::{Boxed, MemoryTransport};
use libp2p::core::Multiaddr;
use libp2p::noise;
use libp2p::tcp::{GenTcpConfig, TcpTransport};
use libp2p::tcp::Transport as TcpTransport;
use libp2p::tcp::{async_io, Config as GenTcpConfig};
use libp2p::{core, dns, identity::Keypair, PeerId, Transport as NetworkTransport};
use serde::{Deserialize, Serialize};

Expand All @@ -45,7 +46,7 @@ pub fn build_network_transport(
socket_timeout: Duration,
) -> Boxed<(PeerId, StreamMuxerBox)> {
let tcp = || {
let tcp = TcpTransport::new(GenTcpConfig::default().nodelay(true));
let tcp = TcpTransport::<async_io::Tcp>::new(GenTcpConfig::default().nodelay(true));

async_std::task::block_on(dns::DnsConfig::system(tcp)).expect("Can't build DNS")
};
Expand Down
2 changes: 1 addition & 1 deletion particle-node/src/behaviour/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use itertools::Itertools;
use libp2p::{
core::{multiaddr::Protocol, Multiaddr},
identify::IdentifyEvent,
identify::Event as IdentifyEvent,
};

use super::FluenceNetworkBehaviour;
Expand Down
11 changes: 6 additions & 5 deletions particle-node/src/behaviour/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use libp2p::identify::IdentifyConfig;
use libp2p::identify::Config as IdentifyConfig;
use libp2p::{
identify::Identify,
ping::{Ping, PingConfig},
identify::Behaviour as Identify,
ping::{Behaviour as Ping, Config as PingConfig},
swarm::NetworkBehaviour,
};

use connection_pool::ConnectionPoolBehaviour;
Expand All @@ -28,7 +29,7 @@ use server_config::NetworkConfig;
use crate::connectivity::Connectivity;

/// Coordinates protocols, so they can cooperate
#[derive(::libp2p::NetworkBehaviour)]
#[derive(NetworkBehaviour)]
pub struct FluenceNetworkBehaviour {
identify: Identify,
ping: Ping,
Expand All @@ -43,7 +44,7 @@ impl FluenceNetworkBehaviour {
IdentifyConfig::new(PROTOCOL_NAME.into(), local_public_key)
.with_agent_version(cfg.node_version.into()),
);
let ping = Ping::new(PingConfig::new().with_keep_alive(false));
let ping = Ping::new(PingConfig::new());

let kad_config = KademliaConfig {
peer_id: cfg.local_peer_id,
Expand Down
2 changes: 1 addition & 1 deletion particle-node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl<RT: AquaRuntime> Node<RT> {
) {
let (behaviour, connectivity, particle_stream) =
FluenceNetworkBehaviour::new(network_config);
let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
let mut swarm = Swarm::with_threadpool_executor(transport, behaviour, local_peer_id);

// Add external addresses to Swarm
external_addresses.iter().cloned().for_each(|addr| {
Expand Down

0 comments on commit 9c3eda8

Please sign in to comment.