Files
bitcoin/src/wallet/load.cpp
Ava Chow 4ae00e9a71 Merge bitcoin/bitcoin#32636: Split CWallet::Create() into CreateNew and LoadExisting
db2effaca4 scripted-diff: refactor: CWallet::Create() -> CreateNew() (David Gumberg)
27e021ebc0 wallet: Correctly log stats for encrypted messages. (David Gumberg)
d8bec61be2 wallet: remove loading logic from CWallet::Create (David Gumberg)
f35acc893f refactor: wallet: Factor out `WriteVersion()` from `PopulateWalletFromDB()` (David Gumberg)
e12ff8aca0 test: wallet: Split create and load (David Gumberg)
70dbc79b09 wallet: Use CWallet::LoadExisting() for loading existing wallets. (David Gumberg)
ae66e01164 wallet: Create separate function for wallet load (David Gumberg)
bc69070416 refactor: Wallet stats logging in its own function (David Gumberg)
a9d64cd49c wallet: Remove redundant birth time update (David Gumberg)
b4a49cc727 wallet: Move argument parsing to before DB load (David Gumberg)
b15a94a618 refactor: Split out wallet argument loading (David Gumberg)
a02c4a82d8 refactor: Move -walletbroadcast setting init (David Gumberg)
411caf7281 wallet: refactor: PopulateWalletFromDB use switch statement. (David Gumberg)
a48e23f566 refactor: wallet: move error handling to PopulateWalletFromDB() (David Gumberg)
0972785fd7 wallet: Delete unnecessary PopulateWalletFromDB() calls (David Gumberg)
f0a046094e scripted-diff: refactor: CWallet::LoadWallet->PopulateWalletFromDB (David Gumberg)

Pull request description:

  This PR is mostly a refactor which splits out logic used for creating wallets and for loading wallets, both of which are presently contained in `CWallet::Create()` into `CWallet::CreateNew()` and `CWallet::LoadExisting()`

  The real win of this PR is that `CWallet::Create()` uses a very bad heuristic for trying to guess whether or not it is supposed to be creating a new wallet or loading an existing wallet:

  370c592612/src/wallet/wallet.cpp (L2882-L2885)

  This heuristic assumes that wallets with no `ScriptPubKeyMans` are being created, which sounds reasonable, but as demonstrated in #32112 and #32111, this can happen when the user tries to load a wallet file that is corrupted, both issues are fixed by this PR and any other misbehavior for wallet files which succeeded the broken heuristic's sniff test for new wallets.

  It was already the case that every caller of `CWallet::Create()` knows whether it is creating a wallet or loading one, so we can avoid replacing this bad heuristic with another one, and just shift the burden to the caller.

ACKs for top commit:
  achow101:
    ACK db2effaca4
  polespinasa:
    approach ACK db2effaca4
  w0xlt:
    reACK db2effaca4
  murchandamus:
    ACK db2effaca4
  rkrux:
    ACK db2effaca4

Tree-SHA512: c28d60e0a3001058da3fd2bdbe0726c7ebe742a4b900a1dee2e5132eccc22e49619cb747a99b4032b000eafd4aa2fdd4ec244c32be2012aba809fdc94b5f6ecd
2026-02-04 11:06:36 -08:00

188 lines
7.6 KiB
C++

// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <wallet/load.h>
#include <common/args.h>
#include <interfaces/chain.h>
#include <scheduler.h>
#include <util/check.h>
#include <util/fs.h>
#include <util/string.h>
#include <util/translation.h>
#include <wallet/context.h>
#include <wallet/spend.h>
#include <wallet/wallet.h>
#include <wallet/walletdb.h>
#include <univalue.h>
#include <system_error>
using util::Join;
namespace wallet {
bool VerifyWallets(WalletContext& context)
{
interfaces::Chain& chain = *context.chain;
ArgsManager& args = *Assert(context.args);
if (args.IsArgSet("-walletdir")) {
const fs::path wallet_dir{args.GetPathArg("-walletdir")};
std::error_code error;
// The canonical path cleans the path, preventing >1 Berkeley environment instances for the same directory
// It also lets the fs::exists and fs::is_directory checks below pass on windows, since they return false
// if a path has trailing slashes, and it strips trailing slashes.
fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error);
if (error || !fs::exists(canonical_wallet_dir)) {
chain.initError(strprintf(_("Specified -walletdir \"%s\" does not exist"), fs::PathToString(wallet_dir)));
return false;
} else if (!fs::is_directory(canonical_wallet_dir)) {
chain.initError(strprintf(_("Specified -walletdir \"%s\" is not a directory"), fs::PathToString(wallet_dir)));
return false;
// The canonical path transforms relative paths into absolute ones, so we check the non-canonical version
} else if (!wallet_dir.is_absolute()) {
chain.initError(strprintf(_("Specified -walletdir \"%s\" is a relative path"), fs::PathToString(wallet_dir)));
return false;
}
args.ForceSetArg("-walletdir", fs::PathToString(canonical_wallet_dir));
}
LogInfo("Using wallet directory %s", fs::PathToString(GetWalletDir()));
// Print general DB information
LogDBInfo();
chain.initMessage(_("Verifying wallet(s)…"));
// For backwards compatibility if an unnamed top level wallet exists in the
// wallets directory, include it in the default list of wallets to load.
if (!args.IsArgSet("wallet")) {
DatabaseOptions options;
DatabaseStatus status;
ReadDatabaseArgs(args, options);
bilingual_str error_string;
options.require_existing = true;
options.verify = false;
if (MakeWalletDatabase("", options, status, error_string)) {
common::SettingsValue wallets(common::SettingsValue::VARR);
wallets.push_back(""); // Default wallet name is ""
// Pass write=false because no need to write file and probably
// better not to. If unnamed wallet needs to be added next startup
// and the setting is empty, this code will just run again.
chain.overwriteRwSetting("wallet", std::move(wallets), interfaces::SettingsAction::SKIP_WRITE);
}
}
// Keep track of each wallet absolute path to detect duplicates.
std::set<fs::path> wallet_paths;
for (const auto& wallet : chain.getSettingsList("wallet")) {
if (!wallet.isStr()) {
chain.initError(_("Invalid value detected for '-wallet' or '-nowallet'. "
"'-wallet' requires a string value, while '-nowallet' accepts only '1' to disable all wallets"));
return false;
}
const auto& wallet_file = wallet.get_str();
const fs::path path = fsbridge::AbsPathJoin(GetWalletDir(), fs::PathFromString(wallet_file));
if (!wallet_paths.insert(path).second) {
chain.initWarning(strprintf(_("Ignoring duplicate -wallet %s."), wallet_file));
continue;
}
DatabaseOptions options;
DatabaseStatus status;
ReadDatabaseArgs(args, options);
options.require_existing = true;
options.verify = true;
bilingual_str error_string;
if (!MakeWalletDatabase(wallet_file, options, status, error_string)) {
if (status == DatabaseStatus::FAILED_NOT_FOUND) {
chain.initWarning(Untranslated(strprintf("Skipping -wallet path that doesn't exist. %s", error_string.original)));
} else if (status == DatabaseStatus::FAILED_LEGACY_DISABLED) {
// Skipping legacy wallets as they will not be loaded.
// This will be properly communicated to the user during the loading process.
continue;
} else {
chain.initError(error_string);
return false;
}
}
}
return true;
}
bool LoadWallets(WalletContext& context)
{
interfaces::Chain& chain = *context.chain;
try {
std::set<fs::path> wallet_paths;
for (const auto& wallet : chain.getSettingsList("wallet")) {
if (!wallet.isStr()) {
chain.initError(_("Invalid value detected for '-wallet' or '-nowallet'. "
"'-wallet' requires a string value, while '-nowallet' accepts only '1' to disable all wallets"));
return false;
}
const auto& name = wallet.get_str();
if (!wallet_paths.insert(fs::PathFromString(name)).second) {
continue;
}
DatabaseOptions options;
DatabaseStatus status;
ReadDatabaseArgs(*context.args, options);
options.require_existing = true;
options.verify = false; // No need to verify, assuming verified earlier in VerifyWallets()
bilingual_str error;
std::vector<bilingual_str> warnings;
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
if (!database) {
if (status == DatabaseStatus::FAILED_NOT_FOUND) continue;
if (status == DatabaseStatus::FAILED_LEGACY_DISABLED) {
// Inform user that legacy wallet is not loaded and suggest upgrade options
chain.initWarning(error);
continue;
}
}
chain.initMessage(_("Loading wallet…"));
std::shared_ptr<CWallet> pwallet = database ? CWallet::LoadExisting(context, name, std::move(database), error, warnings) : nullptr;
if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
if (!pwallet) {
chain.initError(error);
return false;
}
NotifyWalletLoaded(context, pwallet);
AddWallet(context, pwallet);
}
return true;
} catch (const std::runtime_error& e) {
chain.initError(Untranslated(e.what()));
return false;
}
}
void StartWallets(WalletContext& context)
{
for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
pwallet->postInitProcess();
}
context.scheduler->scheduleEvery([&context] { MaybeResendWalletTxs(context); }, 1min);
}
void UnloadWallets(WalletContext& context)
{
auto wallets = GetWallets(context);
while (!wallets.empty()) {
auto wallet = wallets.back();
wallets.pop_back();
std::vector<bilingual_str> warnings;
RemoveWallet(context, wallet, /* load_on_start= */ std::nullopt, warnings);
WaitForDeleteWallet(std::move(wallet));
}
}
} // namespace wallet