wallet: account cache + locking issue.

This commit is contained in:
Christopher Jeffrey 2016-10-03 16:57:27 -07:00
parent d77215009f
commit 3cef641780
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 27 additions and 2 deletions

View File

@ -245,6 +245,9 @@ Account.prototype.open = function open() {
if (!this.initialized)
return Promise.resolve(null);
if (this.receive)
return Promise.resolve(null);
this.receive = this.deriveReceive(this.receiveDepth - 1);
this.change = this.deriveChange(this.changeDepth - 1);

View File

@ -64,6 +64,7 @@ function Wallet(db, options) {
this.db = db;
this.network = db.network;
this.logger = db.logger;
this.readLock = new Locker.Mapped();
this.writeLock = new Locker();
this.fundLock = new Locker();
@ -745,14 +746,35 @@ Wallet.prototype.getAddressHashes = function getAddressHashes() {
*/
Wallet.prototype.getAccount = co(function* getAccount(acct) {
var account;
var index, unlock;
if (this.account) {
if (acct === 0 || acct === 'default')
return this.account;
}
account = yield this.db.getAccount(this.wid, acct);
index = yield this.db.getAccountIndex(this.wid, acct);
if (index === -1)
return;
unlock = yield this.readLock.lock(index);
try {
return yield this._getAccount(index);
} finally {
unlock();
}
});
/**
* Retrieve an account from the database without a lock.
* @param {Number} index
* @returns {Promise} - Returns {@link Account}.
*/
Wallet.prototype._getAccount = co(function* _getAccount(index) {
var account = yield this.db.getAccount(this.wid, index);
if (!account)
return;