From 2738a4ed5d64de0ab82a2a64c581a2efa83043ca Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 25 May 2016 01:57:19 -0700 Subject: [PATCH] merkle refactor. misc. --- lib/bcoin/headers.js | 20 +++++++++++++------- lib/bcoin/merkleblock.js | 31 +++++++++++++++---------------- test/block-test.js | 18 +++++++++++------- 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/lib/bcoin/headers.js b/lib/bcoin/headers.js index 77de43c4..6b034960 100644 --- a/lib/bcoin/headers.js +++ b/lib/bcoin/headers.js @@ -83,13 +83,19 @@ Headers.prototype.getRaw = function getRaw() { */ Headers.prototype.inspect = function inspect() { - var copy = bcoin.headers(this); - copy.__proto__ = null; - delete copy._raw; - copy.hash = this.hash('hex'); - copy.rhash = this.rhash; - copy.date = utils.date(copy.ts); - return copy; + return { + type: 'headers', + hash: this.rhash, + height: this.height, + date: utils.date(this.ts), + version: this.version, + prevBlock: utils.revHex(this.prevBlock), + merkleRoot: utils.revHex(this.merkleRoot), + ts: this.ts, + bits: this.bits, + nonce: this.nonce, + totalTX: this.totalTX + }; }; /** diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index a6dae1c9..59a1fcf5 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -7,6 +7,7 @@ var bcoin = require('./env'); var utils = require('./utils'); +var assert = utils.assert; var constants = bcoin.protocol.constants; /** @@ -41,13 +42,11 @@ function MerkleBlock(data) { bcoin.abstractblock.call(this, data); - this.hashes = (data.hashes || []).map(function(hash) { - if (typeof hash === 'string') - hash = new Buffer(hash, 'hex'); - return hash; - }); + assert(Array.isArray(data.hashes)); + assert(Buffer.isBuffer(data.flags)); - this.flags = data.flags || []; + this.hashes = data.hashes; + this.flags = data.flags; // List of matched TXs this.map = {}; @@ -165,15 +164,14 @@ MerkleBlock.prototype.verifyPartial = function verifyPartial() { */ MerkleBlock.prototype.extractTree = function extractTree() { - var hashes = this.hashes; - var flags = this.flags; var bitsUsed = 0; var hashUsed = 0; var matches = []; var indexes = []; var map = {}; var failed = false; - var bits = new Array(flags.length * 8); + var hashes = new Array(this.hashes.length); + var bits = new Array(this.flags.length * 8); var totalTX = this.totalTX; var height = 0; var root, p; @@ -219,8 +217,11 @@ MerkleBlock.prototype.extractTree = function extractTree() { return utils.dsha256(Buffer.concat([left, right])); } + for (p = 0; p < hashes.length; p++) + hashes[p] = new Buffer(this.hashes[p], 'hex'); + for (p = 0; p < bits.length; p++) - bits[p] = +((flags[p / 8 | 0] & (1 << (p % 8))) !== 0); + bits[p] = +((this.flags[p / 8 | 0] & (1 << (p % 8))) !== 0); if (totalTX === 0) return; @@ -310,11 +311,9 @@ MerkleBlock.prototype.inspect = function inspect() { bits: this.bits, nonce: this.nonce, totalTX: this.totalTX, - hashes: this.hashes.map(function(hash) { - return hash.toString('hex'); - }), - map: this.map, - flags: this.flags + hashes: this.hashes, + flags: this.flags, + map: this.map }; }; @@ -419,7 +418,7 @@ MerkleBlock.fromBlock = function fromBlock(block, bloom) { bits.push(parent); if (height === 0 || !parent) { - hashes.push(hash(height, pos, leaves)); + hashes.push(hash(height, pos, leaves).toString('hex')); return; } diff --git a/test/block-test.js b/test/block-test.js index 8ae0ed62..d1d940be 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -49,6 +49,8 @@ describe('Block', function() { var mblock = bcoin.merkleblock.fromRaw(raw2, 'hex'); + this.timeout(10000); + it('should parse partial merkle tree', function() { assert(mblock.verify()); assert.equal(mblock.matches.length, 2); @@ -137,13 +139,14 @@ describe('Block', function() { }); it('should fail with a bad merkle root', function() { - var merkle = block.merkleRoot; - block.merkleRoot = constants.NULL_HASH; - delete block._valid; - assert(!block.verify()); - delete block._valid; - block.merkleRoot = merkle; - assert(block.verify()); + var block2 = new bcoin.block(block); + block2.merkleRoot = constants.NULL_HASH; + delete block2._valid; + assert(!block2.verify()); + delete block2._valid; + delete block2._hash; + block2.merkleRoot = block.merkleRoot; + assert(block2.verify()); }); it('should fail on merkle block with a bad merkle root', function() { @@ -162,6 +165,7 @@ describe('Block', function() { block2.bits = 403014710; assert(!block2.verify()); delete block2._valid; + delete block2._hash; block2.bits = block.bits; assert(block2.verify()); });