From 9d41f5cd96b58e7b89840369b7d2f3fe22dc1cc1 Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Thu, 2 Jun 2022 21:08:20 +0200 Subject: [PATCH 01/12] Added identify example --- examples/identify.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 examples/identify.rs diff --git a/examples/identify.rs b/examples/identify.rs new file mode 100644 index 00000000000..48485295314 --- /dev/null +++ b/examples/identify.rs @@ -0,0 +1,82 @@ +// Copyright 2018 Parity Technologies (UK) Ltd. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +//! identify example +//! +//! In the first terminal window, run: +//! +//! ```sh +//! cargo run --example identify +//! ``` +//! It will print the PeerId and the listening addresses, e.g. `Listening on +//! "/ip4/0.0.0.0/tcp/24915"` +//! +//! In the second terminal window, start a new instance of the example with: +//! +//! ```sh +//! cargo run --example identify -- /ip4/127.0.0.1/tcp/24915 +//! ``` +//! The two nodes establish a connection, negotiate the identify protocol +//! The dialing node prints out the peer id of the node it is sending identify info to +//! The other node prints out the received identify info + +use futures::prelude::*; +use libp2p::swarm::{Swarm, SwarmEvent}; +use libp2p::{identity, Multiaddr, PeerId}; +use std::error::Error; +use libp2p_identify::{Identify, IdentifyConfig, IdentifyEvent}; + +#[async_std::main] +async fn main() -> Result<(), Box> { + let local_key = identity::Keypair::generate_ed25519(); + let local_peer_id = PeerId::from(local_key.public()); + println!("Local peer id: {:?}", local_peer_id); + + let transport = libp2p::development_transport(local_key.clone()).await?; + + // Create a identify network behaviour. + let behaviour = Identify::new(IdentifyConfig::new("/ipfs/id/1.0.0".to_string(), local_key.public())); + + let mut swarm = Swarm::new(transport, behaviour, local_peer_id); + + // Tell the swarm to listen on all interfaces and a random, OS-assigned + // port. + swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; + + // Dial the peer identified by the multi-address given as the second + // command-line argument, if any. + if let Some(addr) = std::env::args().nth(1) { + let remote: Multiaddr = addr.parse()?; + swarm.dial(remote)?; + println!("Dialed {}", addr) + } + + + loop { + match swarm.select_next_some().await { + SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address), + // prints peer id identify info is being sent to + SwarmEvent::Behaviour(IdentifyEvent::Sent {peer_id , ..}) => println!("Sent identify info to {:?}", peer_id), + // prints out the info received via the identify event + SwarmEvent::Behaviour(IdentifyEvent::Received {info , ..}) => println!("Received {:?}", info), + _ => {} + } + } +} From 04ca78fbe9f3312df2ed71bd27cd40dd35beebf5 Mon Sep 17 00:00:00 2001 From: dadepo Date: Fri, 3 Jun 2022 21:09:51 +0200 Subject: [PATCH 02/12] Update examples/identify.rs Co-authored-by: Max Inden --- examples/identify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/identify.rs b/examples/identify.rs index 48485295314..c9a3c9411ec 100644 --- a/examples/identify.rs +++ b/examples/identify.rs @@ -72,7 +72,7 @@ async fn main() -> Result<(), Box> { loop { match swarm.select_next_some().await { SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address), - // prints peer id identify info is being sent to + // Prints peer id identify info is being sent to. SwarmEvent::Behaviour(IdentifyEvent::Sent {peer_id , ..}) => println!("Sent identify info to {:?}", peer_id), // prints out the info received via the identify event SwarmEvent::Behaviour(IdentifyEvent::Received {info , ..}) => println!("Received {:?}", info), From 1967874b792368aebfead98b582a943e1070d614 Mon Sep 17 00:00:00 2001 From: dadepo Date: Fri, 3 Jun 2022 21:10:01 +0200 Subject: [PATCH 03/12] Update examples/identify.rs Co-authored-by: Max Inden --- examples/identify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/identify.rs b/examples/identify.rs index c9a3c9411ec..cc0f1dbdfb5 100644 --- a/examples/identify.rs +++ b/examples/identify.rs @@ -35,7 +35,7 @@ //! ``` //! The two nodes establish a connection, negotiate the identify protocol //! The dialing node prints out the peer id of the node it is sending identify info to -//! The other node prints out the received identify info +//! The other node prints out the received identify info. use futures::prelude::*; use libp2p::swarm::{Swarm, SwarmEvent}; From 0cfa33c4867cd200fd3048816d1b77ab6689be76 Mon Sep 17 00:00:00 2001 From: dadepo Date: Fri, 3 Jun 2022 21:10:07 +0200 Subject: [PATCH 04/12] Update examples/identify.rs Co-authored-by: Max Inden --- examples/identify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/identify.rs b/examples/identify.rs index cc0f1dbdfb5..3af8ba42c1f 100644 --- a/examples/identify.rs +++ b/examples/identify.rs @@ -34,7 +34,7 @@ //! cargo run --example identify -- /ip4/127.0.0.1/tcp/24915 //! ``` //! The two nodes establish a connection, negotiate the identify protocol -//! The dialing node prints out the peer id of the node it is sending identify info to +//! The dialing node prints out the `PeerId` of the node it is sending identify info to //! The other node prints out the received identify info. use futures::prelude::*; From 83c49a2de744fe331f849cfbe948a75ea2b9ee62 Mon Sep 17 00:00:00 2001 From: dadepo Date: Fri, 3 Jun 2022 21:10:12 +0200 Subject: [PATCH 05/12] Update examples/identify.rs Co-authored-by: Max Inden --- examples/identify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/identify.rs b/examples/identify.rs index 3af8ba42c1f..89a73924bc5 100644 --- a/examples/identify.rs +++ b/examples/identify.rs @@ -25,7 +25,7 @@ //! ```sh //! cargo run --example identify //! ``` -//! It will print the PeerId and the listening addresses, e.g. `Listening on +//! It will print the `PeerId` and the listening addresses, e.g. `Listening on //! "/ip4/0.0.0.0/tcp/24915"` //! //! In the second terminal window, start a new instance of the example with: From 173e82344273f03d0418059c8496a14ca0415bdc Mon Sep 17 00:00:00 2001 From: dadepo Date: Fri, 3 Jun 2022 21:10:28 +0200 Subject: [PATCH 06/12] Update examples/identify.rs Co-authored-by: Max Inden --- examples/identify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/identify.rs b/examples/identify.rs index 89a73924bc5..88865619988 100644 --- a/examples/identify.rs +++ b/examples/identify.rs @@ -26,7 +26,7 @@ //! cargo run --example identify //! ``` //! It will print the `PeerId` and the listening addresses, e.g. `Listening on -//! "/ip4/0.0.0.0/tcp/24915"` +//! "/ip6/2001:db8:: /tcp/24915"` //! //! In the second terminal window, start a new instance of the example with: //! From 53360ca7520ecfe385b03bafe39cfdfdf99ffd49 Mon Sep 17 00:00:00 2001 From: dadepo Date: Fri, 3 Jun 2022 21:10:34 +0200 Subject: [PATCH 07/12] Update examples/identify.rs Co-authored-by: Max Inden --- examples/identify.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/identify.rs b/examples/identify.rs index 88865619988..40484ed4d9f 100644 --- a/examples/identify.rs +++ b/examples/identify.rs @@ -68,7 +68,6 @@ async fn main() -> Result<(), Box> { println!("Dialed {}", addr) } - loop { match swarm.select_next_some().await { SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address), From c4ae6e4dab145bb31eac7d3de3bbacd733793bce Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Fri, 3 Jun 2022 22:25:26 +0200 Subject: [PATCH 08/12] Start the comment with upper case --- examples/identify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/identify.rs b/examples/identify.rs index 40484ed4d9f..2b4f4facdff 100644 --- a/examples/identify.rs +++ b/examples/identify.rs @@ -73,7 +73,7 @@ async fn main() -> Result<(), Box> { SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address), // Prints peer id identify info is being sent to. SwarmEvent::Behaviour(IdentifyEvent::Sent {peer_id , ..}) => println!("Sent identify info to {:?}", peer_id), - // prints out the info received via the identify event + // Prints out the info received via the identify event SwarmEvent::Behaviour(IdentifyEvent::Received {info , ..}) => println!("Received {:?}", info), _ => {} } From f60ad46fd0036f53a1a3e2a50849d4bd7e24bf0d Mon Sep 17 00:00:00 2001 From: dadepo Date: Thu, 23 Jun 2022 18:06:41 +0200 Subject: [PATCH 09/12] Update examples/identify.rs Co-authored-by: Max Inden --- examples/identify.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/identify.rs b/examples/identify.rs index 2b4f4facdff..6def17acb3e 100644 --- a/examples/identify.rs +++ b/examples/identify.rs @@ -25,7 +25,7 @@ //! ```sh //! cargo run --example identify //! ``` -//! It will print the `PeerId` and the listening addresses, e.g. `Listening on +//! It will print the [`PeerId`] and the listening addresses, e.g. `Listening on //! "/ip6/2001:db8:: /tcp/24915"` //! //! In the second terminal window, start a new instance of the example with: From 92188ad941d5f5086f2fd02a45b567f966a1bbd3 Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Thu, 23 Jun 2022 18:17:22 +0200 Subject: [PATCH 10/12] Moved identify example to the identify crate --- examples/README.md | 4 ++++ {examples => protocols/identify/examples}/identify.rs | 2 ++ 2 files changed, 6 insertions(+) rename {examples => protocols/identify/examples}/identify.rs (97%) diff --git a/examples/README.md b/examples/README.md index 98dbfbecf1c..225425e0ff7 100644 --- a/examples/README.md +++ b/examples/README.md @@ -27,6 +27,10 @@ A set of examples showcasing how to use rust-libp2p. A basic key value store demonstrating libp2p and the mDNS and Kademlia protocol. +- [Identify](../protocols/identify/examples/identify.rs) + + Demonstrates how to use identify protocol to query peer information. + - [IPFS Kademlia](ipfs-kad.rs) Demonstrates how to perform Kademlia queries on the IPFS network. diff --git a/examples/identify.rs b/protocols/identify/examples/identify.rs similarity index 97% rename from examples/identify.rs rename to protocols/identify/examples/identify.rs index 6def17acb3e..657556c20c3 100644 --- a/examples/identify.rs +++ b/protocols/identify/examples/identify.rs @@ -41,7 +41,9 @@ use futures::prelude::*; use libp2p::swarm::{Swarm, SwarmEvent}; use libp2p::{identity, Multiaddr, PeerId}; use std::error::Error; +use libp2p_core::{identity, Multiaddr, PeerId}; use libp2p_identify::{Identify, IdentifyConfig, IdentifyEvent}; +use libp2p_swarm::{Swarm, SwarmEvent}; #[async_std::main] async fn main() -> Result<(), Box> { From bd1e117c1e4795c9ff01440bb50963d70645fb2c Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Sat, 25 Jun 2022 00:27:40 +0200 Subject: [PATCH 11/12] Ran rustfmt and added libp2p as an explicit dev-dependency --- protocols/identify/Cargo.toml | 1 + protocols/identify/examples/identify.rs | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index 44a62b5d837..2b5235325cc 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -27,6 +27,7 @@ void = "1.0" [dev-dependencies] async-std = "1.6.2" env_logger = "0.9" +libp2p = { path = "../..", default-features = false, features = ["identify", "tcp-async-io"] } libp2p-mplex = { path = "../../muxers/mplex" } libp2p-noise = { path = "../../transports/noise" } libp2p-tcp = { path = "../../transports/tcp" } diff --git a/protocols/identify/examples/identify.rs b/protocols/identify/examples/identify.rs index 657556c20c3..fcb96f34d32 100644 --- a/protocols/identify/examples/identify.rs +++ b/protocols/identify/examples/identify.rs @@ -38,12 +38,10 @@ //! The other node prints out the received identify info. use futures::prelude::*; -use libp2p::swarm::{Swarm, SwarmEvent}; use libp2p::{identity, Multiaddr, PeerId}; -use std::error::Error; -use libp2p_core::{identity, Multiaddr, PeerId}; use libp2p_identify::{Identify, IdentifyConfig, IdentifyEvent}; use libp2p_swarm::{Swarm, SwarmEvent}; +use std::error::Error; #[async_std::main] async fn main() -> Result<(), Box> { @@ -54,7 +52,10 @@ async fn main() -> Result<(), Box> { let transport = libp2p::development_transport(local_key.clone()).await?; // Create a identify network behaviour. - let behaviour = Identify::new(IdentifyConfig::new("/ipfs/id/1.0.0".to_string(), local_key.public())); + let behaviour = Identify::new(IdentifyConfig::new( + "/ipfs/id/1.0.0".to_string(), + local_key.public(), + )); let mut swarm = Swarm::new(transport, behaviour, local_peer_id); @@ -74,9 +75,13 @@ async fn main() -> Result<(), Box> { match swarm.select_next_some().await { SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address), // Prints peer id identify info is being sent to. - SwarmEvent::Behaviour(IdentifyEvent::Sent {peer_id , ..}) => println!("Sent identify info to {:?}", peer_id), + SwarmEvent::Behaviour(IdentifyEvent::Sent { peer_id, .. }) => { + println!("Sent identify info to {:?}", peer_id) + } // Prints out the info received via the identify event - SwarmEvent::Behaviour(IdentifyEvent::Received {info , ..}) => println!("Received {:?}", info), + SwarmEvent::Behaviour(IdentifyEvent::Received { info, .. }) => { + println!("Received {:?}", info) + } _ => {} } } From f2668f392ac520e13d87c1320a2263c69ee52145 Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Sun, 26 Jun 2022 20:06:08 +0200 Subject: [PATCH 12/12] Updated comments to be more accurate --- protocols/identify/examples/identify.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/protocols/identify/examples/identify.rs b/protocols/identify/examples/identify.rs index fcb96f34d32..278cf0cc991 100644 --- a/protocols/identify/examples/identify.rs +++ b/protocols/identify/examples/identify.rs @@ -26,7 +26,7 @@ //! cargo run --example identify //! ``` //! It will print the [`PeerId`] and the listening addresses, e.g. `Listening on -//! "/ip6/2001:db8:: /tcp/24915"` +//! "/ip4/127.0.0.1/tcp/24915"` //! //! In the second terminal window, start a new instance of the example with: //! @@ -34,8 +34,7 @@ //! cargo run --example identify -- /ip4/127.0.0.1/tcp/24915 //! ``` //! The two nodes establish a connection, negotiate the identify protocol -//! The dialing node prints out the `PeerId` of the node it is sending identify info to -//! The other node prints out the received identify info. +//! and will send each other identify info which is then printed to the console. use futures::prelude::*; use libp2p::{identity, Multiaddr, PeerId};