refactor: locks.

This commit is contained in:
Christopher Jeffrey 2016-09-23 00:08:58 -07:00
parent c2e1e4bfc9
commit 02b19824dc
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 21 additions and 16 deletions

View File

@ -207,17 +207,17 @@ Wallet.prototype.open = co(function* open() {
* @param {Function} callback * @param {Function} callback
*/ */
Wallet.prototype.destroy = function destroy() { Wallet.prototype.destroy = co(function* destroy() {
var unlock1 = yield this.writeLock.lock();
var unlock2 = yield this.fundLock.lock();
try { try {
this.db.unregister(this); this.db.unregister(this);
this.master.destroy(); this.master.destroy();
} catch (e) { } finally {
this.emit('error', e); unlock2();
return Promise.reject(e); unlock1();
} }
});
return Promise.resolve(null);
};
/** /**
* Add a public account key to the wallet (multisig). * Add a public account key to the wallet (multisig).
@ -354,10 +354,10 @@ Wallet.prototype._setPassphrase = co(function* setPassphrase(old, new_) {
old = null; old = null;
} }
if (old) if (old != null)
yield this.master.decrypt(old); yield this.master.decrypt(old);
if (new_) if (new_ != null)
yield this.master.encrypt(new_); yield this.master.encrypt(new_);
this.start(); this.start();
@ -406,9 +406,16 @@ Wallet.prototype._retoken = co(function* retoken(passphrase) {
* Lock the wallet, destroy decrypted key. * Lock the wallet, destroy decrypted key.
*/ */
Wallet.prototype.lock = function lock() { Wallet.prototype.lock = co(function* lock() {
this.master.destroy(); var unlock1 = yield this.writeLock.lock();
}; var unlock2 = yield this.fundLock.lock();
try {
this.master.destroy();
} finally {
unlock2();
unlock1();
}
});
/** /**
* Unlock the key for `timeout` milliseconds. * Unlock the key for `timeout` milliseconds.

View File

@ -136,7 +136,7 @@ function WalletDB(options) {
// We need one read lock for `get` and `create`. // We need one read lock for `get` and `create`.
// It will hold locks specific to wallet ids. // It will hold locks specific to wallet ids.
this.readLock = new bcoin.locker.mapped(this); this.readLock = new bcoin.locker.mapped(this);
this.writeLock = new bcoin.locker.mapped(this); this.writeLock = new bcoin.locker(this);
this.txLock = new bcoin.locker(this); this.txLock = new bcoin.locker(this);
this.walletCache = new bcoin.lru(10000); this.walletCache = new bcoin.lru(10000);
@ -530,13 +530,11 @@ WalletDB.prototype.auth = co(function* auth(wid, token) {
*/ */
WalletDB.prototype.create = co(function* create(options) { WalletDB.prototype.create = co(function* create(options) {
var unlock; var unlock = yield this.writeLock.lock();
if (!options) if (!options)
options = {}; options = {};
unlock = yield this.writeLock.lock(options.id);
try { try {
return yield this._create(options); return yield this._create(options);
} finally { } finally {