wallet: refactor: PopulateWalletFromDB use switch statement.
What changed, and why it matters
This commit is a straightforward code cleanup in Bitcoin Core's wallet loading logic. It replaces a long chain of 'if/else if' checks with a 'switch' statement that handles the same set of wallet database error codes. No behavior changes are visible in the diff, and no security issue is introduced or fixed.
No security action needed. This is a non-functional refactor. Normal code review and merge processes apply.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change refactors CWallet::PopulateWalletFromDB in src/wallet/wallet.cpp. It converts an if/else-if ladder over DBErrors enum values into an exhaustive switch statement. The same error strings are produced for each case, and the comment explicitly notes there is no default case so the compiler can warn about missing enum values. A new explicit case for DBErrors::LOAD_FAIL is added, but it produces the same fallback error message that the previous else branch produced. The wallet_file variable is hoisted out of the conditional block.
Changed components
src/wallet/wallet.cppCWallet::PopulateWalletFromDBInspect captured patch +41 / −30
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 027bfeef..8e1ab28f 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -2312,36 +2312,47 @@ DBErrors CWallet::PopulateWalletFromDB(bilingual_str& error, std::vector<bilingu
assert(m_internal_spk_managers.empty());
}
- if (nLoadWalletRet != DBErrors::LOAD_OK) {
- const auto wallet_file = m_database->Filename();
- if (nLoadWalletRet == DBErrors::CORRUPT) {
- error = strprintf(_("Error loading %s: Wallet corrupted"), wallet_file);
- } else if (nLoadWalletRet == DBErrors::NONCRITICAL_ERROR) {
- warnings.push_back(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
- " or address metadata may be missing or incorrect."),
- wallet_file));
- } else if (nLoadWalletRet == DBErrors::TOO_NEW) {
- error = strprintf(_("Error loading %s: Wallet requires newer version of %s"), wallet_file, CLIENT_NAME);
- } else if (nLoadWalletRet == DBErrors::EXTERNAL_SIGNER_SUPPORT_REQUIRED) {
- error = strprintf(_("Error loading %s: External signer wallet being loaded without external signer support compiled"), wallet_file);
- } else if (nLoadWalletRet == DBErrors::NEED_REWRITE) {
- error = strprintf(_("Wallet needed to be rewritten: restart %s to complete"), CLIENT_NAME);
- } else if (nLoadWalletRet == DBErrors::NEED_RESCAN) {
- warnings.push_back(strprintf(_("Error reading %s! Transaction data may be missing or incorrect."
- " Rescanning wallet."), wallet_file));
- } else if (nLoadWalletRet == DBErrors::UNKNOWN_DESCRIPTOR) {
- error = strprintf(_("Unrecognized descriptor found. Loading wallet %s\n\n"
- "The wallet might have been created on a newer version.\n"
- "Please try running the latest software version.\n"), wallet_file);
- } else if (nLoadWalletRet == DBErrors::UNEXPECTED_LEGACY_ENTRY) {
- error = strprintf(_("Unexpected legacy entry in descriptor wallet found. Loading wallet %s\n\n"
- "The wallet might have been tampered with or created with malicious intent.\n"), wallet_file);
- } else if (nLoadWalletRet == DBErrors::LEGACY_WALLET) {
- error = strprintf(_("Error loading %s: Wallet is a legacy wallet. Please migrate to a descriptor wallet using the migration tool (migratewallet RPC)."), wallet_file);
- } else {
- error = strprintf(_("Error loading %s"), wallet_file);
- }
- }
+ const auto wallet_file = m_database->Filename();
+ switch (nLoadWalletRet) {
+ case DBErrors::LOAD_OK:
+ break;
+ case DBErrors::NONCRITICAL_ERROR:
+ warnings.push_back(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
+ " or address metadata may be missing or incorrect."),
+ wallet_file));
+ break;
+ case DBErrors::NEED_RESCAN:
+ warnings.push_back(strprintf(_("Error reading %s! Transaction data may be missing or incorrect."
+ " Rescanning wallet."), wallet_file));
+ break;
+ case DBErrors::CORRUPT:
+ error = strprintf(_("Error loading %s: Wallet corrupted"), wallet_file);
+ break;
+ case DBErrors::TOO_NEW:
+ error = strprintf(_("Error loading %s: Wallet requires newer version of %s"), wallet_file, CLIENT_NAME);
+ break;
+ case DBErrors::EXTERNAL_SIGNER_SUPPORT_REQUIRED:
+ error = strprintf(_("Error loading %s: External signer wallet being loaded without external signer support compiled"), wallet_file);
+ break;
+ case DBErrors::NEED_REWRITE:
+ error = strprintf(_("Wallet needed to be rewritten: restart %s to complete"), CLIENT_NAME);
+ break;
+ case DBErrors::UNKNOWN_DESCRIPTOR:
+ error = strprintf(_("Unrecognized descriptor found. Loading wallet %s\n\n"
+ "The wallet might have been created on a newer version.\n"
+ "Please try running the latest software version.\n"), wallet_file);
+ break;
+ case DBErrors::UNEXPECTED_LEGACY_ENTRY:
+ error = strprintf(_("Unexpected legacy entry in descriptor wallet found. Loading wallet %s\n\n"
+ "The wallet might have been tampered with or created with malicious intent.\n"), wallet_file);
+ break;
+ case DBErrors::LEGACY_WALLET:
+ error = strprintf(_("Error loading %s: Wallet is a legacy wallet. Please migrate to a descriptor wallet using the migration tool (migratewallet RPC)."), wallet_file);
+ break;
+ case DBErrors::LOAD_FAIL:
+ error = strprintf(_("Error loading %s"), wallet_file);
+ break;
+ } // no default case, so the compiler can warn about missing cases
return nLoadWalletRet;
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.