From 40c80e36b1a204ed133acc403016a6cb1a92051e Mon Sep 17 00:00:00 2001 From: Ava Chow Date: Tue, 19 Dec 2023 16:52:35 -0500 Subject: [PATCH 1/2] wallettool: Don't unilaterally reset wallet_instance if loading error When there is a wallet loading error, it could be a noncritical one so it is not necessary to make wallet_instance a nullptr. The wallet can still go on with normal operation in that case, as we do for loading in bitcoind and bitcoin-qt. --- src/wallet/wallettool.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wallet/wallettool.cpp b/src/wallet/wallettool.cpp index 2f3f8ef77de..c8deda89b56 100644 --- a/src/wallet/wallettool.cpp +++ b/src/wallet/wallettool.cpp @@ -68,7 +68,6 @@ static std::shared_ptr MakeWallet(const std::string& name, const fs::pa } if (load_wallet_ret != DBErrors::LOAD_OK) { - wallet_instance = nullptr; if (load_wallet_ret == DBErrors::CORRUPT) { tfm::format(std::cerr, "Error loading %s: Wallet corrupted", name); return nullptr; From d83bea42d1f0ffb0899a6de3556c489543468995 Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Wed, 13 Oct 2021 18:45:39 -0400 Subject: [PATCH 2/2] wallettool: Don't create CWallet when dumping DB It's not necessary to set up an entire CWallet just so we can get access to the WalletDatabase and read the records. Instead we can go one level lower and make just a WalletDatabase. --- src/wallet/dump.cpp | 7 ++----- src/wallet/dump.h | 5 +++-- src/wallet/wallettool.cpp | 11 ++++++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/wallet/dump.cpp b/src/wallet/dump.cpp index 3ac5cf03b19..7a36910dc12 100644 --- a/src/wallet/dump.cpp +++ b/src/wallet/dump.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -20,7 +21,7 @@ namespace wallet { static const std::string DUMP_MAGIC = "BITCOIN_CORE_WALLET_DUMP"; uint32_t DUMP_VERSION = 1; -bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error) +bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& error) { // Get the dumpfile std::string dump_filename = args.GetArg("-dumpfile", ""); @@ -44,7 +45,6 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error) HashWriter hasher{}; - WalletDatabase& db = wallet.GetDatabase(); std::unique_ptr batch = db.MakeBatch(); bool ret = true; @@ -90,9 +90,6 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error) cursor.reset(); batch.reset(); - // Close the wallet after we're done with it. The caller won't be doing this - wallet.Close(); - if (ret) { // Write the hash tfm::format(dump_file, "checksum,%s\n", HexStr(hasher.GetHash())); diff --git a/src/wallet/dump.h b/src/wallet/dump.h index 5034f95479a..9b44af922e1 100644 --- a/src/wallet/dump.h +++ b/src/wallet/dump.h @@ -14,8 +14,9 @@ struct bilingual_str; class ArgsManager; namespace wallet { -class CWallet; -bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error); +class WalletDatabase; + +bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& error); bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::path& wallet_path, bilingual_str& error, std::vector& warnings); } // namespace wallet diff --git a/src/wallet/wallettool.cpp b/src/wallet/wallettool.cpp index c8deda89b56..cda344ab19c 100644 --- a/src/wallet/wallettool.cpp +++ b/src/wallet/wallettool.cpp @@ -193,10 +193,15 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command) DatabaseOptions options; ReadDatabaseArgs(args, options); options.require_existing = true; - const std::shared_ptr wallet_instance = MakeWallet(name, path, options); - if (!wallet_instance) return false; + DatabaseStatus status; bilingual_str error; - bool ret = DumpWallet(args, *wallet_instance, error); + std::unique_ptr database = MakeDatabase(path, options, status, error); + if (!database) { + tfm::format(std::cerr, "%s\n", error.original); + return false; + } + + bool ret = DumpWallet(args, *database, error); if (!ret && !error.empty()) { tfm::format(std::cerr, "%s\n", error.original); return ret;