From 4e7dbae829b4d867f6c23b4ab4b6241ab28082b9 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 24 Jun 2014 01:05:04 -0500 Subject: [PATCH] fix: minor fixes to block framer and block. --- lib/bcoin/block.js | 6 +++--- lib/bcoin/protocol/framer.js | 13 +++++-------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 776d4272..92803e2e 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -32,11 +32,11 @@ function Block(data, subtype) { tx.ts = tx.ts || self.ts; return tx; }); - this.hashes = this.txs.map(function(tx) { + this.tx = this.txs.map(function(tx) { return tx.hash('hex'); }); - this.tx = this.hashes.slice(); this.invalid = !this._checkBlock(); + this.hashes = this.merkleTree; } this._hash = null; @@ -193,7 +193,7 @@ Block.prototype.toJSON = function toJSON() { v: '1', type: 'block', subtype: this.subtype, - hash: this._hash || this.hash('hex'), + hash: this.hash('hex'), prevBlock: this.prevBlock, ts: this.ts, block: utils.toHex(bcoin.protocol.framer.block(this, this.subtype)) diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 0b216fc9..64e5b9da 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -276,32 +276,29 @@ Framer.block = function _block(block, type) { var p = []; var off = 0; + if (!type) + type = block.subtype; + // version - assert.equal(off, 0); off += writeU32(p, block.version, off); // prev_block - assert.equal(off, 4); utils.toArray(block.prevBlock, 'hex').forEach(function(ch) { p[off++] = ch; }); // merkle_root - assert.equal(off, 36); utils.toArray(block.merkleRoot, 'hex').forEach(function(ch) { p[off++] = ch; }); // timestamp - assert.equal(off, 68); off += writeU32(p, block.ts, off); // bits - assert.equal(off, 72); off += writeU32(p, block.bits, off); // nonce - assert.equal(off, 76); off += writeU32(p, block.nonce, off); assert.equal(off, 80); @@ -310,7 +307,6 @@ Framer.block = function _block(block, type) { // txn_count off += writeU32(p, block.totalTX, off); // hash count - assert.equal(off, 84); off += varint(p, block.hashes.length, off); // hashes block.hashes.forEach(function(hash) { @@ -329,7 +325,8 @@ Framer.block = function _block(block, type) { off += varint(p, block.totalTX, off); // txs block.txs.forEach(function(tx) { - tx._raw.forEach(function(ch) { + var raw = tx._raw || tx.render(); + raw.forEach(function(ch) { p[off++] = ch; }); });