From af6eaaead5a901e1372f391a9828a52d73adfd64 Mon Sep 17 00:00:00 2001
From: librelois <elois@ifee.fr>
Date: Mon, 30 Sep 2019 22:00:47 +0200
Subject: [PATCH] [fix] blockchain: remove fork_blocks only if not already
 removed

---
 lib/modules/blockchain/bc-db-writer/src/blocks.rs          | 7 ++++++-
 .../blockchain/bc-db-writer/src/blocks/fork_tree.rs        | 7 ++++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/lib/modules/blockchain/bc-db-writer/src/blocks.rs b/lib/modules/blockchain/bc-db-writer/src/blocks.rs
index 0cb83ff0..727e05c7 100644
--- a/lib/modules/blockchain/bc-db-writer/src/blocks.rs
+++ b/lib/modules/blockchain/bc-db-writer/src/blocks.rs
@@ -78,7 +78,12 @@ pub fn insert_new_head_block(
         // Remove too old blocks
         for blockstamp in removed_blockstamps {
             let blockstamp_bytes: Vec<u8> = blockstamp.into();
-            fork_blocks_store.delete(w.as_mut(), &blockstamp_bytes)?;
+            if fork_blocks_store
+                .get(w.as_ref(), &blockstamp_bytes)?
+                .is_some()
+            {
+                fork_blocks_store.delete(w.as_mut(), &blockstamp_bytes)?;
+            }
         }
     }
     Ok(())
diff --git a/lib/modules/blockchain/bc-db-writer/src/blocks/fork_tree.rs b/lib/modules/blockchain/bc-db-writer/src/blocks/fork_tree.rs
index 019eefa3..174eb822 100644
--- a/lib/modules/blockchain/bc-db-writer/src/blocks/fork_tree.rs
+++ b/lib/modules/blockchain/bc-db-writer/src/blocks/fork_tree.rs
@@ -87,7 +87,12 @@ pub fn change_main_branch(
     let fork_blocks_store = db.get_store(FORK_BLOCKS);
     for blockstamp in removed_blockstamps {
         let blockstamp_bytes: Vec<u8> = blockstamp.into();
-        fork_blocks_store.delete(w.as_mut(), &blockstamp_bytes)?;
+        if fork_blocks_store
+            .get(w.as_ref(), &blockstamp_bytes)?
+            .is_some()
+        {
+            fork_blocks_store.delete(w.as_mut(), &blockstamp_bytes)?;
+        }
     }
     Ok(())
 }
-- 
GitLab