mtx/wallet: more sanity checks for sending.
This commit is contained in:
parent
ff28b7c4fb
commit
595ada8c86
@ -138,13 +138,17 @@ function FundingError(msg, available, required) {
|
|||||||
if (Error.captureStackTrace)
|
if (Error.captureStackTrace)
|
||||||
Error.captureStackTrace(this, FundingError);
|
Error.captureStackTrace(this, FundingError);
|
||||||
|
|
||||||
msg += ' (available=' + Amount.btc(available) + ',';
|
|
||||||
msg += ' required=' + Amount.btc(required) + ')';
|
|
||||||
|
|
||||||
this.type = 'FundingError';
|
this.type = 'FundingError';
|
||||||
this.message = msg;
|
this.message = msg;
|
||||||
this.availableFunds = available;
|
this.availableFunds = -1;
|
||||||
this.requiredFunds = required;
|
this.requiredFunds = -1;
|
||||||
|
|
||||||
|
if (available != null) {
|
||||||
|
this.message += ' (available=' + Amount.btc(available) + ',';
|
||||||
|
this.message += ' required=' + Amount.btc(required) + ')';
|
||||||
|
this.availableFunds = available;
|
||||||
|
this.requiredFunds = required;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
util.inherits(FundingError, Error);
|
util.inherits(FundingError, Error);
|
||||||
|
|||||||
@ -293,6 +293,16 @@ MTX.prototype.getSigops = function getSigops(flags) {
|
|||||||
return TX.prototype.getSigops.call(this, this.view, flags);
|
return TX.prototype.getSigops.call(this, this.view, flags);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate sigops weight, taking into account witness programs.
|
||||||
|
* @param {VerifyFlags?} flags
|
||||||
|
* @returns {Number} sigop weight
|
||||||
|
*/
|
||||||
|
|
||||||
|
MTX.prototype.getSigopsCost = function getSigopsCost(flags) {
|
||||||
|
return TX.prototype.getSigopsCost.call(this, this.view, flags);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform contextual checks to verify input, output,
|
* Perform contextual checks to verify input, output,
|
||||||
* and fee values, as well as coinbase spend maturity
|
* and fee values, as well as coinbase spend maturity
|
||||||
@ -1697,12 +1707,8 @@ CoinSelector.prototype.selectEstimate = co(function* selectEstimate(fee) {
|
|||||||
|
|
||||||
this.fee = this.getFee(size);
|
this.fee = this.getFee(size);
|
||||||
|
|
||||||
if (this.maxFee > 0 && this.fee > this.maxFee) {
|
if (this.maxFee > 0 && this.fee > this.maxFee)
|
||||||
throw new FundingError(
|
throw new FundingError('Fee is too high.');
|
||||||
'Fee is too high.',
|
|
||||||
this.tx.getInputValue(),
|
|
||||||
this.total());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Failed to get enough funds, add more coins.
|
// Failed to get enough funds, add more coins.
|
||||||
if (!this.isFull())
|
if (!this.isFull())
|
||||||
|
|||||||
@ -1622,16 +1622,24 @@ Wallet.prototype.send = co(function* send(options, passphrase) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Wallet.prototype._send = co(function* send(options, passphrase) {
|
Wallet.prototype._send = co(function* send(options, passphrase) {
|
||||||
var tx = yield this.createTX(options, true);
|
var mtx = yield this.createTX(options, true);
|
||||||
|
var tx;
|
||||||
|
|
||||||
yield this.sign(tx, passphrase);
|
yield this.sign(mtx, passphrase);
|
||||||
|
|
||||||
if (!tx.isSigned())
|
if (!mtx.isSigned())
|
||||||
throw new Error('TX could not be fully signed.');
|
throw new Error('TX could not be fully signed.');
|
||||||
|
|
||||||
assert(tx.getFee() <= constants.tx.MAX_FEE, 'TX exceeds maxfee.');
|
assert(mtx.getFee() <= constants.tx.MAX_FEE, 'TX exceeds maxfee.');
|
||||||
|
|
||||||
tx = tx.toTX();
|
tx = mtx.toTX();
|
||||||
|
|
||||||
|
// Sanity checks.
|
||||||
|
if (tx.getSigopsCost(mtx.view) > constants.tx.MAX_SIGOPS_COST)
|
||||||
|
throw new Error('TX exceeds policy sigops.');
|
||||||
|
|
||||||
|
if (tx.getWeight() > constants.tx.MAX_WEIGHT)
|
||||||
|
throw new Error('TX exceeds policy weight.');
|
||||||
|
|
||||||
yield this.db.addTX(tx);
|
yield this.db.addTX(tx);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user