From b9c7afa0ea0965900452962df6ef934f697410ca Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 18 May 2016 05:12:28 -0700 Subject: [PATCH] coin.fromTX. --- lib/bcoin/chaindb.js | 6 ++--- lib/bcoin/coin.js | 62 +++++++++++++++++++++++--------------------- lib/bcoin/coins.js | 2 +- lib/bcoin/mempool.js | 4 +-- lib/bcoin/mtx.js | 4 +-- lib/bcoin/tx.js | 9 ++++--- lib/bcoin/txdb.js | 6 ++--- test/script-test.js | 2 +- 8 files changed, 49 insertions(+), 46 deletions(-) diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index cf66fc57..8183de9f 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -838,7 +838,7 @@ ChainDB.prototype.connectBlock = function connectBlock(block, batch, callback) { if (output.script.isUnspendable()) continue; - coin = bcoin.coin(tx, j); + coin = bcoin.coin.fromTX(tx, j); if (self.options.indexAddress) { address = output.getHash(); @@ -1021,7 +1021,7 @@ ChainDB.prototype.fillHistory = function fillHistory(tx, callback) { return next(err); if (tx) - input.coin = bcoin.coin(tx, input.prevout.index); + input.coin = bcoin.coin.fromTX(tx, input.prevout.index); next(); }); @@ -1287,7 +1287,7 @@ ChainDB.prototype.fillBlock = function fillBlock(block, callback) { } for (j = 0; j < tx.outputs.length; j++) - coins[hash + '/' + j] = bcoin.coin(tx, j); + coins[hash + '/' + j] = bcoin.coin.fromTX(tx, j); } return callback(null, block); diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index ced0ed9a..e1974c0f 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -15,11 +15,7 @@ var assert = utils.assert; * @exports Coin * @constructor * @extends Output - * @example - * new Coin(tx, i); - * new Coin(options); - * @param {TX|NakedCoin|Coin} tx/options - TX or options object. - * @param {Number?} index - Output index. + * @param {NakedCoin|Coin} options * @property {Number} version - Transaction version. * @property {Number} height - Transaction height (-1 if unconfirmed). * @property {Amount} value - Output value in satoshis. @@ -30,35 +26,22 @@ var assert = utils.assert; * @property {Number} index - Output index. */ -function Coin(tx, index) { - var options; - - if (tx instanceof Coin) - return tx; +function Coin(options) { + if (options instanceof Coin) + return options; if (!(this instanceof Coin)) - return new Coin(tx, index); + return new Coin(options); - assert(tx, 'Coin data is required.'); + assert(options, 'Coin data is required.'); - if (tx instanceof bcoin.tx) { - this.version = tx.version; - this.height = tx.height; - this.value = tx.outputs[index].value; - this.script = bcoin.script(tx.outputs[index].script, false); - this.coinbase = tx.isCoinbase(); - this.hash = tx.hash('hex'); - this.index = index; - } else { - options = tx; - this.version = options.version; - this.height = options.height; - this.value = options.value; - this.script = bcoin.script(options.script, false); - this.coinbase = options.coinbase; - this.hash = options.hash; - this.index = options.index; - } + this.version = options.version; + this.height = options.height; + this.value = options.value; + this.script = bcoin.script(options.script, false); + this.coinbase = options.coinbase; + this.hash = options.hash; + this.index = options.index; assert(typeof this.version === 'number'); assert(utils.isNumber(this.height)); @@ -267,6 +250,25 @@ Coin.fromExtended = function fromExtended(data, enc) { return new Coin(Coin.parseExtended(data, enc)); }; +/** + * Instantiate a coin from a TX + * @param {TX} tx + * @param {Number} index - Output index. + * @returns {Coin} + */ + +Coin.fromTX = function fromTX(tx, index) { + return new Coin({ + version: tx.version, + height: tx.height, + value: tx.outputs[index].value, + script: tx.outputs[index].script, + coinbase: tx.isCoinbase(), + hash: tx.hash('hex'), + index: index + }); +}; + /** * Test an object to see if it is a Coin. * @param {Object} obj diff --git a/lib/bcoin/coins.js b/lib/bcoin/coins.js index 4f55b42e..98d09438 100644 --- a/lib/bcoin/coins.js +++ b/lib/bcoin/coins.js @@ -72,7 +72,7 @@ Coins.prototype.add = function add(tx, i) { return; } - this.outputs[i] = new bcoin.coin(tx, i); + this.outputs[i] = new bcoin.coin.fromTX(tx, i); }; /** diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index b0eb00cb..1cd684d0 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -510,7 +510,7 @@ Mempool.prototype.fillHistory = function fillHistory(tx, callback) { return next(err); if (tx) - input.coin = bcoin.coin(tx, input.prevout.index); + input.coin = bcoin.coin.fromTX(tx, input.prevout.index); next(); }); @@ -1549,7 +1549,7 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry, callback) { if (output.script.isUnspendable()) continue; - coin = bcoin.coin(tx, i).toRaw(); + coin = bcoin.coin.fromTX(tx, i).toRaw(); batch.put('c/' + key, coin); diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index afa2efe3..dd1b644b 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -121,7 +121,7 @@ MTX.prototype.clone = function clone() { * tx.addInput({ prevout: { hash: ... }, sequence: ... }); * tx.addInput(prev, prevIndex); * tx.addInput(coin); - * tx.addInput(bcoin.coin(prev, prevIndex)); + * tx.addInput(bcoin.coin.fromTX(prev, prevIndex)); * @param {Object|TX|Coin} options - Options object, transaction, or coin. * @param {Number?} index - Input of output if `options` is a TX. */ @@ -130,7 +130,7 @@ MTX.prototype.addInput = function addInput(options, index) { var input; if (options instanceof bcoin.tx) - options = bcoin.coin(options, index); + options = bcoin.coin.fromTX(options, index); if (options instanceof bcoin.coin) { assert(typeof options.hash === 'string'); diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index b1a66310..20cf72aa 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -923,7 +923,7 @@ TX.prototype.hasCoins = function hasCoins() { TX.prototype.fillCoins = function fillCoins(coins) { var total = 0; - var inputs, txs, key, i, input; + var inputs, txs, key, i, input, prevout; if ((coins instanceof bcoin.coin) || (coins instanceof bcoin.tx)) coins = [coins]; @@ -945,12 +945,13 @@ TX.prototype.fillCoins = function fillCoins(coins) { for (i = 0; i < this.inputs.length; i++) { input = this.inputs[i]; + prevout = input.prevout; if (!input.coin) { - if (coins[input.prevout.hash]) { - input.coin = bcoin.coin(coins[input.prevout.hash], input.prevout.index); + if (coins[prevout.hash]) { + input.coin = bcoin.coin.fromTX(coins[prevout.hash], prevout.index); } else { - key = input.prevout.hash + '/' + input.prevout.index; + key = prevout.hash + '/' + prevout.index; if (coins[key]) input.coin = coins[key]; } diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 72d8ab37..7d6d5181 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -394,7 +394,7 @@ TXDB.prototype._add = function add(tx, map, callback, force) { if (!prev) return callback(new Error('Could not find double-spent coin.')); - input.coin = bcoin.coin(prev, input.prevout.index); + input.coin = bcoin.coin.fromTX(prev, input.prevout.index); // Skip invalid transactions if (self.options.verify) { @@ -448,7 +448,7 @@ TXDB.prototype._add = function add(tx, map, callback, force) { if (output.script.isUnspendable()) return next(); - coin = bcoin.coin(tx, i); + coin = bcoin.coin.fromTX(tx, i); self._getOrphans(key, function(err, orphans) { var some = false; @@ -1503,7 +1503,7 @@ TXDB.prototype.fillHistory = function fillHistory(tx, callback) { return next(err); if (tx) - input.coin = bcoin.coin(tx, input.prevout.index); + input.coin = bcoin.coin.fromTX(tx, input.prevout.index); next(); }); diff --git a/test/script-test.js b/test/script-test.js index 85403d93..f92bff08 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -320,7 +320,7 @@ describe('Script', function() { hash: coin.hash('hex'), index: 0 }, - coin: bcoin.coin(coin, 0), + coin: bcoin.coin.fromTX(coin, 0), script: input, witness: witness, sequence: 0xffffffff