Skip to content
Snippets Groups Projects
Commit aa39119b authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[fix] #1037 Migrating Entity Merkle

parent 65adbcfe
Branches
Tags
No related merge requests found
...@@ -13,6 +13,7 @@ import {DBWallet} from "./sqliteDAL/WalletDAL" ...@@ -13,6 +13,7 @@ import {DBWallet} from "./sqliteDAL/WalletDAL"
import {DBTx} from "./sqliteDAL/TxsDAL" import {DBTx} from "./sqliteDAL/TxsDAL"
import {DBBlock} from "../db/DBBlock" import {DBBlock} from "../db/DBBlock"
import {DBMembership} from "./sqliteDAL/MembershipDAL" import {DBMembership} from "./sqliteDAL/MembershipDAL"
import {MerkleDTO} from "../dto/MerkleDTO"
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
...@@ -22,7 +23,6 @@ const common = require('duniter-common'); ...@@ -22,7 +23,6 @@ const common = require('duniter-common');
const indexer = require('../indexer').Indexer const indexer = require('../indexer').Indexer
const logger = require('../logger').NewLogger('filedal'); const logger = require('../logger').NewLogger('filedal');
const Configuration = require('../entity/configuration'); const Configuration = require('../entity/configuration');
const Merkle = require('../entity/merkle');
const Transaction = require('../entity/transaction'); const Transaction = require('../entity/transaction');
const constants = require('../constants'); const constants = require('../constants');
...@@ -702,7 +702,7 @@ export class FileDAL { ...@@ -702,7 +702,7 @@ export class FileDAL {
async merkleForPeers() { async merkleForPeers() {
let peers = await this.listAllPeersWithStatusNewUP(); let peers = await this.listAllPeersWithStatusNewUP();
const leaves = peers.map((peer:DBPeer) => peer.hash); const leaves = peers.map((peer:DBPeer) => peer.hash);
const merkle = new Merkle(); const merkle = new MerkleDTO();
merkle.initialize(leaves); merkle.initialize(leaves);
return merkle; return merkle;
} }
......
"use strict"; "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) => { private levels:any[]
let value = json[key]; nodes:any[]
if (key == "number") { depth:number
value = parseInt(value);
}
this[key] = value;
});
this.initialize = (leaves) => { initialize(leaves:string[]) {
const tree = merkle('sha256').sync(leaves); const tree = merkle('sha256').sync(leaves);
this.depth = tree.depth(); this.depth = tree.depth();
this.nodes = tree.nodes(); this.nodes = tree.nodes();
...@@ -23,9 +17,9 @@ function Merkle(json) { ...@@ -23,9 +17,9 @@ function Merkle(json) {
this.levels[i] = tree.level(i); this.levels[i] = tree.level(i);
} }
return this; return this;
}; }
this.remove = (leaf) => { remove(leaf:string) {
// If leaf IS present // If leaf IS present
if(~this.levels[this.depth].indexOf(leaf)){ if(~this.levels[this.depth].indexOf(leaf)){
const leaves = this.leaves(); const leaves = this.leaves();
...@@ -37,10 +31,10 @@ function Merkle(json) { ...@@ -37,10 +31,10 @@ function Merkle(json) {
leaves.sort(); leaves.sort();
this.initialize(leaves); this.initialize(leaves);
} }
}; }
this.removeMany = (leaves) => { removeMany(leaves:string[]) {
leaves.forEach((leaf) => { leaves.forEach((leaf:string) => {
// If leaf IS present // If leaf IS present
if(~this.levels[this.depth].indexOf(leaf)){ if(~this.levels[this.depth].indexOf(leaf)){
const theLeaves = this.leaves(); const theLeaves = this.leaves();
...@@ -55,7 +49,7 @@ function Merkle(json) { ...@@ -55,7 +49,7 @@ function Merkle(json) {
this.initialize(leaves); this.initialize(leaves);
}; };
this.push = (leaf, previous) => { push(leaf:string, previous:string) {
// If leaf is not present // If leaf is not present
if(this.levels[this.depth].indexOf(leaf) == -1){ if(this.levels[this.depth].indexOf(leaf) == -1){
const leaves = this.leaves(); const leaves = this.leaves();
...@@ -71,9 +65,9 @@ function Merkle(json) { ...@@ -71,9 +65,9 @@ function Merkle(json) {
leaves.sort(); leaves.sort();
this.initialize(leaves); this.initialize(leaves);
} }
}; }
this.pushMany = (leaves) => { pushMany(leaves:string[]) {
leaves.forEach((leaf) => { leaves.forEach((leaf) => {
// If leaf is not present // If leaf is not present
if(this.levels[this.depth].indexOf(leaf) == -1){ if(this.levels[this.depth].indexOf(leaf) == -1){
...@@ -82,11 +76,17 @@ function Merkle(json) { ...@@ -82,11 +76,17 @@ function Merkle(json) {
}); });
leaves.sort(); leaves.sort();
this.initialize(leaves); 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
}
} }
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
var should = require('should'); var should = require('should');
var assert = require('assert'); 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(){ 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']); m.initialize(['a', 'b', 'c', 'd', 'e']);
it('should have root 16E6BEB3E080910740A2923D6091618CAA9968AEAD8A52D187D725D199548E2C', function(){ it('should have root 16E6BEB3E080910740A2923D6091618CAA9968AEAD8A52D187D725D199548E2C', function(){
...@@ -40,7 +40,7 @@ describe("Merkle ['a', 'b', 'c', 'd', 'e']", function(){ ...@@ -40,7 +40,7 @@ describe("Merkle ['a', 'b', 'c', 'd', 'e']", function(){
describe("Merkle []", function(){ describe("Merkle []", function(){
var m = new Merkle({ type: 'CollectionName', criteria: '{}'}); var m = new MerkleDTO();
m.initialize([]); m.initialize([]);
it('should have root empty', function(){ it('should have root empty', function(){
......
...@@ -10,7 +10,7 @@ const duniter = require('../../index'); ...@@ -10,7 +10,7 @@ const duniter = require('../../index');
const merkleh = require('../../app/lib/helpers/merkle'); const merkleh = require('../../app/lib/helpers/merkle');
const hashf = require('duniter-common').hashf; const hashf = require('duniter-common').hashf;
const constants = require('../../app/lib/constants'); 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"; const DB_NAME = "unit_tests";
...@@ -31,7 +31,7 @@ describe("CLI", function() { ...@@ -31,7 +31,7 @@ describe("CLI", function() {
const onReadBlockchainChunk = (count, from) => Promise.resolve(blockchain.blocks.slice(from, from + count)); const onReadBlockchainChunk = (count, from) => Promise.resolve(blockchain.blocks.slice(from, from + count));
const onReadParticularBlock = (number) => Promise.resolve(blockchain.blocks[number]); const onReadParticularBlock = (number) => Promise.resolve(blockchain.blocks[number]);
const onPeersRequested = (req) => co(function*() { const onPeersRequested = (req) => co(function*() {
const merkle = new Merkle(); const merkle = new MerkleDTO();
merkle.initialize(leaves); merkle.initialize(leaves);
merkle.leaf = { merkle.leaf = {
"hash": req.params.leaf, "hash": req.params.leaf,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment