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.

Github-Pull: #29117
Rebased-From: d83bea42d1
This commit is contained in:
Andrew Chow 2021-10-13 18:45:39 -04:00 committed by Luke Dashjr
parent 5b57429d58
commit 06926a787b
4 changed files with 17 additions and 10 deletions

View File

@ -8,6 +8,7 @@
#include <util/fs.h>
#include <util/translation.h>
#include <wallet/wallet.h>
#include <wallet/walletdb.h>
#include <algorithm>
#include <fstream>
@ -20,7 +21,7 @@ namespace wallet {
static const std::string DUMP_MAGIC = "BITCOIN_CORE_WALLET_DUMP";
uint32_t DUMP_VERSION = 1;
bool DumpWallet(CWallet& wallet, bilingual_str& error, const std::string& dump_filename)
bool DumpWallet(WalletDatabase& db, bilingual_str& error, const std::string& dump_filename)
{
fs::path path = fs::PathFromString(dump_filename);
path = fs::absolute(path);
@ -37,7 +38,6 @@ bool DumpWallet(CWallet& wallet, bilingual_str& error, const std::string& dump_f
HashWriter hasher{};
WalletDatabase& db = wallet.GetDatabase();
std::unique_ptr<DatabaseBatch> batch = db.MakeBatch();
bool ret = true;

View File

@ -14,8 +14,9 @@ struct bilingual_str;
class ArgsManager;
namespace wallet {
class CWallet;
bool DumpWallet(CWallet& wallet, bilingual_str& error, const std::string& dump_filename);
class WalletDatabase;
bool DumpWallet(WalletDatabase& db, bilingual_str& error, const std::string& dump_filename);
bool CreateFromDump(const ArgsManager& args, const std::string& name, const fs::path& wallet_path, bilingual_str& error, std::vector<bilingual_str>& warnings);
} // namespace wallet

View File

@ -155,7 +155,10 @@ public:
bool backupWallet(const std::string& filename, const WalletBackupFormat format, bilingual_str& error) override {
switch (format) {
case WalletBackupFormat::DbDump:
return DumpWallet(*m_wallet, error, filename);
{
WalletDatabase& db = m_wallet->GetDatabase();
return DumpWallet(db, error, filename);
}
case WalletBackupFormat::Raw:
return m_wallet->BackupWallet(filename);
}

View File

@ -194,8 +194,13 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
DatabaseOptions options;
ReadDatabaseArgs(args, options);
options.require_existing = true;
const std::shared_ptr<CWallet> wallet_instance = MakeWallet(name, path, options);
if (!wallet_instance) return false;
DatabaseStatus status;
bilingual_str error;
std::unique_ptr<WalletDatabase> database = MakeDatabase(path, options, status, error);
if (!database) {
tfm::format(std::cerr, "%s\n", error.original);
return false;
}
// Get the dumpfile
std::string dump_filename = args.GetArg("-dumpfile", "");
@ -204,9 +209,7 @@ bool ExecuteWalletToolFunc(const ArgsManager& args, const std::string& command)
return false;
}
bilingual_str error;
bool ret = DumpWallet(*wallet_instance, error, dump_filename);
wallet_instance->Close();
bool ret = DumpWallet(*database, error, dump_filename);
if (!ret && !error.empty()) {
tfm::format(std::cerr, "%s\n", error.original);
return ret;