From 9dacd34424fb133103ada858a23fc8491b4c988d Mon Sep 17 00:00:00 2001 From: Nodari Chkuaselidze Date: Mon, 23 Jan 2023 14:53:42 +0400 Subject: [PATCH] wallet: preload all wallets before HTTP server is started. This makes sure coin cache that takes long to recover, recovers properly before we start responding to the healthchecks. This is better signal when wallet is back online. --- lib/wallet/node.js | 3 ++- lib/wallet/plugin.js | 3 ++- lib/wallet/walletdb.js | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/wallet/node.js b/lib/wallet/node.js index f751d98d4..927eb2977 100644 --- a/lib/wallet/node.js +++ b/lib/wallet/node.js @@ -52,7 +52,8 @@ class WalletNode extends Node { wipeNoReally: this.config.bool('wipe-no-really'), spv: this.config.bool('spv'), migrate: this.config.uint('migrate'), - checkLookahead: this.config.bool('check-lookahead', false) + checkLookahead: this.config.bool('check-lookahead', false), + preloadAll: this.config.bool('preload-all', false) }); this.rpc = new RPC(this); diff --git a/lib/wallet/plugin.js b/lib/wallet/plugin.js index 358fa6a79..676c89725 100644 --- a/lib/wallet/plugin.js +++ b/lib/wallet/plugin.js @@ -54,7 +54,8 @@ class Plugin extends EventEmitter { wipeNoReally: this.config.bool('wipe-no-really'), spv: node.spv, migrate: this.config.uint('migrate'), - checkLookahead: this.config.bool('check-lookahead', false) + checkLookahead: this.config.bool('check-lookahead', false), + preloadAll: this.config.bool('preload-all', false) }); this.rpc = new RPC(this); diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 8aed28e97..2c0d0b3e7 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -210,6 +210,7 @@ class WalletDB extends EventEmitter { await this.migrateChange(); await this.checkLookahead(); + await this.preloadAll(); } /** @@ -304,6 +305,27 @@ class WalletDB extends EventEmitter { await b.write(); } + /** + * Preload/open all wallets on open. + * @returns {Promise} + */ + + async preloadAll() { + if (!this.options.preloadAll) + return; + + this.logger.info('Preloading all wallets...'); + + const wids = await this.db.keys({ + gte: layout.W.min(), + lte: layout.W.max(), + parse: key => layout.W.decode(key)[0] + }); + + for (const wid of wids) + await this._get(wid); + } + /** * Verify network. * @returns {Promise} @@ -2519,6 +2541,7 @@ class WalletOptions { this.wipeNoReally = false; this.migrate = null; this.checkLookahead = false; + this.preloadAll = false; if (options) this.fromOptions(options); @@ -2606,6 +2629,10 @@ class WalletOptions { this.checkLookahead = options.checkLookahead; } + if (options.preloadAll != null) { + assert(typeof options.preloadAll === 'boolean'); + this.preloadAll = options.preloadAll; + } return this; }