From 4416946b5d7b57101607345d0a2db5b5495ff784 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Mon, 9 Jul 2018 17:33:42 +0200
Subject: [PATCH] [fix] loki data commiting and trimming should be triggered
 every 30 blocks

---
 app/lib/blockchain/DuniterBlockchain.ts  | 12 +++++++++---
 app/lib/common-libs/constants.ts         |  2 ++
 app/lib/computation/BlockchainContext.ts |  5 -----
 app/lib/constants.ts                     |  1 -
 4 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts
index a12d9cbae..fd683b19e 100644
--- a/app/lib/blockchain/DuniterBlockchain.ts
+++ b/app/lib/blockchain/DuniterBlockchain.ts
@@ -18,8 +18,8 @@ import {
   IndexEntry,
   Indexer,
   MindexEntry,
-  SimpleSindexEntryForWallet, SimpleTxEntryForWallet, SimpleUdEntryForWallet,
-  SindexEntry
+  SimpleTxEntryForWallet,
+  SimpleUdEntryForWallet
 } from "../indexer"
 import {ConfDTO} from "../dto/ConfDTO"
 import {BlockDTO} from "../dto/BlockDTO"
@@ -37,7 +37,6 @@ import {DataErrors} from "../common-libs/errors"
 import {NewLogger} from "../logger"
 import {DBTx} from "../db/DBTx"
 import {Underscore} from "../common-libs/underscore"
-import {DividendEntry, UDSource} from "../dal/indexDAL/abstract/DividendDAO"
 import {OtherConstants} from "../other_constants"
 
 export class DuniterBlockchain {
@@ -205,6 +204,13 @@ export class DuniterBlockchain {
       }
 
       logger.info('Block #' + block.number + ' added to the blockchain in %s ms', (Date.now() - start));
+
+      // Periodically, we trim the blockchain
+      if (block.number % CommonConstants.BLOCKS_COLLECT_THRESHOLD === 0) {
+        // Database trimming
+        await dal.loki.flushAndTrimData()
+      }
+
       return BlockDTO.fromJSONObject(added)
     }
     catch(err) {
diff --git a/app/lib/common-libs/constants.ts b/app/lib/common-libs/constants.ts
index ff000fb76..3b16ff3e8 100755
--- a/app/lib/common-libs/constants.ts
+++ b/app/lib/common-libs/constants.ts
@@ -307,6 +307,8 @@ export const CommonConstants = {
 
   MAX_AGE_OF_PEER_IN_BLOCKS: 200, // blocks
   INITIAL_DOWNLOAD_SLOTS: 1, // 1 peer
+
+  BLOCKS_COLLECT_THRESHOLD: 30, // Number of blocks to wait before trimming the loki data
 }
 
 function exact (regexpContent:string) {
diff --git a/app/lib/computation/BlockchainContext.ts b/app/lib/computation/BlockchainContext.ts
index b99cb96c8..9705c2eed 100644
--- a/app/lib/computation/BlockchainContext.ts
+++ b/app/lib/computation/BlockchainContext.ts
@@ -147,11 +147,6 @@ export class BlockchainContext {
     const block = forks[0];
     await this.checkAndAddBlock(BlockDTO.fromJSONObject(block))
     this.logger.debug('Applied block #%s', block.number);
-    // Periodically, we trim the blockchain
-    if (block.number % 30 === 0) {
-      // Database trimming
-      await this.dal.loki.flushAndTrimData()
-    }
   }
 
   async checkAndAddBlock(block:BlockDTO, trim = true) {
diff --git a/app/lib/constants.ts b/app/lib/constants.ts
index bc605076a..7092d69e5 100644
--- a/app/lib/constants.ts
+++ b/app/lib/constants.ts
@@ -176,7 +176,6 @@ module.exports = {
   NO_FORK_ALLOWED: false,
 
   SAFE_FACTOR: 3,
-  BLOCKS_COLLECT_THRESHOLD: 30, // Blocks to collect from memory and persist
 
   MUTE_LOGS_DURING_UNIT_TESTS: OtherConstants.MUTE_LOGS_DURING_UNIT_TESTS,
 
-- 
GitLab