Skip to content

Commit

Permalink
fix(auditor): create auditor directory if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Jul 10, 2024
1 parent 00bd81c commit 8deb188
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
11 changes: 6 additions & 5 deletions sn_auditor/src/dag_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,18 @@ impl SpendDagDb {
client: Client,
encryption_sk: Option<SecretKey>,
) -> Result<Self> {
if !path.exists() {
std::fs::create_dir_all(&path)?;
}
let dag_path = path.join(SPEND_DAG_FILENAME);
info!("Loading DAG from {dag_path:?}...");
let dag = match SpendDag::load_from_file(&dag_path) {
Ok(d) => {
println!("Found a local spend DAG file");
info!("Found a local spend DAG file");
d
}
Err(_) => {
println!("Found no local spend DAG file, starting from Genesis");
info!("Found no local spend DAG file, starting from Genesis");
client.new_dag_with_genesis_only().await?
}
};
Expand Down Expand Up @@ -232,7 +235,7 @@ impl SpendDagDb {
});
Some(tx)
} else {
eprintln!("Foundation secret key not set! Beta rewards will not be processed.");
warn!("Foundation secret key not set! Beta rewards will not be processed.");
None
};

Expand Down Expand Up @@ -364,7 +367,6 @@ impl SpendDagDb {
{
if let Some(user_name) = beta_participants_read.get(&default_user_name_hash) {
warn!("With default key, got forwarded reward {amount} from {user_name} of {amount} at {addr:?}");
println!("With default key, got forwarded reward {amount} from {user_name} of {amount} at {addr:?}");
beta_tracking
.forwarded_payments
.entry(user_name.to_owned())
Expand All @@ -375,7 +377,6 @@ impl SpendDagDb {
}

warn!("Found a forwarded reward {amount} for an unknown participant at {addr:?}: {user_name_hash:?}");
println!("Found a forwarded reward {amount} for an unknown participant at {addr:?}: {user_name_hash:?}");
beta_tracking
.forwarded_payments
.entry(format!("unknown participant: {user_name_hash:?}"))
Expand Down
38 changes: 20 additions & 18 deletions sn_auditor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ fn logging_init(

async fn connect_to_network(peers_args: PeersArgs) -> Result<Client> {
let bootstrap_peers = peers_args.get_peers().await?;
println!(
info!(
"Connecting to the network with {} bootstrap peers",
bootstrap_peers.len(),
);
Expand All @@ -153,7 +153,7 @@ async fn connect_to_network(peers_args: PeersArgs) -> Result<Client> {
.await
.map_err(|err| eyre!("Failed to connect to the network: {err}"))?;

println!("Connected to the network");
info!("Connected to the network");
Ok(client)
}

Expand All @@ -168,10 +168,10 @@ fn initialize_background_rewards_backup(dag: SpendDagDb) {
BETA_REWARDS_BACKUP_INTERVAL_SECS,
))
.await;
println!("Backing up beta rewards...");
info!("Backing up beta rewards...");

if let Err(e) = dag.backup_rewards().await {
eprintln!("Failed to backup beta rewards: {e}");
error!("Failed to backup beta rewards: {e}");
}
}
});
Expand All @@ -187,14 +187,17 @@ async fn initialize_background_spend_dag_collection(
beta_participants: BTreeSet<String>,
foundation_sk: Option<SecretKey>,
) -> Result<SpendDagDb> {
println!("Initialize spend dag...");
info!("Initialize spend dag...");
let path = get_auditor_data_dir_path()?;
if !path.exists() {
std::fs::create_dir_all(&path)?;
}

// clean the local spend DAG if requested
if clean {
println!("Cleaning local spend DAG...");
info!("Cleaning local spend DAG...");
let dag_file = path.join(dag_db::SPEND_DAG_FILENAME);
let _ = std::fs::remove_file(dag_file).map_err(|e| eprintln!("Cleanup interrupted: {e}"));
let _ = std::fs::remove_file(dag_file).map_err(|e| error!("Cleanup interrupted: {e}"));
}

// initialize the DAG
Expand All @@ -205,7 +208,6 @@ async fn initialize_background_spend_dag_collection(
// optional force restart from genesis and merge into our current DAG
// feature guard to prevent a mis-use of opt
if force_from_genesis && cfg!(feature = "dag-collection") {
println!("Forcing DAG to be updated from genesis...");
warn!("Forcing DAG to be updated from genesis...");
let mut d = dag.clone();
let mut genesis_dag = client
Expand All @@ -219,7 +221,7 @@ async fn initialize_background_spend_dag_collection(
let _ = d
.merge(genesis_dag)
.await
.map_err(|e| eprintln!("Failed to merge from genesis DAG into our DAG: {e}"));
.map_err(|e| error!("Failed to merge from genesis DAG into our DAG: {e}"));
});
}

Expand All @@ -233,31 +235,31 @@ async fn initialize_background_spend_dag_collection(
panic!("Foundation SK required to initialize beta rewards program");
};

println!("Initializing beta rewards program tracking...");
info!("Initializing beta rewards program tracking...");
if let Err(e) = dag.track_new_beta_participants(beta_participants).await {
eprintln!("Could not initialize beta rewards: {e}");
error!("Could not initialize beta rewards: {e}");
return Err(e);
}
}

// background thread to update DAG
println!("Starting background DAG collection thread...");
info!("Starting background DAG collection thread...");
let d = dag.clone();
tokio::spawn(async move {
let _ = d
.continuous_background_update()
.await
.map_err(|e| eprintln!("Failed to update DAG in background thread: {e}"));
.map_err(|e| error!("Failed to update DAG in background thread: {e}"));
});

Ok(dag)
}

async fn start_server(dag: SpendDagDb) -> Result<()> {
let server = Server::http("0.0.0.0:4242").expect("Failed to start server");
println!("Starting dag-query server listening on port 4242...");
info!("Starting dag-query server listening on port 4242...");
for request in server.incoming_requests() {
println!(
info!(
"Received request! method: {:?}, url: {:?}",
request.method(),
request.url(),
Expand Down Expand Up @@ -313,7 +315,7 @@ fn load_and_update_beta_participants(
.lines()
.map(|line| line.trim().to_string())
.collect::<Vec<String>>();
println!(
debug!(
"Tracking beta rewards for the {} discord usernames provided in {:?}",
discord_names.len(),
participants_file
Expand All @@ -331,7 +333,7 @@ fn load_and_update_beta_participants(
.lines()
.map(|line| line.trim().to_string())
.collect::<Vec<String>>();
println!(
debug!(
"Restoring beta rewards for the {} discord usernames from {:?}",
discord_names.len(),
local_participants_file
Expand All @@ -340,7 +342,7 @@ fn load_and_update_beta_participants(
}
// write the beta participants to disk
let _ = std::fs::write(local_participants_file, beta_participants.join("\n"))
.map_err(|e| eprintln!("Failed to write beta participants to disk: {e}"));
.map_err(|e| error!("Failed to write beta participants to disk: {e}"));

Ok(beta_participants.into_iter().collect())
}

0 comments on commit 8deb188

Please sign in to comment.