Skip to content

Commit 56d47e1

Browse files
committed
Merge #19619: Remove wallet.dat path handling from wallet.cpp, rpcwallet.cpp
7bf6dfb wallet: Remove path checking code from bitcoin-wallet tool (Russell Yanofsky) 77d5bb7 wallet: Remove path checking code from createwallet RPC (Russell Yanofsky) a987438 wallet: Remove path checking code from loadwallet RPC (Russell Yanofsky) 8b5e729 refactor: Pass wallet database into CWallet::Create (Russell Yanofsky) 3c815cf wallet: Remove Verify and IsLoaded methods (Russell Yanofsky) 0d94e60 refactor: Use DatabaseStatus and DatabaseOptions types (Russell Yanofsky) b5b4141 wallet: Add MakeDatabase function (Russell Yanofsky) 288b4ff Remove WalletLocation class (Russell Yanofsky) Pull request description: Get rid of file path handling in wallet application code and move it down to database layer. There is no change in behavior except for some changed error messages. Motivation for this change is to make code more understandable, but also to prepare for adding SQLite support in #19077 so SQLite implementation can be contained at the database layer and wallet loading code does not need to become more complicated. ACKs for top commit: achow101: ACK 7bf6dfb meshcollider: Code re-review and functional test run ACK 7bf6dfb Tree-SHA512: 23ad18324c9e8947f0cf88a3734c2e9fb25536b2cb4d552cf5d1a4ade320fbffb73bb2d1b3a99585c11630aa7092e0fcfc2dd4fe65b91e3a54161433a5cd13cb
2 parents af8135e + 7bf6dfb commit 56d47e1

30 files changed

+291
-270
lines changed

src/bench/coin_selection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static void CoinSelection(benchmark::Bench& bench)
3131
{
3232
NodeContext node;
3333
auto chain = interfaces::MakeChain(node);
34-
CWallet wallet(chain.get(), WalletLocation(), CreateDummyWalletDatabase());
34+
CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
3535
wallet.SetupLegacyScriptPubKeyMan();
3636
std::vector<std::unique_ptr<CWalletTx>> wtxs;
3737
LOCK(wallet.cs_wallet);
@@ -65,7 +65,7 @@ static void CoinSelection(benchmark::Bench& bench)
6565
typedef std::set<CInputCoin> CoinSet;
6666
static NodeContext testNode;
6767
static auto testChain = interfaces::MakeChain(testNode);
68-
static CWallet testWallet(testChain.get(), WalletLocation(), CreateDummyWalletDatabase());
68+
static CWallet testWallet(testChain.get(), "", CreateDummyWalletDatabase());
6969
std::vector<std::unique_ptr<CWalletTx>> wtxn;
7070

7171
// Copied from src/wallet/test/coinselector_tests.cpp

src/bench/wallet_balance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static void WalletBalance(benchmark::Bench& bench, const bool set_dirty, const b
2626

2727
NodeContext node;
2828
std::unique_ptr<interfaces::Chain> chain = interfaces::MakeChain(node);
29-
CWallet wallet{chain.get(), WalletLocation(), CreateMockWalletDatabase()};
29+
CWallet wallet{chain.get(), "", CreateMockWalletDatabase()};
3030
{
3131
wallet.SetupLegacyScriptPubKeyMan();
3232
bool first_run;

src/interfaces/wallet.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -514,15 +514,22 @@ class WalletClientImpl : public WalletClient
514514
void setMockTime(int64_t time) override { return SetMockTime(time); }
515515

516516
//! WalletClient methods
517-
std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, WalletCreationStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings) override
517+
std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings) override
518518
{
519519
std::shared_ptr<CWallet> wallet;
520-
status = CreateWallet(*m_context.chain, passphrase, wallet_creation_flags, name, true /* load_on_start */, error, warnings, wallet);
521-
return MakeWallet(std::move(wallet));
520+
DatabaseOptions options;
521+
DatabaseStatus status;
522+
options.require_create = true;
523+
options.create_flags = wallet_creation_flags;
524+
options.create_passphrase = passphrase;
525+
return MakeWallet(CreateWallet(*m_context.chain, name, true /* load_on_start */, options, status, error, warnings));
522526
}
523527
std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
524528
{
525-
return MakeWallet(LoadWallet(*m_context.chain, WalletLocation(name), true /* load_on_start */, error, warnings));
529+
DatabaseOptions options;
530+
DatabaseStatus status;
531+
options.require_existing = true;
532+
return MakeWallet(LoadWallet(*m_context.chain, name, true /* load_on_start */, options, status, error, warnings));
526533
}
527534
std::string getWalletDir() override
528535
{

src/interfaces/wallet.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class CWallet;
2929
enum class FeeReason;
3030
enum class OutputType;
3131
enum class TransactionError;
32-
enum class WalletCreationStatus;
3332
enum isminetype : unsigned int;
3433
struct CRecipient;
3534
struct PartiallySignedTransaction;
@@ -311,7 +310,7 @@ class WalletClient : public ChainClient
311310
{
312311
public:
313312
//! Create new wallet.
314-
virtual std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, WalletCreationStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
313+
virtual std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
315314

316315
//! Load existing wallet.
317316
virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;

src/qt/test/addressbooktests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void TestAddAddressesToSendBook(interfaces::Node& node)
6161
{
6262
TestChain100Setup test;
6363
node.setContext(&test.m_node);
64-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), WalletLocation(), CreateMockWalletDatabase());
64+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
6565
wallet->SetupLegacyScriptPubKeyMan();
6666
bool firstRun;
6767
wallet->LoadWallet(firstRun);

src/qt/test/wallettests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void TestGUI(interfaces::Node& node)
139139
test.CreateAndProcessBlock({}, GetScriptForRawPubKey(test.coinbaseKey.GetPubKey()));
140140
}
141141
node.setContext(&test.m_node);
142-
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), WalletLocation(), CreateMockWalletDatabase());
142+
std::shared_ptr<CWallet> wallet = std::make_shared<CWallet>(node.context()->chain.get(), "", CreateMockWalletDatabase());
143143
bool firstRun;
144144
wallet->LoadWallet(firstRun);
145145
{

src/qt/walletcontroller.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,9 @@ void CreateWalletActivity::createWallet()
249249
}
250250

251251
QTimer::singleShot(500, worker(), [this, name, flags] {
252-
WalletCreationStatus status;
253-
std::unique_ptr<interfaces::Wallet> wallet = node().walletClient().createWallet(name, m_passphrase, flags, status, m_error_message, m_warning_message);
252+
std::unique_ptr<interfaces::Wallet> wallet = node().walletClient().createWallet(name, m_passphrase, flags, m_error_message, m_warning_message);
254253

255-
if (status == WalletCreationStatus::SUCCESS) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
254+
if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));
256255

257256
QTimer::singleShot(500, this, &CreateWalletActivity::finish);
258257
});

src/wallet/bdb.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,6 @@ bool WalletDatabaseFileId::operator==(const WalletDatabaseFileId& rhs) const
5252
return memcmp(value, &rhs.value, sizeof(value)) == 0;
5353
}
5454

55-
bool IsBDBWalletLoaded(const fs::path& wallet_path)
56-
{
57-
fs::path env_directory;
58-
std::string database_filename;
59-
SplitWalletPath(wallet_path, env_directory, database_filename);
60-
LOCK(cs_db);
61-
auto env = g_dbenvs.find(env_directory.string());
62-
if (env == g_dbenvs.end()) return false;
63-
auto database = env->second.lock();
64-
return database && database->IsDatabaseLoaded(database_filename);
65-
}
66-
6755
/**
6856
* @param[in] wallet_path Path to wallet directory. Or (for backwards compatibility only) a path to a berkeley btree data file inside a wallet directory.
6957
* @param[out] database_filename Filename of berkeley btree data file inside the wallet directory.
@@ -371,7 +359,6 @@ void BerkeleyDatabase::Open(const char* pszMode)
371359
if (ret != 0) {
372360
throw std::runtime_error(strprintf("BerkeleyDatabase: Error %d, can't open database %s", ret, strFile));
373361
}
374-
m_file_path = (env->Directory() / strFile).string();
375362

376363
// Call CheckUniqueFileid on the containing BDB environment to
377364
// avoid BDB data consistency bugs that happen when different data
@@ -824,3 +811,35 @@ std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(const char* mode, boo
824811
{
825812
return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close);
826813
}
814+
815+
bool ExistsBerkeleyDatabase(const fs::path& path)
816+
{
817+
fs::path env_directory;
818+
std::string data_filename;
819+
SplitWalletPath(path, env_directory, data_filename);
820+
return IsBerkeleyBtree(env_directory / data_filename);
821+
}
822+
823+
std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)
824+
{
825+
std::unique_ptr<BerkeleyDatabase> db;
826+
{
827+
LOCK(cs_db); // Lock env.m_databases until insert in BerkeleyDatabase constructor
828+
std::string data_filename;
829+
std::shared_ptr<BerkeleyEnvironment> env = GetWalletEnv(path, data_filename);
830+
if (env->m_databases.count(data_filename)) {
831+
error = Untranslated(strprintf("Refusing to load database. Data file '%s' is already loaded.", (env->Directory() / data_filename).string()));
832+
status = DatabaseStatus::FAILED_ALREADY_LOADED;
833+
return nullptr;
834+
}
835+
db = MakeUnique<BerkeleyDatabase>(std::move(env), std::move(data_filename));
836+
}
837+
838+
if (options.verify && !db->Verify(error)) {
839+
status = DatabaseStatus::FAILED_VERIFY;
840+
return nullptr;
841+
}
842+
843+
status = DatabaseStatus::SUCCESS;
844+
return db;
845+
}

src/wallet/bdb.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class BerkeleyEnvironment
6363

6464
bool IsMock() const { return fMockDb; }
6565
bool IsInitialized() const { return fDbEnvInit; }
66-
bool IsDatabaseLoaded(const std::string& db_filename) const { return m_databases.find(db_filename) != m_databases.end(); }
6766
fs::path Directory() const { return strPath; }
6867

6968
bool Open(bilingual_str& error);
@@ -87,8 +86,8 @@ class BerkeleyEnvironment
8786
/** Get BerkeleyEnvironment and database filename given a wallet path. */
8887
std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& wallet_path, std::string& database_filename);
8988

90-
/** Return whether a BDB wallet database is currently loaded. */
91-
bool IsBDBWalletLoaded(const fs::path& wallet_path);
89+
/** Check format of database file */
90+
bool IsBerkeleyBtree(const fs::path& path);
9291

9392
class BerkeleyBatch;
9493

@@ -143,7 +142,10 @@ class BerkeleyDatabase : public WalletDatabase
143142
void ReloadDbEnv() override;
144143

145144
/** Verifies the environment and database file */
146-
bool Verify(bilingual_str& error) override;
145+
bool Verify(bilingual_str& error);
146+
147+
/** Return path to main database filename */
148+
std::string Filename() override { return (env->Directory() / strFile).string(); }
147149

148150
/**
149151
* Pointer to shared database environment.
@@ -224,4 +226,10 @@ class BerkeleyBatch : public DatabaseBatch
224226

225227
std::string BerkeleyDatabaseVersion();
226228

229+
//! Check if Berkeley database exists at specified path.
230+
bool ExistsBerkeleyDatabase(const fs::path& path);
231+
232+
//! Return object giving access to Berkeley database at specified path.
233+
std::unique_ptr<BerkeleyDatabase> MakeBerkeleyDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
234+
227235
#endif // BITCOIN_WALLET_BDB_H

src/wallet/db.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,3 @@ void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::
2323
database_filename = "wallet.dat";
2424
}
2525
}
26-
27-
fs::path WalletDataFilePath(const fs::path& wallet_path)
28-
{
29-
fs::path env_directory;
30-
std::string database_filename;
31-
SplitWalletPath(wallet_path, env_directory, database_filename);
32-
return env_directory / database_filename;
33-
}

0 commit comments

Comments
 (0)