Skip to content

Commit

Permalink
fix(auditor): extend the beta particpants list
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin authored and joshuef committed May 22, 2024
1 parent 4380a4e commit 97607a4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
22 changes: 15 additions & 7 deletions sn_auditor/src/dag_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,24 +270,32 @@ 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
{
let mut fwd_payments = self
.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<bool> {
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,
Expand Down
38 changes: 18 additions & 20 deletions sn_auditor/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -93,37 +92,36 @@ 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),
);
}

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(())
Expand Down

0 comments on commit 97607a4

Please sign in to comment.