Skip to content

Commit 3c815cf

Browse files
committed
wallet: Remove Verify and IsLoaded methods
Checks are now consolidated in MakeBerkeleyDatabase function instead of happening in higher level code. This commit does not change behavior except for error messages which now include more complete information.
1 parent 0d94e60 commit 3c815cf

File tree

9 files changed

+16
-59
lines changed

9 files changed

+16
-59
lines changed

src/wallet/bdb.cpp

Lines changed: 0 additions & 12 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.

src/wallet/bdb.h

Lines changed: 1 addition & 5 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,9 +86,6 @@ 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);
92-
9389
/** Check format of database file */
9490
bool IsBerkeleyBtree(const fs::path& path);
9591

@@ -146,7 +142,7 @@ class BerkeleyDatabase : public WalletDatabase
146142
void ReloadDbEnv() override;
147143

148144
/** Verifies the environment and database file */
149-
bool Verify(bilingual_str& error) override;
145+
bool Verify(bilingual_str& error);
150146

151147
/**
152148
* Pointer to shared database environment.

src/wallet/db.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,6 @@ class WalletDatabase
147147
unsigned int nLastFlushed;
148148
int64_t nLastWalletUpdate;
149149

150-
/** Verifies the environment and database file */
151-
virtual bool Verify(bilingual_str& error) = 0;
152-
153150
std::string m_file_path;
154151

155152
/** Make a DatabaseBatch connected to this database */
@@ -192,7 +189,6 @@ class DummyDatabase : public WalletDatabase
192189
bool PeriodicFlush() override { return true; }
193190
void IncrementUpdateCounter() override { ++nUpdateCounter; }
194191
void ReloadDbEnv() override {}
195-
bool Verify(bilingual_str& errorStr) override { return true; }
196192
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override { return MakeUnique<DummyBatch>(); }
197193
};
198194

src/wallet/load.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ bool VerifyWallets(interfaces::Chain& chain, const std::vector<std::string>& wal
5252
return false;
5353
}
5454

55+
DatabaseOptions options;
56+
DatabaseStatus status;
57+
options.verify = true;
5558
bilingual_str error_string;
56-
std::vector<bilingual_str> warnings;
57-
bool verify_success = CWallet::Verify(chain, wallet_file, error_string, warnings);
58-
if (!warnings.empty()) chain.initWarning(Join(warnings, Untranslated("\n")));
59-
if (!verify_success) {
59+
if (!MakeWalletDatabase(wallet_file, options, status, error_string)) {
6060
chain.initError(error_string);
6161
return false;
6262
}

src/wallet/wallet.cpp

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ 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 (!CWallet::Verify(chain, name, error, warnings)) {
206+
if (!MakeWalletDatabase(name, options, status, error)) {
207207
error = Untranslated("Wallet file verification failed.") + Untranslated(" ") + error;
208208
return nullptr;
209209
}
@@ -260,7 +260,7 @@ std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::strin
260260
}
261261

262262
// Wallet::Verify will check if we're trying to create a wallet with a duplicate name.
263-
if (!CWallet::Verify(chain, name, error, warnings)) {
263+
if (!MakeWalletDatabase(name, options, status, error)) {
264264
error = Untranslated("Wallet file verification failed.") + Untranslated(" ") + error;
265265
status = DatabaseStatus::FAILED_VERIFY;
266266
return nullptr;
@@ -3779,15 +3779,14 @@ std::vector<std::string> CWallet::GetDestValues(const std::string& prefix) const
37793779
return values;
37803780
}
37813781

3782-
bool CWallet::Verify(interfaces::Chain& chain, const std::string& name, bilingual_str& error_string, std::vector<bilingual_str>& warnings)
3782+
std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error_string)
37833783
{
37843784
// Do some checking on wallet path. It should be either a:
37853785
//
37863786
// 1. Path where a directory can be created.
37873787
// 2. Path to an existing directory.
37883788
// 3. Path to a symlink to a directory.
37893789
// 4. For backwards compatibility, the name of a data file in -walletdir.
3790-
LOCK(cs_wallets);
37913790
const fs::path& wallet_path = fs::absolute(name, GetWalletDir());
37923791
fs::file_type path_type = fs::symlink_status(wallet_path).type();
37933792
if (!(path_type == fs::file_not_found || path_type == fs::directory_file ||
@@ -3798,24 +3797,10 @@ bool CWallet::Verify(interfaces::Chain& chain, const std::string& name, bilingua
37983797
"database/log.?????????? files can be stored, a location where such a directory could be created, "
37993798
"or (for backwards compatibility) the name of an existing data file in -walletdir (%s)",
38003799
name, GetWalletDir()));
3801-
return false;
3802-
}
3803-
3804-
// Make sure that the wallet path doesn't clash with an existing wallet path
3805-
if (IsWalletLoaded(wallet_path)) {
3806-
error_string = Untranslated(strprintf("Error loading wallet %s. Duplicate -wallet filename specified.", name));
3807-
return false;
3808-
}
3809-
3810-
// Keep same database environment instance across Verify/Recover calls below.
3811-
std::unique_ptr<WalletDatabase> database = CreateWalletDatabase(wallet_path);
3812-
3813-
try {
3814-
return database->Verify(error_string);
3815-
} catch (const fs::filesystem_error& e) {
3816-
error_string = Untranslated(strprintf("Error loading wallet %s. %s", name, fsbridge::get_filesystem_error_message(e)));
3817-
return false;
3800+
status = DatabaseStatus::FAILED_BAD_PATH;
3801+
return nullptr;
38183802
}
3803+
return MakeDatabase(wallet_path, options, status, error_string);
38193804
}
38203805

38213806
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)

src/wallet/wallet.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ std::shared_ptr<CWallet> GetWallet(const std::string& name);
5757
std::shared_ptr<CWallet> LoadWallet(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);
5858
std::shared_ptr<CWallet> CreateWallet(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);
5959
std::unique_ptr<interfaces::Handler> HandleLoadWallet(LoadWalletFn load_wallet);
60+
std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
6061

6162
//! -paytxfee default
6263
constexpr CAmount DEFAULT_PAY_TX_FEE = 0;
@@ -1144,9 +1145,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
11441145
/** Mark a transaction as replaced by another transaction (e.g., BIP 125). */
11451146
bool MarkReplaced(const uint256& originalHash, const uint256& newHash);
11461147

1147-
//! Verify wallet naming and perform salvage on the wallet if required
1148-
static bool Verify(interfaces::Chain& chain, const std::string& name, bilingual_str& error_string, std::vector<bilingual_str>& warnings);
1149-
11501148
/* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
11511149
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);
11521150

src/wallet/walletdb.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,11 +1032,6 @@ std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const Databas
10321032
return MakeBerkeleyDatabase(path, options, status, error);
10331033
}
10341034

1035-
bool IsWalletLoaded(const fs::path& wallet_path)
1036-
{
1037-
return IsBDBWalletLoaded(wallet_path);
1038-
}
1039-
10401035
/** Return object for accessing database at specified path. */
10411036
std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path)
10421037
{

src/wallet/walletdb.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,6 @@ using KeyFilterFn = std::function<bool(const std::string&)>;
285285
//! Unserialize a given Key-Value pair and load it into the wallet
286286
bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, std::string& strType, std::string& strErr, const KeyFilterFn& filter_fn = nullptr);
287287

288-
/** Return whether a wallet database is currently loaded. */
289-
bool IsWalletLoaded(const fs::path& wallet_path);
290-
291288
/** Return object for accessing database at specified path. */
292289
std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path);
293290

test/functional/wallet_multiwallet.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,12 @@ def wallet_file(name):
247247
assert_raises_rpc_error(-18, 'Wallet wallets not found.', self.nodes[0].loadwallet, 'wallets')
248248

249249
# Fail to load duplicate wallets
250-
assert_raises_rpc_error(-4, 'Wallet file verification failed. Error loading wallet w1. Duplicate -wallet filename specified.', self.nodes[0].loadwallet, wallet_names[0])
250+
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "w1", "wallet.dat")
251+
assert_raises_rpc_error(-4, "Wallet file verification failed. Refusing to load database. Data file '{}' is already loaded.".format(path), self.nodes[0].loadwallet, wallet_names[0])
251252

252253
# Fail to load duplicate wallets by different ways (directory and filepath)
253-
assert_raises_rpc_error(-4, "Wallet file verification failed. Error loading wallet wallet.dat. Duplicate -wallet filename specified.", self.nodes[0].loadwallet, 'wallet.dat')
254+
path = os.path.join(self.options.tmpdir, "node0", "regtest", "wallets", "wallet.dat")
255+
assert_raises_rpc_error(-4, "Wallet file verification failed. Refusing to load database. Data file '{}' is already loaded.".format(path), self.nodes[0].loadwallet, 'wallet.dat')
254256

255257
# Fail to load if one wallet is a copy of another
256258
assert_raises_rpc_error(-4, "BerkeleyDatabase: Can't open database w8_copy (duplicates fileid", self.nodes[0].loadwallet, 'w8_copy')

0 commit comments

Comments
 (0)