locker: more locker cleanup.

This commit is contained in:
Christopher Jeffrey 2016-08-17 23:35:01 -07:00
parent 97a2f2e86a
commit 990d5c2baf
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 96 additions and 36 deletions

View File

@ -1126,10 +1126,10 @@ Chain.prototype.reset = function reset(height, callback, force) {
* @param {Function} callback * @param {Function} callback
*/ */
Chain.prototype.resetTime = function resetTime(ts, callback, force) { Chain.prototype.resetTime = function resetTime(ts, callback) {
var self = this; var self = this;
callback = this._lock(resetTime, [ts, callback], force); callback = this._lock(resetTime, [ts, callback]);
if (!callback) if (!callback)
return; return;
@ -1172,13 +1172,13 @@ Chain.prototype.isBusy = function isBusy() {
* @param {Function} callback - Returns [{@link VerifyError}]. * @param {Function} callback - Returns [{@link VerifyError}].
*/ */
Chain.prototype.add = function add(block, callback, force) { Chain.prototype.add = function add(block, callback) {
var self = this; var self = this;
var ret = {}; var ret = {};
assert(this.loaded); assert(this.loaded);
callback = this._lock(add, [block, callback], force); callback = this._lock(add, [block, callback]);
if (!callback) if (!callback)
return; return;
@ -1708,13 +1708,13 @@ Chain.prototype.getProgress = function getProgress() {
* @param {Function} callback - Returns [Error, {@link Hash}[]]. * @param {Function} callback - Returns [Error, {@link Hash}[]].
*/ */
Chain.prototype.getLocator = function getLocator(start, callback, force) { Chain.prototype.getLocator = function getLocator(start, callback) {
var self = this; var self = this;
var hashes = []; var hashes = [];
var step = 1; var step = 1;
var height; var height;
callback = this._lock(getLocator, [start, callback], force); callback = this._lock(getLocator, [start, callback]);
if (!callback) if (!callback)
return; return;

View File

@ -138,8 +138,10 @@ Locker.prototype.lock = function lock(func, args, force) {
*/ */
Locker.prototype.destroy = function destroy() { Locker.prototype.destroy = function destroy() {
this.pending.length = 0; if (this.add) {
this.pendingMap = {}; this.pending.length = 0;
this.pendingMap = {};
}
this.jobs.length = 0; this.jobs.length = 0;
}; };
@ -149,6 +151,8 @@ Locker.prototype.destroy = function destroy() {
*/ */
Locker.prototype.onDrain = function onDrain(callback) { Locker.prototype.onDrain = function onDrain(callback) {
assert(this.add, 'Cannot wait for drain without add method.');
if (this.pending.length === 0) if (this.pending.length === 0)
return callback(); return callback();

View File

@ -163,6 +163,11 @@ Mempool.prototype.addBlock = function addBlock(block, callback) {
var entries = []; var entries = [];
var i, entry, tx, hash; var i, entry, tx, hash;
callback = this._lock(addBlock, [block, callback]);
if (!callback)
return;
for (i = block.txs.length - 1; i >= 0; i--) { for (i = block.txs.length - 1; i >= 0; i--) {
tx = block.txs[i]; tx = block.txs[i];
hash = tx.hash('hex'); hash = tx.hash('hex');
@ -189,7 +194,7 @@ Mempool.prototype.addBlock = function addBlock(block, callback) {
if (this.fees) if (this.fees)
this.fees.processBlock(block.height, entries, this.chain.isFull()); this.fees.processBlock(block.height, entries, this.chain.isFull());
callback(); utils.nextTick(callback);
}; };
/** /**
@ -199,11 +204,11 @@ Mempool.prototype.addBlock = function addBlock(block, callback) {
* @param {Function} callback * @param {Function} callback
*/ */
Mempool.prototype.removeBlock = function removeBlock(block, callback, force) { Mempool.prototype.removeBlock = function removeBlock(block, callback) {
var self = this; var self = this;
var entry; var entry;
callback = this._lock(removeBlock, [block, callback], force); callback = this._lock(removeBlock, [block, callback]);
if (!callback) if (!callback)
return; return;
@ -512,14 +517,14 @@ Mempool.prototype.has = function has(hash) {
* @param {Function} callback - Returns [{@link VerifyError}]. * @param {Function} callback - Returns [{@link VerifyError}].
*/ */
Mempool.prototype.addTX = function addTX(tx, callback, force) { Mempool.prototype.addTX = function addTX(tx, callback) {
var self = this; var self = this;
var lockFlags = constants.flags.STANDARD_LOCKTIME_FLAGS; var lockFlags = constants.flags.STANDARD_LOCKTIME_FLAGS;
var hash = tx.hash('hex'); var hash = tx.hash('hex');
var ret = {}; var ret = {};
var entry; var entry;
callback = this._lock(addTX, [tx, callback], force); callback = this._lock(addTX, [tx, callback]);
if (!callback) if (!callback)
return; return;

View File

@ -1253,7 +1253,7 @@ TXDB.prototype.unconfirm = function unconfirm(hash, callback, force) {
* @param {Function} callback * @param {Function} callback
*/ */
TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) { TXDB.prototype._unconfirm = function unconfirm(tx, info, callback) {
var self = this; var self = this;
var hash = tx.hash('hex'); var hash = tx.hash('hex');
var i, account; var i, account;
@ -2265,17 +2265,16 @@ TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
* @param {Function} callback * @param {Function} callback
*/ */
TXDB.prototype.zap = function zap(account, age, callback, force) { TXDB.prototype.zap = function zap(account, age, callback) {
var self = this; var self = this;
if (typeof age === 'function') { if (typeof age === 'function') {
force = callback;
callback = age; callback = age;
age = account; age = account;
account = null; account = null;
} }
callback = this._lock(zap, [account, age, callback], force); callback = this._lock(zap, [account, age, callback]);
if (!callback) if (!callback)
return; return;
@ -2304,7 +2303,7 @@ TXDB.prototype.zap = function zap(account, age, callback, force) {
* @param {Function} callback * @param {Function} callback
*/ */
TXDB.prototype.abandon = function abandon(hash, callback, force) { TXDB.prototype.abandon = function abandon(hash, callback) {
var self = this; var self = this;
this.has(layout.p(hash), function(err, result) { this.has(layout.p(hash), function(err, result) {
if (err) if (err)
@ -2313,7 +2312,7 @@ TXDB.prototype.abandon = function abandon(hash, callback, force) {
if (!result) if (!result)
return callback(new Error('TX not eligible.')); return callback(new Error('TX not eligible.'));
self.remove(hash, callback, force); self.remove(hash, callback);
}); });
}; };

View File

@ -75,6 +75,24 @@ function Wallet(db, options) {
utils.inherits(Wallet, EventEmitter); utils.inherits(Wallet, EventEmitter);
/**
* Invoke write mutex lock.
* @private
*/
Wallet.prototype._lockWrite = function _lockWrite(func, args, force) {
return this.writeLock.lock(func, args, force);
};
/**
* Invoke funding mutex lock.
* @private
*/
Wallet.prototype._lockFund = function _lockFund(func, args, force) {
return this.fundLock.lock(func, args, force);
};
/** /**
* Inject properties from options object. * Inject properties from options object.
* @private * @private
@ -245,7 +263,7 @@ Wallet.prototype.addKey = function addKey(account, key, callback) {
account = 0; account = 0;
} }
callback = this.writeLock.lock(addKey, [account, key, callback]); callback = this._lockWrite(addKey, [account, key, callback]);
if (!callback) if (!callback)
return; return;
@ -289,7 +307,7 @@ Wallet.prototype.removeKey = function removeKey(account, key, callback) {
account = 0; account = 0;
} }
callback = this.writeLock.lock(removeKey, [account, key, callback]); callback = this._lockWrite(removeKey, [account, key, callback]);
if (!callback) if (!callback)
return; return;
@ -333,7 +351,7 @@ Wallet.prototype.setPassphrase = function setPassphrase(old, new_, callback) {
old = null; old = null;
} }
callback = this.writeLock.lock(setPassphrase, [old, new_, callback]); callback = this._lockWrite(setPassphrase, [old, new_, callback]);
if (!callback) if (!callback)
return; return;
@ -367,7 +385,7 @@ Wallet.prototype.retoken = function retoken(passphrase, callback) {
passphrase = null; passphrase = null;
} }
callback = this.writeLock.lock(retoken, [passphrase, callback]); callback = this._lockWrite(retoken, [passphrase, callback]);
if (!callback) if (!callback)
return; return;
@ -468,16 +486,18 @@ Wallet.prototype.getToken = function getToken(master, nonce) {
* @param {Function} callback - Returns [Error, {@link Account}]. * @param {Function} callback - Returns [Error, {@link Account}].
*/ */
Wallet.prototype.createAccount = function createAccount(options, callback, force) { Wallet.prototype.createAccount = function createAccount(options, callback) {
var self = this; var self = this;
var passphrase = options.passphrase;
var timeout = options.timeout;
var key; var key;
callback = this.writeLock.lock(createAccount, [options, callback], force); callback = this._lockWrite(createAccount, [options, callback]);
if (!callback) if (!callback)
return; return;
this.unlock(options.passphrase, options.timeout, function(err, master) { this.unlock(passphrase, timeout, function(err, master) {
if (err) if (err)
return callback(err); return callback(err);
@ -640,7 +660,7 @@ Wallet.prototype.createAddress = function createAddress(account, change, callbac
account = 0; account = 0;
} }
callback = this.writeLock.lock(createAddress, [account, change, callback]); callback = this._lockWrite(createAddress, [account, change, callback]);
if (!callback) if (!callback)
return; return;
@ -810,7 +830,7 @@ Wallet.prototype.fund = function fund(tx, options, callback, force) {
// We use a lock here to ensure we // We use a lock here to ensure we
// don't end up double spending coins. // don't end up double spending coins.
callback = this.fundLock.lock(fund, [tx, options, callback], force); callback = this._lockFund(fund, [tx, options, callback], force);
if (!callback) if (!callback)
return; return;
@ -947,7 +967,7 @@ Wallet.prototype.createTX = function createTX(options, callback, force) {
Wallet.prototype.send = function send(options, callback) { Wallet.prototype.send = function send(options, callback) {
var self = this; var self = this;
callback = this.fundLock.lock(send, [options, callback]); callback = this._lockFund(send, [options, callback]);
if (!callback) if (!callback)
return; return;
@ -1177,7 +1197,7 @@ Wallet.prototype.syncOutputDepth = function syncOutputDepth(info, callback) {
var accounts = {}; var accounts = {};
var i, path; var i, path;
callback = this.writeLock.lock(syncOutputDepth, [info, callback]); callback = this._lockWrite(syncOutputDepth, [info, callback]);
if (!callback) if (!callback)
return; return;

View File

@ -216,6 +216,33 @@ WalletDB.prototype._init = function _init() {
} }
}; };
/**
* Invoke wallet read mutex lock.
* @private
*/
WalletDB.prototype._lockRead = function _lockRead(key, func, args, force) {
return this.readLock.lock(key, func, args, force);
};
/**
* Invoke wallet write mutex lock.
* @private
*/
WalletDB.prototype._lockWrite = function _lockWrite(key, func, args, force) {
return this.writeLock.lock(key, func, args, force);
};
/**
* Invoke tx handling mutex lock.
* @private
*/
WalletDB.prototype._lockTX = function _lockTX(func, args, force) {
return this.txLock.lock(func, args, force);
};
/** /**
* Open the walletdb, wait for the database to load. * Open the walletdb, wait for the database to load.
* @alias WalletDB#open * @alias WalletDB#open
@ -539,7 +566,7 @@ WalletDB.prototype._get = function get(wid, callback) {
var self = this; var self = this;
var wallet; var wallet;
callback = this.readLock.lock(wid, get, [wid, callback]); callback = this._lockRead(wid, get, [wid, callback]);
if (!callback) if (!callback)
return; return;
@ -613,7 +640,7 @@ WalletDB.prototype.create = function create(options, callback) {
options = {}; options = {};
} }
callback = this.writeLock.lock(options.id, create, [options, callback]); callback = this._lockWrite(options.id, create, [options, callback]);
if (!callback) if (!callback)
return; return;
@ -1337,11 +1364,11 @@ WalletDB.prototype.getWalletsByTX = function getWalletsByTX(hash, callback) {
* @param {Function} callback * @param {Function} callback
*/ */
WalletDB.prototype.addBlock = function addBlock(entry, txs, callback, force) { WalletDB.prototype.addBlock = function addBlock(entry, txs, callback) {
var self = this; var self = this;
var block, matches, hash; var block, matches, hash;
callback = this.txLock.lock(addBlock, [entry, txs, callback], force); callback = this._lockTX(addBlock, [entry, txs, callback]);
if (!callback) if (!callback)
return; return;
@ -1391,10 +1418,10 @@ WalletDB.prototype.addBlock = function addBlock(entry, txs, callback, force) {
* @param {Function} callback * @param {Function} callback
*/ */
WalletDB.prototype.removeBlock = function removeBlock(entry, callback, force) { WalletDB.prototype.removeBlock = function removeBlock(entry, callback) {
var self = this; var self = this;
callback = this.txLock.lock(removeBlock, [entry, callback], force); callback = this._lockTX(removeBlock, [entry, callback]);
if (!callback) if (!callback)
return; return;
@ -1464,6 +1491,11 @@ WalletDB.prototype.removeBlock = function removeBlock(entry, callback, force) {
WalletDB.prototype.addTX = function addTX(tx, callback, force) { WalletDB.prototype.addTX = function addTX(tx, callback, force) {
var self = this; var self = this;
callback = this._lockTX(addTX, [tx, callback], force);
if (!callback)
return;
assert(!tx.mutable, 'Cannot add mutable TX to wallet.'); assert(!tx.mutable, 'Cannot add mutable TX to wallet.');
// Note: // Note: