From 9ed439b60c9ae07c01c9a8ec2b9a324403a8f887 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 18 Oct 2016 07:03:35 -0700 Subject: [PATCH] txdb: reject rbf. --- lib/wallet/browser.js | 3 +++ lib/wallet/txdb.js | 42 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/lib/wallet/browser.js b/lib/wallet/browser.js index c7d1935c..bbd85fae 100644 --- a/lib/wallet/browser.js +++ b/lib/wallet/browser.js @@ -176,6 +176,9 @@ layout.txdb = { }, Cc: function Cc(key) { return this.ihii(key); + }, + r: function r(hash) { + return this.ha('r', hash); } }; diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 1ff036d0..380fee7d 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -189,6 +189,9 @@ var layout = { }, Cc: function Cc(key) { return layout.ihii(key); + }, + r: function r(hash) { + return layout.ha(0x72, hash); } }; @@ -688,18 +691,30 @@ TXDB.prototype.add = co(function* add(tx) { */ TXDB.prototype._add = co(function* add(tx, info) { - var hash, path, account; + var hash = tx.hash('hex'); + var path, account; var i, result, input, output, coin; var prevout, key, spender, raw; assert(!tx.mutable, 'Cannot add mutable TX to wallet.'); if (tx.height === -1) { + // We ignore double spends from the mempool. if (yield this.isDoubleSpend(tx)) return false; + + // We ignore any unconfirmed txs + // that are replace-by-fee. + if (yield this.isRBF(tx)) { + this.put(layout.r(hash), DUMMY); + return false; + } } else { // This potentially removes double-spenders. yield this.removeConflicts(tx, info); + + // Delete the replace-by-fee record. + this.del(layout.r(hash)); } // Attempt to confirm tx before adding it. @@ -709,8 +724,6 @@ TXDB.prototype._add = co(function* add(tx, info) { if (result) return true; - hash = tx.hash('hex'); - this.put(layout.t(hash), tx.toExtended()); if (tx.height === -1) @@ -903,6 +916,29 @@ TXDB.prototype.isDoubleSpend = co(function* isDoubleSpend(tx) { return false; }); +/** + * Test an entire transaction to see + * if any of its outpoints are replace by fee. + * @param {TX} tx + * @returns {Promise} - Returns Boolean. + */ + +TXDB.prototype.isRBF = co(function* isRBF(tx) { + var i, input, prevout; + + if (tx.isRBF()) + return true; + + for (i = 0; i < tx.inputs.length; i++) { + input = tx.inputs[i]; + prevout = input.prevout; + if (yield this.has(layout.r(prevout.hash))) + return true; + } + + return false; +}); + /** * Test a whether a coin has been spent. * @param {Hash} hash