Skip to content

Commit 04978c2

Browse files
committed
Merge bitcoin/bitcoin#29117: wallettool: Always be able to dump a wallet's database
d83bea4 wallettool: Don't create CWallet when dumping DB (Andrew Chow) 40c80e3 wallettool: Don't unilaterally reset wallet_instance if loading error (Ava Chow) Pull request description: bitcoin/bitcoin#29109 (comment) reports that a wallet with noncritical errors cannot be dumped with `bitcoin-wallet dump`. This was caused by an erroneous reset of the wallet pointer when the loading the wallet returns something other than `LOAD_OK`. Not all errors are errors that require aborting, so unilaterally resetting the pointer at that time is incorrect. The first commit resolves this issue. Furthermore, if a wallet has loading errors, that should not prevent the wallet tool from dumping the wallet. The wallet application logic should not get in the way of performing such a low level database operation, especially when it's primary usage is for debugging potentially corrupted wallets. The 2nd commit is taken from #28710 and changes the `dump` to stop at making a `WalletDatabase` rather than making a `CWallet` only to retrieve the underlying `WalletDatabase`. ACKs for top commit: furszy: Code review ACK d83bea4 BrandonOdiwuor: Code Review ACK d83bea4 Tree-SHA512: 425d712dfff1002bd81272aca0bae1016f9126a3c89506f8cb7cf0a0ec9f33d0c03b8d03896394f3a45c2998e59047e19218dfd08dc8a5f40e8625134e886b0f
2 parents cb6d619 + d83bea4 commit 04978c2

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/wallet/dump.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <util/fs.h>
99
#include <util/translation.h>
1010
#include <wallet/wallet.h>
11+
#include <wallet/walletdb.h>
1112

1213
#include <algorithm>
1314
#include <fstream>
@@ -20,7 +21,7 @@ namespace wallet {
2021
static const std::string DUMP_MAGIC = "BITCOIN_CORE_WALLET_DUMP";
2122
uint32_t DUMP_VERSION = 1;
2223

23-
bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
24+
bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& error)
2425
{
2526
// Get the dumpfile
2627
std::string dump_filename = args.GetArg("-dumpfile", "");
@@ -44,7 +45,6 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
4445

4546
HashWriter hasher{};
4647

47-
WalletDatabase& db = wallet.GetDatabase();
4848
std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
4949

5050
bool ret = true;
@@ -90,9 +90,6 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error)
9090
cursor.reset();
9191
batch.reset();
9292

93-
// Close the wallet after we're done with it. The caller won't be doing this
94-
wallet.Close();
95-
9693
if (ret) {
9794
// Write the hash
9895
tfm::format(dump_file, "checksum,%s\n", HexStr(hasher.GetHash()));

src/wallet/dump.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ struct bilingual_str;
1414
class ArgsManager;
1515

1616
namespace wallet {
17-
class CWallet;
18-
bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error);
17+
class WalletDatabase;
18+
19+
bool DumpWallet(const ArgsManager& args, WalletDatabase& db, bilingual_str& error);
1920
bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::path& wallet_path, bilingual_str& error, std::vector<bilingual_str>& warnings);
2021
} // namespace wallet
2122

src/wallet/wallettool.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ static std::shared_ptr<CWallet> MakeWallet(const std::string& name, const fs::pa
6868
}
6969

7070
if (load_wallet_ret != DBErrors::LOAD_OK) {
71-
wallet_instance = nullptr;
7271
if (load_wallet_ret == DBErrors::CORRUPT) {
7372
tfm::format(std::cerr, "Error loading %s: Wallet corrupted", name);
7473
return nullptr;
@@ -194,10 +193,15 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
194193
DatabaseOptions options;
195194
ReadDatabaseArgs(args, options);
196195
options.require_existing = true;
197-
const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
198-
if (!wallet_instance) return false;
196+
DatabaseStatus status;
199197
bilingual_str error;
200-
bool ret = DumpWallet(args, *wallet_instance, error);
198+
std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
199+
if (!database) {
200+
tfm::format(std::cerr, "%s\n", error.original);
201+
return false;
202+
}
203+
204+
bool ret = DumpWallet(args, *database, error);
201205
if (!ret && !error.empty()) {
202206
tfm::format(std::cerr, "%s\n", error.original);
203207
return ret;

0 commit comments

Comments
 (0)