Skip to content

Commit

Permalink
feat: add basic logger/tracing, replacing printlns (#132)
Browse files Browse the repository at this point in the history
Adding `tracing` and replace all the `println`. We should add more info
in a followup pr.
Thx!
  • Loading branch information
pepoviola authored Nov 8, 2023
1 parent 216cc45 commit 1ceb498
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 65 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ sp-core = "22.0.0"
libp2p = { version = "0.52" }
subxt = "0.32.0"
subxt-signer = { version = "0.32.0", features = ["subxt"]}
tracing = "0.1.35"
1 change: 1 addition & 0 deletions crates/orchestrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ libp2p = { workspace = true }
subxt = { workspace = true }
subxt-signer = { workspace = true }
reqwest = { workspace = true }
tracing = { workspace = true }
27 changes: 2 additions & 25 deletions crates/orchestrator/src/generators/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use provider::{
};
use serde_json::json;
use support::fs::FileSystem;
use tracing::warn;

use super::errors::GeneratorError;
use crate::{
Expand Down Expand Up @@ -399,32 +400,12 @@ impl ChainSpec {
}
}

// TODO: move to logger
// println!(
// "{:#?}",
// chain_spec_json.pointer(format!("{}/session/keys", pointer).as_str())
// );
// Clear authorities
clear_authorities(&pointer, &mut chain_spec_json);

// TODO: move to logger
// println!(
// "{:#?}",
// chain_spec_json.pointer(format!("{}/session/keys", pointer).as_str())
// );

// TODO: add to logger
// println!("BALANCES");
// println!("{:#?}", chain_spec_json.pointer(format!("{}/balances",pointer).as_str()));
// add balances
add_balances(&pointer, &mut chain_spec_json, &relaychain.nodes, 0);

// TODO: move to logger
// println!(
// "{:#?}",
// chain_spec_json.pointer(format!("{}/balances", pointer).as_str())
// );

// Get validators to add as authorities
let validators: Vec<&NodeSpec> = relaychain
.nodes
Expand All @@ -447,10 +428,6 @@ impl ChainSpec {

// staking && nominators

// TODO: add to logger
// println!("KEYS");
// println!("{:#?}", chain_spec_json.pointer(format!("{}/session/keys",pointer).as_str()));

// add_hrmp_channels

// paras
Expand Down Expand Up @@ -674,7 +651,7 @@ fn add_balances(
if let Some(val) = chain_spec_json.pointer_mut(runtime_config_ptr) {
let Some(balances) = val.pointer("/balances/balances") else {
// should be a info log
println!("NO 'balances' key in runtime config, skipping...");
warn!("NO 'balances' key in runtime config, skipping...");
return;
};

Expand Down
16 changes: 5 additions & 11 deletions crates/orchestrator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ use network_spec::{parachain::ParachainSpec, NetworkSpec};
use provider::{constants::LOCALHOST, types::TransferedFile, Provider};
use support::fs::{FileSystem, FileSystemError};
use tokio::time::timeout;
use tracing::{debug, info};

use crate::{
generators::chain_spec::ParaGenesisConfig, shared::types::RegisterParachainOptions,
spawner::SpawnNodeCtx,
};

pub struct Orchestrator<T, P>
where
T: FileSystem + Sync + Send,
Expand Down Expand Up @@ -68,15 +68,13 @@ where
mut network_spec: NetworkSpec,
) -> Result<Network<T>, OrchestratorError> {
// main driver for spawn the network
// TODO: move to logger
// println!("{:#?}", network_spec);
debug!("Network spec to spawn, {:#?}", network_spec);

// create namespace
let ns = self.provider.create_namespace().await?;

println!("\n\n");
println!("🧰 ns: {:#?}", ns.name());
println!("🧰 base_dir: {:#?}", ns.base_dir());
info!("🧰 ns: {}", ns.name());
info!("🧰 base_dir: {:?}", ns.base_dir());

// TODO: noop for native
// Static setup
Expand All @@ -91,9 +89,6 @@ where
.build(&ns, &scoped_fs)
.await?;

// TODO: move to logger
// println!("{:#?}", network_spec.relaychain.chain_spec);

// Create parachain artifacts (chain-spec, wasm, state)
let relay_chain_id = network_spec
.relaychain
Expand All @@ -106,8 +101,7 @@ where
let para_cloned = para.clone();
let chain_spec_raw_path = if let Some(chain_spec) = para.chain_spec.as_mut() {
chain_spec.build(&ns, &scoped_fs).await?;
// TODO: move to logger
// println!("{:#?}", chain_spec);
debug!("chain_spec: {:#?}", chain_spec);

chain_spec
.customize_para(&para_cloned, &relay_chain_id, &scoped_fs)
Expand Down
5 changes: 3 additions & 2 deletions crates/orchestrator/src/network/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
use subxt::{dynamic::Value, OnlineClient, SubstrateConfig};
use subxt_signer::{sr25519::Keypair, SecretUri};
use support::fs::FileSystem;
use tracing::info;

// use crate::generators::key::generate_pair;
// use sp_core::{sr25519, Pair};
Expand Down Expand Up @@ -50,7 +51,7 @@ impl Parachain {
options: RegisterParachainOptions,
scoped_fs: &ScopedFilesystem<'_, impl FileSystem>,
) -> Result<(), anyhow::Error> {
println!("Registering parachain: {:?}", options);
info!("Registering parachain: {:?}", options);
// get the seed
let sudo: Keypair;
if let Some(possible_seed) = options.seed {
Expand Down Expand Up @@ -100,7 +101,7 @@ impl Parachain {
.await?;

let result = result.wait_for_in_block().await?;
println!("In block: {:#?}", result.block_hash());
info!("In block: {:#?}", result.block_hash());
Ok(())
}
}
4 changes: 2 additions & 2 deletions crates/orchestrator/src/network_helper/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::time::Duration;

use tokio::time::timeout;
use tracing::trace;

use crate::network::node::NetworkNode;

Expand All @@ -18,8 +19,7 @@ async fn check_nodes(nodes: &[&NetworkNode]) {
let tasks: Vec<_> = nodes
.iter()
.map(|node| {
// TODO: move to logger
// println!("getting from {}", node.name);
trace!("🔎 checking node: {} ", node.name);
reqwest::get(node.prometheus_uri.clone())
})
.collect();
Expand Down
21 changes: 12 additions & 9 deletions crates/orchestrator/src/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use provider::{
DynNamespace,
};
use support::fs::FileSystem;
use tracing::info;

use crate::{
generators,
Expand Down Expand Up @@ -124,9 +125,12 @@ where
* ZombieRole::Companion => todo!(), */
};

println!("\n");
println!("🚀 {}, spawning.... with command:", node.name);
println!("{program} {}", args.join(" "));
info!(
"🚀 {}, spawning.... with command: {} {}",
node.name,
program,
args.join(" ")
);

let spawn_ops = SpawnNodeOptions {
name: node.name.clone(),
Expand All @@ -150,14 +154,13 @@ where

let ws_uri = format!("ws://{}:{}", LOCALHOST, node.rpc_port.0);
let prometheus_uri = format!("http://{}:{}/metrics", LOCALHOST, node.prometheus_port.0);
println!("🚀 {}, should be running now", node.name);
println!(
"🚀 {} : direct link https://polkadot.js.org/apps/?rpc={ws_uri}#/explorer",
info!("🚀 {}, should be running now", node.name);
info!(
"🚀 {}: direct link https://polkadot.js.org/apps/?rpc={ws_uri}#/explorer",
node.name
);
println!("🚀 {} : metrics link {prometheus_uri}", node.name);
println!("📓 logs cmd: tail -f {}/{}.log", base_dir, node.name);
println!("\n");
info!("🚀 {}: metrics link {prometheus_uri}", node.name);
info!("📓 logs cmd: tail -f {}/{}.log", base_dir, node.name);
Ok(NetworkNode::new(
node.name.clone(),
ws_uri,
Expand Down
Empty file.
5 changes: 3 additions & 2 deletions crates/orchestrator/src/tx_helper/register_para.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ use subxt_signer::{sr25519::Keypair, SecretUri};
use support::fs::FileSystem;

use crate::{shared::types::RegisterParachainOptions, ScopedFilesystem};
use tracing::{debug, info, trace};


pub async fn register(
options: RegisterParachainOptions,
scoped_fs: &ScopedFilesystem<'_, impl FileSystem>,
) -> Result<(), anyhow::Error> {
println!("Registering parachain: {:?}", options);
debug!("Registering parachain: {:?}", options);
// get the seed
let sudo: Keypair;
if let Some(possible_seed) = options.seed {
Expand Down Expand Up @@ -61,6 +62,6 @@ pub async fn register(
.await?;

let result = result.wait_for_in_block().await?;
println!("In block: {:#?}", result.block_hash());
debug!("In block: {:#?}", result.block_hash());
Ok(())
}
13 changes: 4 additions & 9 deletions crates/orchestrator/src/tx_helper/validator_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use std::str::FromStr;

use subxt::{dynamic::Value, OnlineClient, SubstrateConfig};
use subxt_signer::{sr25519::Keypair, SecretUri};
use tracing::{debug, info, trace};


pub async fn register(
validator_ids: Vec<String>,
node_ws_url: &str,
) -> Result<(), anyhow::Error> {
println!("Registering validators: {:?}", validator_ids);
debug!("Registering validators: {:?}", validator_ids);
// get the seed
// let sudo: Keypair;
// if let Some(possible_seed) = options.seed {
Expand All @@ -18,12 +19,7 @@ pub async fn register(
let sudo = Keypair::from_uri(&uri)?;
// }

println!("pse");
let api = OnlineClient::<SubstrateConfig>::from_url(node_ws_url).await?;
println!("pse connected");

// let bytes: Vec<Value> = validator_ids.iter().map(|id| Value::from_bytes(id)).collect();
// println!("{:?}", bytes);

let register_call = subxt::dynamic::tx(
"ValidatorManager",
Expand All @@ -33,16 +29,15 @@ pub async fn register(

let sudo_call = subxt::dynamic::tx("Sudo", "sudo", vec![register_call.into_value()]);

println!("pse1");
// TODO: uncomment below and fix the sign and submit (and follow afterwards until
// finalized block) to register the parachain
let result = api
.tx()
.sign_and_submit_then_watch_default(&sudo_call, &sudo)
.await?;

println!("result: {:#?}", result);
debug!("result: {:#?}", result);
let result = result.wait_for_in_block().await?;
println!("In block: {:#?}", result.block_hash());
debug!("In block: {:#?}", result.block_hash());
Ok(())
}
3 changes: 2 additions & 1 deletion crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ tokio = { workspace = true, features = [
thiserror = { workspace = true }
anyhow = { workspace = true }
uuid = { workspace = true, features = ["v4"] }
nix = { workspace = true, features = ["signal"] }
nix = { workspace = true, features = ["signal"] }
tracing = { workspace = true }
12 changes: 8 additions & 4 deletions crates/provider/src/native/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use async_trait::async_trait;
use futures::{future::try_join_all, try_join};
use support::{fs::FileSystem, process::ProcessManager};
use tokio::sync::RwLock;
use tracing::trace;
use uuid::Uuid;

use super::{
Expand Down Expand Up @@ -189,10 +190,13 @@ where
local_output_path,
} in options.commands
{
// TODO: move to logger
// println!("{:#?}, {:#?}", command, args);
// println!("{:#?}", self.base_dir.to_string_lossy());
// println!("{:#?}", local_output_path.as_os_str());
trace!(
"🏗 building file {:?} in path {} with command {} {}",
local_output_path.as_os_str(),
self.base_dir.to_string_lossy(),
program,
args.join(" ")
);
let local_output_full_path = format!(
"{}{}{}",
self.base_dir.to_string_lossy(),
Expand Down

0 comments on commit 1ceb498

Please sign in to comment.