Skip to content

Commit

Permalink
perf: improve DAG verification redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach authored and joshuef committed May 16, 2024
1 parent ca75e52 commit bba9a50
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
4 changes: 3 additions & 1 deletion sn_cli/src/bin/subcommands/wallet/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ async fn gather_spend_dag(client: &Client, root_dir: &Path) -> Result<SpendDag>
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_CASHNOTE.unique_pubkey());
client.spend_dag_build_from(genesis_addr, None).await?
client
.spend_dag_build_from(genesis_addr, None, true)
.await?
}
};

Expand Down
33 changes: 18 additions & 15 deletions sn_client/src/audit/spend_dag_building.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ use std::collections::BTreeSet;
impl Client {
/// Builds a SpendDag from a given SpendAddress recursively following descendants all the way to UTxOs
/// Started from Genesis this gives the entire SpendDag of the Network at a certain point in time
/// Once the DAG collected, verifies and records errors in the DAG
/// Once the DAG collected, optionally verifies and records errors in the DAG
pub async fn spend_dag_build_from(
&self,
spend_addr: SpendAddress,
max_depth: Option<u32>,
verify: bool,
) -> WalletResult<SpendDag> {
info!("Building spend DAG from {spend_addr:?}");
let mut dag = SpendDag::new(spend_addr);
Expand Down Expand Up @@ -120,17 +121,19 @@ impl Client {
info!("Finished building SpendDAG from {spend_addr:?} in {elapsed:?}");

// verify the DAG
info!("Now verifying SpendDAG from {spend_addr:?} and recording errors...");
let start = std::time::Instant::now();
if let Err(e) = dag.record_faults(&dag.source()) {
let s = format!(
"Collected DAG starting at {spend_addr:?} is invalid, this is probably a bug: {e}"
);
error!("{s}");
return Err(WalletError::Dag(s));
if verify {
info!("Now verifying SpendDAG from {spend_addr:?} and recording errors...");
let start = std::time::Instant::now();
if let Err(e) = dag.record_faults(&dag.source()) {
let s = format!(
"Collected DAG starting at {spend_addr:?} is invalid, this is probably a bug: {e}"
);
error!("{s}");
return Err(WalletError::Dag(s));
}
let elapsed = start.elapsed();
info!("Finished verifying SpendDAG from {spend_addr:?} in {elapsed:?}");
}
let elapsed = start.elapsed();
info!("Finished verifying SpendDAG from {spend_addr:?} in {elapsed:?}");
Ok(dag)
}

Expand Down Expand Up @@ -271,7 +274,10 @@ impl Client {
let mut stream = futures::stream::iter(utxos.into_iter())
.map(|utxo| async move {
debug!("Queuing task to gather DAG from utxo: {:?}", utxo);
(self.spend_dag_build_from(utxo, max_depth).await, utxo)
(
self.spend_dag_build_from(utxo, max_depth, false).await,
utxo,
)
})
.buffer_unordered(crate::MAX_CONCURRENT_TASKS);

Expand All @@ -287,9 +293,6 @@ impl Client {
};
}

dag.record_faults(&dag.source())
.map_err(|e| WalletError::Dag(e.to_string()))?;

info!("Done gathering spend DAG from utxos");
Ok(())
}
Expand Down

0 comments on commit bba9a50

Please sign in to comment.