diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 95a91542..e62473b1 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -47,18 +47,14 @@ function Block(data) { bcoin.abstractblock.call(this, data); this.type = 'block'; - this.txs = data.txs || []; + this.txs = []; this._cbHeight = null; this._commitmentHash = null; this._raw = null; - for (i = 0; i < this.txs.length; i++) { - tx = this.txs[i]; - - if (tx instanceof bcoin.tx) - continue; - - this.txs[i] = new bcoin.tx(tx, this, i); + if (data.txs) { + for (i = 0; i < data.txs.length; i++) + this.addTX(data.txs[i]); } } @@ -208,6 +204,26 @@ Block.prototype.hasWitness = function hasWitness() { return false; }; +/** + * Add a transaction to the block's tx vector. + * @param {TX|NakedTX} tx + * @returns {TX} + */ + +Block.prototype.addTX = function addTX(tx) { + var index; + + if (!(tx instanceof bcoin.tx)) + tx = new bcoin.tx(tx); + + index = this.txs.push(tx) - 1; + + if (!this.mutable) + tx.setBlock(this, index); + + return tx; +}; + /** * Test the block's transaction vector against a hash. * @param {Hash|TX} hash diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index 5da01172..47e16c12 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -92,6 +92,23 @@ MerkleBlock.prototype.getRaw = function getRaw() { return this._raw; }; +/** + * Add a transaction to the block's tx vector. + * @param {TX|NakedTX} tx + * @returns {TX} + */ + +MerkleBlock.prototype.addTX = function addTX(tx) { + if (!(tx instanceof bcoin.tx)) + tx = new bcoin.tx(tx); + + this.txs.push(tx); + + tx.setBlock(this, -1); + + return tx; +}; + /** * Test the block's _matched_ transaction vector against a hash. * @param {Hash|TX} hash diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index ec40b199..c5dc7ed6 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -407,7 +407,7 @@ function MinerBlock(options) { height: this.height }); - this.block.txs.push(this.coinbase); + this.block.addTX(this.coinbase); if (options.witness) { // Set up the witness nonce and @@ -489,7 +489,7 @@ MinerBlock.prototype.addTX = function addTX(tx) { return false; // Add the tx to our block - this.block.txs.push(tx); + this.block.addTX(tx); // Update coinbase value this.updateCoinbase(); diff --git a/lib/bcoin/peer.js b/lib/bcoin/peer.js index 96123ae9..683470af 100644 --- a/lib/bcoin/peer.js +++ b/lib/bcoin/peer.js @@ -581,18 +581,18 @@ Peer.prototype._onPacket = function onPacket(packet) { case 'filterclear': return this._handleFilterClear(payload); case 'block': - payload = bcoin.compactblock(payload); + payload = new bcoin.compactblock(payload); this._emit(cmd, payload); break; case 'merkleblock': - payload = bcoin.merkleblock(payload); + payload = new bcoin.merkleblock(payload); this.lastBlock = payload; break; case 'tx': - payload = bcoin.tx(payload, this.lastBlock); + payload = new bcoin.tx(payload); if (this.lastBlock) { - if (payload.block) { - this.lastBlock.txs.push(payload); + if (this.lastBlock.hasTX(payload)) { + this.lastBlock.addTX(payload); break; } this._emitMerkle(); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index a04505c0..b1a66310 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -20,9 +20,6 @@ var BufferWriter = require('./writer'); * @exports TX * @constructor * @param {NakedTX} data - Transaction fields. - * @param {(Block|MerkleBlock)?} block - Block the transaction was included in. - * @param {Number?} index - The index of the transaction in the block's - * transaction vector. * @property {String} type - "tx" (inv type). * @property {Number} version - Transaction version. Note that BCoin reads * versions as unsigned even though they are signed at the protocol level. @@ -51,11 +48,11 @@ var BufferWriter = require('./writer'); * witness is present. All zeroes if coinbase). */ -function TX(data, block, index) { +function TX(data) { var i; if (!(this instanceof TX)) - return new TX(data, block, index); + return new TX(data); assert(data, 'TX data is required.'); assert(typeof data.version === 'number'); @@ -92,15 +89,6 @@ function TX(data, block, index) { for (i = 0; i < data.outputs.length; i++) this.outputs.push(new bcoin.output(data.outputs[i])); - - if (block && this.ts === 0) { - if (block.type === 'merkleblock') { - if (block.hasTX(this)) - this.setBlock(block, index); - } else { - this.setBlock(block, index); - } - } } /** @@ -164,7 +152,7 @@ TX.prototype.setBlock = function setBlock(block, index) { this.ts = block.ts; this.block = block.hash('hex'); this.height = block.height; - this.index = index; + this.index = index == null ? -1 : index; this.ps = 0; };