Skip to content

Commit

Permalink
feat(auditor): dump fetched spends to local disk
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi authored and joshuef committed Jul 17, 2024
1 parent 2f4568e commit 7a16c14
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
29 changes: 26 additions & 3 deletions sn_auditor/src/dag_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down Expand Up @@ -423,7 +446,7 @@ impl SpendDagDb {
spend.spend.reason
);
eprintln!(
"Incorreect royalty spend has {} royalties, {:?} - {:?}",
"Incorrect royalty spend has {} royalties, {:?} - {:?}",
spend.spend.network_royalties.len(),
spend.spend.spent_tx.inputs,
spend.spend.spent_tx.outputs
Expand All @@ -436,7 +459,7 @@ impl SpendDagDb {
spend.spend.reason
);
warn!(
"Incorreect royalty spend has {} royalties, {:?} - {:?}",
"Incorrect royalty spend has {} royalties, {:?} - {:?}",
spend.spend.network_royalties.len(),
spend.spend.spent_tx.inputs,
spend.spend.spent_tx.outputs
Expand Down
8 changes: 7 additions & 1 deletion sn_auditor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down Expand Up @@ -186,6 +191,7 @@ async fn initialize_background_spend_dag_collection(
clean: bool,
beta_participants: BTreeSet<String>,
foundation_sk: Option<SecretKey>,
storage_dir: PathBuf,
) -> Result<SpendDagDb> {
info!("Initialize spend dag...");
let path = get_auditor_data_dir_path()?;
Expand Down Expand Up @@ -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}"));
});
Expand Down

0 comments on commit 7a16c14

Please sign in to comment.