From aa39119b0d04913f33c00cb9386f1ad58d5aebb4 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Wed, 19 Jul 2017 17:36:13 +0200 Subject: [PATCH] [fix] #1037 Migrating Entity Merkle --- app/lib/dal/fileDAL.ts | 4 +- .../{entity/merkle.js => dto/MerkleDTO.ts} | 48 +++++++++---------- test/fast/merkle.js | 6 +-- test/integration/cli.js | 4 +- 4 files changed, 31 insertions(+), 31 deletions(-) rename app/lib/{entity/merkle.js => dto/MerkleDTO.ts} (72%) diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts index 33bea271c..a8d4208dd 100644 --- a/app/lib/dal/fileDAL.ts +++ b/app/lib/dal/fileDAL.ts @@ -13,6 +13,7 @@ import {DBWallet} from "./sqliteDAL/WalletDAL" import {DBTx} from "./sqliteDAL/TxsDAL" import {DBBlock} from "../db/DBBlock" import {DBMembership} from "./sqliteDAL/MembershipDAL" +import {MerkleDTO} from "../dto/MerkleDTO" const fs = require('fs') const path = require('path') @@ -22,7 +23,6 @@ const common = require('duniter-common'); const indexer = require('../indexer').Indexer const logger = require('../logger').NewLogger('filedal'); const Configuration = require('../entity/configuration'); -const Merkle = require('../entity/merkle'); const Transaction = require('../entity/transaction'); const constants = require('../constants'); @@ -702,7 +702,7 @@ export class FileDAL { async merkleForPeers() { let peers = await this.listAllPeersWithStatusNewUP(); const leaves = peers.map((peer:DBPeer) => peer.hash); - const merkle = new Merkle(); + const merkle = new MerkleDTO(); merkle.initialize(leaves); return merkle; } diff --git a/app/lib/entity/merkle.js b/app/lib/dto/MerkleDTO.ts similarity index 72% rename from app/lib/entity/merkle.js rename to app/lib/dto/MerkleDTO.ts index d6b2370db..09531529f 100644 --- a/app/lib/entity/merkle.js +++ b/app/lib/dto/MerkleDTO.ts @@ -1,20 +1,14 @@ "use strict"; -const _ = require('underscore'); -const merkle = require('merkle'); -module.exports = Merkle; +const merkle = require('merkle'); -function Merkle(json) { +export class MerkleDTO { - _(json || {}).keys().forEach((key) => { - let value = json[key]; - if (key == "number") { - value = parseInt(value); - } - this[key] = value; - }); + private levels:any[] + nodes:any[] + depth:number - this.initialize = (leaves) => { + initialize(leaves:string[]) { const tree = merkle('sha256').sync(leaves); this.depth = tree.depth(); this.nodes = tree.nodes(); @@ -23,9 +17,9 @@ function Merkle(json) { this.levels[i] = tree.level(i); } return this; - }; + } - this.remove = (leaf) => { + remove(leaf:string) { // If leaf IS present if(~this.levels[this.depth].indexOf(leaf)){ const leaves = this.leaves(); @@ -37,10 +31,10 @@ function Merkle(json) { leaves.sort(); this.initialize(leaves); } - }; + } - this.removeMany = (leaves) => { - leaves.forEach((leaf) => { + removeMany(leaves:string[]) { + leaves.forEach((leaf:string) => { // If leaf IS present if(~this.levels[this.depth].indexOf(leaf)){ const theLeaves = this.leaves(); @@ -55,7 +49,7 @@ function Merkle(json) { this.initialize(leaves); }; - this.push = (leaf, previous) => { + push(leaf:string, previous:string) { // If leaf is not present if(this.levels[this.depth].indexOf(leaf) == -1){ const leaves = this.leaves(); @@ -71,9 +65,9 @@ function Merkle(json) { leaves.sort(); this.initialize(leaves); } - }; + } - this.pushMany = (leaves) => { + pushMany(leaves:string[]) { leaves.forEach((leaf) => { // If leaf is not present if(this.levels[this.depth].indexOf(leaf) == -1){ @@ -82,11 +76,17 @@ function Merkle(json) { }); leaves.sort(); this.initialize(leaves); - }; + } - this.root = () => this.levels.length > 0 ? this.levels[0][0] : ''; + root() { + return this.levels.length > 0 ? this.levels[0][0] : '' + } - this.leaves = () => this.levels[this.depth]; + leaves() { + return this.levels[this.depth] + } - this.count = () => this.leaves().length; + count() { + return this.leaves().length + } } diff --git a/test/fast/merkle.js b/test/fast/merkle.js index 9f3db2901..55e8a0918 100644 --- a/test/fast/merkle.js +++ b/test/fast/merkle.js @@ -2,11 +2,11 @@ var should = require('should'); var assert = require('assert'); -var Merkle = require('../../app/lib/entity/merkle'); +var MerkleDTO = require('../../app/lib/dto/MerkleDTO').MerkleDTO describe("Merkle ['a', 'b', 'c', 'd', 'e']", function(){ - var m = new Merkle({ type: 'CollectionName', criteria: '{}'}); + var m = new MerkleDTO(); m.initialize(['a', 'b', 'c', 'd', 'e']); it('should have root 16E6BEB3E080910740A2923D6091618CAA9968AEAD8A52D187D725D199548E2C', function(){ @@ -40,7 +40,7 @@ describe("Merkle ['a', 'b', 'c', 'd', 'e']", function(){ describe("Merkle []", function(){ - var m = new Merkle({ type: 'CollectionName', criteria: '{}'}); + var m = new MerkleDTO(); m.initialize([]); it('should have root empty', function(){ diff --git a/test/integration/cli.js b/test/integration/cli.js index bfda2271b..f6572d0ed 100644 --- a/test/integration/cli.js +++ b/test/integration/cli.js @@ -10,7 +10,7 @@ const duniter = require('../../index'); const merkleh = require('../../app/lib/helpers/merkle'); const hashf = require('duniter-common').hashf; const constants = require('../../app/lib/constants'); -const Merkle = require('../../app/lib/entity/merkle'); +const MerkleDTO = require('../../app/lib/dto/MerkleDTO').MerkleDTO const DB_NAME = "unit_tests"; @@ -31,7 +31,7 @@ describe("CLI", function() { const onReadBlockchainChunk = (count, from) => Promise.resolve(blockchain.blocks.slice(from, from + count)); const onReadParticularBlock = (number) => Promise.resolve(blockchain.blocks[number]); const onPeersRequested = (req) => co(function*() { - const merkle = new Merkle(); + const merkle = new MerkleDTO(); merkle.initialize(leaves); merkle.leaf = { "hash": req.params.leaf, -- GitLab