From d9a51c7f33ef662032b2c82fdb405ae72e5d4c40 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Mon, 28 Aug 2017 13:31:38 +0200
Subject: [PATCH] [fix] #1091 The fork window was not accurate enough

---
 app/lib/blockchain/DuniterBlockchain.ts | 8 ++++----
 app/lib/computation/QuickSync.ts        | 2 +-
 test/dal/triming.js                     | 1 +
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts
index af3cbbc68..0b446a737 100644
--- a/app/lib/blockchain/DuniterBlockchain.ts
+++ b/app/lib/blockchain/DuniterBlockchain.ts
@@ -213,15 +213,15 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
 
     const TAIL = await dal.bindexDAL.tail();
     const bindexSize = [
-      block.issuersCount,
-      block.issuersFrame,
+      TAIL.issuersCount,
+      TAIL.issuersFrame,
       conf.medianTimeBlocks,
       conf.dtDiffEval
     ].reduce((max, value) => {
       return Math.max(max, value);
     }, 0);
-    const MAX_BINDEX_SIZE = 2 * bindexSize;
-    const currentSize = indexes.HEAD.number - TAIL.number + 1;
+    const MAX_BINDEX_SIZE = conf.forksize + bindexSize
+    const currentSize = indexes.HEAD.number - TAIL.number + 1
     if (currentSize > MAX_BINDEX_SIZE) {
       await dal.trimIndexes(indexes.HEAD.number - MAX_BINDEX_SIZE);
     }
diff --git a/app/lib/computation/QuickSync.ts b/app/lib/computation/QuickSync.ts
index 0bf46bd7b..1979c7cb1 100644
--- a/app/lib/computation/QuickSync.ts
+++ b/app/lib/computation/QuickSync.ts
@@ -203,7 +203,7 @@ export class QuickSynchronizer {
         }
 
         // Trim the bindex
-        sync_bindexSize = [
+        sync_bindexSize = this.conf.forksize + [
           block.issuersCount,
           block.issuersFrame,
           this.conf.medianTimeBlocks,
diff --git a/test/dal/triming.js b/test/dal/triming.js
index 114decf96..db2c0cb84 100644
--- a/test/dal/triming.js
+++ b/test/dal/triming.js
@@ -128,6 +128,7 @@ describe("Triming", function(){
         pub: 'HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd',
         sec: '51w4fEShBk1jCMauWu4mLpmDVfHksKmWcygpxriqCEZizbtERA6de4STKRkQBpxmMUwsKXRjSzuQ8ECwmqN1u2DP'
       },
+      forksize: 9,
       sigQty: 1,
       dtDiffEval: 2,
       medianTimeBlocks: 3
-- 
GitLab