txdb: fix removeConflict.

This commit is contained in:
Christopher Jeffrey 2016-10-23 15:14:39 -07:00
parent 8896ff9665
commit e9fc3c816a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -1042,11 +1042,11 @@ TXDB.prototype._add = co(function* add(tx, block) {
if (!(yield this.removeConflicts(tx, true))) if (!(yield this.removeConflicts(tx, true)))
return; return;
} else { } else {
// Delete the replace-by-fee record.
this.del(layout.r(hash));
// Potentially remove double-spenders. // Potentially remove double-spenders.
yield this.removeConflicts(tx, false); yield this.removeConflicts(tx, false);
// Delete the replace-by-fee record.
this.del(layout.r(hash));
} }
// Finally we can do a regular insertion. // Finally we can do a regular insertion.
@ -1712,13 +1712,10 @@ TXDB.prototype.disconnect = co(function* disconnect(tx, block) {
* @returns {Promise} - Returns Boolean. * @returns {Promise} - Returns Boolean.
*/ */
TXDB.prototype.removeConflict = co(function* removeConflict(hash, ref) { TXDB.prototype.removeConflict = co(function* removeConflict(tx) {
var tx = yield this.getTX(hash);
var details; var details;
assert(tx); this.logger.warning('Handling conflicting tx: %s.', tx.rhash);
this.logger.warning('Handling conflicting tx: %s.', utils.revHex(hash));
this.drop(); this.drop();
@ -1745,7 +1742,7 @@ TXDB.prototype.removeConflict = co(function* removeConflict(hash, ref) {
TXDB.prototype.removeConflicts = co(function* removeConflicts(tx, conf) { TXDB.prototype.removeConflicts = co(function* removeConflicts(tx, conf) {
var hash = tx.hash('hex'); var hash = tx.hash('hex');
var spends = []; var spends = [];
var i, input, prevout, spent; var i, input, prevout, spent, spender;
if (tx.isCoinbase()) if (tx.isCoinbase())
return true; return true;
@ -1765,23 +1762,26 @@ TXDB.prototype.removeConflicts = co(function* removeConflicts(tx, conf) {
if (spent.hash === hash) if (spent.hash === hash)
continue; continue;
if (conf && tx.height !== -1) spender = yield this.getTX(spent.hash);
assert(spender);
if (conf && spender.height !== -1)
return false; return false;
spends[i] = spent; spends[i] = spender;
} }
// Once we know we're not going to // Once we know we're not going to
// screw things up, remove the double // screw things up, remove the double
// spenders. // spenders.
for (i = 0; i < tx.inputs.length; i++) { for (i = 0; i < tx.inputs.length; i++) {
spent = spends[i]; spender = spends[i];
if (!spent) if (!spender)
continue; continue;
// Remove the double spender. // Remove the double spender.
yield this.removeConflict(spent.hash, tx); yield this.removeConflict(spender);
} }
return true; return true;