From 8f72a8ebe127fefad1ea3344ec644c9bbb82ee77 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Sun, 27 Aug 2017 21:18:05 +0200
Subject: [PATCH] [fix] #1091 The fork window must be taken into account during
 `sync`

---
 app/lib/computation/BlockchainContext.ts | 2 +-
 app/lib/computation/QuickSync.ts         | 7 +++++--
 app/service/BlockchainService.ts         | 2 +-
 3 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/app/lib/computation/BlockchainContext.ts b/app/lib/computation/BlockchainContext.ts
index 35be72791..426ed2dc8 100644
--- a/app/lib/computation/BlockchainContext.ts
+++ b/app/lib/computation/BlockchainContext.ts
@@ -157,7 +157,7 @@ export class BlockchainContext {
     }
   }
 
-  quickApplyBlocks(blocks:BlockDTO[], to: number | null): Promise<any> {
+  quickApplyBlocks(blocks:BlockDTO[], to: number): Promise<any> {
     return this.quickSynchronizer.quickApplyBlocks(blocks, to)
   }
 }
diff --git a/app/lib/computation/QuickSync.ts b/app/lib/computation/QuickSync.ts
index 1047aeda6..0bf46bd7b 100644
--- a/app/lib/computation/QuickSync.ts
+++ b/app/lib/computation/QuickSync.ts
@@ -88,7 +88,7 @@ export class QuickSynchronizer {
     return this.dal.updateTransactions(txs);
   }
 
-  async quickApplyBlocks(blocks:BlockDTO[], to: number | null): Promise<void> {
+  async quickApplyBlocks(blocks:BlockDTO[], to: number): Promise<void> {
 
     sync_memoryDAL.sindexDAL = { getAvailableForConditions: (conditions:string) => this.dal.sindexDAL.getAvailableForConditions(conditions) }
     let blocksToSave: BlockDTO[] = [];
@@ -103,7 +103,7 @@ export class QuickSynchronizer {
         sync_currConf = BlockDTO.getConf(block);
       }
 
-      if (block.number != to) {
+      if (block.number <= to - this.conf.forksize) {
         blocksToSave.push(dto);
         const index:any = Indexer.localIndex(dto, sync_currConf);
         const local_iindex = Indexer.iindex(index);
@@ -239,6 +239,9 @@ export class QuickSynchronizer {
         const nonEmptyKeys = _.filter(conditions, (k: any) => sync_memoryWallets[k] && sync_memoryWallets[k].balance > 0)
         const walletsToRecord = nonEmptyKeys.map((k: any) => sync_memoryWallets[k])
         await this.dal.walletDAL.insertBatch(walletsToRecord)
+        for (const cond of conditions) {
+          delete sync_memoryWallets[cond]
+        }
 
         // Last block: cautious mode to trigger all the INDEX expiry mechanisms
         const { index, HEAD } = await DuniterBlockchain.checkBlock(dto, constants.WITH_SIGNATURES_AND_POW, this.conf, this.dal)
diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts
index dade3d0e1..9631d6fdd 100644
--- a/app/service/BlockchainService.ts
+++ b/app/service/BlockchainService.ts
@@ -399,7 +399,7 @@ export class BlockchainService extends FIFOService {
    * @param blocks An array of blocks to insert.
    * @param to The final block number of the fast insertion.
    */
-  fastBlockInsertions(blocks:BlockDTO[], to:number | null) {
+  fastBlockInsertions(blocks:BlockDTO[], to:number) {
     return this.mainContext.quickApplyBlocks(blocks, to)
   }
 }
-- 
GitLab