Skip to content

Commit

Permalink
wallet: migrate change and rescan using config option
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Aug 7, 2020
1 parent 7490534 commit 223c481
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/wallet/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class WalletNode extends Node {
maxFiles: this.config.uint('max-files'),
cacheSize: this.config.mb('cache-size'),
wipeNoReally: this.config.bool('wipe-no-really'),
spv: this.config.bool('spv')
spv: this.config.bool('spv'),
migrate: this.config.uint('migrate')
});

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 @@ -51,7 +51,8 @@ class Plugin extends EventEmitter {
maxFiles: this.config.uint('max-files'),
cacheSize: this.config.mb('cache-size'),
wipeNoReally: this.config.bool('wipe-no-really'),
spv: node.spv
spv: node.spv,
migrate: this.config.uint('migrate')
});

this.rpc = new RPC(this);
Expand Down
35 changes: 23 additions & 12 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,7 @@ class WalletDB extends EventEmitter {

b.put(layout.M.encode(0), null);

if (!this.state.marked) {
await b.write();
return;
}

this.logger.warning('Running change address migration.');
this.logger.warning('Checking change address corruption...');

const wids = await this.db.keys({
gte: layout.W.min(),
Expand All @@ -244,19 +239,29 @@ class WalletDB extends EventEmitter {
for (const wid of wids) {
const wallet = await this.get(wid);

this.logger.info('Regenerating change addresses (id=%s, wid=%d).',
this.logger.info('Checking wallet (id=%s, wid=%d).',
wallet.id, wid);

total += await wallet.migrateChange(b);
}

await b.write();
if (this.options.migrate === 0)
await b.write();

if (total > 0) {
this.logger.warning('Regenerated %d change addresses.', total);
this.logger.warning('Rescanning...');

await this.scan();
if (this.options.migrate === 0) {
this.logger.warning('Regenerated %d change addresses.', total);
this.logger.warning('Rescanning...');

await this.scan();
} else {
throw new Error(
'Wallet is corrupted.\n' +
'Back up wallet and then restart with\n' +
'`hsd --wallet-migrate=0` or `hs-wallet --migrate=0`\n' +
'(Full node required)'
);
}
} else {
this.logger.info('All change addresses present.');
}
Expand Down Expand Up @@ -2375,6 +2380,7 @@ class WalletOptions {

this.spv = false;
this.wipeNoReally = false;
this.migrate = null;

if (options)
this.fromOptions(options);
Expand Down Expand Up @@ -2452,6 +2458,11 @@ class WalletOptions {
this.wipeNoReally = options.wipeNoReally;
}

if (options.migrate != null) {
assert((options.migrate >>> 0) === options.migrate);
this.migrate = options.migrate;
}

return this;
}

Expand Down

0 comments on commit 223c481

Please sign in to comment.