From d6576c51f1676002971c6b246887a8c4282ceb5a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 16 Sep 2016 19:16:32 -0700 Subject: [PATCH] merkleblock: use buffers for tree hashes. --- lib/env.js | 3 ++- lib/primitives/merkleblock.js | 42 +++++++++++++++++++++++++---------- test/block-test.js | 6 +++-- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/lib/env.js b/lib/env.js index 721fea72..0b3ba86c 100644 --- a/lib/env.js +++ b/lib/env.js @@ -340,6 +340,7 @@ Environment.prototype.cache = function cache() { require('./utils/reader'); require('./utils/writer'); require('./utils/lru'); + require('./utils/bloom'); require('./utils/uri'); require('./utils/errors'); require('./crypto/ec'); @@ -356,7 +357,6 @@ Environment.prototype.cache = function cache() { require('./script/witness'); require('./script/program'); require('./script/sigcache'); - require('./primitives/bloom'); require('./primitives/address'); require('./primitives/outpoint'); require('./primitives/input'); @@ -371,6 +371,7 @@ Environment.prototype.cache = function cache() { require('./primitives/merkleblock'); require('./primitives/headers'); require('./primitives/keyring'); + require('./primitives/netaddress'); require('./hd/hd'); require('./node/logger'); require('./node/config'); diff --git a/lib/primitives/merkleblock.js b/lib/primitives/merkleblock.js index 2b0277c1..a219dc1a 100644 --- a/lib/primitives/merkleblock.js +++ b/lib/primitives/merkleblock.js @@ -54,12 +54,20 @@ utils.inherits(MerkleBlock, AbstractBlock); */ MerkleBlock.prototype.fromOptions = function fromOptions(options) { + var i, hash; + assert(options, 'MerkleBlock data is required.'); assert(Array.isArray(options.hashes)); assert(Buffer.isBuffer(options.flags)); - if (options.hashes) - this.hashes = options.hashes; + if (options.hashes) { + for (i = 0; i < options.hashes.length; i++) { + hash = options.hashes[i]; + if (typeof hash === 'string') + hash = new Buffer(hash, 'hex'); + this.hashes.push(hash); + } + } if (options.flags) this.flags = options.flags; @@ -206,7 +214,7 @@ MerkleBlock.prototype.extractTree = function extractTree() { hash = hashes[hashUsed++]; if (height === 0 && parent) { txid = hash.toString('hex'); - matches.push(txid); + matches.push(hash); indexes.push(pos); map[txid] = pos; } @@ -229,7 +237,7 @@ MerkleBlock.prototype.extractTree = function extractTree() { } for (p = 0; p < this.hashes.length; p++) - hashes.push(new Buffer(this.hashes[p], 'hex')); + hashes.push(this.hashes[p]); if (totalTX === 0) return; @@ -317,7 +325,9 @@ MerkleBlock.prototype.inspect = function inspect() { bits: this.bits, nonce: this.nonce, totalTX: this.totalTX, - hashes: this.hashes, + hashes: this.hashes.map(function(hash) { + return hash.toString('hex'); + }), flags: this.flags, map: this.map, txs: this.txs.length @@ -376,7 +386,7 @@ MerkleBlock.prototype.fromRaw = function fromRaw(data) { hashCount = p.readVarint(); for (i = 0; i < hashCount; i++) - this.hashes.push(p.readHash('hex')); + this.hashes.push(p.readHash()); this.flags = p.readVarBytes(); @@ -416,7 +426,9 @@ MerkleBlock.prototype.toJSON = function toJSON() { bits: this.bits, nonce: this.nonce, totalTX: this.totalTX, - hashes: this.hashes, + hashes: this.hashes.map(function(hash) { + return utils.revHex(hash.toString('hex')); + }), flags: this.flags.toString('hex') }; }; @@ -428,6 +440,8 @@ MerkleBlock.prototype.toJSON = function toJSON() { */ MerkleBlock.prototype.fromJSON = function fromJSON(json) { + var i, hash; + assert(json, 'MerkleBlock data is required.'); assert.equal(json.type, 'merkleblock'); assert(Array.isArray(json.hashes)); @@ -435,7 +449,11 @@ MerkleBlock.prototype.fromJSON = function fromJSON(json) { this.parseJSON(json); - this.hashes = json.hashes; + for (i = 0; i < json.hashes.length; i++) { + hash = utils.revHex(json.hashes[i]); + this.hashes.push(new Buffer(hash, 'hex')); + } + this.flags = new Buffer(json.flags, 'hex'); return this; @@ -490,9 +508,9 @@ MerkleBlock.fromHashes = function fromHashes(block, hashes) { for (i = 0; i < hashes.length; i++) { hash = hashes[i]; - if (Buffer.isBuffer(hash)) - hash = hash.toString('hex'); - filter[hash] = true; + if (typeof hash === 'string') + hash = new Buffer(hash, 'hex'); + filter[hash.toString('hex')] = true; } for (i = 0; i < block.txs.length; i++) { @@ -563,7 +581,7 @@ MerkleBlock.fromMatches = function fromMatches(block, matches) { bits.push(parent); if (height === 0 || !parent) { - hashes.push(hash(height, pos, leaves).toString('hex')); + hashes.push(hash(height, pos, leaves)); return; } diff --git a/test/block-test.js b/test/block-test.js index 129afd7d..0769311e 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -14,6 +14,8 @@ var bip152 = require('../lib/net/bip152'); var cmpct2 = fs.readFileSync(__dirname + '/data/cmpct2', 'utf8').trim(); var cmpct2block = fs.readFileSync(__dirname + '/data/cmpct2.bin'); +bcoin.cache(); + describe('Block', function() { var mblock = bcoin.merkleblock({ version: 2, @@ -66,10 +68,10 @@ describe('Block', function() { assert.equal(mblock.rhash, '0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c'); assert.equal( - mblock.matches[0], + mblock.matches[0].toString('hex'), '7393f84cd04ca8931975c66282ebf1847c78d8de6c2578d4f9bae23bc6f30857'); assert.equal( - mblock.matches[1], + mblock.matches[1].toString('hex'), 'ec8c51de3170301430ec56f6703533d9ea5b05c6fa7068954bcb90eed8c2ee5c'); });