From b696d3084020da6ea62f242a6370fc2a9e58b789 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:28:16 +0200
Subject: [PATCH] [fix] Protocol: the UD must be produced when a new member
 joins on UD time

(cherry picked from commit 8c306c5)
---
 app/lib/computation/QuickSync.ts | 32 +++++++++++++++++---------------
 app/lib/dal/fileDAL.ts           |  3 +--
 app/lib/indexer.ts               |  4 ++--
 doc/Protocol.md                  | 15 +++++++++++++++
 4 files changed, 35 insertions(+), 19 deletions(-)

diff --git a/app/lib/computation/QuickSync.ts b/app/lib/computation/QuickSync.ts
index 5d8853884..36fdbb26e 100644
--- a/app/lib/computation/QuickSync.ts
+++ b/app/lib/computation/QuickSync.ts
@@ -123,11 +123,8 @@ export class QuickSynchronizer {
         const local_cindex = Indexer.cindex(index);
         const local_sindex = Indexer.sindex(index);
         const local_mindex = Indexer.mindex(index);
-        sync_iindex = sync_iindex.concat(local_iindex);
-        sync_cindex = sync_cindex.concat(local_cindex);
-        sync_mindex = sync_mindex.concat(local_mindex);
 
-        const HEAD = await Indexer.quickCompleteGlobalScope(block, sync_currConf, sync_bindex, sync_iindex, sync_mindex, sync_cindex, {
+        const HEAD = await Indexer.quickCompleteGlobalScope(block, sync_currConf, sync_bindex, local_iindex, local_mindex, local_cindex, {
           getBlock: (number: number) => {
             return Promise.resolve(sync_allBlocks[number]);
           },
@@ -184,12 +181,12 @@ export class QuickSynchronizer {
           await this.dal.iindexDAL.insertBatch(sync_iindex);
           await this.dal.sindexDAL.insertBatch(sync_sindex);
           await this.dal.cindexDAL.insertBatch(sync_cindex);
-          sync_mindex = [];
-          sync_iindex = [];
-          sync_cindex = [];
-          sync_sindex = local_sindex;
+          sync_iindex = local_iindex
+          sync_cindex = local_cindex
+          sync_mindex = local_mindex
+          sync_sindex = local_sindex
 
-          sync_sindex = sync_sindex.concat(await Indexer.ruleIndexGenDividend(HEAD, this.dal));
+          sync_sindex = sync_sindex.concat(await Indexer.ruleIndexGenDividend(HEAD, local_iindex, this.dal));
           sync_sindex = sync_sindex.concat(await Indexer.ruleIndexGarbageSmallAccounts(HEAD, sync_sindex, sync_memoryDAL));
           if (nextExpiringChanged) {
             sync_cindex = sync_cindex.concat(await Indexer.ruleIndexGenCertificationExpiry(HEAD, this.dal));
@@ -201,21 +198,26 @@ export class QuickSynchronizer {
           // Update balances with UD + local garbagings
           await this.blockchain.updateWallets(sync_sindex, sync_memoryDAL)
 
-          // --> Update links
-          await this.dal.updateWotbLinks(local_cindex.concat(sync_cindex));
-
-          // Flush the INDEX again
-          await this.dal.mindexDAL.insertBatch(sync_mindex);
+          // Flush the INDEX again (needs to be done *before* the update of wotb links because of block#0)
           await this.dal.iindexDAL.insertBatch(sync_iindex);
+          await this.dal.mindexDAL.insertBatch(sync_mindex);
           await this.dal.sindexDAL.insertBatch(sync_sindex);
           await this.dal.cindexDAL.insertBatch(sync_cindex);
-          sync_mindex = [];
+
+          // --> Update links
+          await this.dal.updateWotbLinks(local_cindex.concat(sync_cindex));
           sync_iindex = [];
+          sync_mindex = [];
           sync_cindex = [];
           sync_sindex = [];
 
           // Create/Update nodes in wotb
           await this.blockchain.updateMembers(block, this.dal)
+        } else {
+          // Concat the results to the pending data
+          sync_iindex = sync_iindex.concat(local_iindex);
+          sync_cindex = sync_cindex.concat(local_cindex);
+          sync_mindex = sync_mindex.concat(local_mindex);
         }
 
         // Trim the bindex
diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts
index 2cbd5d102..4e441a89b 100644
--- a/app/lib/dal/fileDAL.ts
+++ b/app/lib/dal/fileDAL.ts
@@ -28,7 +28,6 @@ import {DBBlock} from "../db/DBBlock"
 import {DBMembership} from "./sqliteDAL/MembershipDAL"
 import {MerkleDTO} from "../dto/MerkleDTO"
 import {CommonConstants} from "../common-libs/constants"
-import { ProxiesConf } from '../proxy';
 import {PowDAL} from "./fileDALs/PowDAL";
 
 const fs      = require('fs')
@@ -697,7 +696,7 @@ export class FileDAL {
     let iindex = indexer.iindex(index);
     let sindex = indexer.sindex(index);
     let cindex = indexer.cindex(index);
-    sindex = sindex.concat(await indexer.ruleIndexGenDividend(HEAD, this));
+    sindex = sindex.concat(await indexer.ruleIndexGenDividend(HEAD, iindex, this));
     sindex = sindex.concat(await indexer.ruleIndexGarbageSmallAccounts(HEAD, sindex, this));
     cindex = cindex.concat(await indexer.ruleIndexGenCertificationExpiry(HEAD, this));
     mindex = mindex.concat(await indexer.ruleIndexGenMembershipExpiry(HEAD, this));
diff --git a/app/lib/indexer.ts b/app/lib/indexer.ts
index 42b8cea88..8b05ab264 100644
--- a/app/lib/indexer.ts
+++ b/app/lib/indexer.ts
@@ -1542,10 +1542,10 @@ export class Indexer {
   }
 
   // BR_G91
-  static async ruleIndexGenDividend(HEAD: DBHead, dal: any) {
+  static async ruleIndexGenDividend(HEAD: DBHead, local_iindex: IindexEntry[], dal: any) {
     const dividends = [];
     if (HEAD.new_dividend) {
-      const members = await dal.iindexDAL.getMembersPubkeys()
+      const members = (await dal.iindexDAL.getMembersPubkeys()).concat(local_iindex.filter(i => i.member))
       for (const MEMBER of members) {
         dividends.push({
           op: 'CREATE',
diff --git a/doc/Protocol.md b/doc/Protocol.md
index f8b7c0b58..b34e1f1c9 100644
--- a/doc/Protocol.md
+++ b/doc/Protocol.md
@@ -2678,6 +2678,21 @@ For each `REDUCE_BY(GLOBAL_IINDEX[member=true], 'pub') as IDTY` then if `IDTY.me
         consumed = false
     )
 
+For each `LOCAL_IINDEX[member=true] as IDTY` add a new LOCAL_SINDEX entry:
+
+    SINDEX (
+        op = 'CREATE'
+        identifier = IDTY.pub
+        pos = HEAD.number
+        written_on = BLOCKSTAMP
+        written_time = MedianTime
+        amount = HEAD.dividend
+        base = HEAD.unitBase
+        locktime = null
+        conditions = REQUIRE_SIG(MEMBER.pub)
+        consumed = false
+    )
+
 ###### BR_G106 - Low accounts
 
 Set:
-- 
GitLab