merkle-patricia-tree
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -110,26 +110,33 @@ const assert = require('assert') | ||
// retrieves a node from dbs by hash | ||
Trie.prototype._lookupNode = function(node, cb) { | ||
var self = this | ||
function dbGet(db, cb) { | ||
db.get(node, { | ||
/** | ||
* Writes a value directly to the underlining db | ||
* @method getRaw | ||
* @param {Buffer} key | ||
*/ | ||
Trie.prototype.getRaw = function(key, cb){ | ||
function dbGet(db, cb2) { | ||
db.get(key, { | ||
keyEncoding: 'binary', | ||
valueEncoding: 'binary' | ||
}, function(err, foundNode) { | ||
if (err || !foundNode) | ||
cb(null, null) | ||
else { | ||
foundNode = rlp.decode(foundNode) | ||
cb(null, new TrieNode(foundNode)) | ||
if (err || !foundNode){ | ||
cb2(null, null) | ||
} else { | ||
cb2(null, foundNode) | ||
} | ||
}) | ||
} | ||
asyncFirstSeries(this._getDBs, dbGet, cb) | ||
} | ||
// retrieves a node from dbs by hash | ||
Trie.prototype._lookupNode = function(node, cb) { | ||
if (TrieNode.isRawNode(node)) | ||
cb(new TrieNode(node)) | ||
else { | ||
asyncFirstSeries(this._getDBs, dbGet, function(err, result){ | ||
// if (!result) console.log('READ FAILED:', node.toString('hex')) | ||
cb(result) | ||
this.getRaw(node, function(err, value){ | ||
if(value) | ||
value = new TrieNode(rlp.decode(value)) | ||
cb(value) | ||
}) | ||
@@ -139,28 +146,28 @@ } | ||
// writes a single node to dbs | ||
Trie.prototype._putNode = function(node, cb) { | ||
var self = this | ||
var hash = node.hash() | ||
var serialized = node.serialize() | ||
function dbPut(db, cb) { | ||
// console.log('PUT DB#'+(db === self.db ? 'DB':'SCRATCH'), 'node:', hash.toString('hex')) | ||
db.put(hash, serialized, { | ||
/** | ||
* Writes a value directly to the underlining db | ||
* @method putRaw | ||
* @param {Buffer} key | ||
* @param {Buffer} key | ||
*/ | ||
Trie.prototype.putRaw = function(key, val, cb){ | ||
function dbPut(db, cb2) { | ||
db.put(key, val, { | ||
keyEncoding: 'binary', | ||
valueEncoding: 'binary', | ||
}, cb) | ||
}, cb2) | ||
} | ||
async.each(this._putDBs, dbPut, cb) | ||
} | ||
// writes a single node to dbs | ||
Trie.prototype._putNode = function(node, cb) { | ||
var hash = node.hash() | ||
var serialized = node.serialize() | ||
this.putRaw(hash, serialized, cb) | ||
} | ||
// writes many nodes to db | ||
Trie.prototype._batchNodes = function(opStack, cb) { | ||
var self = this | ||
function dbBatch(db, cb) { | ||
// console.log('BATCH-'+opStack.length, 'DB#'+(db === self.db ? 'DB':'SCRATCH')) | ||
// opStack.forEach(function(op){ | ||
// console.log(' -'+op.type.toUpperCase(), 'key:', op.key.toString('hex')) | ||
// }) | ||
db.batch(opStack, { | ||
@@ -167,0 +174,0 @@ keyEncoding: 'binary', |
@@ -32,3 +32,2 @@ const levelup = require('levelup') | ||
trie.copy = copy.bind(trie, trie.copy.bind(trie)) | ||
} | ||
@@ -35,0 +34,0 @@ |
{ | ||
"name": "merkle-patricia-tree", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "This is an implementation of the modified merkle patricia tree as speficed in the Ethereum's yellow paper.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,27 +0,27 @@ | ||
var async = require('async'); | ||
var async = require('async') | ||
var Trie = require('../secure.js'); | ||
var trie = new Trie(); | ||
var a = new Buffer('f8448080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0a155280bc3c09fd31b0adebbdd4ef3d5128172c0d2008be964dc9e10e0f0fedf', 'hex'); | ||
var Trie = require('../secure.js') | ||
var trie = new Trie() | ||
var a = new Buffer('f8448080a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0a155280bc3c09fd31b0adebbdd4ef3d5128172c0d2008be964dc9e10e0f0fedf', 'hex') | ||
var ak = new Buffer('095e7baea6a6c7c4c2dfeb977efac326af552d87', 'hex') | ||
var b = new Buffer('f844802ea056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0db94dc4aab9b6a1a11956906ea34f3252f394576aece12199b23b269bb2738ab', 'hex'); | ||
var bk = new Buffer('945304eb96065b2a98b57a48a06ae28d285a71b5', 'hex'); | ||
var b = new Buffer('f844802ea056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0db94dc4aab9b6a1a11956906ea34f3252f394576aece12199b23b269bb2738ab', 'hex') | ||
var bk = new Buffer('945304eb96065b2a98b57a48a06ae28d285a71b5', 'hex') | ||
var c = new Buffer('f84c80880de0b6b3a7640000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', 'hex'); | ||
var ck = new Buffer('a94f5374fce5edbc8e2a8697c15331677e6ebf0b' ,'hex'); | ||
var c = new Buffer('f84c80880de0b6b3a7640000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', 'hex') | ||
var ck = new Buffer('a94f5374fce5edbc8e2a8697c15331677e6ebf0b' ,'hex') | ||
// checkpoint | ||
// checkpoint | ||
// commit | ||
var d = new Buffer('f8488084535500b1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0a155280bc3c09fd31b0adebbdd4ef3d5128172c0d2008be964dc9e10e0f0fedf', 'hex'); | ||
var d = new Buffer('f8488084535500b1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0a155280bc3c09fd31b0adebbdd4ef3d5128172c0d2008be964dc9e10e0f0fedf', 'hex') | ||
var dk = new Buffer('095e7baea6a6c7c4c2dfeb977efac326af552d87', 'hex') | ||
var e = new Buffer('f8478083010851a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0db94dc4aab9b6a1a11956906ea34f3252f394576aece12199b23b269bb2738ab', 'hex'); | ||
var ek = new Buffer('945304eb96065b2a98b57a48a06ae28d285a71b5', 'hex'); | ||
var e = new Buffer('f8478083010851a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0db94dc4aab9b6a1a11956906ea34f3252f394576aece12199b23b269bb2738ab', 'hex') | ||
var ek = new Buffer('945304eb96065b2a98b57a48a06ae28d285a71b5', 'hex') | ||
var f = new Buffer('f84c01880de0b6b3540df72ca056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', 'hex'); | ||
var fk = new Buffer('a94f5374fce5edbc8e2a8697c15331677e6ebf0b' ,'hex'); | ||
var f = new Buffer('f84c01880de0b6b3540df72ca056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', 'hex') | ||
var fk = new Buffer('a94f5374fce5edbc8e2a8697c15331677e6ebf0b' ,'hex') | ||
// commit | ||
var g = new Buffer('f8488084535500b1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0a155280bc3c09fd31b0adebbdd4ef3d5128172c0d2008be964dc9e10e0f0fedf', 'hex'); | ||
var g = new Buffer('f8488084535500b1a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0a155280bc3c09fd31b0adebbdd4ef3d5128172c0d2008be964dc9e10e0f0fedf', 'hex') | ||
var gk = new Buffer('095e7baea6a6c7c4c2dfeb977efac326af552d87', 'hex') | ||
@@ -33,4 +33,4 @@ | ||
function(done){ | ||
console.log('done'); | ||
trie.put(ak, a, done); | ||
console.log('done') | ||
trie.put(ak, a, done) | ||
}, | ||
@@ -41,23 +41,23 @@ function(done){ | ||
function(done){ | ||
trie.put(ck, c, done); | ||
trie.put(ck, c, done) | ||
}, | ||
function(done){ | ||
trie.checkpoint(); | ||
trie.checkpoint(); | ||
done(); | ||
trie.checkpoint() | ||
trie.checkpoint() | ||
done() | ||
}, | ||
function(done){ | ||
trie.commit(done); | ||
trie.commit(done) | ||
}, | ||
function(done){ | ||
trie.put(dk, d, done); | ||
trie.put(dk, d, done) | ||
}, | ||
function(done){ | ||
trie.put(ek, e, done); | ||
trie.put(ek, e, done) | ||
}, | ||
function(done){ | ||
trie.put(fk, f, done); | ||
trie.put(fk, f, done) | ||
}, | ||
function(done){ | ||
trie.commit(done); | ||
trie.commit(done) | ||
}, | ||
@@ -68,5 +68,5 @@ function(done){ | ||
], function(){ | ||
done(); | ||
}); | ||
}); | ||
}); | ||
done() | ||
}) | ||
}) | ||
}) |
@@ -5,30 +5,30 @@ var Trie = require('../index.js') | ||
var assert = require('assert') | ||
var jsonTests = require('ethereum-tests').trieTests | ||
var ethUtil = require('ethereumjs-util') | ||
var crypto = require('crypto') | ||
describe('simple save and retrive', function () { | ||
var trie = new Trie(); | ||
it('should not crash if given a non-existant root', function (done) { | ||
var root = new Buffer('3f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d', 'hex'); | ||
var trie11 = new Trie(null, root); | ||
var root = new Buffer('3f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d', 'hex') | ||
var trie = new Trie(null, root) | ||
trie11.get('test', function (err, value) { | ||
assert.equal(value, null); | ||
done(); | ||
}); | ||
}); | ||
trie.get('test', function (err, value) { | ||
assert.equal(value, null) | ||
done() | ||
}) | ||
}) | ||
var trie = new Trie() | ||
it('save a value', function (done) { | ||
trie.put('test', 'one', function () { | ||
done(); | ||
}); | ||
}); | ||
done() | ||
}) | ||
}) | ||
it('should get a value', function (done) { | ||
trie.get('test', function (err, value) { | ||
assert.equal(value.toString(), 'one'); | ||
done(); | ||
}); | ||
}); | ||
assert.equal(value.toString(), 'one') | ||
done() | ||
}) | ||
}) | ||
@@ -38,7 +38,7 @@ it('should update a value', function (done) { | ||
trie.get('test', function (err, value) { | ||
assert.equal(value.toString(), 'two'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
assert.equal(value.toString(), 'two') | ||
done() | ||
}) | ||
}) | ||
}) | ||
@@ -49,33 +49,33 @@ | ||
trie.get('test', function (err, value) { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('should recreate a value', function (done) { | ||
trie.put('test', 'one', function () { | ||
done(); | ||
}); | ||
}); | ||
done() | ||
}) | ||
}) | ||
it('should get updated a value', function (done) { | ||
trie.get('test', function (err, value) { | ||
assert.equal(value.toString(), 'one'); | ||
done(); | ||
}); | ||
}); | ||
assert.equal(value.toString(), 'one') | ||
done() | ||
}) | ||
}) | ||
it('should create a branch here', function (done) { | ||
trie.put('doge', 'coin', function () { | ||
assert.equal('de8a34a8c1d558682eae1528b47523a483dd8685d6db14b291451a66066bf0fc', trie.root.toString('hex')); | ||
done(); | ||
}); | ||
}); | ||
assert.equal('de8a34a8c1d558682eae1528b47523a483dd8685d6db14b291451a66066bf0fc', trie.root.toString('hex')) | ||
done() | ||
}) | ||
}) | ||
it('should get a value that is in a branch', function (done) { | ||
trie.get('doge', function (err, value) { | ||
assert.equal(value.toString(), 'coin'); | ||
done(); | ||
}); | ||
}); | ||
assert.equal(value.toString(), 'coin') | ||
done() | ||
}) | ||
}) | ||
@@ -85,166 +85,162 @@ it('should delete from a branch', function (done) { | ||
trie.get('doge', function (err, value) { | ||
assert.equal(value, null); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
assert.equal(value, null) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}); | ||
}) | ||
describe('storing longer values', function () { | ||
var trie2 = new Trie(); | ||
var trie = new Trie() | ||
var longString = 'this will be a really really really long value' | ||
var longStringRoot = 'b173e2db29e79c78963cff5196f8a983fbe0171388972106b114ef7f5c24dfa3' | ||
var longString = 'this will be a really really really long value'; | ||
var longStringRoot = 'b173e2db29e79c78963cff5196f8a983fbe0171388972106b114ef7f5c24dfa3'; | ||
it('should store a longer string', function (done) { | ||
trie2.put('done', longString, function (err, value) { | ||
trie2.put('doge', 'coin', function (err, value) { | ||
assert.equal(longStringRoot, trie2.root.toString('hex')); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
trie.put('done', longString, function (err, value) { | ||
trie.put('doge', 'coin', function (err, value) { | ||
assert.equal(longStringRoot, trie.root.toString('hex')) | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('should retreive a longer value', function (done) { | ||
trie2.get('done', function (err, value) { | ||
assert.equal(value.toString(), longString); | ||
done(); | ||
}); | ||
}); | ||
trie.get('done', function (err, value) { | ||
assert.equal(value.toString(), longString) | ||
done() | ||
}) | ||
}) | ||
it('should when being modiefied delete the old value', function (done) { | ||
trie2.put('done', 'test', function () { | ||
done(); | ||
}); | ||
}); | ||
trie.put('done', 'test', function () { | ||
done() | ||
}) | ||
}) | ||
}); | ||
}) | ||
describe('testing Extentions and branches', function () { | ||
var trie3 = new Trie(); | ||
var trie = new Trie() | ||
it('should store a value', function (done) { | ||
trie3.put('doge', 'coin', function () { | ||
done(); | ||
}); | ||
}); | ||
trie.put('doge', 'coin', function () { | ||
done() | ||
}) | ||
}) | ||
it('should create extention to store this value', function (done) { | ||
trie3.put('do', 'verb', function () { | ||
assert.equal('f803dfcb7e8f1afd45e88eedb4699a7138d6c07b71243d9ae9bff720c99925f9', trie3.root.toString('hex')); | ||
done(); | ||
}); | ||
}); | ||
trie.put('do', 'verb', function () { | ||
assert.equal('f803dfcb7e8f1afd45e88eedb4699a7138d6c07b71243d9ae9bff720c99925f9', trie.root.toString('hex')) | ||
done() | ||
}) | ||
}) | ||
it('should store this value under the extention ', function (done) { | ||
trie3.put('done', 'finished', function () { | ||
assert.equal('409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', trie3.root.toString('hex')); | ||
done(); | ||
}); | ||
}); | ||
trie.put('done', 'finished', function () { | ||
assert.equal('409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', trie.root.toString('hex')) | ||
done() | ||
}) | ||
}) | ||
}); | ||
}) | ||
describe('testing Extentions and branches - reverse', function () { | ||
var trie3 = new Trie(); | ||
var trie = new Trie() | ||
it('should create extention to store this value', function (done) { | ||
trie3.put('do', 'verb', function () { | ||
done(); | ||
}); | ||
}); | ||
trie.put('do', 'verb', function () { | ||
done() | ||
}) | ||
}) | ||
it('should store a value', function (done) { | ||
trie3.put('doge', 'coin', function () { | ||
done(); | ||
}); | ||
}); | ||
trie.put('doge', 'coin', function () { | ||
done() | ||
}) | ||
}) | ||
it('should store this value under the extention ', function (done) { | ||
trie3.put('done', 'finished', function () { | ||
assert.equal('409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', trie3.root.toString('hex')); | ||
done(); | ||
}); | ||
}); | ||
trie.put('done', 'finished', function () { | ||
assert.equal('409cff4d820b394ed3fb1cd4497bdd19ffa68d30ae34157337a7043c94a3e8cb', trie.root.toString('hex')) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}); | ||
describe('testing deletions cases', function () { | ||
var trie3 = new Trie(); | ||
var trie = new Trie() | ||
it('should delete from a branch->branch-branch', function (done) { | ||
async.parallel([ | ||
async.apply(trie3.put.bind(trie3), new Buffer([11, 11, 11]), 'first'), | ||
async.apply(trie3.put.bind(trie3), new Buffer([12, 22, 22]), 'create the first branch'), | ||
async.apply(trie3.put.bind(trie3), new Buffer([12, 34, 44]), 'create the last branch') | ||
async.apply(trie.put.bind(trie), new Buffer([11, 11, 11]), 'first'), | ||
async.apply(trie.put.bind(trie), new Buffer([12, 22, 22]), 'create the first branch'), | ||
async.apply(trie.put.bind(trie), new Buffer([12, 34, 44]), 'create the last branch') | ||
], function () { | ||
trie3.del(new Buffer([12, 22, 22]), function () { | ||
trie3.get(new Buffer([12, 22, 22]), function (err, val) { | ||
assert.equal(null, val); | ||
trie.del(new Buffer([12, 22, 22]), function () { | ||
trie.get(new Buffer([12, 22, 22]), function (err, val) { | ||
assert.equal(null, val) | ||
trie = new Trie() | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
trie3 = new Trie(); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it('should delete from a branch->branch-extention', function (done) { | ||
async.parallel([ | ||
async.apply(trie3.put.bind(trie3), new Buffer([11, 11, 11]), 'first'), | ||
async.apply(trie3.put.bind(trie3), new Buffer([12, 22, 22]), 'create the first branch'), | ||
async.apply(trie3.put.bind(trie3), new Buffer([12, 33, 33]), 'create the middle branch'), | ||
async.apply(trie3.put.bind(trie3), new Buffer([12, 33, 44]), 'create the last branch') | ||
async.apply(trie.put.bind(trie), new Buffer([11, 11, 11]), 'first'), | ||
async.apply(trie.put.bind(trie), new Buffer([12, 22, 22]), 'create the first branch'), | ||
async.apply(trie.put.bind(trie), new Buffer([12, 33, 33]), 'create the middle branch'), | ||
async.apply(trie.put.bind(trie), new Buffer([12, 33, 44]), 'create the last branch') | ||
], function () { | ||
trie3.del(new Buffer([12, 22, 22]), function () { | ||
trie3.get(new Buffer([12, 22, 22]), function (err, val) { | ||
assert.equal(null, val); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
trie.del(new Buffer([12, 22, 22]), function () { | ||
trie.get(new Buffer([12, 22, 22]), function (err, val) { | ||
assert.equal(null, val) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
it('should delete from a extention->branch-extention', function (done) { | ||
trie3.put(new Buffer([11, 11, 11]), 'first', function () { | ||
trie.put(new Buffer([11, 11, 11]), 'first', function () { | ||
//create the top branch | ||
trie3.put(new Buffer([12, 22, 22]), 'create the first branch', function () { | ||
trie.put(new Buffer([12, 22, 22]), 'create the first branch', function () { | ||
//crete the middle branch | ||
trie3.put(new Buffer([12, 33, 33]), 'create the middle branch', function () { | ||
trie3.put(new Buffer([12, 33, 44]), 'create the last branch', function () { | ||
trie.put(new Buffer([12, 33, 33]), 'create the middle branch', function () { | ||
trie.put(new Buffer([12, 33, 44]), 'create the last branch', function () { | ||
//delete the middle branch | ||
trie3.del(new Buffer([11, 11, 11]), function () { | ||
trie3.get(new Buffer([11, 11, 11]), function (err, val) { | ||
assert.equal(null, val); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
trie.del(new Buffer([11, 11, 11]), function () { | ||
trie.get(new Buffer([11, 11, 11]), function (err, val) { | ||
assert.equal(null, val) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
it('should delete from a extention->branch-branch', function (done) { | ||
trie3.put(new Buffer([11, 11, 11]), 'first', function () { | ||
trie.put(new Buffer([11, 11, 11]), 'first', function () { | ||
//create the top branch | ||
trie3.put(new Buffer([12, 22, 22]), 'create the first branch', function () { | ||
trie.put(new Buffer([12, 22, 22]), 'create the first branch', function () { | ||
//crete the middle branch | ||
trie3.put(new Buffer([12, 33, 33]), 'create the middle branch', function () { | ||
trie3.put(new Buffer([12, 34, 44]), 'create the last branch', function () { | ||
trie.put(new Buffer([12, 33, 33]), 'create the middle branch', function () { | ||
trie.put(new Buffer([12, 34, 44]), 'create the last branch', function () { | ||
//delete the middle branch | ||
trie3.del(new Buffer([11, 11, 11]), function () { | ||
trie3.get(new Buffer([11, 11, 11]), function (err, val) { | ||
assert.equal(null, val); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
trie.del(new Buffer([11, 11, 11]), function () { | ||
trie.get(new Buffer([11, 11, 11]), function (err, val) { | ||
assert.equal(null, val) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
@@ -255,18 +251,18 @@ describe('testing checkpoints', function () { | ||
preRoot, | ||
postRoot; | ||
postRoot | ||
before(function (done) { | ||
trie = new Trie(); | ||
trie = new Trie() | ||
trie.put('do', 'verb', function () { | ||
trie.put('doge', 'coin', function () { | ||
preRoot = trie.root.toString('hex'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
preRoot = trie.root.toString('hex') | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('should create a checkpoint', function (done) { | ||
trie.checkpoint(); | ||
done(); | ||
}); | ||
trie.checkpoint() | ||
done() | ||
}) | ||
@@ -276,48 +272,90 @@ it('should save to the cache', function (done) { | ||
trie.put('love', 'emotion', function () { | ||
postRoot = trie.root.toString('hex'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
postRoot = trie.root.toString('hex') | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('should revert to the orginal root', function (done) { | ||
assert(trie.isCheckpoint === true); | ||
assert(trie.isCheckpoint === true) | ||
trie.revert(function(){ | ||
assert(trie.root.toString('hex') === preRoot); | ||
assert(trie.isCheckpoint === false); | ||
done(); | ||
}); | ||
}); | ||
assert(trie.root.toString('hex') === preRoot) | ||
assert(trie.isCheckpoint === false) | ||
done() | ||
}) | ||
}) | ||
it('should commit a checkpoint', function (done) { | ||
trie.checkpoint(); | ||
trie.checkpoint() | ||
trie.put('test', 'something', function () { | ||
trie.put('love', 'emotion', function () { | ||
trie.commit(function () { | ||
assert(trie.isCheckpoint === false); | ||
assert(trie.root.toString('hex') === postRoot); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
assert(trie.isCheckpoint === false) | ||
assert(trie.root.toString('hex') === postRoot) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
it('should commit a nested checkpoint', function (done) { | ||
trie.checkpoint(); | ||
var root; | ||
trie.checkpoint() | ||
var root | ||
trie.put('test', 'something else', function () { | ||
root = trie.root; | ||
trie.checkpoint(); | ||
root = trie.root | ||
trie.checkpoint() | ||
trie.put('the feels', 'emotion', function () { | ||
trie.revert(); | ||
trie.revert() | ||
trie.commit(function () { | ||
assert(trie.isCheckpoint === false); | ||
assert(trie.root.toString('hex') === root.toString('hex')); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
assert(trie.isCheckpoint === false) | ||
assert(trie.root.toString('hex') === root.toString('hex')) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('put & get raw functions', function(){ | ||
var trie = new Trie() | ||
var key = crypto.randomBytes(32) | ||
var val = crypto.randomBytes(32) | ||
it('putRaw', function(done){ | ||
trie.putRaw(key, val, done) | ||
}) | ||
it('getRaw', function(done){ | ||
trie.getRaw(key, function(err, rVal){ | ||
assert(val.toString('hex') === rVal.toString('hex')) | ||
done() | ||
}) | ||
}) | ||
it('should checkpoint and get the rawVal', function(done){ | ||
trie.checkpoint() | ||
trie.getRaw(key, function(err, rVal){ | ||
assert(val.toString('hex') === rVal.toString('hex')) | ||
done() | ||
}) | ||
}) | ||
var key2 = crypto.randomBytes(32) | ||
var val2 = crypto.randomBytes(32) | ||
it('should store while in a checkpoint', function(done){ | ||
trie.putRaw(key2, val2, done) | ||
}) | ||
it('should retrieve from a checkpoint', function(done){ | ||
trie.getRaw(key2, function(err, rVal){ | ||
assert(val2.toString('hex') === rVal.toString('hex')) | ||
done() | ||
}) | ||
}) | ||
it('should not retiev after revert', function(done){ | ||
trie.revert(done) | ||
}) | ||
}) | ||
describe('it should create the genesis state root from ethereum', function () { | ||
@@ -331,15 +369,15 @@ | ||
stateRoot = new Buffer(32); | ||
stateRoot = new Buffer(32) | ||
stateRoot.fill(0); | ||
var startAmount = new Buffer(26); | ||
startAmount.fill(0); | ||
startAmount[0] = 1; | ||
var account = [startAmount, 0, stateRoot, ethUtil.sha3()]; | ||
var rlpAccount = rlp.encode(account); | ||
cppRlp = 'f85e9a010000000000000000000000000000000000000000000000000080a00000000000000000000000000000000000000000000000000000000000000000a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'; | ||
stateRoot.fill(0) | ||
var startAmount = new Buffer(26) | ||
startAmount.fill(0) | ||
startAmount[0] = 1 | ||
var account = [startAmount, 0, stateRoot, ethUtil.sha3()] | ||
var rlpAccount = rlp.encode(account) | ||
cppRlp = 'f85e9a010000000000000000000000000000000000000000000000000080a00000000000000000000000000000000000000000000000000000000000000000a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470' | ||
var genesisStateRoot = '2f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d'; | ||
assert.equal(cppRlp, rlpAccount.toString('hex')); | ||
//console.log(rlpAccount.toString('hex')); | ||
var genesisStateRoot = '2f4399b08efe68945c1cf90ffe85bbe3ce978959da753f9e649f034015b8817d' | ||
assert.equal(cppRlp, rlpAccount.toString('hex')) | ||
//console.log(rlpAccount.toString('hex')) | ||
@@ -351,90 +389,10 @@ it('shall match the root given unto us by the Master Coder Gav', function (done) { | ||
trie4.put(a, rlpAccount, function () { | ||
assert.equal(trie4.root.toString('hex'), genesisStateRoot); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
assert.equal(trie4.root.toString('hex'), genesisStateRoot) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
describe('offical tests', function () { | ||
var testNames, | ||
trie; | ||
before(function () { | ||
testNames = Object.keys(jsonTests.trietest); | ||
trie = new Trie(); | ||
}); | ||
it('pass all tests', function (done) { | ||
async.eachSeries(testNames, function (i, done) { | ||
console.log(i); | ||
var inputs = jsonTests.trietest[i].in; | ||
var expect = jsonTests.trietest[i].root; | ||
async.eachSeries(inputs, function (input, done) { | ||
for(i = 0; i < 2; i++){ | ||
if(input[i] && input[i].slice(0,2) === '0x'){ | ||
input[i] = new Buffer(input[i].slice(2), 'hex'); | ||
} | ||
} | ||
trie.put(input[0], input[1], function () { | ||
done(); | ||
}); | ||
}, function () { | ||
assert.equal('0x' + trie.root.toString('hex'), expect); | ||
trie = new Trie(); | ||
done(); | ||
}); | ||
}, function () { | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('offical tests any order', function () { | ||
var testNames, | ||
trie; | ||
before(function () { | ||
testNames = Object.keys(jsonTests.trieanyorder); | ||
trie = new Trie(); | ||
}); | ||
it('pass all tests', function (done) { | ||
async.eachSeries(testNames, function (i, done) { | ||
console.log(i); | ||
var test = jsonTests.trieanyorder[i]; | ||
var keys = Object.keys(test.in); | ||
async.eachSeries(keys, function (key, done) { | ||
var val = test.in[key]; | ||
if(key.slice(0,2) === '0x'){ | ||
key = new Buffer(key.slice(2), 'hex'); | ||
} | ||
if(val && val.slice(0,2) === '0x'){ | ||
val = new Buffer(val.slice(2), 'hex'); | ||
} | ||
trie.put(key, val, function () { | ||
done(); | ||
}); | ||
}, function () { | ||
assert.equal('0x' + trie.root.toString('hex'), test.root); | ||
trie = new Trie(); | ||
done(); | ||
}); | ||
}, function () { | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -1,4 +0,4 @@ | ||
const Trie = require('../index.js'); | ||
const async = require('async'); | ||
const assert = require('assert'); | ||
const Trie = require('../index.js') | ||
const async = require('async') | ||
const assert = require('assert') | ||
@@ -8,3 +8,3 @@ | ||
var trie = new Trie(); | ||
var trie = new Trie() | ||
var init = [ | ||
@@ -28,29 +28,29 @@ { type: 'del', key: 'father' } | ||
, { type: 'put', key: 'occupssation', value: 'Clown' } | ||
]; | ||
] | ||
var valObj = {}; | ||
var valObj = {} | ||
init.forEach(function(i) { | ||
if (i.type === 'put') valObj[i.key] = i.value | ||
}); | ||
}) | ||
it('should populate trie', function(done) { | ||
trie.batch(init, function() { | ||
done(); | ||
done() | ||
}) | ||
}); | ||
}) | ||
it('should fetch all of the nodes', function(done) { | ||
var stream = trie.createReadStream(); | ||
var stream = trie.createReadStream() | ||
stream.on('data', function(d) { | ||
assert(valObj[d.key.toString()] === d.value.toString()) | ||
delete valObj[d.key.toString()]; | ||
}); | ||
delete valObj[d.key.toString()] | ||
}) | ||
stream.on('end', function() { | ||
var keys = Object.keys(valObj); | ||
assert(keys.length === 0); | ||
done(); | ||
}); | ||
}); | ||
var keys = Object.keys(valObj) | ||
assert(keys.length === 0) | ||
done() | ||
}) | ||
}) | ||
}); | ||
}) | ||
@@ -60,3 +60,3 @@ | ||
var trie = new Trie(); | ||
var trie = new Trie() | ||
var init = [ | ||
@@ -69,3 +69,3 @@ { type: 'put', key: 'color', value: 'purple' }, | ||
{ type: 'put', key: 'color', value: 'pink' }, | ||
]; | ||
] | ||
@@ -78,11 +78,11 @@ var expectedNodes = { | ||
'2386bfb0de9cf93902a110f5ab07b917ffc0b9ea599cb7f4f8bb6fd1123c866c': true, | ||
}; | ||
} | ||
it('should populate trie', function(done) { | ||
trie.checkpoint(); | ||
trie.batch(init, done); | ||
}); | ||
trie.checkpoint() | ||
trie.batch(init, done) | ||
}) | ||
it('should only fetch nodes in the current trie', function(done) { | ||
var stream = trie.createScratchReadStream(); | ||
var stream = trie.createScratchReadStream() | ||
stream.on('data', function(d) { | ||
@@ -92,9 +92,9 @@ var key = d.key.toString('hex') | ||
delete expectedNodes[key] | ||
}); | ||
}) | ||
stream.on('end', function(){ | ||
assert.equal(Object.keys(expectedNodes).length, 0) | ||
done() | ||
}); | ||
}); | ||
}) | ||
}) | ||
}); | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
92706
20
1700