Skip to content

Commit 8b5e729

Browse files
committed
refactor: Pass wallet database into CWallet::Create
No changes in behavior
1 parent 3c815cf commit 8b5e729

File tree

8 files changed

+27
-25
lines changed

8 files changed

+27
-25
lines changed

src/wallet/bdb.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ void BerkeleyDatabase::Open(const char* pszMode)
359359
if (ret != 0) {
360360
throw std::runtime_error(strprintf("BerkeleyDatabase: Error %d, can't open database %s", ret, strFile));
361361
}
362-
m_file_path = (env->Directory() / strFile).string();
363362

364363
// Call CheckUniqueFileid on the containing BDB environment to
365364
// avoid BDB data consistency bugs that happen when different data

src/wallet/bdb.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ class BerkeleyDatabase : public WalletDatabase
144144
/** Verifies the environment and database file */
145145
bool Verify(bilingual_str& error);
146146

147+
/** Return path to main database filename */
148+
std::string Filename() override { return (env->Directory() / strFile).string(); }
149+
147150
/**
148151
* Pointer to shared database environment.
149152
*

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-
}

src/wallet/db.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
struct bilingual_str;
2020

21-
/** Given a wallet directory path or legacy file path, return path to main data file in the wallet database. */
22-
fs::path WalletDataFilePath(const fs::path& wallet_path);
2321
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
2422

2523
/** RAII class that provides access to a WalletDatabase */
@@ -142,13 +140,14 @@ class WalletDatabase
142140

143141
virtual void ReloadDbEnv() = 0;
144142

143+
/** Return path to main database file for logs and error messages. */
144+
virtual std::string Filename() = 0;
145+
145146
std::atomic<unsigned int> nUpdateCounter;
146147
unsigned int nLastSeen;
147148
unsigned int nLastFlushed;
148149
int64_t nLastWalletUpdate;
149150

150-
std::string m_file_path;
151-
152151
/** Make a DatabaseBatch connected to this database */
153152
virtual std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0;
154153
};
@@ -189,6 +188,7 @@ class DummyDatabase : public WalletDatabase
189188
bool PeriodicFlush() override { return true; }
190189
void IncrementUpdateCounter() override { ++nUpdateCounter; }
191190
void ReloadDbEnv() override {}
191+
std::string Filename() override { return "dummy"; }
192192
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override { return MakeUnique<DummyBatch>(); }
193193
};
194194

src/wallet/load.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,14 @@ bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wal
6868
bool LoadWallets(interfaces::Chain& chain, const std::vector<std::string>& wallet_files)
6969
{
7070
try {
71-
for (const std::string& walletFile : wallet_files) {
71+
for (const std::string& name : wallet_files) {
72+
DatabaseOptions options;
73+
DatabaseStatus status;
74+
options.verify = false; // No need to verify, assuming verified earlier in VerifyWallets()
7275
bilingual_str error;
7376
std::vector<bilingual_str> warnings;
74-
std::shared_ptr<CWallet> pwallet = CWallet::CreateWalletFromFile(chain, walletFile, error, warnings);
77+
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
78+
std::shared_ptr<CWallet> pwallet = database ? CWallet::Create(chain, name, std::move(database), options.create_flags, error, warnings) : nullptr;
7579
if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
7680
if (!pwallet) {
7781
chain.initError(error);

src/wallet/test/wallet_tests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ BOOST_FIXTURE_TEST_SUITE(wallet_tests, WalletTestingSetup)
3737

3838
static std::shared_ptr<CWallet> TestLoadWallet(interfaces::Chain& chain)
3939
{
40+
DatabaseOptions options;
41+
DatabaseStatus status;
4042
bilingual_str error;
4143
std::vector<bilingual_str> warnings;
42-
auto wallet = CWallet::CreateWalletFromFile(chain, "", error, warnings);
44+
auto database = MakeWalletDatabase("", options, status, error);
45+
auto wallet = CWallet::Create(chain, "", std::move(database), options.create_flags, error, warnings);
4346
wallet->postInitProcess();
4447
return wallet;
4548
}

src/wallet/wallet.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,13 @@ namespace {
203203
std::shared_ptr<CWallet> LoadWalletInternal(interfaces::Chain& chain, const std::string& name, Optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings)
204204
{
205205
try {
206-
if (!MakeWalletDatabase(name, options, status, error)) {
206+
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
207+
if (!database) {
207208
error = Untranslated("Wallet file verification failed.") + Untranslated(" ") + error;
208209
return nullptr;
209210
}
210211

211-
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, name, error, warnings);
212+
std::shared_ptr<CWallet> wallet = CWallet::Create(chain, name, std::move(database), options.create_flags, error, warnings);
212213
if (!wallet) {
213214
error = Untranslated("Wallet loading failed.") + Untranslated(" ") + error;
214215
return nullptr;
@@ -260,7 +261,8 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::strin
260261
}
261262

262263
// Wallet::Verify will check if we're trying to create a wallet with a duplicate name.
263-
if (!MakeWalletDatabase(name, options, status, error)) {
264+
std::unique_ptr<WalletDatabase> database = MakeWalletDatabase(name, options, status, error);
265+
if (!database) {
264266
error = Untranslated("Wallet file verification failed.") + Untranslated(" ") + error;
265267
status = DatabaseStatus::FAILED_VERIFY;
266268
return nullptr;
@@ -274,7 +276,7 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::strin
274276
}
275277

276278
// Make the wallet
277-
std::shared_ptr<CWallet> wallet = CWallet::CreateWalletFromFile(chain, name, error, warnings, wallet_creation_flags);
279+
std::shared_ptr<CWallet> wallet = CWallet::Create(chain, name, std::move(database), wallet_creation_flags, error, warnings);
278280
if (!wallet) {
279281
error = Untranslated("Wallet creation failed.") + Untranslated(" ") + error;
280282
status = DatabaseStatus::FAILED_CREATE;
@@ -3803,18 +3805,17 @@ std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, cons
38033805
return MakeDatabase(wallet_path, options, status, error_string);
38043806
}
38053807

3806-
std::shared_ptr<CWallet> CWallet::CreateWalletFromFile(interfaces::Chain& chain, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, uint64_t wallet_creation_flags)
3808+
std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings)
38073809
{
3808-
fs::path path = fs::absolute(name, GetWalletDir());
3809-
const std::string walletFile = WalletDataFilePath(path).string();
3810+
const std::string& walletFile = database->Filename();
38103811

38113812
chain.initMessage(_("Loading wallet...").translated);
38123813

38133814
int64_t nStart = GetTimeMillis();
38143815
bool fFirstRun = true;
38153816
// TODO: Can't use std::make_shared because we need a custom deleter but
38163817
// should be possible to use std::allocate_shared.
3817-
std::shared_ptr<CWallet> walletInstance(new CWallet(&chain, name, CreateWalletDatabase(path)), ReleaseWallet);
3818+
std::shared_ptr<CWallet> walletInstance(new CWallet(&chain, name, std::move(database)), ReleaseWallet);
38183819
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
38193820
if (nLoadWalletRet != DBErrors::LOAD_OK) {
38203821
if (nLoadWalletRet == DBErrors::CORRUPT) {

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
11461146
bool MarkReplaced(const uint256& originalHash, const uint256& newHash);
11471147

11481148
/* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
1149-
static std::shared_ptr<CWallet> CreateWalletFromFile(interfaces::Chain& chain, const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings, uint64_t wallet_creation_flags = 0);
1149+
static std::shared_ptr<CWallet> Create(interfaces::Chain& chain, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings);
11501150

11511151
/**
11521152
* Wallet post-init setup

0 commit comments

Comments
 (0)