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
*/
Wallet.prototype.destroy = function destroy() {
Wallet.prototype.destroy = co(function* destroy() {
var unlock1 = yield this.writeLock.lock();
var unlock2 = yield this.fundLock.lock();
try {
this.db.unregister(this);
this.master.destroy();
} catch (e) {
this.emit('error', e);
return Promise.reject(e);
} finally {
unlock2();
unlock1();
}
return Promise.resolve(null);
};
});
/**
* Add a public account key to the wallet (multisig).
@ -354,10 +354,10 @@ Wallet.prototype._setPassphrase = co(function* setPassphrase(old, new_) {
old = null;
}
if (old)
if (old != null)
yield this.master.decrypt(old);
if (new_)
if (new_ != null)
yield this.master.encrypt(new_);
this.start();
@ -406,9 +406,16 @@ Wallet.prototype._retoken = co(function* retoken(passphrase) {
* Lock the wallet, destroy decrypted key.
*/
Wallet.prototype.lock = function lock() {
this.master.destroy();
};
Wallet.prototype.lock = co(function* lock() {
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.

View File

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