Skip to content

Commit 28fc562

Browse files
committed
wallet, interfaces: Include database format in listWalletDir
1 parent 1873e41 commit 28fc562

File tree

8 files changed

+33
-25
lines changed

8 files changed

+33
-25
lines changed

src/interfaces/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ class WalletLoader : public ChainClient
343343
virtual util::Result<WalletMigrationResult> migrateWallet(const std::string& name, const SecureString& passphrase) = 0;
344344

345345
//! Return available wallets in wallet directory.
346-
virtual std::vector<std::string> listWalletDir() = 0;
346+
virtual std::vector<std::pair<std::string, std::string>> listWalletDir() = 0;
347347

348348
//! Return interfaces for accessing wallets (if any).
349349
virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;

src/qt/bitcoingui.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,15 @@ void BitcoinGUI::createActions()
396396
connect(openAction, &QAction::triggered, this, &BitcoinGUI::openClicked);
397397
connect(m_open_wallet_menu, &QMenu::aboutToShow, [this] {
398398
m_open_wallet_menu->clear();
399-
for (const std::pair<const std::string, bool>& i : m_wallet_controller->listWalletDir()) {
400-
const std::string& path = i.first;
399+
for (const auto& [path, info] : m_wallet_controller->listWalletDir()) {
400+
const auto& [loaded, _] = info;
401401
QString name = path.empty() ? QString("["+tr("default wallet")+"]") : QString::fromStdString(path);
402402
// An single ampersand in the menu item's text sets a shortcut for this item.
403403
// Single & are shown when && is in the string. So replace & with &&.
404404
name.replace(QChar('&'), QString("&&"));
405405
QAction* action = m_open_wallet_menu->addAction(name);
406406

407-
if (i.second) {
407+
if (loaded) {
408408
// This wallet is already loaded
409409
action->setEnabled(false);
410410
continue;

src/qt/walletcontroller.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ WalletController::~WalletController()
6565
delete m_activity_worker;
6666
}
6767

68-
std::map<std::string, bool> WalletController::listWalletDir() const
68+
std::map<std::string, std::pair<bool, std::string>> WalletController::listWalletDir() const
6969
{
7070
QMutexLocker locker(&m_mutex);
71-
std::map<std::string, bool> wallets;
72-
for (const std::string& name : m_node.walletLoader().listWalletDir()) {
73-
wallets[name] = false;
71+
std::map<std::string, std::pair<bool, std::string>> wallets;
72+
for (const auto& [name, format] : m_node.walletLoader().listWalletDir()) {
73+
wallets[name] = std::make_pair(false, format);
7474
}
7575
for (WalletModel* wallet_model : m_wallets) {
7676
auto it = wallets.find(wallet_model->wallet().getWalletName());
77-
if (it != wallets.end()) it->second = true;
77+
if (it != wallets.end()) it->second.first = true;
7878
}
7979
return wallets;
8080
}

src/qt/walletcontroller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class WalletController : public QObject
6161

6262
//! Returns all wallet names in the wallet dir mapped to whether the wallet
6363
//! is loaded.
64-
std::map<std::string, bool> listWalletDir() const;
64+
std::map<std::string, std::pair<bool, std::string>> listWalletDir() const;
6565

6666
void closeWallet(WalletModel* wallet_model, QWidget* parent = nullptr);
6767
void closeAllWallets(QWidget* parent = nullptr);

src/wallet/db.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ namespace wallet {
1919
bool operator<(BytePrefix a, Span<const std::byte> b) { return a.prefix < b.subspan(0, std::min(a.prefix.size(), b.size())); }
2020
bool operator<(Span<const std::byte> a, BytePrefix b) { return a.subspan(0, std::min(a.size(), b.prefix.size())) < b.prefix; }
2121

22-
std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
22+
std::vector<std::pair<fs::path, std::string>> ListDatabases(const fs::path& wallet_dir)
2323
{
24-
std::vector<fs::path> paths;
24+
std::vector<std::pair<fs::path, std::string>> paths;
2525
std::error_code ec;
2626

2727
for (auto it = fs::recursive_directory_iterator(wallet_dir, ec); it != fs::recursive_directory_iterator(); it.increment(ec)) {
@@ -38,21 +38,29 @@ std::vector<fs::path> ListDatabases(const fs::path& wallet_dir)
3838
try {
3939
const fs::path path{it->path().lexically_relative(wallet_dir)};
4040

41-
if (it->status().type() == fs::file_type::directory &&
42-
(IsBDBFile(BDBDataFile(it->path())) || IsSQLiteFile(SQLiteDataFile(it->path())))) {
43-
// Found a directory which contains wallet.dat btree file, add it as a wallet.
44-
paths.emplace_back(path);
41+
if (it->status().type() == fs::file_type::directory) {
42+
if (IsBDBFile(BDBDataFile(it->path()))) {
43+
// Found a directory which contains wallet.dat btree file, add it as a wallet with BERKELEY format.
44+
paths.emplace_back(path, "bdb");
45+
} else if (IsSQLiteFile(SQLiteDataFile(it->path()))) {
46+
// Found a directory which contains wallet.dat sqlite file, add it as a wallet with SQLITE format.
47+
paths.emplace_back(path, "sqlite");
48+
}
4549
} else if (it.depth() == 0 && it->symlink_status().type() == fs::file_type::regular && it->path().extension() != ".bak") {
4650
if (it->path().filename() == "wallet.dat") {
47-
// Found top-level wallet.dat btree file, add top level directory ""
51+
// Found top-level wallet.dat file, add top level directory ""
4852
// as a wallet.
49-
paths.emplace_back();
53+
if (IsBDBFile(it->path())) {
54+
paths.emplace_back(fs::path(), "bdb");
55+
} else if (IsSQLiteFile(it->path())) {
56+
paths.emplace_back(fs::path(), "sqlite");
57+
}
5058
} else if (IsBDBFile(it->path())) {
5159
// Found top-level btree file not called wallet.dat. Current bitcoin
5260
// software will never create these files but will allow them to be
5361
// opened in a shared database environment for backwards compatibility.
5462
// Add it to the list of available wallets.
55-
paths.emplace_back(path);
63+
paths.emplace_back(path, "bdb");
5664
}
5765
}
5866
} catch (const std::exception& e) {

src/wallet/db.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ enum class DatabaseStatus {
216216
};
217217

218218
/** Recursively list database paths in directory. */
219-
std::vector<fs::path> ListDatabases(const fs::path& path);
219+
std::vector<std::pair<fs::path, std::string>> ListDatabases(const fs::path& path);
220220

221221
void ReadDatabaseArgs(const ArgsManager& args, DatabaseOptions& options);
222222
std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);

src/wallet/interfaces.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,11 @@ class WalletLoaderImpl : public WalletLoader
656656
{
657657
return fs::PathToString(GetWalletDir());
658658
}
659-
std::vector<std::string> listWalletDir() override
659+
std::vector<std::pair<std::string, std::string>> listWalletDir() override
660660
{
661-
std::vector<std::string> paths;
662-
for (auto& path : ListDatabases(GetWalletDir())) {
663-
paths.push_back(fs::PathToString(path));
661+
std::vector<std::pair<std::string, std::string>> paths;
662+
for (auto& [path, format] : ListDatabases(GetWalletDir())) {
663+
paths.emplace_back(fs::PathToString(path), format);
664664
}
665665
return paths;
666666
}

src/wallet/rpc/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static RPCHelpMan listwalletdir()
169169
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
170170
{
171171
UniValue wallets(UniValue::VARR);
172-
for (const auto& path : ListDatabases(GetWalletDir())) {
172+
for (const auto& [path, _] : ListDatabases(GetWalletDir())) {
173173
UniValue wallet(UniValue::VOBJ);
174174
wallet.pushKV("name", path.utf8string());
175175
wallets.push_back(std::move(wallet));

0 commit comments

Comments
 (0)