mempool: cleanup. more sync methods.

This commit is contained in:
Christopher Jeffrey 2016-08-16 10:54:33 -07:00
parent e517a7a9f7
commit 82ca244669
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 134 additions and 200 deletions

View File

@ -1597,20 +1597,7 @@ RPC.prototype.prioritisetransaction = function prioritisetransaction(args, callb
if (entry.fees < 0) if (entry.fees < 0)
entry.fees = 0; entry.fees = 0;
this.mempool.fillAllCoins(entry.tx, function(err) { return callback(null, true);
if (err)
return callback(err);
if (!entry.tx.hasCoins())
return callback(new RPCError('Transaction not in mempool.'));
self.mempool.addUnchecked(entry, function(err) {
if (err)
return callback(err);
callback(null, true);
});
});
}; };
RPC.prototype.submitblock = function submitblock(args, callback) { RPC.prototype.submitblock = function submitblock(args, callback) {

View File

@ -149,54 +149,37 @@ Mempool.prototype._lock = function _lock(func, args, force) {
* @param {Function} callback * @param {Function} callback
*/ */
Mempool.prototype.addBlock = function addBlock(block, callback, force) { Mempool.prototype.addBlock = function addBlock(block, callback) {
var self = this; var entries = [];
var unlock = this._lock(addBlock, [block, callback], force); var i, entry, tx, hash;
var len, entries, entry;
if (!unlock) for (i = block.txs.length - 1; i >= 0; i--) {
return; tx = block.txs[i];
hash = tx.hash('hex');
callback = utils.wrap(callback, unlock);
len = block.txs.length - 1;
entries = [];
utils.forRangeSerial(0, block.txs.length, function(i, next) {
var tx = block.txs[len--];
var hash = tx.hash('hex');
if (tx.isCoinbase()) if (tx.isCoinbase())
return next(); continue;
entry = self.getEntry(hash); entry = this.getEntry(hash);
if (!entry) { if (!entry) {
self.removeOrphan(hash); this.removeOrphan(hash);
return next(); continue;
} }
self.removeUnchecked(entry, false, function(err) { this.removeUnchecked(entry);
if (err) this.emit('confirmed', tx, block);
return next(err);
self.emit('confirmed', tx, block); entries.push(entry);
}
entries.push(entry); this.blockSinceBump = true;
this.lastFeeUpdate = utils.now();
return next(); if (this.fees)
}, true); this.fees.processBlock(block.height, entries, this.chain.isFull());
}, function(err) {
if (err)
return callback(err);
self.blockSinceBump = true; return callback();
self.lastFeeUpdate = utils.now();
if (self.fees)
self.fees.processBlock(block.height, entries, self.chain.isFull());
return callback();
});
}; };
/** /**
@ -245,62 +228,54 @@ Mempool.prototype.removeBlock = function removeBlock(block, callback, force) {
* @param {Function} callback * @param {Function} callback
*/ */
Mempool.prototype.limitMempoolSize = function limitMempoolSize(entryHash, callback) { Mempool.prototype.limitMempoolSize = function limitMempoolSize(entryHash) {
var self = this;
var trimmed = false; var trimmed = false;
var hashes, end, entry; var i, hashes, hash, end, entry;
if (this.getSize() <= this.maxSize) if (this.getSize() <= this.maxSize)
return callback(null, trimmed); return trimmed;
hashes = Object.keys(this.tx); hashes = this.getSnapshot();
end = utils.now() - constants.mempool.MEMPOOL_EXPIRY; end = utils.now() - constants.mempool.MEMPOOL_EXPIRY;
utils.forEachSerial(hashes, function(hash, next) { for (i = 0; i < hashes.length; i++) {
var entry = self.getEntry(hash); hash = hashes[i];
entry = this.getEntry(hash);
if (!entry) if (!entry)
return next(); continue;
if (self.getSize() <= self.maxSize)
return callback(null, trimmed);
if (entry.ts >= end) if (entry.ts >= end)
return next(); continue;
if (!trimmed && hash === entryHash) if (!trimmed && hash === entryHash)
trimmed = true; trimmed = true;
self.removeUnchecked(entry, true, next, true); this.removeUnchecked(entry, true);
}, function(err) {
if (err)
return callback(err);
if (self.getSize() <= self.maxSize) if (this.getSize() <= this.maxSize)
return callback(null, trimmed); return trimmed;
}
hashes = Object.keys(self.tx); hashes = this.getSnapshot();
utils.forEachSerial(hashes, function(hash, next) { for (i = 0; i < hashes.length; i++) {
if (self.getSize() <= self.maxSize) hash = hashes[i];
return callback(null, trimmed); entry = this.getEntry(hash);
entry = self.getEntry(hash); if (!entry)
continue;
if (!entry) if (!trimmed && hash === entryHash)
return next(); trimmed = true;
if (!trimmed && hash === entryHash) this.removeUnchecked(entry, true);
trimmed = true;
self.removeUnchecked(entry, true, next, true); if (this.getSize() <= this.maxSize)
}, function(err) { return trimmed;
if (err) }
return callback(err);
return callback(null, trimmed); return trimmed;
});
});
}; };
/** /**
@ -595,11 +570,11 @@ Mempool.prototype.addTX = function addTX(tx, callback, force) {
} }
} }
this.chain.checkFinal(this.chain.tip, tx, lockFlags, function(err, isFinal) { this.chain.checkFinal(this.chain.tip, tx, lockFlags, function(err, result) {
if (err) if (err)
return callback(err); return callback(err);
if (!isFinal) { if (!result) {
return callback(new VerifyError(tx, return callback(new VerifyError(tx,
'nonstandard', 'nonstandard',
'non-final', 'non-final',
@ -650,19 +625,14 @@ Mempool.prototype.addTX = function addTX(tx, callback, force) {
if (err) if (err)
return callback(err); return callback(err);
self.limitMempoolSize(hash, function(err, trimmed) { if (self.limitMempoolSize(hash)) {
if (err) return callback(new VerifyError(tx,
return callback(err); 'insufficientfee',
'mempool full',
0));
}
if (trimmed) { return callback();
return callback(new VerifyError(tx,
'insufficientfee',
'mempool full',
0));
}
return callback();
});
}, true); }, true);
}); });
}); });
@ -739,48 +709,35 @@ Mempool.prototype.addUnchecked = function addUnchecked(entry, callback, force) {
* @param {Function} callback * @param {Function} callback
*/ */
Mempool.prototype.removeUnchecked = function removeUnchecked(entry, limit, callback, force) { Mempool.prototype.removeUnchecked = function removeUnchecked(entry, limit) {
var self = this; var rate, hash;
var unlock, rate, hash;
unlock = this._lock(removeUnchecked, [entry, limit, callback], force);
if (!unlock)
return;
callback = utils.wrap(callback, unlock);
this.removeOrphan(entry.tx); this.removeOrphan(entry.tx);
this._removeUnchecked(entry, limit, function(err) { this._removeUnchecked(entry, limit);
if (err)
return callback(err);
self.totalSpent -= entry.tx.inputs.length; this.totalSpent -= entry.tx.inputs.length;
self.size -= self.memUsage(entry.tx); this.size -= this.memUsage(entry.tx);
self.totalTX--; this.totalTX--;
if (self.fees) { if (this.fees) {
hash = entry.tx.hash('hex'); hash = entry.tx.hash('hex');
self.fees.removeTX(hash); this.fees.removeTX(hash);
}
if (limit) {
this.logger.spam('Removed tx %s from mempool.', entry.tx.rhash);
rate = bcoin.tx.getRate(entry.sizes, entry.fees);
rate += this.minReasonableFee;
if (rate > this.minFeeRate) {
this.minFeeRate = rate;
this.blockSinceBump = false;
} }
} else {
this.logger.spam('Removed block tx %s from mempool.', entry.tx.rhash);
}
if (limit) { this.emit('remove tx', entry.tx);
self.logger.spam('Removed tx %s from mempool.', entry.tx.rhash);
rate = bcoin.tx.getRate(entry.sizes, entry.fees);
rate += self.minReasonableFee;
if (rate > self.minFeeRate) {
self.minFeeRate = rate;
self.blockSinceBump = false;
}
} else {
self.logger.spam('Removed block tx %s from mempool.', entry.tx.rhash);
}
self.emit('remove tx', entry.tx);
return callback();
});
}; };
/** /**
@ -1001,7 +958,7 @@ Mempool.prototype.countDescendants = function countDescendants(tx) {
if (!next) if (!next)
continue; continue;
count = 1; count = 1;
count += this.countDescendants(next); count += this.countDescendants(next.tx);
if (count > max) if (count > max)
max = count; max = count;
} }
@ -1081,13 +1038,15 @@ Mempool.prototype.storeOrphan = function storeOrphan(tx) {
*/ */
Mempool.prototype.getBalance = function getBalance() { Mempool.prototype.getBalance = function getBalance() {
var keys = Object.keys(this.tx); var hashes = this.getSnapshot();
var total = 0; var total = 0;
var i, j, key, tx, hash, coin; var i, j, tx, hash, coin;
for (i = 0; i < keys.length; i++) { for (i = 0; i < hashes.length; i++) {
key = keys[i]; hash = hashes[i];
tx = this.tx[key].tx; tx = this.getTX(hash);
if (!tx)
continue;
hash = tx.hash('hex'); hash = tx.hash('hex');
for (j = 0; j < tx.outputs.length; j++) { for (j = 0; j < tx.outputs.length; j++) {
coin = this.getCoin(hash, j); coin = this.getCoin(hash, j);
@ -1105,13 +1064,13 @@ Mempool.prototype.getBalance = function getBalance() {
*/ */
Mempool.prototype.getHistory = function getHistory() { Mempool.prototype.getHistory = function getHistory() {
var keys = Object.keys(this.tx); var hashes = this.getSnapshot();
var txs = []; var txs = [];
var i, key, tx; var i, hash, tx;
for (i = 0; i < keys.length; i++) { for (i = 0; i < hashes.length; i++) {
key = keys[i]; hash = hashes[i];
tx = this.getTX(key); tx = this.getTX(hash);
if (!tx) if (!tx)
continue; continue;
txs.push(tx); txs.push(tx);
@ -1304,7 +1263,7 @@ Mempool.prototype.fillAllCoins = function fillAllCoins(tx, callback) {
/** /**
* Get a snapshot of all transaction hashes in the mempool. Used * Get a snapshot of all transaction hashes in the mempool. Used
* for generating INV packets in response to MEMPOOL packets. * for generating INV packets in response to MEMPOOL packets.
* @param {Function} callback - Returns [Error, {@link Hash}[]]. * @returns {Hash[]}
*/ */
Mempool.prototype.getSnapshot = function getSnapshot() { Mempool.prototype.getSnapshot = function getSnapshot() {
@ -1421,16 +1380,16 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry) {
if (this.options.indexAddress) if (this.options.indexAddress)
this.coinIndex.removeCoin(input.coin); this.coinIndex.removeCoin(input.coin);
this.spents[key] = bcoin.outpoint.fromTX(tx, i); this.spents[key] = entry;
} }
for (i = 0; i < tx.outputs.length; i++) { if (this.options.indexAddress) {
output = tx.outputs[i]; for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i];
if (output.script.isUnspendable()) if (output.script.isUnspendable())
continue; continue;
if (this.options.indexAddress) {
coin = bcoin.coin.fromTX(tx, i); coin = bcoin.coin.fromTX(tx, i);
this.coinIndex.addCoin(coin); this.coinIndex.addCoin(coin);
} }
@ -1460,50 +1419,49 @@ Mempool.prototype._addUnchecked = function _addUnchecked(entry) {
* @param {Function} callback * @param {Function} callback
*/ */
Mempool.prototype._removeUnchecked = function _removeUnchecked(entry, limit, callback) { Mempool.prototype._removeUnchecked = function _removeUnchecked(entry, limit) {
var self = this;
var tx = entry.tx; var tx = entry.tx;
var hash = tx.hash('hex'); var hash = tx.hash('hex');
var i, input, output, key, coin; var i, input, output, key, coin;
this._removeSpenders(entry, limit, function(err) { // We do not remove spenders if this is
if (err) // being removed for a block. The spenders
return callback(err); // are still spending valid coins (which
// now exist on the blockchain).
if (limit)
this._removeSpenders(entry);
delete self.tx[hash]; delete this.tx[hash];
if (self.options.indexAddress) if (this.options.indexAddress)
self.txIndex.addTX(tx); this.txIndex.removeTX(tx);
for (i = 0; i < tx.inputs.length; i++) { for (i = 0; i < tx.inputs.length; i++) {
input = tx.inputs[i]; input = tx.inputs[i];
key = input.prevout.hash + input.prevout.index; key = input.prevout.hash + input.prevout.index;
if (tx.isCoinbase()) if (tx.isCoinbase())
break; break;
delete self.spents[key]; delete this.spents[key];
if (self.options.indexAddress) { assert(input.coin);
assert(input.coin);
self.coinIndex.removeCoin(input.coin);
}
}
if (this.options.indexAddress)
this.coinIndex.removeCoin(input.coin);
}
if (this.options.indexAddress) {
for (i = 0; i < tx.outputs.length; i++) { for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i]; output = tx.outputs[i];
if (output.script.isUnspendable()) if (output.script.isUnspendable())
continue; continue;
if (self.options.indexAddress) { coin = bcoin.coin.fromTX(tx, i);
coin = bcoin.coin.fromTX(tx, i); this.coinIndex.removeCoin(coin);
self.coinIndex.removeCoin(coin);
}
} }
}
return callback();
});
}; };
/** /**
@ -1514,33 +1472,19 @@ Mempool.prototype._removeUnchecked = function _removeUnchecked(entry, limit, cal
* @param {Function} callback * @param {Function} callback
*/ */
Mempool.prototype._removeSpenders = function _removeSpenders(entry, limit, callback) { Mempool.prototype._removeSpenders = function _removeSpenders(entry) {
var self = this;
var tx = entry.tx; var tx = entry.tx;
var hash, spender; var hash = tx.hash('hex');
var i, spender;
// We do not remove spenders if this is for (i = 0; i < tx.outputs.length; i++) {
// being removed for a block. The spenders spender = this.isSpent(hash, i);
// are still spending valid coins (which
// now exist on the blockchain).
if (!limit)
return callback();
hash = tx.hash('hex');
utils.forEachSerial(tx.outputs, function(output, next, i) {
spender = self.isSpent(hash, i);
if (!spender) if (!spender)
return next(); continue;
entry = self.getEntry(spender.hash); this.removeUnchecked(spender, true);
}
if (!entry)
return next();
self.removeUnchecked(entry, limit, next, true);
}, callback);
}; };
/** /**
@ -1575,11 +1519,14 @@ Mempool.prototype.memUsage = function memUsage(tx) {
mem += 8; mem += 8;
// Mempool Entry
mem += 40;
// Map entry // Map entry
mem += 64; mem += 64;
// Spent entry // Spent entry
mem += tx.outputs.length * 72; mem += 64;
return mem; return mem;
}; };