Skip to content

Commit

Permalink
Merge PR #803 from 'nodech/preload-wallets'
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Dec 4, 2023
2 parents aac7fa7 + 3b03d7e commit 7999477
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 18 deletions.
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,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 @@ -215,11 +215,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 @@ -246,6 +245,9 @@ class WalletDB extends EventEmitter {
}
}

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

this.logger.info('WalletDB opened.');
this.emit('open');
}
Expand All @@ -265,6 +267,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 @@ -913,7 +931,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 @@ -929,7 +947,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 @@ -946,7 +964,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 @@ -961,7 +979,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 @@ -983,7 +1001,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 @@ -2542,8 +2560,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 @@ -2636,6 +2655,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 @@ -2667,6 +2691,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

0 comments on commit 7999477

Please sign in to comment.