Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logs): enable faucet logs #987

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions sn_client/src/faucet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,26 @@ pub async fn get_tokens_from_faucet(
/// With all balance transferred from the genesis_wallet to the faucet_wallet.
pub async fn load_faucet_wallet_from_genesis_wallet(client: &Client) -> Result<LocalWallet> {
println!("Loading faucet...");
info!("Loading faucet...");
let mut faucet_wallet = create_faucet_wallet();

let faucet_balance = faucet_wallet.balance();
if !faucet_balance.is_zero() {
println!("Faucet wallet balance: {faucet_balance}");
debug!("Faucet wallet balance: {faucet_balance}");
return Ok(faucet_wallet);
}

println!("Loading genesis...");
debug!("Loading genesis...");
let genesis_wallet = load_genesis_wallet()?;

// Transfer to faucet. We will transfer almost all of the genesis wallet's
// balance to the faucet,.

let faucet_balance = genesis_wallet.balance();
println!("Sending {faucet_balance} from genesis to faucet wallet..");
debug!("Sending {faucet_balance} from genesis to faucet wallet..");
let cash_note = send(
genesis_wallet,
faucet_balance,
Expand All @@ -56,12 +60,16 @@ pub async fn load_faucet_wallet_from_genesis_wallet(client: &Client) -> Result<L
.deposit_and_store_to_disk(&vec![cash_note.clone()])
.expect("Faucet wallet shall be stored successfully.");
println!("Faucet wallet balance: {}", faucet_wallet.balance());
debug!("Faucet wallet balance: {}", faucet_wallet.balance());

println!("Verifying the transfer from genesis...");
debug!("Verifying the transfer from genesis...");
if let Err(error) = client.verify(&cash_note).await {
error!("Could not verify the transfer from genesis: {error:?}. Panicking.");
panic!("Could not verify the transfer from genesis: {error:?}");
} else {
println!("Successfully verified the transfer from genesis on the second try.");
info!("Successfully verified the transfer from genesis on the second try.");
}

Ok(faucet_wallet)
Expand Down
34 changes: 23 additions & 11 deletions sn_faucet/src/faucet_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use color_eyre::eyre::{eyre, Result};
use sn_client::Client;
use std::path;
use tiny_http::{Response, Server};
use tracing::{debug, error, trace};

/// Run the faucet server.
///
Expand All @@ -33,36 +34,47 @@ use tiny_http::{Response, Server};

pub async fn run_faucet_server(client: &Client) -> Result<()> {
let server =
Server::http("0.0.0.0:8000").map_err(|e| eyre!("Failed to start server: {}", e))?;
claim_genesis(client).await.map_err(|e| {
Server::http("0.0.0.0:8000").map_err(|err| eyre!("Failed to start server: {err}"))?;
claim_genesis(client).await.map_err(|err| {
eprintln!("Faucet Server couldn't start as we failed to claim Genesis");
e
error!("Faucet Server couldn't start as we failed to claim Genesis");
err
})?;

println!("Starting http server listening on port 8000...");
debug!("Starting http server listening on port 8000...");
for request in server.incoming_requests() {
println!(
"received request! method: {:?}, url: {:?}, headers: {:?}",
request.method(),
request.url(),
request.headers()
);
trace!(
"received request! method: {:?}, url: {:?}, headers: {:?}",
request.method(),
request.url(),
request.headers()
);
let key = request.url().trim_matches(path::is_separator);

match send_tokens(client, "100", key).await {
Ok(transfer) => {
println!("Sent tokens to {}", key);
println!("Sent tokens to {key}");
debug!("Sent tokens to {key}");
let response = Response::from_string(transfer);
let _ = request
.respond(response)
.map_err(|e| eprintln!("Failed to send response: {}", e));
let _ = request.respond(response).map_err(|err| {
eprintln!("Failed to send response: {err}");
error!("Failed to send response: {err}");
});
}
Err(e) => {
eprintln!("Failed to send tokens to {}: {}", key, e);
let response = Response::from_string(format!("Failed to send tokens: {}", e));
Err(err) => {
eprintln!("Failed to send tokens to {key}: {err}");
error!("Failed to send tokens to {key}: {err}");
let response = Response::from_string(format!("Failed to send tokens: {err}"));
let _ = request
.respond(response.with_status_code(500))
.map_err(|e| eprintln!("Failed to send response: {}", e));
.map_err(|err| eprintln!("Failed to send response: {err}"));
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions sn_faucet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sn_logging::{LogBuilder, LogOutputDest};
use sn_peers_acquisition::{parse_peers_args, PeersArgs};
use sn_transfers::{parse_main_pubkey, NanoTokens, Transfer};
use std::path::PathBuf;
use tracing::info;
use tracing::{error, info};
use tracing_core::Level;

#[tokio::main]
Expand All @@ -34,11 +34,11 @@ async fn main() -> Result<()> {
let _log_appender_guard = if let Some(log_output_dest) = opt.log_output_dest {
let logging_targets = vec![
// TODO: Reset to nice and clean defaults once we have a better idea of what we want
("faucet".to_string(), Level::TRACE),
("sn_faucet".to_string(), Level::TRACE),
("sn_networking".to_string(), Level::DEBUG),
("safenode".to_string(), Level::TRACE),
("sn_build_info".to_string(), Level::TRACE),
("sn_logging".to_string(), Level::TRACE),
("sn_node".to_string(), Level::TRACE),
("sn_peers_acquisition".to_string(), Level::TRACE),
("sn_protocol".to_string(), Level::TRACE),
("sn_registers".to_string(), Level::TRACE),
Expand All @@ -55,9 +55,14 @@ async fn main() -> Result<()> {
info!("Instantiating a SAFE Test Faucet...");

let secret_key = bls::SecretKey::random();
let client = Client::new(secret_key, bootstrap_peers, None).await?;

faucet_cmds(opt.cmd, &client).await?;
match Client::new(secret_key, bootstrap_peers, None).await {
Ok(client) => {
if let Err(err) = faucet_cmds(opt.cmd.clone(), &client).await {
error!("Failed to run faucet cmd {:?} with err {err:?}", opt.cmd)
}
}
Err(err) => error!("Failed to get Client with err {err:?}"),
}

Ok(())
}
Expand Down Expand Up @@ -87,7 +92,7 @@ struct Opt {
pub cmd: SubCmd,
}

#[derive(Subcommand, Debug)]
#[derive(Subcommand, Debug, Clone)]
enum SubCmd {
/// Claim the amount in the genesis CashNote and deposit it to the faucet local wallet.
/// This needs to be run before a testnet is opened to the public, as to not have
Expand Down
6 changes: 6 additions & 0 deletions sn_logging/src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,19 @@ fn get_logging_targets(logging_env_value: &str) -> Result<Vec<(String, Level)>>
// extend will overwrite values inside `targets`
targets.extend(vec![
networking_log_level,
// bins
("faucet".to_string(), Level::TRACE),
("safenode".to_string(), Level::TRACE),
("safenode_rpc_client".to_string(), Level::TRACE),
("safe".to_string(), Level::TRACE),
// libs
("sn_build_info".to_string(), Level::TRACE),
("sn_cli".to_string(), Level::TRACE),
("sn_client".to_string(), Level::TRACE),
("sn_faucet".to_string(), Level::TRACE),
("sn_logging".to_string(), Level::TRACE),
("sn_node".to_string(), Level::TRACE),
("sn_node_rpc_client".to_string(), Level::TRACE),
("sn_peers_acquisition".to_string(), Level::TRACE),
("sn_protocol".to_string(), Level::TRACE),
("sn_registers".to_string(), Level::INFO),
Expand Down