From f3055e57bfcfb9d23322b3536a8e153f7b5a429a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 2 Nov 2016 06:32:08 -0700 Subject: [PATCH] walletdb: account path indexing. --- lib/wallet/wallet.js | 76 +++++++++++++++++++++++++++++------------- lib/wallet/walletdb.js | 17 ++++++++++ 2 files changed, 70 insertions(+), 23 deletions(-) diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index ce91138b..d4bf19aa 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -770,13 +770,31 @@ Wallet.prototype.getAccounts = function getAccounts() { /** * Get all wallet address hashes. + * @param {(String|Number)?} acct * @returns {Promise} - Returns Array. */ -Wallet.prototype.getAddressHashes = function getAddressHashes() { +Wallet.prototype.getAddressHashes = function getAddressHashes(acct) { + if (acct != null) + return this.getAccountHashes(acct); return this.db.getWalletHashes(this.wid); }; +/** + * Get all account address hashes. + * @param {String|Number} acct + * @returns {Promise} - Returns Array. + */ + +Wallet.prototype.getAccountHashes = co(function* getAccountHashes(acct) { + var index = yield this.ensureIndex(acct); + + if (index == null) + throw new Error('Account not provided.'); + + return yield this.db.getAccountHashes(this.wid, index); +}); + /** * Retrieve an account from the database. * @param {Number|String} acct @@ -1075,12 +1093,6 @@ Wallet.prototype.getPath = co(function* getPath(address) { assert(path.name); - // account = yield this.getAccount(path.account); - // assert(account); - // path.name = account.name; - // path.version = account.getWitnessVersion(path); - // path.type = account.getAddressType(path); - this.pathCache.set(hash, path); return path; @@ -1111,29 +1123,47 @@ Wallet.prototype.hasPath = co(function* hasPath(address) { */ Wallet.prototype.getPaths = co(function* getPaths(acct) { - var index = yield this.ensureIndex(acct); - var paths = yield this.db.getWalletPaths(this.wid); - var result = []; - var i, path; + var i, paths, path, result; + + if (acct != null) + return yield this.getAccountPaths(acct); + + paths = yield this.db.getWalletPaths(this.wid); + result = []; for (i = 0; i < paths.length; i++) { path = paths[i]; - if (index == null || path.account === index) { - path.id = this.id; - path.name = yield this.getAccountName(path.account); + path.id = this.id; + path.name = yield this.getAccountName(path.account); - assert(path.name); + assert(path.name); - // account = yield this.getAccount(path.account); - // assert(account); - // path.name = account.name; - // path.version = account.getWitnessVersion(path); - // path.type = account.getAddressType(path); + this.pathCache.set(path.hash, path); - this.pathCache.set(path.hash, path); + result.push(path); + } - result.push(path); - } + return result; +}); + +/** + * Get all account paths. + * @param {String|Number} acct + * @returns {Promise} - Returns {@link Path}. + */ + +Wallet.prototype.getAccountPaths = co(function* getAccountPaths(acct) { + var index = yield this.ensureIndex(acct); + var hashes = yield this.getAccountHashes(index); + var result = []; + var i, hash, path; + + for (i = 0; i < hashes.length; i++) { + hash = hashes[i]; + path = yield this.getPath(hash); + assert(path); + assert(path.account === index); + result.push(path); } return result; diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 62bcaa07..5b9f9166 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -32,6 +32,7 @@ var PathMapRecord = records.PathMapRecord; var OutpointMapRecord = records.OutpointMapRecord; var TXDB = require('./txdb'); var U32 = utils.U32; +var DUMMY = new Buffer([0]); /* * Database Layout: @@ -1174,6 +1175,7 @@ WalletDB.prototype.savePath = co(function* savePath(wallet, path) { batch.put(layout.p(hash), map.toRaw()); batch.put(layout.P(wid, hash), path.toRaw()); + batch.put(layout.r(wid, path.account, hash), DUMMY); }); /** @@ -1265,6 +1267,21 @@ WalletDB.prototype.getWalletHashes = function getWalletHashes(wid) { }); }; +/** + * Get all account address hashes. + * @param {WalletID} wid + * @param {Number} account + * @returns {Promise} + */ + +WalletDB.prototype.getAccountHashes = function getAccountHashes(wid, account) { + return this.db.keys({ + gte: layout.r(wid, account, constants.NULL_HASH), + lte: layout.r(wid, account, constants.HIGH_HASH), + parse: layout.rr + }); +}; + /** * Get all paths for a wallet. * @param {WalletID} wid