block.addTX.

This commit is contained in:
Christopher Jeffrey 2016-05-18 05:00:15 -07:00
parent 070af4643f
commit c6e36353fa
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
5 changed files with 51 additions and 30 deletions

View File

@ -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

View File

@ -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

View File

@ -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();

View File

@ -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();

View File

@ -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;
};