From f1a164d83229a32e6734968bd9367f43e4270b8c Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Sun, 3 Feb 2019 16:44:44 +0100
Subject: [PATCH] [fix] #1338 Quick sync UD computation might be incorrect
 sometimes (cherry-picked + adapted from dev)

---
 app/lib/blockchain/DuniterBlockchain.ts | 22 +++++++++++++---------
 app/lib/computation/QuickSync.ts        |  5 +++--
 app/lib/indexer.ts                      | 17 +++++++----------
 3 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts
index 37e0e3016..d0f4e65e2 100644
--- a/app/lib/blockchain/DuniterBlockchain.ts
+++ b/app/lib/blockchain/DuniterBlockchain.ts
@@ -226,15 +226,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
 
     if (trim) {
       const TAIL = await dal.bindexDAL.tail();
-      const bindexSize = [
-        TAIL.issuersCount,
-        TAIL.issuersFrame,
-        conf.medianTimeBlocks,
-        conf.dtDiffEval
-      ].reduce((max, value) => {
-        return Math.max(max, value);
-      }, 0);
-      const MAX_BINDEX_SIZE = conf.forksize + bindexSize
+      const MAX_BINDEX_SIZE = requiredBindexSizeForTail(TAIL, conf)
       const currentSize = indexes.HEAD.number - TAIL.number + 1
       if (currentSize > MAX_BINDEX_SIZE) {
         await dal.trimIndexes(indexes.HEAD.number - MAX_BINDEX_SIZE);
@@ -554,3 +546,15 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
     await indexRevert(head.number)
   }
 }
+
+export function requiredBindexSizeForTail(TAIL: { issuersCount: number, issuersFrame: number }, conf: { medianTimeBlocks: number, dtDiffEval: number, forksize: number }) {
+  const bindexSize = [
+    TAIL.issuersCount,
+    TAIL.issuersFrame,
+    conf.medianTimeBlocks,
+    conf.dtDiffEval
+  ].reduce((max, value) => {
+    return Math.max(max, value);
+  }, 0);
+  return conf.forksize + bindexSize
+}
diff --git a/app/lib/computation/QuickSync.ts b/app/lib/computation/QuickSync.ts
index 66fdf0bf6..ddf6bf8f3 100644
--- a/app/lib/computation/QuickSync.ts
+++ b/app/lib/computation/QuickSync.ts
@@ -12,7 +12,7 @@
 // GNU Affero General Public License for more details.
 
 "use strict"
-import {DuniterBlockchain} from "../blockchain/DuniterBlockchain";
+import {DuniterBlockchain, requiredBindexSizeForTail} from "../blockchain/DuniterBlockchain"
 import {BlockDTO} from "../dto/BlockDTO";
 import {DBTransaction} from "../db/DBTransaction";
 import {Indexer} from "../indexer";
@@ -121,7 +121,8 @@ export class QuickSynchronizer {
         sync_currConf = BlockDTO.getConf(block);
       }
 
-      if (block.number <= to - this.conf.forksize) {
+      const bindexSize = requiredBindexSizeForTail(block, this.conf)
+      if (block.number <= to - bindexSize - 1) {
         blocksToSave.push(dto);
         const index:any = Indexer.localIndex(dto, sync_currConf);
         const local_iindex = Indexer.iindex(index);
diff --git a/app/lib/indexer.ts b/app/lib/indexer.ts
index c5e2b7cf8..bbb156f81 100644
--- a/app/lib/indexer.ts
+++ b/app/lib/indexer.ts
@@ -483,15 +483,6 @@ export class Indexer {
 
     const HEAD_1 = await head(1);
 
-    if (HEAD.number == 0) {
-      HEAD.dividend = conf.ud0;
-    }
-    else if (!HEAD.dividend) {
-      HEAD.dividend = HEAD_1.dividend;
-    } else {
-      HEAD.new_dividend = HEAD.dividend;
-    }
-
     // BR_G04
     await Indexer.prepareIssuersCount(HEAD, range, HEAD_1);
 
@@ -510,6 +501,12 @@ export class Indexer {
     // BR_G11
     Indexer.prepareUDTime(HEAD, HEAD_1, conf)
 
+    // BR_G13
+    Indexer.prepareDividend(HEAD, HEAD_1, conf)
+
+    // BR_G14
+    Indexer.prepareUnitBase(HEAD);
+
     // BR_G15
     Indexer.prepareMass(HEAD, HEAD_1);
 
@@ -1032,7 +1029,7 @@ export class Indexer {
   }
 
   // BR_G13
-  static prepareDividend(HEAD: DBHead, HEAD_1: DBHead, conf: ConfDTO) {
+  static prepareDividend(HEAD: DBHead, HEAD_1: DBHead, conf: CurrencyConfDTO) {
     // UD re-evaluation
     if (HEAD.number == 0) {
       HEAD.dividend = conf.ud0;
-- 
GitLab