From 66f569ff0825775a566eb495ddc93d857aa77f82 Mon Sep 17 00:00:00 2001 From: Arpan Kapoor Date: Thu, 28 Sep 2023 09:51:59 +0530 Subject: [PATCH] add high cpu usage example --- Cargo.lock | 3 +-- examples/ping/Cargo.toml | 5 ++--- examples/ping/src/main.rs | 33 ++++++++++++++++++++++++--------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 89d3b70ba8c..3fcf81ae2f7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4008,11 +4008,10 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" name = "ping-example" version = "0.1.0" dependencies = [ - "async-std", - "async-trait", "env_logger 0.10.0", "futures", "libp2p", + "tokio", ] [[package]] diff --git a/examples/ping/Cargo.toml b/examples/ping/Cargo.toml index 33c9e56b45c..4c00d820cb4 100644 --- a/examples/ping/Cargo.toml +++ b/examples/ping/Cargo.toml @@ -6,8 +6,7 @@ publish = false license = "MIT" [dependencies] -async-std = { version = "1.12", features = ["attributes"] } -async-trait = "0.1" env_logger = "0.10.0" futures = "0.3.28" -libp2p = { path = "../../libp2p", features = ["async-std", "dns", "macros", "noise", "ping", "tcp", "websocket", "yamux"] } +libp2p = { path = "../../libp2p", features = ["dns", "identify", "macros", "noise", "ping", "tcp", "tokio", "upnp", "websocket", "yamux"] } +tokio = { version = "1.32.0", features = ["full"] } diff --git a/examples/ping/src/main.rs b/examples/ping/src/main.rs index 898a25813e0..37783603a6c 100644 --- a/examples/ping/src/main.rs +++ b/examples/ping/src/main.rs @@ -23,29 +23,44 @@ use futures::prelude::*; use libp2p::core::upgrade::Version; use libp2p::{ - identity, noise, ping, - swarm::{SwarmBuilder, SwarmEvent}, - tcp, yamux, Multiaddr, PeerId, Transport, + identify, identity, noise, ping, + swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent}, + tcp, upnp, yamux, Multiaddr, PeerId, Transport, }; use std::error::Error; use std::time::Duration; -#[async_std::main] +#[derive(NetworkBehaviour)] +struct Behaviour { + ping: ping::Behaviour, + identify: identify::Behaviour, + upnp: upnp::tokio::Behaviour, +} + +#[tokio::main] async fn main() -> Result<(), Box> { env_logger::init(); let local_key = identity::Keypair::generate_ed25519(); let local_peer_id = PeerId::from(local_key.public()); - let transport = tcp::async_io::Transport::default() + let transport = tcp::tokio::Transport::default() .upgrade(Version::V1Lazy) .authenticate(noise::Config::new(&local_key)?) .multiplex(yamux::Config::default()) .boxed(); - let mut swarm = - SwarmBuilder::with_async_std_executor(transport, ping::Behaviour::default(), local_peer_id) - .idle_connection_timeout(Duration::from_secs(60)) // For illustrative purposes, keep idle connections alive for a minute so we can observe a few pings. - .build(); + let behaviour = Behaviour { + ping: ping::Behaviour::new(ping::Config::new()), + identify: identify::Behaviour::new(identify::Config::new( + "/TODO/0.0.1".to_string(), + local_key.public(), + )), + upnp: upnp::tokio::Behaviour::default(), + }; + + let mut swarm = SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id) + .idle_connection_timeout(Duration::from_secs(60)) // For illustrative purposes, keep idle connections alive for a minute so we can observe a few pings. + .build(); // Tell the swarm to listen on all interfaces and a random, OS-assigned // port.