Sfoglia il codice sorgente

Handle more errors

Vecna 5 mesi fa
parent
commit
355dfc9226
2 ha cambiato i file con 80 aggiunte e 39 eliminazioni
  1. 73 31
      src/bin/simulation.rs
  2. 7 8
      src/simulation/user.rs

+ 73 - 31
src/bin/simulation.rs

@@ -134,15 +134,20 @@ pub async fn main() {
         // Level trusted users up to level 4
 
         // Advance LA's time
-        la_net_test
+        let result = la_net_test
             .request(
                 "/advancedays".to_string(),
                 serde_json::to_string(&(UNTRUSTED_INTERVAL as u16))
                     .unwrap()
                     .into(),
             )
-            .await
-            .unwrap();
+            .await;
+        if result.is_ok() {
+            result.unwrap();
+        } else {
+            eprintln!("Failed to advance time for LA");
+        }
+
         // Advance simulated time
         set_simulated_date(get_date() + UNTRUSTED_INTERVAL);
 
@@ -170,15 +175,20 @@ pub async fn main() {
 
         for i in 1..LEVEL_INTERVAL.len() - 2 {
             // Advance LA's time
-            la_net_test
+            let result = la_net_test
                 .request(
                     "/advancedays".to_string(),
                     serde_json::to_string(&(LEVEL_INTERVAL[i] as u16))
                         .unwrap()
                         .into(),
                 )
-                .await
-                .unwrap();
+                .await;
+            if result.is_ok() {
+                result.unwrap();
+            } else {
+                eprintln!("Failed to advance time for LA");
+            }
+
             // Advance simulated time
             set_simulated_date(get_date() + LEVEL_INTERVAL[i]);
 
@@ -186,16 +196,20 @@ pub async fn main() {
                 let reachcred_res = get_bucket(&sconfig.la_net, &user.primary_cred).await;
                 if reachcred_res.is_ok() {
                     let reachcred = reachcred_res.unwrap().1;
-                    let new_cred = level_up(
-                        &sconfig.la_net,
-                        &user.primary_cred,
-                        &reachcred.unwrap(),
-                        get_lox_pub(&sconfig.la_pubkeys),
-                        get_reachability_pub(&sconfig.la_pubkeys),
-                    )
-                    .await;
-                    if new_cred.is_ok() {
-                        user.primary_cred = new_cred.unwrap();
+                    if reachcred.is_some() {
+                        let new_cred = level_up(
+                            &sconfig.la_net,
+                            &user.primary_cred,
+                            &reachcred.unwrap(),
+                            get_lox_pub(&sconfig.la_pubkeys),
+                            get_reachability_pub(&sconfig.la_pubkeys),
+                        )
+                        .await;
+                        if new_cred.is_ok() {
+                            user.primary_cred = new_cred.unwrap();
+                        } else {
+                            eprintln!("Failed to level up trusted user's credential at start");
+                        }
                     } else {
                         eprintln!("Failed to level up trusted user's credential at start");
                     }
@@ -204,13 +218,17 @@ pub async fn main() {
         }
 
         // Advance LA's time to tomorrow
-        la_net_test
+        let result = la_net_test
             .request(
                 "/advancedays".to_string(),
                 serde_json::to_string(&(1 as u16)).unwrap().into(),
             )
-            .await
-            .unwrap();
+            .await;
+        if result.is_ok() {
+            result.unwrap();
+        } else {
+            eprintln!("Failed to advance time for LA");
+        }
 
         // Advance simulated time to tomorrow
         increment_simulated_date();
@@ -243,6 +261,11 @@ pub async fn main() {
             "    The censor has learned {} bridges",
             censor.known_bridges.len()
         );
+        println!("    Accuracy thus far:");
+        println!("        True Positives: {}", true_pos);
+        println!("        True Negatives: {}", true_neg);
+        println!("        False Positives: {}", false_pos);
+        println!("        False Negatives: {}", false_neg);
 
         if let Some(usage) = memory_stats() {
             if usage.physical_mem > max_physical_mem {
@@ -364,21 +387,36 @@ pub async fn main() {
         }
 
         // Publish all the bridges' extra-infos for today
-        extra_infos_net
+        let result = extra_infos_net
             .request(
                 "/add".to_string(),
                 serde_json::to_string(&new_extra_infos).unwrap().into(),
             )
-            .await
-            .unwrap();
+            .await;
+        if result.is_ok() {
+            result.unwrap();
+        } else {
+            eprintln!("Failed to publish new extra-infos");
+        }
 
         // TROLL PATROL TASKS
-        let new_blockages_resp = tp_net_test
-            .request("/update".to_string(), vec![])
-            .await
-            .unwrap();
-        let new_blockages: HashMap<String, HashSet<String>> =
-            serde_json::from_slice(&new_blockages_resp).unwrap();
+        let new_blockages_resp = tp_net_test.request("/update".to_string(), vec![]).await;
+        let new_blockages = match new_blockages_resp {
+            Ok(resp) => match serde_json::from_slice(&resp) {
+                Ok(new_blockages) => new_blockages,
+                Err(e) => {
+                    eprintln!("Failed to deserialize new blockages, error: {}", e);
+                    HashMap::<String, HashSet<String>>::new()
+                }
+            },
+            Err(e) => {
+                eprintln!(
+                    "Failed to get new blockages from Troll Patrol, error: {}",
+                    e
+                );
+                HashMap::<String, HashSet<String>>::new()
+            }
+        };
 
         // Since we have only one censor, just convert to a set of bridges
         let mut blocked_bridges = HashSet::<[u8; 20]>::new();
@@ -417,13 +455,17 @@ pub async fn main() {
         // LOX AUTHORITY TASKS
 
         // Advance LA's time to tomorrow
-        la_net_test
+        let result = la_net_test
             .request(
                 "/advancedays".to_string(),
                 serde_json::to_string(&(1 as u16)).unwrap().into(),
             )
-            .await
-            .unwrap();
+            .await;
+        if result.is_ok() {
+            result.unwrap();
+        } else {
+            eprintln!("Failed to advance time for LA");
+        }
 
         // SIMULATION TASKS
 

+ 7 - 8
src/simulation/user.rs

@@ -210,22 +210,21 @@ impl User {
         reports: Vec<NegativeReport>,
     ) -> Result<()> {
         let date = get_date();
-        let pubkey = serde_json::from_slice::<Option<PublicKey>>(
+        let pubkey = match serde_json::from_slice::<Option<PublicKey>>(
             &config
                 .tp_net
-                .request(
-                    "/nrkey".to_string(),
-                    serde_json::to_string(&date).unwrap().into(),
-                )
+                .request("/nrkey".to_string(), serde_json::to_string(&date)?.into())
                 .await?,
-        )?
-        .unwrap();
+        )? {
+            Some(v) => v,
+            None => return Err(anyhow!("No available negative report encryption key")),
+        };
         for report in reports {
             config
                 .tp_net
                 .request(
                     "/negativereport".to_string(),
-                    bincode::serialize(&report.encrypt(&pubkey)).unwrap(),
+                    bincode::serialize(&report.encrypt(&pubkey))?,
                 )
                 .await?;
         }