diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 2b4d67f0..330259ea 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -23,6 +23,21 @@ function Block(data, subtype) { this.tx = []; this.invalid = false; + if (this.subtype === 'block') { + var self = this; + this.txs = data.txs || []; + this.txs = this.txs.map(function(tx) { + tx = bcoin.tx(tx); + tx.block = self.hash('hex'); + tx.ts = tx.ts || self.ts; + return tx; + }); + this.hashes = this.txs.map(function(tx) { + return tx.hash('hex'); + }); + this.tx = this.hashes.slice(); + } + this._hash = null; // Verify partial merkle tree and fill `ts` array @@ -64,6 +79,11 @@ Block.prototype.hasTX = function hasTX(hash) { Block.prototype._verifyMerkle = function verifyMerkle() { var height = 0; + if (this.subtype === 'block') { + this.invalid = !utils.testTarget(this.bits, this.hash()); + return; + } + // Count leafs for (var i = this.totalTX; i > 0; i >>= 1) height++; diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 471eafe0..39757250 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -293,6 +293,10 @@ Pool.prototype._addPeer = function _addPeer(backoff) { self.emit('block', block, peer); }); + peer.on('block', function(block) { + self.emit('block', block, peer); + }); + // Just FYI peer.on('reject', function(payload) { self.emit('reject', payload, peer); diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 8238b436..4bceca0f 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -218,6 +218,17 @@ Parser.prototype.parseBlock = function parseBlock(p) { if (p.length < 84) return this._error('Invalid block size'); + var result = readIntv(p, 80); + var off = result.off; + var totalTX = result.r; + var txs = []; + + for (var i = 0; i < totalTX; i++) { + var tx = this.parseTX(p.slice(off)); + off += tx._off; + txs.push(tx); + } + return { version: readU32(p, 0), prevBlock: p.slice(4, 36), @@ -225,7 +236,8 @@ Parser.prototype.parseBlock = function parseBlock(p) { ts: readU32(p, 68), bits: readU32(p, 72), nonce: readU32(p, 76), - totalTX: readU32(p, 80) + totalTX: totalTX, + txs: txs }; }; @@ -316,7 +328,8 @@ Parser.prototype.parseTX = function parseTX(p) { version: readU32(p, 0), inputs: txIn, outputs: txOut, - lock: readU32(p, off) + lock: readU32(p, off), + _off: off + 4 }; };