fix(rpc): Improve upgradetohd (#5455)

## Issue being fixed or feature implemented
Allow `upgradetohd` in IBD, better errors, no GUI lock-up

## What was done?
Pls see individual commits. Most of it is changes in whitespaces, might
want to use ?w=1 to review i.e.
https://github.com/dashpay/dash/pull/5455/files?w=1

## How Has This Been Tested?
run tests, try `upgradetohd` on testnet

## Breaking Changes
n/a

## Checklist:
- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added or updated relevant unit/integration/functional/e2e
tests
- [ ] I have made corresponding changes to the documentation
- [x] I have assigned this pull request to a milestone _(for repository
code-owners and collaborators only)_
This commit is contained in:
UdjinM6 2023-07-01 03:23:49 +03:00 committed by GitHub
parent bfb3f4b0e0
commit 5e99e3f516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 58 deletions

View File

@ -201,7 +201,6 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "createwallet", 1, "disable_private_keys"},
{ "createwallet", 2, "blank"},
{ "createwallet", 4, "avoid_reuse"},
{ "upgradetohd", 3, "rescan"},
{ "createwallet", 5, "load_on_startup"},
{ "loadwallet", 1, "load_on_startup"},
{ "unloadwallet", 1, "load_on_startup"},

View File

@ -2665,6 +2665,9 @@ static UniValue upgradetohd(const JSONRPCRequest& request)
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
}
bool generate_mnemonic = request.params[0].isNull() || request.params[0].get_str().empty();
{
LOCK(pwallet->cs_wallet);
// Do not do anything to HD wallets
@ -2697,14 +2700,9 @@ static UniValue upgradetohd(const JSONRPCRequest& request)
}
}
bool generate_mnemonic = request.params[0].isNull() || request.params[0].get_str().empty();
SecureString secureMnemonic;
secureMnemonic.reserve(256);
if (!generate_mnemonic) {
if (pwallet->chain().isInitialBlockDownload()) {
throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Cannot set mnemonic while still in Initial Block Download");
}
secureMnemonic = request.params[0].get_str().c_str();
}
@ -2729,6 +2727,7 @@ static UniValue upgradetohd(const JSONRPCRequest& request)
}
}
}
}
// If you are generating new mnemonic it is assumed that the addresses have never gotten a transaction before, so you don't need to rescan for transactions
bool rescan = request.params[3].isNull() ? !generate_mnemonic : request.params[3].get_bool();
@ -2737,7 +2736,16 @@ static UniValue upgradetohd(const JSONRPCRequest& request)
if (!reserver.reserve()) {
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait.");
}
pwallet->ScanForWalletTransactions(pwallet->chain().getBlockHash(0), 0, {}, reserver, true);
CWallet::ScanResult result = pwallet->ScanForWalletTransactions(pwallet->chain().getBlockHash(0), 0, {}, reserver, true);
switch (result.status) {
case CWallet::ScanResult::SUCCESS:
break;
case CWallet::ScanResult::FAILURE:
throw JSONRPCError(RPC_MISC_ERROR, "Rescan failed. Potentially corrupted data files.");
case CWallet::ScanResult::USER_ABORT:
throw JSONRPCError(RPC_MISC_ERROR, "Rescan aborted.");
// no default case, so the compiler can warn about missing cases
}
}
return true;