diff --git a/sn_auditor/src/dag_db.rs b/sn_auditor/src/dag_db.rs index 4a3d90e309..e7da297fce 100644 --- a/sn_auditor/src/dag_db.rs +++ b/sn_auditor/src/dag_db.rs @@ -218,7 +218,7 @@ impl SpendDagDb { } /// Update DAG from Network continuously - pub async fn continuous_background_update(self) -> Result<()> { + pub async fn continuous_background_update(self, storage_dir: PathBuf) -> Result<()> { let client = if let Some(client) = &self.client { client.clone() } else { @@ -242,9 +242,32 @@ impl SpendDagDb { ); tokio::spawn(async move { let mut double_spends = BTreeSet::new(); + let mut detected_spends = BTreeSet::new(); while let Some((spend, utxos_for_further_track, is_double_spend)) = rx.recv().await { + let content_hash = spend.spend.hash(); + + if detected_spends.insert(content_hash) { + let hex_content_hash = content_hash.to_hex(); + let addr_hex = spend.address().to_hex(); + let file_name = format!("{addr_hex}_{hex_content_hash}"); + let spend_copy = spend.clone(); + let file_path = storage_dir.join(&file_name); + + tokio::spawn(async move { + let bytes = spend_copy.to_bytes(); + match std::fs::write(&file_path, bytes) { + Ok(_) => { + info!("Wrote spend {file_name} to disk!"); + } + Err(err) => { + error!("Error writing spend {file_name}, error: {err:?}"); + } + } + }); + } + if is_double_spend { self_clone .beta_background_process_double_spend( diff --git a/sn_auditor/src/main.rs b/sn_auditor/src/main.rs index 6d2421bd69..bf559d1195 100644 --- a/sn_auditor/src/main.rs +++ b/sn_auditor/src/main.rs @@ -101,12 +101,17 @@ async fn main() -> Result<()> { } let client = connect_to_network(opt.peers).await?; + + let storage_dir = get_auditor_data_dir_path()?.join("fetched_spends"); + std::fs::create_dir_all(&storage_dir).expect("fetched_spends path to be successfully created."); + let dag = initialize_background_spend_dag_collection( client.clone(), opt.force_from_genesis, opt.clean, beta_participants, maybe_sk, + storage_dir, ) .await?; @@ -186,6 +191,7 @@ async fn initialize_background_spend_dag_collection( clean: bool, beta_participants: BTreeSet, foundation_sk: Option, + storage_dir: PathBuf, ) -> Result { info!("Initialize spend dag..."); let path = get_auditor_data_dir_path()?; @@ -248,7 +254,7 @@ async fn initialize_background_spend_dag_collection( let d = dag.clone(); tokio::spawn(async move { let _ = d - .continuous_background_update() + .continuous_background_update(storage_dir) .await .map_err(|e| error!("Failed to update DAG in background thread: {e}")); });