Skip to content

Commit

Permalink
feat(swarm): log local peer id on swarm creation
Browse files Browse the repository at this point in the history
Closes #4394.

Pull-Request: #4439.
  • Loading branch information
sufferpurrityxd committed Sep 12, 2023
1 parent 581b1e4 commit b0c3da7
Show file tree
Hide file tree
Showing 18 changed files with 18 additions and 23 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/autonat/src/bin/autonat_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ async fn main() -> Result<(), Box<dyn Error>> {

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 = tcp::async_io::Transport::default()
.upgrade(Version::V1Lazy)
Expand Down
1 change: 0 additions & 1 deletion examples/autonat/src/bin/autonat_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ async fn main() -> Result<(), Box<dyn Error>> {

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 = tcp::async_io::Transport::default()
.upgrade(Version::V1Lazy)
Expand Down
2 changes: 1 addition & 1 deletion examples/chat-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ struct MyBehaviour {
#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Create a random PeerId
env_logger::init();
let id_keys = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(id_keys.public());
println!("Local peer id: {local_peer_id}");

// Set up an encrypted DNS-enabled TCP Transport over the yamux protocol.
let tcp_transport = tcp::async_io::Transport::new(tcp::Config::default().nodelay(true))
Expand Down
1 change: 0 additions & 1 deletion examples/dcutr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ fn main() -> Result<(), Box<dyn Error>> {

let local_key = generate_ed25519(opts.secret_key_seed);
let local_peer_id = PeerId::from(local_key.public());
info!("Local peer id: {:?}", local_peer_id);

let (relay_transport, client) = relay::client::new(local_peer_id);

Expand Down
1 change: 1 addition & 0 deletions examples/identify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ license = "MIT"
[dependencies]
async-std = { version = "1.12", features = ["attributes"] }
async-trait = "0.1"
env_logger = "0.10"
futures = "0.3.28"
libp2p = { path = "../../libp2p", features = ["async-std", "dns", "dcutr", "identify", "macros", "noise", "ping", "relay", "rendezvous", "tcp", "tokio", "yamux"] }
2 changes: 1 addition & 1 deletion examples/identify/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ use std::error::Error;

#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
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 = tcp::async_io::Transport::default()
.upgrade(Version::V1Lazy)
Expand Down
1 change: 0 additions & 1 deletion examples/metrics/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ fn main() -> Result<(), Box<dyn Error>> {
let local_key = identity::Keypair::generate_ed25519();
let local_peer_id = PeerId::from(local_key.public());
let local_pub_key = local_key.public();
info!("Local peer id: {local_peer_id:?}");

let mut swarm = SwarmBuilder::without_executor(
tcp::async_io::Transport::default()
Expand Down
1 change: 1 addition & 0 deletions examples/ping-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ 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"] }
2 changes: 1 addition & 1 deletion examples/ping-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ use std::error::Error;

#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();
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 = tcp::async_io::Transport::default()
.upgrade(Version::V1Lazy)
Expand Down
1 change: 0 additions & 1 deletion examples/relay-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ fn main() -> Result<(), Box<dyn Error>> {
// Create a static known PeerId based on given secret
let local_key: identity::Keypair = generate_ed25519(opt.secret_key_seed);
let local_peer_id = PeerId::from(local_key.public());
println!("Local peer id: {local_peer_id:?}");

let tcp_transport = tcp::async_io::Transport::default();

Expand Down
2 changes: 0 additions & 2 deletions examples/rendezvous/src/bin/rzv-discover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ async fn main() {
)
.build();

log::info!("Local peer id: {}", swarm.local_peer_id());

swarm.dial(rendezvous_point_address.clone()).unwrap();

let mut discover_tick = tokio::time::interval(Duration::from_secs(30));
Expand Down
2 changes: 0 additions & 2 deletions examples/rendezvous/src/bin/rzv-identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ async fn main() {
)
.build();

log::info!("Local peer id: {}", swarm.local_peer_id());

let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap());

swarm.dial(rendezvous_point_address.clone()).unwrap();
Expand Down
2 changes: 0 additions & 2 deletions examples/rendezvous/src/bin/rzv-register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ async fn main() {
let external_address = "/ip4/127.0.0.1/tcp/0".parse::<Multiaddr>().unwrap();
swarm.add_external_address(external_address);

log::info!("Local peer id: {}", swarm.local_peer_id());

swarm.dial(rendezvous_point_address.clone()).unwrap();

while let Some(event) = swarm.next().await {
Expand Down
2 changes: 0 additions & 2 deletions examples/rendezvous/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ async fn main() {
)
.build();

log::info!("Local peer id: {}", swarm.local_peer_id());

let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/62649".parse().unwrap());

while let Some(event) = swarm.next().await {
Expand Down
16 changes: 10 additions & 6 deletions libp2p/src/tutorials/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
//! [dependencies]
//! libp2p = { version = "0.50", features = ["tcp", "dns", "async-std", "noise", "yamux", "websocket", "ping", "macros"] }
//! futures = "0.3.21"
//! env_logger = "0.10.0"
//! async-std = { version = "1.12.0", features = ["attributes"] }
//! ```
//!
Expand Down Expand Up @@ -177,7 +178,11 @@
//! carried out by a [`Swarm`]. Put simply, a [`Swarm`] drives both a
//! [`Transport`] and a [`NetworkBehaviour`] forward, passing commands from the
//! [`NetworkBehaviour`] to the [`Transport`] as well as events from the
//! [`Transport`] to the [`NetworkBehaviour`].
//! [`Transport`] to the [`NetworkBehaviour`]. As you can see, after [`Swarm`] initialization, we
//! removed the print of the local [`PeerId`](crate::PeerId) because every time a [`Swarm`] is
//! created, it prints the local [`PeerId`](crate::PeerId) in the logs at the INFO level. In order
//! to continue to see the local [`PeerId`](crate::PeerId) you must initialize the logger
//! (In our example, `env_logger` is used)
//!
//! ```rust
//! use libp2p::swarm::{keep_alive, NetworkBehaviour, SwarmBuilder};
Expand All @@ -186,9 +191,9 @@
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! env_logger::init();
//! 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).await?;
//!
Expand Down Expand Up @@ -243,9 +248,9 @@
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! env_logger::init();
//! 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).await?;
//!
Expand Down Expand Up @@ -293,9 +298,9 @@
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! env_logger::init();
//! 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).await?;
//!
Expand Down Expand Up @@ -349,9 +354,8 @@
//! cargo run --example ping
//! ```
//!
//! It will print the PeerId and the new listening addresses, e.g.
//! It will print the new listening addresses, e.g.
//! ```sh
//! Local peer id: PeerId("12D3KooWT1As4mwh3KYBnNTw9bSrRbYQGJTm9SSte82JSumqgCQG")
//! Listening on "/ip4/127.0.0.1/tcp/24915"
//! Listening on "/ip4/192.168.178.25/tcp/24915"
//! Listening on "/ip4/172.17.0.1/tcp/24915"
Expand Down
1 change: 0 additions & 1 deletion misc/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ async fn main() -> Result<(), Box<dyn Error>> {

(peer_id, keypair)
};
info!("Local peer id: {local_peer_id}");

let transport = {
let tcp_transport =
Expand Down
1 change: 1 addition & 0 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,7 @@ where

/// Builds a `Swarm` with the current configuration.
pub fn build(self) -> Swarm<TBehaviour> {
log::info!("Local peer id: {}", self.local_peer_id);
Swarm {
local_peer_id: self.local_peer_id,
transport: self.transport,
Expand Down

0 comments on commit b0c3da7

Please sign in to comment.