From b522aa2b0607563b31bc80d6b70986212695fd0d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 22 Dec 2015 15:03:17 -0800 Subject: [PATCH] handle merkleblock vs block better. --- lib/bcoin/block.js | 17 ++++++++--------- lib/bcoin/tx.js | 10 ++++++---- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 4348cead..63be46c5 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -45,23 +45,22 @@ function Block(data, subtype) { this.tx = []; this.invalid = false; - if (this.subtype === 'block') { + if (this.subtype === 'merkleblock') { + // Verify partial merkle tree and fill `ts` array + this.invalid = !this._verifyMerkle(); + } else if (this.subtype === 'block') { this.txs = this.txs.map(function(tx) { tx.network = self.network; tx.relayedBy = self.relayedBy; tx = bcoin.tx(tx); tx.block = self.hash('hex'); tx.ts = tx.ts || self.ts; - // self.tx.push(tx.hash('hex')); return tx; }); this.invalid = !this._checkBlock(); } this._hash = null; - - // Verify partial merkle tree and fill `ts` array - this._verifyMerkle(); } Block.prototype.hash = function hash(enc) { @@ -153,12 +152,12 @@ Block.prototype._verifyMerkle = function verifyMerkle() { root = visit(1); - if (!root || root !== this.merkleRoot) { - this.invalid = true; - return; - } + if (!root || root !== this.merkleRoot) + return false; this.tx = tx; + + return true; }; Block.prototype.getMerkleRoot = function getMerkleRoot() { diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 2fe9d925..023bb41e 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -46,16 +46,18 @@ function TX(data, block) { this.input(input, null); }, this); } + if (data.outputs) { data.outputs.forEach(function(out) { this.out(out, null); }, this); } - if (!data.ts && block && block.hasTX(this.hash('hex'))) { - // if (block) { - this.ts = block.ts; - this.block = block.hash('hex'); + if (block && block.subtype === 'merkleblock') { + if (!data.ts && block && block.hasTX(this.hash('hex'))) { + this.ts = block.ts; + this.block = block.hash('hex'); + } } // ps = Pending Since