walletdb: do not insert txs redeeming from relevant txs.

This commit is contained in:
Christopher Jeffrey 2016-10-20 23:49:08 -07:00
parent 9e0542dba1
commit 919f87f9d0
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 34 additions and 7 deletions

View File

@ -948,6 +948,7 @@ TXDB.prototype._add = co(function* add(tx) {
TXDB.prototype.insert = co(function* insert(tx) { TXDB.prototype.insert = co(function* insert(tx) {
var hash = tx.hash('hex'); var hash = tx.hash('hex');
var details = new Details(this, tx); var details = new Details(this, tx);
var updated = false;
var i, input, output, coin; var i, input, output, coin;
var prevout, credit, path, account; var prevout, credit, path, account;
@ -1013,6 +1014,8 @@ TXDB.prototype.insert = co(function* insert(tx) {
this.pending.confirmed -= coin.value; this.pending.confirmed -= coin.value;
this.removeCredit(credit, path); this.removeCredit(credit, path);
} }
updated = true;
} }
} }
@ -1028,8 +1031,10 @@ TXDB.prototype.insert = co(function* insert(tx) {
// Attempt to resolve an input we // Attempt to resolve an input we
// did not know was ours at the time. // 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; continue;
}
credit = Credit.fromTX(tx, i); credit = Credit.fromTX(tx, i);
@ -1040,6 +1045,16 @@ TXDB.prototype.insert = co(function* insert(tx) {
this.pending.confirmed += output.value; this.pending.confirmed += output.value;
this.saveCredit(credit, path); 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 // Save and index the transaction in bcoin's

View File

@ -1832,10 +1832,18 @@ Wallet.prototype.add = co(function* add(tx) {
Wallet.prototype._add = co(function* add(tx) { Wallet.prototype._add = co(function* add(tx) {
var resolved = yield this.txdb.resolve(tx); var resolved = yield this.txdb.resolve(tx);
var result = false;
var i; var i;
for (i = 0; i < resolved.length; i++) if (resolved.length === 0)
yield this._insert(resolved[i]); return true;
for (i = 0; i < resolved.length; i++) {
if (yield this._insert(resolved[i]))
result = true;
}
return result;
}); });
/** /**

View File

@ -1452,7 +1452,6 @@ WalletDB.prototype._addBlock = co(function* addBlock(entry, txs) {
// the database back into the correct state. // the database back into the correct state.
for (i = 0; i < txs.length; i++) { for (i = 0; i < txs.length; i++) {
tx = txs[i]; tx = txs[i];
wallets = yield this._insertTX(tx); wallets = yield this._insertTX(tx);
if (!wallets) if (!wallets)
@ -1577,6 +1576,7 @@ WalletDB.prototype._addTX = co(function* addTX(tx) {
*/ */
WalletDB.prototype._insertTX = co(function* insertTX(tx) { WalletDB.prototype._insertTX = co(function* insertTX(tx) {
var result = false;
var i, wallets, wid, wallet; var i, wallets, wid, wallet;
assert(!tx.mutable, 'Cannot add mutable TX to wallet.'); assert(!tx.mutable, 'Cannot add mutable TX to wallet.');
@ -1596,11 +1596,15 @@ WalletDB.prototype._insertTX = co(function* insertTX(tx) {
assert(wallet); assert(wallet);
this.logger.debug('Adding tx to wallet: %s', wallet.id); if (yield wallet.add(tx)) {
this.logger.debug('Added transaction to wallet: %s', wallet.id);
yield wallet.add(tx); result = true;
}
} }
if (!result)
return;
return wallets; return wallets;
}); });