Procházet zdrojové kódy

Begin work on cleaning up old nonces

Vecna před 1 rokem
rodič
revize
5c6a076289
1 změnil soubory, kde provedl 35 přidání a 21 odebrání
  1. 35 21
      src/lib.rs

+ 35 - 21
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::<String, Vec<SerializableNegativeReport>>::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::<SerializableNegativeReport>::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::<String, Vec<SerializableNegativeReport>>::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::<SerializableNegativeReport>::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