Skip to content

Commit

Permalink
chore: Migrate from log to tracing (#55)
Browse files Browse the repository at this point in the history
* chore: Migrate from `log` to `tracing`

* replace all println!() with debug!()
  • Loading branch information
oblique authored Sep 11, 2023
1 parent bd6394b commit b61521c
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 36 deletions.
113 changes: 110 additions & 3 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion celestia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ license = "Apache-2.0"
crate-type = ["rlib", "cdylib"]

[dependencies]
anyhow = "1.0.71"
celestia-node = { workspace = true }
celestia-rpc = { workspace = true }

anyhow = "1.0.71"
tracing = "0.1.37"
tracing-appender = "0.2.2"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
dotenvy = "0.15.7"
libp2p = { version = "0.52.3", features = [
Expand Down
50 changes: 36 additions & 14 deletions celestia/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,17 @@ use std::env;
use anyhow::{Context, Result};
use celestia_node::node::{Node, NodeConfig};
use celestia_rpc::prelude::*;
use libp2p::{core::upgrade::Version, identity, noise, tcp, yamux, Transport};
use libp2p::{core::upgrade::Version, identity, noise, tcp, yamux, Multiaddr, Transport};
use tracing::info;

const WS_URL: &str = "ws://localhost:26658";

#[tokio::main]
pub async fn run() -> Result<()> {
let _ = dotenvy::dotenv();
let _guard = init_tracing();

// Get the address of the local bridge node
let auth_token = env::var("CELESTIA_NODE_AUTH_TOKEN_ADMIN")?;
let client = celestia_rpc::client::new_websocket(WS_URL, Some(&auth_token)).await?;
let bridge_info = client.p2p_info().await?;
println!("bridge id: {:?}", bridge_info.id);
println!("bridge listens on: {:?}", bridge_info.addrs);

let bridge_ma = bridge_info
.addrs
.into_iter()
.find(|ma| ma.protocol_stack().any(|protocol| protocol == "tcp"))
.context("Bridge doesn't listen on tcp")?;

let bridge_ma = fetch_bridge_multiaddr().await?;
let p2p_local_keypair = identity::Keypair::generate_ed25519();

let p2p_transport = tcp::tokio::Transport::default()
Expand All @@ -44,3 +34,35 @@ pub async fn run() -> Result<()> {

Ok(())
}

/// Get the address of the local bridge node
async fn fetch_bridge_multiaddr() -> Result<Multiaddr> {
let auth_token = env::var("CELESTIA_NODE_AUTH_TOKEN_ADMIN")?;
let client = celestia_rpc::client::new_websocket(WS_URL, Some(&auth_token)).await?;
let bridge_info = client.p2p_info().await?;
info!("bridge id: {:?}", bridge_info.id);
info!("bridge listens on: {:?}", bridge_info.addrs);

let bridge_ma = bridge_info
.addrs
.into_iter()
.find(|ma| ma.protocol_stack().any(|protocol| protocol == "tcp"))
.context("Bridge doesn't listen on tcp")?;

Ok(bridge_ma)
}

fn init_tracing() -> tracing_appender::non_blocking::WorkerGuard {
let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout());

let filter = tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into())
.from_env_lossy();

tracing_subscriber::fmt()
.with_env_filter(filter)
.with_writer(non_blocking)
.init();

guard
}
2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ libp2p = { version = "0.52.3", features = [
"macros",
"request-response",
] }
log = "0.4.20"
prost = "0.12.0"
thiserror = "1.0.48"
tokio = { version = "1.32.0", features = ["macros", "sync"] }
tracing = "0.1.37"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1.32.0", features = ["rt-multi-thread"] }
Expand Down
20 changes: 9 additions & 11 deletions node/src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use libp2p::swarm::{
THandlerErr,
};
use libp2p::{identify, request_response, Multiaddr, PeerId, TransportError};
use log::{error, trace, warn};
use tendermint_proto::Protobuf;
use tokio::select;
use tracing::{debug, instrument, warn};

use crate::executor::{spawn, Executor};
use crate::utils::gossipsub_ident_topic;
Expand Down Expand Up @@ -187,12 +187,11 @@ impl Worker {
}
}

#[instrument(level = "trace", skip(self))]
async fn on_swarm_event(
&mut self,
ev: &SwarmEvent<BehaviourEvent, THandlerErr<Behaviour>>,
) -> Result<()> {
trace!("{ev:?}");

#[allow(clippy::single_match)]
match ev {
SwarmEvent::Behaviour(ev) => match ev {
Expand All @@ -207,9 +206,8 @@ impl Worker {
Ok(())
}

#[instrument(level = "trace", skip(self))]
async fn on_command(&mut self, cmd: P2pCmd) -> Result<()> {
trace!("{cmd:?}");

match cmd {
P2pCmd::NetworkInfo { respond_to } => {
let _ = respond_to.send(self.swarm.network_info());
Expand All @@ -234,15 +232,15 @@ impl Worker {
response,
},
} => {
println!(
debug!(
"Response for request: {request_id}, from peer: {peer}, status: {:?}",
response.status_code()
);
let header = ExtendedHeader::decode(&response.body[..]).unwrap();
// TODO: Forward response back with one shot channel
println!("Header: {header:?}");
debug!("Header: {header:?}");
}
_ => println!("Unhandled header_ex event: {ev:?}"),
_ => debug!("Unhandled header_ex event: {ev:?}"),
}

Ok(())
Expand All @@ -259,12 +257,12 @@ impl Worker {
let header = ExtendedHeader::decode(&message.data[..]).unwrap();
// TODO: produce event

println!("New header from header-sub: {header:?}");
debug!("New header from header-sub: {header:?}");
} else {
println!("New gossipsub message, id: {message_id}, message: {message:?}");
debug!("New gossipsub message, id: {message_id}, message: {message:?}");
}
}
_ => println!("Unhandled gossipsub event: {ev:?}"),
_ => debug!("Unhandled gossipsub event: {ev:?}"),
}

Ok(())
Expand Down
5 changes: 3 additions & 2 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ thiserror = "1.0.40"
jsonrpsee = { version = "0.20", features = ["client-core", "macros"] }
celestia-types = { workspace = true }
serde = { version = "1.0.188", features = ["derive"] }
tracing = "0.1.37"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
http = "0.2.9"
Expand All @@ -18,10 +19,10 @@ jsonrpsee = { version = "0.20", features = ["http-client", "ws-client"] }
anyhow = "1.0.71"
dotenvy = "0.15.7"
futures = "0.3.28"
libp2p = "0.52.3"
log = "0.4"
libp2p = { version = "0.52.3", features = ["tokio", "macros", "tcp", "noise", "yamux"] }
rand = "0.8.5"
tokio = { version = "1.32.0", features = ["rt", "macros"] }
tracing = "0.1.37"

[features]
default = ["p2p"]
Expand Down
Loading

0 comments on commit b61521c

Please sign in to comment.