tx: some misc cleanup.
This commit is contained in:
parent
94460ba592
commit
5ee83e9114
@ -84,7 +84,7 @@ MempoolEntry.fromOptions = function fromOptions(options) {
|
|||||||
MempoolEntry.prototype.fromTX = function fromTX(tx, view, height) {
|
MempoolEntry.prototype.fromTX = function fromTX(tx, view, height) {
|
||||||
var flags = Script.flags.STANDARD_VERIFY_FLAGS;
|
var flags = Script.flags.STANDARD_VERIFY_FLAGS;
|
||||||
var priority = tx.getPriority(view, height);
|
var priority = tx.getPriority(view, height);
|
||||||
var value = tx.getChainValue(view, height);
|
var value = tx.getChainValue(view);
|
||||||
var sigops = tx.getSigopsCost(view, flags);
|
var sigops = tx.getSigopsCost(view, flags);
|
||||||
var dependencies = false;
|
var dependencies = false;
|
||||||
var size = tx.getVirtualSize();
|
var size = tx.getVirtualSize();
|
||||||
|
|||||||
@ -800,14 +800,13 @@ MTX.prototype.signature = function signature(index, prev, value, key, type, vers
|
|||||||
if (type == null)
|
if (type == null)
|
||||||
type = Script.hashType.ALL;
|
type = Script.hashType.ALL;
|
||||||
|
|
||||||
if (typeof type === 'string')
|
if (typeof type === 'string') {
|
||||||
type = Script.hashType[type.toUpperCase()];
|
type = Script.hashType[type.toUpperCase()];
|
||||||
|
assert(type != null, 'Unknown sighash type.');
|
||||||
|
}
|
||||||
|
|
||||||
// Get the hash of the current tx, minus the other
|
|
||||||
// inputs, plus the sighash type.
|
|
||||||
hash = this.signatureHash(index, prev, value, type, version);
|
hash = this.signatureHash(index, prev, value, type, version);
|
||||||
|
|
||||||
// Sign the transaction with our one input
|
|
||||||
return Script.sign(hash, key, type);
|
return Script.sign(hash, key, type);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1508,8 +1507,15 @@ CoinSelector.prototype.fromOptions = function fromOptions(options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options.subtractFee != null) {
|
if (options.subtractFee != null) {
|
||||||
this.subtractFee = options.subtractFee;
|
if (typeof options.subtractFee === 'number') {
|
||||||
this.shouldSubtract = options.subtractFee !== false;
|
assert(util.isNumber(options.subtractFee));
|
||||||
|
this.subtractFee = options.subtractFee;
|
||||||
|
this.shouldSubtract = true;
|
||||||
|
} else {
|
||||||
|
assert(typeof options.subtractFee === 'boolean');
|
||||||
|
this.subtractFee = options.subtractFee;
|
||||||
|
this.shouldSubtract = options.subtractFee;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.height != null) {
|
if (options.height != null) {
|
||||||
|
|||||||
@ -40,8 +40,7 @@ var BAD_NONSTD_P2WSH = 3;
|
|||||||
* A static transaction object.
|
* A static transaction object.
|
||||||
* @exports TX
|
* @exports TX
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param {NakedTX} options - Transaction fields.
|
* @param {Object} options - Transaction fields.
|
||||||
* @property {String} type - "tx" (inv type).
|
|
||||||
* @property {Number} version - Transaction version. Note that BCoin reads
|
* @property {Number} version - Transaction version. Note that BCoin reads
|
||||||
* versions as unsigned even though they are signed at the protocol level.
|
* versions as unsigned even though they are signed at the protocol level.
|
||||||
* This value will never be negative.
|
* This value will never be negative.
|
||||||
@ -446,8 +445,10 @@ TX.prototype.hasWitness = function hasWitness() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
TX.prototype.signatureHash = function signatureHash(index, prev, value, type, version) {
|
TX.prototype.signatureHash = function signatureHash(index, prev, value, type, version) {
|
||||||
if (typeof type === 'string')
|
if (typeof type === 'string') {
|
||||||
type = Script.hashType[type.toUpperCase()];
|
type = Script.hashType[type.toUpperCase()];
|
||||||
|
assert(type != null, 'Unknown sighash type.');
|
||||||
|
}
|
||||||
|
|
||||||
assert(index >= 0 && index < this.inputs.length);
|
assert(index >= 0 && index < this.inputs.length);
|
||||||
assert(prev instanceof Script);
|
assert(prev instanceof Script);
|
||||||
@ -1698,7 +1699,7 @@ TX.prototype.getWitnessStandard = function getWitnessStandard(view) {
|
|||||||
ret = BAD_NONSTD_P2WSH;
|
ret = BAD_NONSTD_P2WSH;
|
||||||
}
|
}
|
||||||
|
|
||||||
redeem = Script.fromRaw(redeem);
|
redeem = new Script(redeem);
|
||||||
|
|
||||||
if (redeem.isPubkey()) {
|
if (redeem.isPubkey()) {
|
||||||
if (input.witness.length - 1 !== 1)
|
if (input.witness.length - 1 !== 1)
|
||||||
@ -1769,7 +1770,6 @@ TX.prototype.checkInputs = function checkInputs(view, height, ret) {
|
|||||||
coins = view.get(input.prevout.hash);
|
coins = view.get(input.prevout.hash);
|
||||||
|
|
||||||
if (!coins) {
|
if (!coins) {
|
||||||
// Note: don't trigger dos score here.
|
|
||||||
ret.reason = 'bad-txns-inputs-missingorspent';
|
ret.reason = 'bad-txns-inputs-missingorspent';
|
||||||
ret.score = 0;
|
ret.score = 0;
|
||||||
return false;
|
return false;
|
||||||
@ -1783,7 +1783,13 @@ TX.prototype.checkInputs = function checkInputs(view, height, ret) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
coin = view.getOutput(input);
|
coin = coins.getOutput(input.prevout.index);
|
||||||
|
|
||||||
|
if (!coin) {
|
||||||
|
ret.reason = 'bad-txns-inputs-missingorspent';
|
||||||
|
ret.score = 0;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (coin.value < 0 || coin.value > consensus.MAX_MONEY) {
|
if (coin.value < 0 || coin.value > consensus.MAX_MONEY) {
|
||||||
ret.reason = 'bad-txns-inputvalues-outofrange';
|
ret.reason = 'bad-txns-inputvalues-outofrange';
|
||||||
@ -1898,22 +1904,16 @@ TX.prototype.getPriority = function getPriority(view, height, size) {
|
|||||||
/**
|
/**
|
||||||
* Calculate the transaction's on-chain value.
|
* Calculate the transaction's on-chain value.
|
||||||
* @param {CoinView} view
|
* @param {CoinView} view
|
||||||
* @param {Number} height
|
|
||||||
* @returns {Number}
|
* @returns {Number}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
TX.prototype.getChainValue = function getChainValue(view, height) {
|
TX.prototype.getChainValue = function getChainValue(view) {
|
||||||
var value = 0;
|
var value = 0;
|
||||||
var i, input, coin, coinHeight;
|
var i, input, coin, coinHeight;
|
||||||
|
|
||||||
assert(typeof height === 'number', 'Must pass in height.');
|
|
||||||
|
|
||||||
if (this.isCoinbase())
|
if (this.isCoinbase())
|
||||||
return value;
|
return value;
|
||||||
|
|
||||||
if (height == null)
|
|
||||||
height = Infinity;
|
|
||||||
|
|
||||||
for (i = 0; i < this.inputs.length; i++) {
|
for (i = 0; i < this.inputs.length; i++) {
|
||||||
input = this.inputs[i];
|
input = this.inputs[i];
|
||||||
coin = view.getOutput(input);
|
coin = view.getOutput(input);
|
||||||
@ -1926,8 +1926,7 @@ TX.prototype.getChainValue = function getChainValue(view, height) {
|
|||||||
if (coinHeight === -1)
|
if (coinHeight === -1)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (coinHeight <= height)
|
value += coin.value;
|
||||||
value += coin.value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user