coin.fromTX.

This commit is contained in:
Christopher Jeffrey 2016-05-18 05:12:28 -07:00
parent c6e36353fa
commit b9c7afa0ea
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
8 changed files with 49 additions and 46 deletions

View File

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

View File

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

View File

@ -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);
};
/**

View File

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

View File

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

View File

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

View File

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

View File

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