Skip to content

Commit

Permalink
perf: further improve fast mode gathering speed
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach committed May 21, 2024
1 parent 22cac19 commit 31a36ac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
10 changes: 5 additions & 5 deletions sn_auditor/src/dag_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use bls::SecretKey;
use color_eyre::eyre::{eyre, Result};
#[cfg(feature = "svg")]
#[cfg(feature = "svg-dag")]
use graphviz_rust::{cmd::Format, exec, parse, printer::PrinterContext};
use serde::{Deserialize, Serialize};
use sn_client::networking::NetworkError;
Expand Down Expand Up @@ -149,7 +149,7 @@ impl SpendDagDb {
}

/// Dump current DAG as svg to disk
#[cfg(feature = "svg")]
#[cfg(feature = "svg-dag")]
pub fn dump_dag_svg(&self) -> Result<()> {
info!("Dumping DAG to svg...");
std::fs::create_dir_all(&self.path)?;
Expand Down Expand Up @@ -191,7 +191,7 @@ impl SpendDagDb {
*w_handle = dag;
std::mem::drop(w_handle);

#[cfg(feature = "svg")]
#[cfg(feature = "svg-dag")]
{
// update and save svg to file in a background thread so we don't block
//
Expand Down Expand Up @@ -337,7 +337,7 @@ pub async fn new_dag_with_genesis_only(client: &Client) -> Result<SpendDag> {
Ok(dag)
}

#[cfg(feature = "svg")]
#[cfg(feature = "svg-dag")]
fn dag_to_svg(dag: &SpendDag) -> Result<Vec<u8>> {
let dot = dag.dump_dot_format();
let graph = parse(&dot).map_err(|err| eyre!("Failed to parse dag from dot: {err}"))?;
Expand All @@ -357,7 +357,7 @@ fn dag_to_svg(dag: &SpendDag) -> Result<Vec<u8>> {
// - marks poisoned spends as red
// - marks UTXOs and unknown ancestors as yellow
// - just pray it works on windows
#[cfg(feature = "svg")]
#[cfg(feature = "svg-dag")]
fn quick_edit_svg(svg: Vec<u8>, dag: &SpendDag) -> Result<Vec<u8>> {
let mut str = String::from_utf8(svg).map_err(|err| eyre!("Failed svg conversion: {err}"))?;

Expand Down
4 changes: 2 additions & 2 deletions sn_auditor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async fn main() -> Result<()> {

if let Some(dag_to_view) = opt.offline_viewer {
let dag = SpendDagDb::offline(dag_to_view)?;
#[cfg(feature = "svg")]
#[cfg(feature = "svg-dag")]
{
dag.dump_dag_svg()?;
}
Expand Down Expand Up @@ -213,7 +213,7 @@ async fn initialize_background_spend_dag_collection(
}

// initialize svg
#[cfg(feature = "svg")]
#[cfg(feature = "svg-dag")]
dag.dump_dag_svg()?;

// initialize beta rewards program tracking
Expand Down
27 changes: 15 additions & 12 deletions sn_cli/src/bin/subcommands/wallet/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,35 @@ async fn step_by_step_spend_dag_gathering(client: &Client, mut dag: SpendDag) ->
Ok(dag)
}

/// Gather the Spend DAG from the Network and store it on disk
/// If a DAG is found on disk, it will continue from it
/// If fast_mode is true, gathers in a silent and fast way
/// else enjoy a step by step slow narrated gathering
async fn gather_spend_dag(client: &Client, root_dir: &Path, fast_mode: bool) -> Result<SpendDag> {
let dag_path = root_dir.join(SPEND_DAG_FILENAME);
let mut inital_dag = match SpendDag::load_from_file(&dag_path) {
Ok(dag) => {
let inital_dag = match SpendDag::load_from_file(&dag_path) {
Ok(mut dag) => {
println!("Found a local spend dag on disk, continuing from it...");
if fast_mode {
client
.spend_dag_continue_from_utxos(&mut dag, None, false)
.await?;
}
dag
}
Err(err) => {
println!("Starting from Genesis as found no local spend dag on disk...");
info!("Starting from Genesis as failed to load spend dag from disk: {err}");
let genesis_addr = SpendAddress::from_unique_pubkey(&GENESIS_SPEND_UNIQUE_KEY);
let stop_after = if fast_mode { None } else { Some(1) };
client
.spend_dag_build_from(genesis_addr, Some(1), true)
.spend_dag_build_from(genesis_addr, stop_after, true)
.await?
}
};

let dag = match fast_mode {
// fast but silent DAG collection
true => {
client
.spend_dag_continue_from_utxos(&mut inital_dag, None, false)
.await?;
inital_dag
}
// slow but step by step narrated DAG collection
true => inital_dag,
false => step_by_step_spend_dag_gathering(client, inital_dag).await?,
};

Expand All @@ -103,7 +106,7 @@ pub async fn audit(
root_dir: &Path,
foundation_sk: Option<SecretKey>,
) -> Result<()> {
let fast_mode = to_dot || royalties;
let fast_mode = to_dot || royalties || foundation_sk.is_some();
let dag = gather_spend_dag(client, root_dir, fast_mode).await?;

if to_dot {
Expand Down
4 changes: 4 additions & 0 deletions sn_cli/src/bin/subcommands/wallet/hot_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ pub enum WalletCmds {
/// Audit the Currency
/// Note that this might take a very long time
/// Analogous to verifying the entire blockchain in Bitcoin
///
/// When run without any flags, runs in verbose mode,
/// a slower but more informative mode where DAG collection progress is diplayed
Audit {
/// EXPERIMENTAL Dump Audit DAG in dot format on stdout
#[clap(long, default_value = "false")]
Expand All @@ -121,6 +124,7 @@ pub enum WalletCmds {
#[clap(long, default_value = "false")]
royalties: bool,
/// Hex string of the Foundation SK.
/// Providing this key allow displaying rewards statistics gathered from the DAG.
#[clap(long, name = "sk_str")]
sk_str: Option<String>,
},
Expand Down

0 comments on commit 31a36ac

Please sign in to comment.