From 1fcd227c1f013d9a10a2be56ac92574ca0baf48a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Moreau?= <cem.moreau@gmail.com>
Date: Sat, 2 Jun 2018 17:27:18 +0200
Subject: [PATCH] [fix] Disable the trimming within fork resolution

(cherry picked from commit 7104a89)
---
 app/lib/blockchain/DuniterBlockchain.ts  | 34 +++++++++++++-----------
 app/lib/computation/BlockchainContext.ts |  8 +++---
 app/service/BlockchainService.ts         |  2 +-
 3 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts
index 6d1ca0321..48a3215e8 100644
--- a/app/lib/blockchain/DuniterBlockchain.ts
+++ b/app/lib/blockchain/DuniterBlockchain.ts
@@ -174,13 +174,13 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
     return { index, HEAD }
   }
 
-  async pushTheBlock(obj:BlockDTO, index:IndexEntry[], HEAD:DBHead | null, conf:ConfDTO, dal:any, logger:any) {
+  async pushTheBlock(obj: BlockDTO, index: IndexEntry[], HEAD: DBHead | null, conf: ConfDTO, dal: any, logger: any, trim = true) {
     const start = Date.now();
     const block = BlockDTO.fromJSONObject(obj)
     try {
       const currentBlock = await dal.getCurrentBlockOrNull();
       block.fork = false;
-      const added = await this.saveBlockData(currentBlock, block, conf, dal, logger, index, HEAD);
+      const added = await this.saveBlockData(currentBlock, block, conf, dal, logger, index, HEAD, trim);
 
       try {
         await DuniterBlockchain.pushStatsForBlocks([block], dal);
@@ -201,7 +201,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
     // await supra.recordIndex(index)
   }
 
-  async saveBlockData(current:DBBlock, block:BlockDTO, conf:ConfDTO, dal:any, logger:any, index:IndexEntry[], HEAD:DBHead | null) {
+  async saveBlockData(current: DBBlock, block: BlockDTO, conf: ConfDTO, dal: any, logger: any, index: IndexEntry[], HEAD: DBHead | null, trim: boolean) {
     if (block.number == 0) {
       await this.saveParametersForRoot(block, conf, dal);
     }
@@ -224,19 +224,21 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
     // Update the wallets' blances
     await this.updateWallets(indexes.sindex, dal)
 
-    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 currentSize = indexes.HEAD.number - TAIL.number + 1
-    if (currentSize > MAX_BINDEX_SIZE) {
-      await dal.trimIndexes(indexes.HEAD.number - MAX_BINDEX_SIZE);
+    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 currentSize = indexes.HEAD.number - TAIL.number + 1
+      if (currentSize > MAX_BINDEX_SIZE) {
+        await dal.trimIndexes(indexes.HEAD.number - MAX_BINDEX_SIZE);
+      }
     }
 
     const dbb = DBBlock.fromBlockDTO(block)
diff --git a/app/lib/computation/BlockchainContext.ts b/app/lib/computation/BlockchainContext.ts
index 09b387a01..5a511b3e2 100644
--- a/app/lib/computation/BlockchainContext.ts
+++ b/app/lib/computation/BlockchainContext.ts
@@ -118,8 +118,8 @@ export class BlockchainContext {
     return DuniterBlockchain.checkBlock(block, withPoWAndSignature, this.conf, this.dal)
   }
 
-  private async addBlock(obj: BlockDTO, index: any = null, HEAD: DBHead | null = null): Promise<any> {
-    const block = await this.blockchain.pushTheBlock(obj, index, HEAD, this.conf, this.dal, this.logger)
+  private async addBlock(obj: BlockDTO, index: any = null, HEAD: DBHead | null = null, trim: boolean): Promise<any> {
+    const block = await this.blockchain.pushTheBlock(obj, index, HEAD, this.conf, this.dal, this.logger, trim)
     this.vHEAD_1 = this.vHEAD = this.HEADrefreshed = null
     return block
   }
@@ -150,9 +150,9 @@ export class BlockchainContext {
     this.logger.debug('Applied block #%s', block.number);
   }
 
-  async checkAndAddBlock(block:BlockDTO) {
+  async checkAndAddBlock(block:BlockDTO, trim = true) {
     const { index, HEAD } = await this.checkBlock(block, constants.WITH_SIGNATURES_AND_POW);
-    return await this.addBlock(block, index, HEAD);
+    return await this.addBlock(block, index, HEAD, trim);
   }
 
   current(): Promise<any> {
diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts
index 2d4f583dd..1337f3ee8 100644
--- a/app/service/BlockchainService.ts
+++ b/app/service/BlockchainService.ts
@@ -104,7 +104,7 @@ export class BlockchainService extends FIFOService {
       }
 
       async addBlock(block: BlockDTO): Promise<BlockDTO> {
-        return await this.bcService.mainContext.checkAndAddBlock(block)
+        return await this.bcService.mainContext.checkAndAddBlock(block, false)
       }
 
     })(this)
-- 
GitLab