From 5c6a076289c36b2cef28c9371e782eb6df4fd47f Mon Sep 17 00:00:00 2001 From: Vecna Date: Fri, 12 Apr 2024 12:50:40 -0400 Subject: [PATCH] Begin work on cleaning up old nonces --- src/lib.rs | 56 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 33ec69f..02666ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -321,30 +321,44 @@ pub async fn update_extra_infos( /// We store to-be-processed negative reports as a vector. Add this NR /// to that vector (or create a new vector if necessary) pub fn save_negative_report_to_process(db: &Db, nr: NegativeReport) { - let mut reports = match db.get("nrs-to-process").unwrap() { + // TODO: Purge these database entries sometimes + let mut nonces = match db.get(format!("nonces_{}", &nr.date)).unwrap() { Some(v) => bincode::deserialize(&v).unwrap(), - None => BTreeMap::>::new(), + None => HashSet::<[u8; 32]>::new(), }; - // Store to-be-processed reports with key [fingerprint]_[country]_[date] - let map_key = format!( - "{}_{}_{}", - array_bytes::bytes2hex("", &nr.fingerprint), - &nr.country, - &nr.date, - ); - if reports.contains_key(&map_key) { - reports - .get_mut(&map_key) - .unwrap() - .push(nr.to_serializable_report()); - } else { - let mut nrs = Vec::::new(); - nrs.push(nr.to_serializable_report()); - reports.insert(map_key, nrs); - } - // Commit changes to database - db.insert("nrs-to-process", bincode::serialize(&reports).unwrap()) + + // Just ignore the report if we've seen the nonce before + if nonces.insert(nr.nonce) { + db.insert( + format!("nonces_{}", &nr.date), + bincode::serialize(&nonces).unwrap(), + ) .unwrap(); + let mut reports = match db.get("nrs-to-process").unwrap() { + Some(v) => bincode::deserialize(&v).unwrap(), + None => BTreeMap::>::new(), + }; + // Store to-be-processed reports with key [fingerprint]_[country]_[date] + let map_key = format!( + "{}_{}_{}", + array_bytes::bytes2hex("", &nr.fingerprint), + &nr.country, + &nr.date, + ); + if reports.contains_key(&map_key) { + reports + .get_mut(&map_key) + .unwrap() + .push(nr.to_serializable_report()); + } else { + let mut nrs = Vec::::new(); + nrs.push(nr.to_serializable_report()); + reports.insert(map_key, nrs); + } + // Commit changes to database + db.insert("nrs-to-process", bincode::serialize(&reports).unwrap()) + .unwrap(); + } } /// Sends a collection of negative reports to the Lox Authority and returns the