From c03e36bf5099c17055aeca8ea10c7242794e9f4d Mon Sep 17 00:00:00 2001
From: bgallois <benjamin@gallois.cc>
Date: Wed, 9 Oct 2024 18:02:19 +0200
Subject: [PATCH] fix #255

---
 client/distance/src/lib.rs | 41 ++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 17 deletions(-)

diff --git a/client/distance/src/lib.rs b/client/distance/src/lib.rs
index 1b67e85fe..4f0d6d95f 100644
--- a/client/distance/src/lib.rs
+++ b/client/distance/src/lib.rs
@@ -40,17 +40,22 @@ where
     Backend: sc_client_api::Backend<B>,
     IdtyIndex: Decode + Encode + PartialEq + TypeInfo,
 {
-    let pool_index = client
+    let pool_index = if let Some(index) = client
         .storage(
             parent,
             &StorageKey(
                 frame_support::storage::storage_prefix(b"Distance", b"CurrentPoolIndex").to_vec(),
             ),
         )
-        .expect("CurrentIndex is Err")
-        .map_or(0, |raw| {
-            u32::decode(&mut &raw.0[..]).expect("cannot decode CurrentIndex")
-        });
+        .ok()
+        .flatten()
+        .map_or(None, |raw| u32::decode(&mut &raw.0[..]).ok())
+    {
+        index
+    } else {
+        log::error!("🧙 [distance oracle] CurrentPoolIndex cannot be decoded");
+        return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
+    };
 
     let published_results = client
         .storage(
@@ -67,22 +72,24 @@ where
                 )
                 .to_vec(),
             ),
-        )?
-        .map_or_else(Default::default, |raw| {
-            pallet_distance::EvaluationPool::<AccountId32, IdtyIndex>::decode(&mut &raw.0[..])
-                .expect("cannot decode EvaluationPool")
+        )
+        .ok()
+        .flatten()
+        .map_or(None, |raw| {
+            pallet_distance::EvaluationPool::<AccountId32, IdtyIndex>::decode(&mut &raw.0[..]).ok()
         });
 
     // Have we already published a result for this period?
     // The block author is guaranteed to be in the owner_keys.
-    let owner_keys = owner_keys
-        .iter()
-        .map(|&key| sp_runtime::AccountId32::new(key.0))
-        .any(|key| published_results.evaluators.contains(&key));
-
-    if owner_keys {
-        log::debug!("🧙 [distance oracle] Already published a result for this period");
-        return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
+    if let Some(results) = published_results {
+        if owner_keys
+            .iter()
+            .map(|&key| sp_runtime::AccountId32::new(key.0))
+            .any(|key| results.evaluators.contains(&key))
+        {
+            log::debug!("🧙 [distance oracle] Already published a result for this period");
+            return Ok(sp_distance::InherentDataProvider::<IdtyIndex>::new(None));
+        }
     }
 
     // Read evaluation result from file, if it exists
-- 
GitLab