Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet: --preload-all option to load all wallet on open. #803

Merged
merged 3 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ you run it for the first time.**

### Wallet Changes:
#### Configuration
Wallet now has option `wallet-migrate-no-rescan`/`migrate-no-rescan` if you
want to disable rescan when migration recommends it. It may result in the
incorrect txdb state, but can be useful if you know the issue does not affect
your wallet or is not critical.
- Wallet now has option `wallet-migrate-no-rescan`/`migrate-no-rescan` if you
want to disable rescan when migration recommends it. It may result in the
incorrect txdb state, but can be useful if you know the issue does not affect
your wallet or is not critical.
- Add `--wallet-preload-all` (or `--preload-all` for standalone wallet node)
that will open all wallets before starting other services (e.g. HTTP).
By default this is set to `false`.

#### Wallet API

Expand Down
3 changes: 2 additions & 1 deletion lib/wallet/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class WalletNode extends Node {
wipeNoReally: this.config.bool('wipe-no-really'),
spv: this.config.bool('spv'),
walletMigrate: this.config.uint('migrate'),
icannlockup: this.config.bool('icannlockup', false),
migrateNoRescan: this.config.bool('migrate-no-rescan', false),
icannlockup: this.config.bool('icannlockup', false)
preloadAll: this.config.bool('preload-all', false)
});

this.rpc = new RPC(this);
Expand Down
3 changes: 2 additions & 1 deletion lib/wallet/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ class Plugin extends EventEmitter {
wipeNoReally: this.config.bool('wipe-no-really'),
spv: node.spv,
walletMigrate: this.config.uint('migrate'),
icannlockup: this.config.bool('icannlockup', false),
migrateNoRescan: this.config.bool('migrate-no-rescan', false),
icannlockup: this.config.bool('icannlockup', false)
preloadAll: this.config.bool('preload-all', false)
});

this.rpc = new RPC(this);
Expand Down
7 changes: 3 additions & 4 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ class Wallet extends EventEmitter {
const account = await this._createAccount(options, passphrase);
assert(account);

this.logger.info('Wallet initialized (%s).', this.id);
await this.txdb.open(this);

return this.txdb.open(this);
this.logger.info('Wallet initialized (%s).', this.id);
}

/**
Expand All @@ -215,9 +215,8 @@ class Wallet extends EventEmitter {
if (!account)
throw new Error('Default account not found.');

await this.txdb.open(this);
this.logger.info('Wallet opened (%s).', this.id);

return this.txdb.open(this);
}

/**
Expand Down
45 changes: 37 additions & 8 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,10 @@ class WalletDB extends EventEmitter {
if (this.options.wipeNoReally)
await this.wipe();

await this.watch();
await this.loadState();

this.logger.info(
'WalletDB loaded (depth=%d, height=%d, start=%d).',
'WalletDB is loading (depth=%d, height=%d, start=%d).',
this.depth,
this.state.height,
this.state.startHeight);
Expand All @@ -239,6 +238,9 @@ class WalletDB extends EventEmitter {
}
}

await this.preloadAll();
await this.watch();

this.logger.info('WalletDB opened.');
this.emit('open');
}
Expand All @@ -258,6 +260,22 @@ class WalletDB extends EventEmitter {
b.put(layout.V.encode(), value);
}

/**
* Preload all wallets.
* @returns {Promise}
*/

async preloadAll() {
if (!this.options.preloadAll)
return;

this.logger.info('Preloading all wallets...');
const wallets = await this.getWallets();

for (const wname of wallets)
await this.get(wname);
}

/**
* Verify network.
* @returns {Promise}
Expand Down Expand Up @@ -899,7 +917,7 @@ class WalletDB extends EventEmitter {
/**
* Map wallet id to wid.
* @param {String|Number} id
* @returns {Promise} - Returns {Number}.
* @returns {Promise<Number>}
*/

async ensureWID(id) {
Expand All @@ -915,7 +933,7 @@ class WalletDB extends EventEmitter {
/**
* Map wallet id to wid.
* @param {String} id
* @returns {Promise} - Returns {Number}.
* @returns {Promise<Number>}
*/

async getWID(id) {
Expand All @@ -932,7 +950,7 @@ class WalletDB extends EventEmitter {
/**
* Map wallet wid to id.
* @param {Number} wid
* @returns {Promise} - Returns {String}.
* @returns {Promise<String?>}
*/

async getID(wid) {
Expand All @@ -947,7 +965,7 @@ class WalletDB extends EventEmitter {
/**
* Get a wallet from the database, setup watcher.
* @param {Number|String} id
* @returns {Promise} - Returns {@link Wallet}.
* @returns {Promise<Wallet?>}
*/

async get(id) {
Expand All @@ -969,7 +987,7 @@ class WalletDB extends EventEmitter {
* Get a wallet from the database without a lock.
* @private
* @param {Number} wid
* @returns {Promise} - Returns {@link Wallet}.
* @returns {Promise<Wallet?>}
*/

async _get(wid) {
Expand Down Expand Up @@ -2527,8 +2545,9 @@ class WalletOptions {
this.spv = false;
this.wipeNoReally = false;
this.walletMigrate = -1;
this.migrateNoRescan = false;
this.icannlockup = false;
this.migrateNoRescan = false;
this.preloadAll = false;

if (options)
this.fromOptions(options);
Expand Down Expand Up @@ -2621,6 +2640,11 @@ class WalletOptions {
this.migrateNoRescan = options.migrateNoRescan;
}

if (options.preloadAll != null) {
assert(typeof options.preloadAll === 'boolean');
this.preloadAll = options.preloadAll;
}

return this;
}

Expand Down Expand Up @@ -2652,6 +2676,11 @@ function fromString(str) {
return buf;
}

/**
* @param {Buffer} buf
* @returns {String}
*/

function toString(buf) {
assert(buf.length > 0);
assert(buf[0] === buf.length - 1);
Expand Down
Loading