diff --git a/app/lib/blockchain/DuniterBlockchain.ts b/app/lib/blockchain/DuniterBlockchain.ts index a7b0fdc5a494c136a775ff88e3e982cf511a5c40..af3cbbc68febd192b9fb8d9fdd9dea865e9e62ed 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 f11ea78bc6300d58de485f38b5d9d9720ccdbef9..a2be66b6e8df12667c7b894ce8102fb6553926d1 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 0e2db2fab38c86af595d08dafb206eef8e8afcde..a0d46d08f7c83315511d1a8722c13b4e31d5359e 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 18dcb8966d041706537e48f703b56547fcfc3554..fe8c810bb108fc0de4de201872b24fc8abeb7a95 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) {