Skip to content

Commit bfa585d

Browse files
authored
feat(wallet): TopUpKeyPool improvements (#5456)
## Issue being fixed or feature implemented - make progress calculations sane - show progress in GUI but only when you need 100+ new keys - make it stop on shutdown request - spam less in debug.log ## What was done? ## How Has This Been Tested? run tests, run `keypoolrefill` with `1100` (add 100 keys, no gui popup) and `10000` (100+ keys, progress bar) on testnet wallet, check logs, verify it can be interrupted on shutdown ## 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)_
1 parent a65b1fb commit bfa585d

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <logging.h>
88
#include <script/descriptor.h>
99
#include <script/sign.h>
10+
#include <shutdown.h>
1011
#include <ui_interface.h>
1112
#include <util/bip32.h>
1213
#include <util/strencodings.h>
@@ -1414,30 +1415,48 @@ bool LegacyScriptPubKeyMan::TopUpInner(unsigned int kpSize)
14141415
{
14151416
// don't create extra internal keys
14161417
missingInternal = 0;
1417-
} else {
1418-
nTargetSize *= 2;
14191418
}
1419+
1420+
const int64_t total_missing = missingInternal + missingExternal;
1421+
if (total_missing == 0) return true;
1422+
1423+
constexpr int64_t PROGRESS_REPORT_INTERVAL = 1; // in seconds
1424+
const bool should_show_progress = total_missing > 100;
1425+
const std::string strMsg = _("Topping up keypool...").translated;
1426+
1427+
int64_t progress_report_time = GetTime();
1428+
WalletLogPrintf("%s\n", strMsg);
1429+
if (should_show_progress) {
1430+
uiInterface.ShowProgress(strMsg, 0, false);
1431+
}
1432+
14201433
bool fInternal = false;
1434+
int64_t current_index{0};
14211435
WalletBatch batch(m_storage.GetDatabase());
1422-
for (int64_t i = missingInternal + missingExternal; i--;)
1423-
{
1424-
if (i < missingInternal) {
1436+
1437+
for (current_index = 0; current_index < total_missing; ++current_index) {
1438+
if (current_index == missingExternal) {
14251439
fInternal = true;
14261440
}
14271441

14281442
// TODO: implement keypools for all accounts?
14291443
CPubKey pubkey(GenerateNewKey(batch, 0, fInternal));
14301444
AddKeypoolPubkeyWithDB(pubkey, fInternal, batch);
14311445

1432-
if (missingInternal + missingExternal > 0) {
1433-
WalletLogPrintf("keypool added %d keys (%d internal), size=%u (%u internal)\n",
1434-
missingInternal + missingExternal, missingInternal,
1435-
setInternalKeyPool.size() + setExternalKeyPool.size(), setInternalKeyPool.size());
1446+
if (GetTime() >= progress_report_time + PROGRESS_REPORT_INTERVAL) {
1447+
const double dProgress = 100.f * current_index / total_missing;
1448+
progress_report_time = GetTime();
1449+
WalletLogPrintf("Still topping up. At key %lld. Progress=%f\n", current_index, dProgress);
1450+
if (should_show_progress) {
1451+
uiInterface.ShowProgress(strMsg, static_cast<int>(dProgress), false);
1452+
}
14361453
}
1437-
1438-
double dProgress = 100.f * m_max_keypool_index / (nTargetSize + 1);
1439-
std::string strMsg = strprintf(_("Loading wallet... (%3.2f %%)").translated, dProgress);
1440-
uiInterface.InitMessage(strMsg);
1454+
if (ShutdownRequested()) break;
1455+
}
1456+
WalletLogPrintf("Keypool added %d keys, size=%u (%u internal)\n",
1457+
current_index + 1, setInternalKeyPool.size() + setExternalKeyPool.size(), setInternalKeyPool.size());
1458+
if (should_show_progress) {
1459+
uiInterface.ShowProgress("", 100, false);
14411460
}
14421461
}
14431462
NotifyCanGetAddressesChanged();

0 commit comments

Comments
 (0)