aboutsummaryrefslogtreecommitdiff
path: root/src/wallet
diff options
context:
space:
mode:
Diffstat (limited to 'src/wallet')
-rw-r--r--src/wallet/bdb.cpp32
-rw-r--r--src/wallet/bdb.h15
-rw-r--r--src/wallet/load.cpp4
-rw-r--r--src/wallet/wallet.cpp9
-rw-r--r--src/wallet/wallet.h5
-rw-r--r--src/wallet/wallettool.cpp6
6 files changed, 41 insertions, 30 deletions
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp
index 44d1bafaf6..0989dc21c3 100644
--- a/src/wallet/bdb.cpp
+++ b/src/wallet/bdb.cpp
@@ -324,6 +324,15 @@ void BerkeleyEnvironment::CheckpointLSN(const std::string& strFile)
dbenv->lsn_reset(strFile.c_str(), 0);
}
+BerkeleyDatabase::~BerkeleyDatabase()
+{
+ if (env) {
+ LOCK(cs_db);
+ size_t erased = env->m_databases.erase(strFile);
+ assert(erased == 1);
+ env->m_fileids.erase(strFile);
+ }
+}
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr)
{
@@ -685,22 +694,17 @@ bool BerkeleyDatabase::Backup(const std::string& strDest) const
}
}
-void BerkeleyDatabase::Flush(bool shutdown)
+void BerkeleyDatabase::Flush()
{
if (!IsDummy()) {
- env->Flush(shutdown);
- if (shutdown) {
- LOCK(cs_db);
- g_dbenvs.erase(env->Directory().string());
- env = nullptr;
- } else {
- // TODO: To avoid g_dbenvs.erase erasing the environment prematurely after the
- // first database shutdown when multiple databases are open in the same
- // environment, should replace raw database `env` pointers with shared or weak
- // pointers, or else separate the database and environment shutdowns so
- // environments can be shut down after databases.
- env->m_fileids.erase(strFile);
- }
+ env->Flush(false);
+ }
+}
+
+void BerkeleyDatabase::Close()
+{
+ if (!IsDummy()) {
+ env->Flush(true);
}
}
diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h
index e54776fc0d..52cd9d92df 100644
--- a/src/wallet/bdb.h
+++ b/src/wallet/bdb.h
@@ -115,12 +115,7 @@ public:
assert(inserted.second);
}
- ~BerkeleyDatabase() {
- if (env) {
- size_t erased = env->m_databases.erase(strFile);
- assert(erased == 1);
- }
- }
+ ~BerkeleyDatabase();
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
*/
@@ -130,9 +125,13 @@ public:
*/
bool Backup(const std::string& strDest) const;
- /** Make sure all changes are flushed to disk.
+ /** Make sure all changes are flushed to database file.
+ */
+ void Flush();
+ /** Flush to the database file and close the database.
+ * Also close the environment if no other databases are open in it.
*/
- void Flush(bool shutdown);
+ void Close();
/* flush the wallet passively (TRY_LOCK)
ideal to be called periodically */
bool PeriodicFlush();
diff --git a/src/wallet/load.cpp b/src/wallet/load.cpp
index c2818a41e7..2a81d30133 100644
--- a/src/wallet/load.cpp
+++ b/src/wallet/load.cpp
@@ -99,14 +99,14 @@ void StartWallets(CScheduler& scheduler, const ArgsManager& args)
void FlushWallets()
{
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
- pwallet->Flush(false);
+ pwallet->Flush();
}
}
void StopWallets()
{
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
- pwallet->Flush(true);
+ pwallet->Close();
}
}
diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp
index 8eec00993f..cee2f2214c 100644
--- a/src/wallet/wallet.cpp
+++ b/src/wallet/wallet.cpp
@@ -439,9 +439,14 @@ bool CWallet::HasWalletSpend(const uint256& txid) const
return (iter != mapTxSpends.end() && iter->first.hash == txid);
}
-void CWallet::Flush(bool shutdown)
+void CWallet::Flush()
{
- database->Flush(shutdown);
+ database->Flush();
+}
+
+void CWallet::Close()
+{
+ database->Close();
}
void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> range)
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h
index 8cb2a64484..a761caf38c 100644
--- a/src/wallet/wallet.h
+++ b/src/wallet/wallet.h
@@ -1087,7 +1087,10 @@ public:
bool HasWalletSpend(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
//! Flush wallet (bitdb flush)
- void Flush(bool shutdown=false);
+ void Flush();
+
+ //! Close wallet database
+ void Close();
/** Wallet is about to be unloaded */
boost::signals2::signal<void ()> NotifyUnload;
diff --git a/src/wallet/wallettool.cpp b/src/wallet/wallettool.cpp
index 8a45d81456..9f25b1ae7d 100644
--- a/src/wallet/wallettool.cpp
+++ b/src/wallet/wallettool.cpp
@@ -17,7 +17,7 @@ namespace WalletTool {
static void WalletToolReleaseWallet(CWallet* wallet)
{
wallet->WalletLogPrintf("Releasing wallet\n");
- wallet->Flush(true);
+ wallet->Close();
delete wallet;
}
@@ -133,7 +133,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
std::shared_ptr<CWallet> wallet_instance = CreateWallet(name, path);
if (wallet_instance) {
WalletShowInfo(wallet_instance.get());
- wallet_instance->Flush(true);
+ wallet_instance->Close();
}
} else if (command == "info" || command == "salvage") {
if (!fs::exists(path)) {
@@ -145,7 +145,7 @@ bool ExecuteWalletToolFunc(const std::string& command, const std::string& name)
std::shared_ptr<CWallet> wallet_instance = LoadWallet(name, path);
if (!wallet_instance) return false;
WalletShowInfo(wallet_instance.get());
- wallet_instance->Flush(true);
+ wallet_instance->Close();
} else if (command == "salvage") {
return SalvageWallet(path);
}