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
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......
"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
}
}
......@@ -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(){
......
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment