From 14e1245145fb1ecffb933d2f0bc4ba476f482421 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Wed, 15 Jan 2020 18:15:52 +0100
Subject: [PATCH] [enh] add `trim-indexes` command: manually trims the indexes
 if necessary

---
 app/lib/blockchain/DuniterBlockchain.ts | 17 ++++++++++-------
 app/modules/dump.ts                     | 12 ++++++++++++
 app/service/BlockchainService.ts        |  6 ++++++
 3 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts
index 08015ba1b..1d68dd89f 100644
--- a/app/lib/blockchain/DuniterBlockchain.ts
+++ b/app/lib/blockchain/DuniterBlockchain.ts
@@ -239,13 +239,7 @@ export class DuniterBlockchain {
     await this.updateWallets(indexes.sindex, indexes.dividends, dal)
 
     if (trim) {
-      const TAIL = await dal.bindexDAL.tail();
-      const MAX_BINDEX_SIZE = requiredBindexSizeForTail(TAIL, conf)
-      const currentSize = indexes.HEAD.number - TAIL.number + 1
-      if (currentSize > MAX_BINDEX_SIZE) {
-        await dal.archiveBlocks()
-        await dal.trimIndexes(indexes.HEAD.number - MAX_BINDEX_SIZE);
-      }
+      await DuniterBlockchain.trimIndexes(dal, indexes.HEAD, conf)
     }
 
     const dbb = DBBlock.fromBlockDTO(block)
@@ -573,6 +567,15 @@ export class DuniterBlockchain {
       throw err;
     }
   }
+
+  public static async trimIndexes(dal: FileDAL, HEAD: { number: number }, conf: ConfDTO) {
+    const TAIL = await dal.bindexDAL.tail();
+    const MAX_BINDEX_SIZE = requiredBindexSizeForTail(TAIL, conf)
+    const currentSize = HEAD.number - TAIL.number + 1
+    if (currentSize > MAX_BINDEX_SIZE) {
+      await dal.trimIndexes(HEAD.number - MAX_BINDEX_SIZE);
+    }
+  }
 }
 
 export function requiredBindexSizeForTail(TAIL: { issuersCount: number, issuersFrame: number }, conf: { medianTimeBlocks: number, dtDiffEval: number, forksize: number }) {
diff --git a/app/modules/dump.ts b/app/modules/dump.ts
index 30d336499..d90a6d165 100644
--- a/app/modules/dump.ts
+++ b/app/modules/dump.ts
@@ -76,6 +76,18 @@ module.exports = {
         // Save DB
         await server.disconnect();
       }
+    }, {
+      name: 'trim-indexes',
+      desc: 'Force trimming of indexes',
+      logs: true,
+      preventIfRunning: true,
+
+      onConfiguredExecute: async (server:Server) => {
+        await server.dal.init(server.conf)
+        await server.BlockchainService.trimIndexes()
+        // Save DB
+        await server.disconnect();
+      }
     }, {
       name: 'dump [what] [name] [cond]',
       desc: 'Dumps data of the blockchain.',
diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts
index ccd46bd06..e159bed27 100644
--- a/app/service/BlockchainService.ts
+++ b/app/service/BlockchainService.ts
@@ -459,4 +459,10 @@ export class BlockchainService extends FIFOService {
     return this.dal.getBlocksBetween(from, from + count - 1);
   }
 
+  async trimIndexes() {
+    const HEAD = await this.dal.getCurrentBlockOrNull()
+    if (HEAD) {
+      return DuniterBlockchain.trimIndexes(this.dal, HEAD, this.conf)
+    }
+  }
 }
-- 
GitLab