Skip to content

Commit

Permalink
rewritten
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Aug 2, 2018
1 parent aee079a commit e78069e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 400 deletions.
14 changes: 11 additions & 3 deletions bench/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,15 @@ async function doProof(tree, i, key, expect) {
console.log('Proof %d time: %d.', i, util.now() - now);

let size = 0;
for (const {prefix, node} of proof.nodes)
size += 2 + prefix.data.length + node.length;
if (proof.nodes.length > 0) {
if (Buffer.isBuffer(proof.nodes[0])) {
for (const node of proof.nodes)
size += 2 + node.length;
} else {
for (const {prefix, node} of proof.nodes)
size += 2 + prefix.data.length + node.length;
}
}

size += proof.value.length;

Expand All @@ -129,7 +136,8 @@ async function doProof(tree, i, key, expect) {
assert.strictEqual(value.length, 300);
assert.bufferEqual(value, expect);

console.log('Proof %d depth: %d', i, proof.depth);
if (proof.depth != null)
console.log('Proof %d depth: %d', i, proof.depth);
console.log('Proof %d length: %d', i, proof.nodes.length);
console.log('Proof %d size: %d', i, size);
console.log('Proof %d compressed size: %d',
Expand Down
4 changes: 4 additions & 0 deletions lib/bits.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class Bits {
return this.slice(depth, depth + size);
}

collision(key, depth) {
return this._count(depth, key, depth);
}

join(bits, bit) {
const size = this.size + bits.size + 1;
const out = this.constructor.alloc(size);
Expand Down
20 changes: 2 additions & 18 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ const assert = require('bsert');
* Constants
*/

const SKIP_PREFIX = Buffer.from([0x02]);
const INTERNAL_PREFIX = Buffer.from([0x01]);
const LEAF_PREFIX = Buffer.from([0x00]);
const EMPTY = Buffer.alloc(0);
const size = Buffer.alloc(2);

/*
* Common
Expand All @@ -34,22 +32,8 @@ function setBit(key, index, b) {
key[oct] |= b << (7 - bit);
}

function hashInternal(hash, prefix, left, right) {
if (prefix.size === 0)
return hash.multi(INTERNAL_PREFIX, left, right);

const ctx = hash.ctx;

writeU16(size, prefix.size, 0);

ctx.init();
ctx.update(SKIP_PREFIX);
ctx.update(size);
ctx.update(prefix.data);
ctx.update(left);
ctx.update(right);

return ctx.final();
function hashInternal(hash, left, right) {
return hash.multi(INTERNAL_PREFIX, left, right);
}

function hashLeaf(hash, key, valueHash) {
Expand Down
84 changes: 24 additions & 60 deletions lib/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,8 @@ class Null extends Node {
*/

class Internal extends Node {
constructor(prefix, left, right) {
constructor(left, right) {
super(null, null);
this.prefix = prefix;
this.left = left;
this.right = right;
}
Expand Down Expand Up @@ -296,11 +295,10 @@ class Internal extends Node {

hash(hash) {
if (!this.data) {
const prefix = this.prefix;
const left = this.left.hash(hash);
const right = this.right.hash(hash);

this.data = hashInternal(hash, prefix, left, right);
this.data = hashInternal(hash, left, right);
}

return this.data;
Expand All @@ -309,10 +307,10 @@ class Internal extends Node {
flags() {
let flags = 0;

if (this.prefix.size > 0)
if (!this.left.isNull())
flags += 1;

if (this.prefix.size > 255)
if (!this.right.isNull())
flags += 2;

return flags;
Expand All @@ -321,19 +319,15 @@ class Internal extends Node {
getSize(hash, bits) {
let size = 1;

if (this.prefix.size > 255) {
size += 2;
size += this.prefix.data.length;
} else if (this.prefix.size > 0) {
size += 1;
size += this.prefix.data.length;
if (!this.left.isNull()) {
size += PTR_SIZE;
size += hash.size;
}

size += PTR_SIZE;
size += hash.size;

size += PTR_SIZE;
size += hash.size;
if (!this.right.isNull()) {
size += PTR_SIZE;
size += hash.size;
}

return size;
}
Expand All @@ -342,24 +336,17 @@ class Internal extends Node {
data[off] = this.flags() * 16 + INTERNAL;
off += 1;

if (this.prefix.size > 255) {
off = writeU16(data, this.prefix.size, off);
off += this.prefix.data.copy(data, off);
} else if (this.prefix.size > 0) {
data[off] = this.prefix.size;
off += 1;
off += this.prefix.data.copy(data, off);
if (!this.left.isNull()) {
assert(this.left.ptr);
off = this.left.ptr.write(data, off);
off += this.left.hash(hash).copy(data, off);
}

assert(this.left.ptr);

off = this.left.ptr.write(data, off);
off += this.left.hash(hash).copy(data, off);

assert(this.right.ptr);

off = this.right.ptr.write(data, off);
off += this.right.hash(hash).copy(data, off);
if (!this.right.isNull()) {
assert(this.right.ptr);
off = this.right.ptr.write(data, off);
off += this.right.hash(hash).copy(data, off);
}

return off;
}
Expand All @@ -374,28 +361,6 @@ class Internal extends Node {
assert((type & 15) === INTERNAL);

if (flags & 1) {
let size = 0;

if (flags & 2) {
size = readU16(data, off);
off += 2;
} else {
size = data[off];
off += 1;
}

const bytes = (size + 7) >>> 3;

const prefix = new Bits();
prefix.size = size;
prefix.data = data.slice(off, off + bytes);

off += bytes;

this.prefix = prefix;
}

{
const ptr = Pointer.read(data, off);
off += PTR_SIZE;

Expand All @@ -405,7 +370,7 @@ class Internal extends Node {
this.left = new Hash(buf, ptr);
}

{
if (flags & 2) {
const ptr = Pointer.read(data, off);
off += PTR_SIZE;

Expand All @@ -420,21 +385,20 @@ class Internal extends Node {

inspect() {
return {
prefix: this.prefix.toString(),
left: this.left,
right: this.right
};
}

static decode(data, hash, bits) {
const NIL = exports.NIL;
const node = new this(Bits.EMPTY, NIL, NIL);
const node = new this(NIL, NIL);
return node.decode(data, hash, bits);
}

static from(prefix, x, y, bit) {
static from(x, y, bit) {
const NIL = exports.NIL;
const node = new this(prefix, NIL, NIL);
const node = new this(NIL, NIL);
node.set(bit, x);
node.set(bit ^ 1, y);
return node;
Expand Down
Loading

0 comments on commit e78069e

Please sign in to comment.