From 06d17afa4f05cf1948a96417b413ee05d4f32fd1 Mon Sep 17 00:00:00 2001
From: cgeek <cem.moreau@gmail.com>
Date: Tue, 22 Aug 2017 10:45:31 +0200
Subject: [PATCH] [fix] #1080 WebSocket: monetaryMass is missing in post
 connection block

---
 app/lib/blockchain/DuniterBlockchain.ts |  6 ++--
 app/lib/dto/BlockDTO.ts                 |  8 ++++-
 app/service/BlockchainService.ts        |  4 +--
 test/integration/http_api.js            | 40 +++++++++++++++++--------
 4 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts
index a7b0fdc5a..af3cbbc68 100644
--- a/app/lib/blockchain/DuniterBlockchain.ts
+++ b/app/lib/blockchain/DuniterBlockchain.ts
@@ -167,7 +167,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
     try {
       const currentBlock = await dal.getCurrentBlockOrNull();
       block.fork = false;
-      await this.saveBlockData(currentBlock, block, conf, dal, logger, index, HEAD);
+      const added = await this.saveBlockData(currentBlock, block, conf, dal, logger, index, HEAD);
 
       try {
         await DuniterBlockchain.pushStatsForBlocks([block], dal);
@@ -176,7 +176,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
       }
 
       logger.info('Block #' + block.number + ' added to the blockchain in %s ms', (Date.now() - start));
-      return block;
+      return BlockDTO.fromJSONObject(added)
     }
     catch(err) {
       throw err;
@@ -245,7 +245,7 @@ export class DuniterBlockchain extends MiscIndexedBlockchain {
     // Saves the block (DAL)
     await dal.saveBlock(dbb);
 
-    return block;
+    return dbb
   }
 
   async saveParametersForRoot(block:BlockDTO, conf:ConfDTO, dal:any) {
diff --git a/app/lib/dto/BlockDTO.ts b/app/lib/dto/BlockDTO.ts
index f11ea78bc..a2be66b6e 100644
--- a/app/lib/dto/BlockDTO.ts
+++ b/app/lib/dto/BlockDTO.ts
@@ -1,7 +1,7 @@
 import {TransactionDTO} from "./TransactionDTO"
 import {CurrencyConfDTO} from "./ConfDTO"
 import {hashf} from "../common"
-import {Cloneable} from "./Cloneable";
+import {Cloneable} from "./Cloneable"
 
 const DEFAULT_DOCUMENT_VERSION = 10
 
@@ -209,6 +209,12 @@ export class BlockDTO implements Cloneable {
     dto.time = parseInt(obj.time)
     dto.powMin = parseInt(obj.powMin)
     dto.monetaryMass = parseInt(obj.monetaryMass)
+    if (isNaN(dto.monetaryMass) && obj.mass !== undefined) {
+      dto.monetaryMass = parseInt(obj.mass)
+    }
+    if (isNaN(dto.monetaryMass)) {
+      dto.monetaryMass = 0
+    }
     dto.unitbase = parseInt(obj.unitbase)
     dto.membersCount = parseInt(obj.membersCount)
     dto.issuersCount = parseInt(obj.issuersCount)
diff --git a/app/service/BlockchainService.ts b/app/service/BlockchainService.ts
index 0e2db2fab..a0d46d08f 100644
--- a/app/service/BlockchainService.ts
+++ b/app/service/BlockchainService.ts
@@ -191,11 +191,11 @@ export class BlockchainService extends FIFOService {
       while (!added && i < potentials.length) {
         const dto = BlockDTO.fromJSONObject(potentials[i])
         try {
-          await this.mainContext.checkAndAddBlock(dto)
+          const addedBlock = await this.mainContext.checkAndAddBlock(dto)
           added = true
           this.push({
             bcEvent: OtherConstants.BC_EVENT.HEAD_CHANGED,
-            block: dto
+            block: addedBlock
           })
         } catch (e) {
           this.logger.error(e)
diff --git a/test/integration/http_api.js b/test/integration/http_api.js
index 18dcb8966..fe8c810bb 100644
--- a/test/integration/http_api.js
+++ b/test/integration/http_api.js
@@ -211,23 +211,37 @@ describe("HTTP API", function() {
       });
     });
 
-    it('/block (number 5) should send a block', () => co(function*() {
+    it('/block (number 5,6,7) should send a block', () => co(function*() {
       let completed = false
       yield commit({ time: now + 120 * 5 });
       const client = new ws('ws://127.0.0.1:7777/ws/block');
-      return new Promise(res => {
-        client.once('message', function message(data) {
-          const block = JSON.parse(data);
-          should(block).have.property('number', 5);
-          should(block).have.property('dividend').equal(100)
-          should(block).have.property('monetaryMass').equal(600)
-          should(block).have.property('monetaryMass').not.equal("600")
-          if (!completed) {
-            completed = true;
-            res();
-          }
-        })
+      let resolve5, resolve6, resolve7
+      const p5 = new Promise(res => resolve5 = res)
+      const p6 = new Promise(res => resolve6 = res)
+      const p7 = new Promise(res => resolve7 = res)
+      client.on('message', function message(data) {
+        const block = JSON.parse(data);
+        if (block.number === 5) resolve5(block)
+        if (block.number === 6) resolve6(block)
+        if (block.number === 7) resolve7(block)
       })
+      yield commit({ time: now + 120 * 6 });
+      yield commit({ time: now + 120 * 7 });
+      const b5 = yield p5
+      should(b5).have.property('number', 5);
+      should(b5).have.property('dividend').equal(100)
+      should(b5).have.property('monetaryMass').equal(600)
+      should(b5).have.property('monetaryMass').not.equal("600")
+      const b6 = yield p6
+      should(b6).have.property('number', 6);
+      should(b6).have.property('dividend').equal(null)
+      should(b6).have.property('monetaryMass').equal(600)
+      should(b6).have.property('monetaryMass').not.equal("600")
+      const b7 = yield p7
+      should(b7).have.property('number', 7);
+      should(b7).have.property('dividend').equal(100)
+      should(b7).have.property('monetaryMass').equal(800)
+      should(b7).have.property('monetaryMass').not.equal("800")
     }))
 
     it('/block should answer to pings', function(done) {
-- 
GitLab