From 4b1915ca491ba6cdd9ff154e3afe02c65f4370c6 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 21 Jun 2016 16:54:49 -0700 Subject: [PATCH] funding error. --- lib/bcoin/errors.js | 35 +++++++++++++++++++++++++++++++++-- lib/bcoin/keyring.js | 4 +++- lib/bcoin/mtx.js | 19 ++++++++++++------- lib/bcoin/pool.js | 7 ++++--- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/lib/bcoin/errors.js b/lib/bcoin/errors.js index 4bf49ca1..7b6dd9a3 100644 --- a/lib/bcoin/errors.js +++ b/lib/bcoin/errors.js @@ -16,7 +16,7 @@ var constants = bcoin.protocol.constants; * a mempool transaction validation error or a blockchain * block verification error. Ultimately used to send * `reject` packets to peers. - * @global + * @exports VerifyError * @constructor * @extends Error * @param {Block|TX} msg @@ -64,7 +64,7 @@ utils.inherits(VerifyError, Error); /** * An error thrown from the scripting system, * potentially pertaining to Script execution. - * @global + * @exports ScriptError * @constructor * @extends Error * @param {String} code - Error code. @@ -111,9 +111,40 @@ function ScriptError(code, op, ip) { utils.inherits(ScriptError, Error); +/** + * An error thrown from the coin selector. + * @exports FundingError + * @constructor + * @extends Error + * @param {String} msg + * @param {Amount} available + * @param {Amount} required + * @property {String} message - Error message. + * @property {Amount} availableFunds + * @property {Amount} requiredFunds + */ + +function FundingError(msg, available, required) { + Error.call(this); + + if (Error.captureStackTrace) + Error.captureStackTrace(this, FundingError); + + msg += ' (available=' + utils.btc(available) + ','; + msg += ' required=' + utils.btc(required) + ')'; + + this.type = 'FundingError'; + this.message = msg; + this.availableFunds = available; + this.requiredFunds = required; +} + +utils.inherits(FundingError, Error); + /* * Expose */ exports.VerifyError = VerifyError; exports.ScriptError = ScriptError; +exports.FundingError = FundingError; diff --git a/lib/bcoin/keyring.js b/lib/bcoin/keyring.js index e7bee97a..d75726f6 100644 --- a/lib/bcoin/keyring.js +++ b/lib/bcoin/keyring.js @@ -540,7 +540,9 @@ KeyRing.prototype.toJSON = function toJSON() { index: this.index, key: utils.toBase58(this.key), keys: this.keys.map(utils.toBase58), - address: this.getAddress() + keyAddress: this.getKeyAddress(), + scriptAddress: this.getScriptAddress(), + programAddress: this.getProgramAddress() }; }; diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index c7ed0d13..6a27f5b7 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -14,6 +14,7 @@ var constants = bcoin.protocol.constants; var Script = bcoin.script; var opcodes = constants.opcodes; var HASH160 = constants.ZERO_HASH.slice(0, 20); +var FundingError = bcoin.errors.FundingError; /** * A mutable transaction object. @@ -964,7 +965,7 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { var index = 0; var tx = this.clone(); var outputValue = tx.getOutputValue(); - var tryFree, i, size, change, fee, min, output, err; + var tryFree, i, size, change, fee, min, output; if (!options) options = {}; @@ -1030,6 +1031,9 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { if (options.fee != null) { fee = options.fee; + if (fee > constants.tx.MAX_FEE) + fee = constants.tx.MAX_FEE; + // Transfer `total` funds maximum. addCoins(); } else { @@ -1072,6 +1076,9 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { else fee = tx.getMinFee(size, options.rate); + if (fee > constants.tx.MAX_FEE) + fee = constants.tx.MAX_FEE; + // Failed to get enough funds, add more coins. if (!isFull()) addCoins(); @@ -1080,14 +1087,12 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) { if (!isFull()) { // Still failing to get enough funds. - err = new Error('Could not select coins.'); - err.requiredFunds = total(); - throw err; + throw new FundingError( + 'Not enough funds.', + tx.getInputValue(), + total()); } - if (fee > constants.tx.MAX_FEE) - fee = constants.tx.MAX_FEE; - // How much money is left after filling outputs. change = tx.getInputValue() - total(); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 03d31dab..b65a1f51 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -1188,12 +1188,12 @@ Pool.prototype._handleTX = function _handleTX(tx, peer, callback) { } } - if (!self.mempool) { - self.emit('tx', tx, peer); + if (!this.mempool) { + this.emit('tx', tx, peer); return callback(); } - self.mempool.addTX(tx, function(err) { + this.mempool.addTX(tx, function(err) { if (err) { if (err.type === 'VerifyError') { if (err.score !== -1) @@ -1201,6 +1201,7 @@ Pool.prototype._handleTX = function _handleTX(tx, peer, callback) { self.rejects.add(tx.hash()); return callback(err); } + return callback(err); } self.emit('tx', tx, peer);