diff --git a/app/lib/dal/fileDAL.ts b/app/lib/dal/fileDAL.ts index 33bea271cf5e86747e2a3ca1bc550f14a183abd8..a8d4208dd8c6269304d1ce9761aabf84bfe78d91 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 d6b2370dbaf47d45cd249c6dcf6026ac6c359469..09531529f5bfffd622fe2d3b906cea60b4856298 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 9f3db29010bf9714885771443cc4ad801dbdc199..55e8a0918c2d36ed8ce519b330cf8cd9fc0c5bd0 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 bfda2271bd00c1a671c659247b17735dec77baa1..f6572d0ed5c7b3616dbbaead747d954d9c7dba91 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,