From d109eaba6cc0d9120880915bbc144215acaf7fe1 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 17 Dec 2015 19:47:20 -0800 Subject: [PATCH] handle reject packet. --- lib/bcoin/pool.js | 2 +- lib/bcoin/protocol/constants.js | 16 ++++++++++++++++ lib/bcoin/protocol/parser.js | 9 +++++++-- lib/bcoin/tx.js | 3 ++- lib/bcoin/wallet.js | 2 +- 5 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index 2f67f49b..c29baf9b 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -318,7 +318,7 @@ Pool.prototype._addPeer = function _addPeer(backoff) { var result = peer.broadcast(entry.tx); if (!result) return; result[0].once('request', function() { - entry.e.emit('ack'); + entry.e.emit('ack', peer); }); }); diff --git a/lib/bcoin/protocol/constants.js b/lib/bcoin/protocol/constants.js index 90af855d..d2531113 100644 --- a/lib/bcoin/protocol/constants.js +++ b/lib/bcoin/protocol/constants.js @@ -156,6 +156,22 @@ exports.script = { maxOps: 201 }; +exports.reject = { + malformed: 0x01, + invalid: 0x10, + obsolete: 0x11, + duplicate: 0x12, + nonstandard: 0x40, + dust: 0x41, + insufficientfee: 0x42, + checkpoint: 0x43 +}; + +exports.rejectByVal = Object.keys(exports.reject).reduce(function(out, name) { + out[exports.reject[name]] = name; + return out; +}, {}); + exports.locktimeThreshold = 500000000; // Tue Nov 5 00:53:20 1985 UTC exports.oneHash = utils.toArray( diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index f201d3cf..160f2d6a 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -409,10 +409,15 @@ Parser.prototype.parseReject = function parseReject(p) { var reason = utils.stringify(p.slice(off, off + reasonLen)); + off += reasonLen; + + var data = p.slice(off, off + 32); + return { message: message, - ccode: ccode, - reason: reason + ccode: constants.rejectByVal[ccode] || ccode, + reason: reason, + data: data }; }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 07721581..a09087e2 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -746,7 +746,6 @@ TX.prototype.utxos = function utxos(unspent) { if (this.funds('in').cmp(total) < 0) { this.inputs = inputs; this.outputs.pop(); - this.total = total; return null; } @@ -770,6 +769,8 @@ TX.prototype.utxos = function utxos(unspent) { TX.prototype.fillUnspent = function fillUnspent(unspent, changeAddress) { var result = unspent.utxos ? unspent : this.utxos(unspent); + this.filled = result; + this.changeAddress = changeAddress || this.changeAddress; if (!result) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 2b3e6b9c..8d5419aa 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -535,7 +535,7 @@ Wallet.prototype.fill = function fill(tx, cb) { var result = tx.fillUnspent(this.unspent(), this.getAddress()); if (!result) { var err = new Error('Not enough funds'); - err.minBalance = tx.total; + err.minBalance = tx.filled.total; cb(err); return null; }