From 97607a417d1c9f2e17170bb32221582aaababc6d Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Tue, 21 May 2024 17:23:27 +0530 Subject: [PATCH] fix(auditor): extend the beta particpants list --- sn_auditor/src/dag_db.rs | 22 +++++++++++++++------- sn_auditor/src/routes.rs | 38 ++++++++++++++++++-------------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/sn_auditor/src/dag_db.rs b/sn_auditor/src/dag_db.rs index 4cd0334ebb..aa46799063 100644 --- a/sn_auditor/src/dag_db.rs +++ b/sn_auditor/src/dag_db.rs @@ -270,9 +270,11 @@ impl SpendDagDb { .beta_participants .write() .map_err(|e| eyre!("Failed to get beta participants write lock: {e}"))?; - for p in participants.iter() { - beta_participants.insert(Hash::hash(p.as_bytes()), p.clone()); - } + beta_participants.extend( + participants + .iter() + .map(|p| (Hash::hash(p.as_bytes()), p.clone())), + ); } // initialize forwarded payments { @@ -280,14 +282,20 @@ impl SpendDagDb { .forwarded_payments .write() .map_err(|e| eyre!("Failed to get forwarded payments write lock: {e}"))?; - *fwd_payments = participants - .into_iter() - .map(|n| (n, BTreeSet::new())) - .collect(); + fwd_payments.extend(participants.into_iter().map(|p| (p, BTreeSet::new()))); } Ok(()) } + /// Check if a participant is being tracked + pub(crate) fn is_participant_tracked(&self, discord_id: &str) -> Result { + let beta_participants = self + .beta_participants + .read() + .map_err(|e| eyre!("Failed to get beta participants read lock: {e}"))?; + Ok(beta_participants.contains_key(&Hash::hash(discord_id.as_bytes()))) + } + /// Initialize reward forward tracking, gathers current rewards from the DAG pub(crate) async fn init_reward_forward_tracking( &self, diff --git a/sn_auditor/src/routes.rs b/sn_auditor/src/routes.rs index edc9892246..f64d5ddd36 100644 --- a/sn_auditor/src/routes.rs +++ b/sn_auditor/src/routes.rs @@ -12,7 +12,6 @@ use sn_client::transfers::SpendAddress; use std::{ fs::{File, OpenOptions}, io::{Cursor, Write}, - path::PathBuf, str::FromStr, }; use tiny_http::{Request, Response}; @@ -93,20 +92,9 @@ pub(crate) fn add_participant( ); } - if let Err(err) = dag.track_new_beta_participants(vec![discord_id.to_owned()]) { + if let Err(err) = track_new_participant(dag, discord_id.to_owned()) { return Ok( - Response::from_string(format!("Failed to add participant: {err}")) - .with_status_code(400), - ); - } - - // append the new participant to the local file - let local_participants_file = dag.path.join(dag_db::BETA_PARTICIPANTS_FILENAME); - if let Err(err) = - write_discord_id_to_local_file(&local_participants_file, discord_id.to_owned()) - { - return Ok( - Response::from_string(format!("Failed to cache participant to file: {err}")) + Response::from_string(format!("Failed to track new participant: {err}")) .with_status_code(400), ); } @@ -114,16 +102,26 @@ pub(crate) fn add_participant( Ok(Response::from_string("Successfully added participant ")) } -fn write_discord_id_to_local_file(path: &PathBuf, id: String) -> Result<()> { - if path.exists() { +fn track_new_participant(dag: &SpendDagDb, discord_id: String) -> Result<()> { + dag.track_new_beta_participants(vec![discord_id.to_owned()])?; + + // only append new ids + if dag.is_participant_tracked(&discord_id)? { + return Ok(()); + } + + let local_participants_file = dag.path.join(dag_db::BETA_PARTICIPANTS_FILENAME); + + if local_participants_file.exists() { let mut file = OpenOptions::new() .append(true) - .open(path) + .open(local_participants_file) .map_err(|e| eyre!("Failed to open file: {e}"))?; - writeln!(file, "{id}")?; + writeln!(file, "{discord_id}")?; } else { - let mut file = File::create(path).map_err(|e| eyre!("Failed to create file: {e}"))?; - writeln!(file, "{id}")?; + let mut file = File::create(local_participants_file) + .map_err(|e| eyre!("Failed to create file: {e}"))?; + writeln!(file, "{discord_id}")?; } Ok(())