diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index a989328c..42f97e53 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -948,6 +948,7 @@ TXDB.prototype._add = co(function* add(tx) { TXDB.prototype.insert = co(function* insert(tx) { var hash = tx.hash('hex'); var details = new Details(this, tx); + var updated = false; var i, input, output, coin; var prevout, credit, path, account; @@ -1013,6 +1014,8 @@ TXDB.prototype.insert = co(function* insert(tx) { this.pending.confirmed -= coin.value; this.removeCredit(credit, path); } + + updated = true; } } @@ -1028,8 +1031,10 @@ TXDB.prototype.insert = co(function* insert(tx) { // Attempt to resolve an input we // did not know was ours at the time. - if (yield this.resolveInput(tx, i, path)) + if (yield this.resolveInput(tx, i, path)) { + updated = true; continue; + } credit = Credit.fromTX(tx, i); @@ -1040,6 +1045,16 @@ TXDB.prototype.insert = co(function* insert(tx) { this.pending.confirmed += output.value; this.saveCredit(credit, path); + + updated = true; + } + + // If this didn't update any coins, + // it's not our transaction. + if (!updated) { + // Clear the spent list inserts. + this.clear(); + return; } // Save and index the transaction in bcoin's diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 79f6b7d4..26b463e1 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -1832,10 +1832,18 @@ Wallet.prototype.add = co(function* add(tx) { Wallet.prototype._add = co(function* add(tx) { var resolved = yield this.txdb.resolve(tx); + var result = false; var i; - for (i = 0; i < resolved.length; i++) - yield this._insert(resolved[i]); + if (resolved.length === 0) + return true; + + for (i = 0; i < resolved.length; i++) { + if (yield this._insert(resolved[i])) + result = true; + } + + return result; }); /** diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index bcb3c7c9..bc71cd40 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -1452,7 +1452,6 @@ WalletDB.prototype._addBlock = co(function* addBlock(entry, txs) { // the database back into the correct state. for (i = 0; i < txs.length; i++) { tx = txs[i]; - wallets = yield this._insertTX(tx); if (!wallets) @@ -1577,6 +1576,7 @@ WalletDB.prototype._addTX = co(function* addTX(tx) { */ WalletDB.prototype._insertTX = co(function* insertTX(tx) { + var result = false; var i, wallets, wid, wallet; assert(!tx.mutable, 'Cannot add mutable TX to wallet.'); @@ -1596,11 +1596,15 @@ WalletDB.prototype._insertTX = co(function* insertTX(tx) { assert(wallet); - this.logger.debug('Adding tx to wallet: %s', wallet.id); - - yield wallet.add(tx); + if (yield wallet.add(tx)) { + this.logger.debug('Added transaction to wallet: %s', wallet.id); + result = true; + } } + if (!result) + return; + return wallets; });