Skip to content

Commit 9d4b3d8

Browse files
committed
Merge #19334: wallet: Introduce WalletDatabase abstract class
d416ae5 walletdb: Introduce WalletDatabase abstract class (Andrew Chow) 2179dbc walletdb: Add BerkeleyDatabase::Open dummy function (Andrew Chow) 71d28e7 walletdb: Introduce AddRef and RemoveRef functions (Andrew Chow) 27b2766 walletdb: Move BerkeleyDatabase::Flush(true) to Close() (Andrew Chow) Pull request description: A `WalletDatabase` abstract class is created from `BerkeleyDatabase` and is implemented by `BerkeleyDatabase`. First, to get to the point that this is possible, 4 functions need to be added to `BerkeleyDatabase`: `AddRef`, `RemoveRef`, `Open`, and `Close`. First the increment and decrement of `mapFileUseCount` is refactored into separate functions `AddRef` and `RemoveRef`. `Open` is introduced as a dummy function. This will raise an exception so that it always fails. `Close` is refactored from `Flush`. The `shutdown` argument in `Flush` is removed and instead `Flush(true)` is now the `Close` function. Split from #18971 Requires #19325 ACKs for top commit: ryanofsky: Code review ACK d416ae5. Only changes since last review were rebasing after base PR #19334 merge, and adding cs_db lock in BerkeleyDatabase destructor, which should avoid races accessing env->m_databases and env->m_fileids fjahr: Code review ACK d416ae5 meshcollider: Code review & test run ACK d416ae5 Tree-SHA512: 98d05ec093d7446c4488e2b0914584222a331e9a2f4d5be6af98e3f6d78fdd8e75526c12f91a8a52d4820c25bce02aa02aabe92d38bee7eb2fce07d0691b7b0d
2 parents ccef102 + d416ae5 commit 9d4b3d8

File tree

10 files changed

+152
-64
lines changed

10 files changed

+152
-64
lines changed

src/qt/rpcconsole.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <univalue.h>
2525

2626
#ifdef ENABLE_WALLET
27-
#include <wallet/bdb.h>
2827
#include <wallet/db.h>
2928
#include <wallet/wallet.h>
3029
#endif

src/wallet/bdb.cpp

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,17 @@ void BerkeleyEnvironment::CheckpointLSN(const std::string& strFile)
312312
dbenv->lsn_reset(strFile.c_str(), 0);
313313
}
314314

315+
BerkeleyDatabase::~BerkeleyDatabase()
316+
{
317+
if (env) {
318+
LOCK(cs_db);
319+
size_t erased = env->m_databases.erase(strFile);
320+
assert(erased == 1);
321+
env->m_fileids.erase(strFile);
322+
}
323+
}
315324

316-
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr)
325+
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database)
317326
{
318327
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
319328
fFlushOnClose = fFlushOnCloseIn;
@@ -388,11 +397,16 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
388397
fReadOnly = fTmp;
389398
}
390399
}
391-
++env->mapFileUseCount[strFilename];
400+
database.AddRef();
392401
strFile = strFilename;
393402
}
394403
}
395404

405+
void BerkeleyDatabase::Open(const char* mode)
406+
{
407+
throw std::logic_error("BerkeleyDatabase does not implement Open. This function should not be called.");
408+
}
409+
396410
void BerkeleyBatch::Flush()
397411
{
398412
if (activeTxn)
@@ -426,11 +440,7 @@ void BerkeleyBatch::Close()
426440
if (fFlushOnClose)
427441
Flush();
428442

429-
{
430-
LOCK(cs_db);
431-
--env->mapFileUseCount[strFile];
432-
}
433-
env->m_db_in_use.notify_all();
443+
m_database.RemoveRef();
434444
}
435445

436446
void BerkeleyEnvironment::CloseDb(const std::string& strFile)
@@ -675,22 +685,17 @@ bool BerkeleyDatabase::Backup(const std::string& strDest) const
675685
}
676686
}
677687

678-
void BerkeleyDatabase::Flush(bool shutdown)
688+
void BerkeleyDatabase::Flush()
679689
{
680690
if (!IsDummy()) {
681-
env->Flush(shutdown);
682-
if (shutdown) {
683-
LOCK(cs_db);
684-
g_dbenvs.erase(env->Directory().string());
685-
env = nullptr;
686-
} else {
687-
// TODO: To avoid g_dbenvs.erase erasing the environment prematurely after the
688-
// first database shutdown when multiple databases are open in the same
689-
// environment, should replace raw database `env` pointers with shared or weak
690-
// pointers, or else separate the database and environment shutdowns so
691-
// environments can be shut down after databases.
692-
env->m_fileids.erase(strFile);
693-
}
691+
env->Flush(false);
692+
}
693+
}
694+
695+
void BerkeleyDatabase::Close()
696+
{
697+
if (!IsDummy()) {
698+
env->Flush(true);
694699
}
695700
}
696701

@@ -832,7 +837,22 @@ bool BerkeleyBatch::HasKey(CDataStream&& key)
832837
return ret == 0;
833838
}
834839

835-
std::unique_ptr<BerkeleyBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
840+
void BerkeleyDatabase::AddRef()
841+
{
842+
LOCK(cs_db);
843+
++env->mapFileUseCount[strFile];
844+
}
845+
846+
void BerkeleyDatabase::RemoveRef()
847+
{
848+
{
849+
LOCK(cs_db);
850+
--env->mapFileUseCount[strFile];
851+
}
852+
env->m_db_in_use.notify_all();
853+
}
854+
855+
std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
836856
{
837857
return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close);
838858
}

src/wallet/bdb.h

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -98,56 +98,59 @@ class BerkeleyBatch;
9898
/** An instance of this class represents one database.
9999
* For BerkeleyDB this is just a (env, strFile) tuple.
100100
**/
101-
class BerkeleyDatabase
101+
class BerkeleyDatabase : public WalletDatabase
102102
{
103103
friend class BerkeleyBatch;
104104
public:
105105
/** Create dummy DB handle */
106-
BerkeleyDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(nullptr)
106+
BerkeleyDatabase() : WalletDatabase(), env(nullptr)
107107
{
108108
}
109109

110110
/** Create DB handle to real database */
111111
BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, std::string filename) :
112-
nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0), env(std::move(env)), strFile(std::move(filename))
112+
WalletDatabase(), env(std::move(env)), strFile(std::move(filename))
113113
{
114114
auto inserted = this->env->m_databases.emplace(strFile, std::ref(*this));
115115
assert(inserted.second);
116116
}
117117

118-
~BerkeleyDatabase() {
119-
if (env) {
120-
size_t erased = env->m_databases.erase(strFile);
121-
assert(erased == 1);
122-
}
123-
}
118+
~BerkeleyDatabase() override;
119+
120+
/** Open the database if it is not already opened.
121+
* Dummy function, doesn't do anything right now, but is needed for class abstraction */
122+
void Open(const char* mode) override;
124123

125124
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
126125
*/
127-
bool Rewrite(const char* pszSkip=nullptr);
126+
bool Rewrite(const char* pszSkip=nullptr) override;
127+
128+
/** Indicate the a new database user has began using the database. */
129+
void AddRef() override;
130+
/** Indicate that database user has stopped using the database and that it could be flushed or closed. */
131+
void RemoveRef() override;
128132

129133
/** Back up the entire database to a file.
130134
*/
131-
bool Backup(const std::string& strDest) const;
135+
bool Backup(const std::string& strDest) const override;
132136

133-
/** Make sure all changes are flushed to disk.
137+
/** Make sure all changes are flushed to database file.
138+
*/
139+
void Flush() override;
140+
/** Flush to the database file and close the database.
141+
* Also close the environment if no other databases are open in it.
134142
*/
135-
void Flush(bool shutdown);
143+
void Close() override;
136144
/* flush the wallet passively (TRY_LOCK)
137145
ideal to be called periodically */
138-
bool PeriodicFlush();
146+
bool PeriodicFlush() override;
139147

140-
void IncrementUpdateCounter();
141-
142-
void ReloadDbEnv();
148+
void IncrementUpdateCounter() override;
143149

144-
std::atomic<unsigned int> nUpdateCounter;
145-
unsigned int nLastSeen;
146-
unsigned int nLastFlushed;
147-
int64_t nLastWalletUpdate;
150+
void ReloadDbEnv() override;
148151

149152
/** Verifies the environment and database file */
150-
bool Verify(bilingual_str& error);
153+
bool Verify(bilingual_str& error) override;
151154

152155
/**
153156
* Pointer to shared database environment.
@@ -164,7 +167,7 @@ class BerkeleyDatabase
164167
std::unique_ptr<Db> m_db;
165168

166169
/** Make a BerkeleyBatch connected to this database */
167-
std::unique_ptr<BerkeleyBatch> MakeBatch(const char* mode, bool flush_on_close);
170+
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override;
168171

169172
private:
170173
std::string strFile;
@@ -213,6 +216,7 @@ class BerkeleyBatch : public DatabaseBatch
213216
bool fReadOnly;
214217
bool fFlushOnClose;
215218
BerkeleyEnvironment *env;
219+
BerkeleyDatabase& m_database;
216220

217221
public:
218222
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);

src/wallet/db.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
#include <fs.h>
1111
#include <streams.h>
1212

13+
#include <atomic>
14+
#include <memory>
1315
#include <string>
1416

17+
struct bilingual_str;
18+
1519
/** Given a wallet directory path or legacy file path, return path to main data file in the wallet database. */
1620
fs::path WalletDataFilePath(const fs::path& wallet_path);
1721
void SplitWalletPath(const fs::path& wallet_path, fs::path& env_directory, std::string& database_filename);
@@ -94,4 +98,60 @@ class DatabaseBatch
9498
virtual bool TxnAbort() = 0;
9599
};
96100

101+
/** An instance of this class represents one database.
102+
**/
103+
class WalletDatabase
104+
{
105+
public:
106+
/** Create dummy DB handle */
107+
WalletDatabase() : nUpdateCounter(0), nLastSeen(0), nLastFlushed(0), nLastWalletUpdate(0) {}
108+
virtual ~WalletDatabase() {};
109+
110+
/** Open the database if it is not already opened. */
111+
virtual void Open(const char* mode) = 0;
112+
113+
//! Counts the number of active database users to be sure that the database is not closed while someone is using it
114+
std::atomic<int> m_refcount{0};
115+
/** Indicate the a new database user has began using the database. Increments m_refcount */
116+
virtual void AddRef() = 0;
117+
/** Indicate that database user has stopped using the database and that it could be flushed or closed. Decrement m_refcount */
118+
virtual void RemoveRef() = 0;
119+
120+
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
121+
*/
122+
virtual bool Rewrite(const char* pszSkip=nullptr) = 0;
123+
124+
/** Back up the entire database to a file.
125+
*/
126+
virtual bool Backup(const std::string& strDest) const = 0;
127+
128+
/** Make sure all changes are flushed to database file.
129+
*/
130+
virtual void Flush() = 0;
131+
/** Flush to the database file and close the database.
132+
* Also close the environment if no other databases are open in it.
133+
*/
134+
virtual void Close() = 0;
135+
/* flush the wallet passively (TRY_LOCK)
136+
ideal to be called periodically */
137+
virtual bool PeriodicFlush() = 0;
138+
139+
virtual void IncrementUpdateCounter() = 0;
140+
141+
virtual void ReloadDbEnv() = 0;
142+
143+
std::atomic<unsigned int> nUpdateCounter;
144+
unsigned int nLastSeen;
145+
unsigned int nLastFlushed;
146+
int64_t nLastWalletUpdate;
147+
148+
/** Verifies the environment and database file */
149+
virtual bool Verify(bilingual_str& error) = 0;
150+
151+
std::string m_file_path;
152+
153+
/** Make a DatabaseBatch connected to this database */
154+
virtual std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0;
155+
};
156+
97157
#endif // BITCOIN_WALLET_DB_H

src/wallet/load.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ void StartWallets(CScheduler& scheduler, const ArgsManager& args)
9999
void FlushWallets()
100100
{
101101
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
102-
pwallet->Flush(false);
102+
pwallet->Flush();
103103
}
104104
}
105105

106106
void StopWallets()
107107
{
108108
for (const std::shared_ptr<CWallet>& pwallet : GetWallets()) {
109-
pwallet->Flush(true);
109+
pwallet->Close();
110110
}
111111
}
112112

src/wallet/wallet.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,14 @@ bool CWallet::HasWalletSpend(const uint256& txid) const
439439
return (iter != mapTxSpends.end() && iter->first.hash == txid);
440440
}
441441

442-
void CWallet::Flush(bool shutdown)
442+
void CWallet::Flush()
443443
{
444-
database->Flush(shutdown);
444+
database->Flush();
445+
}
446+
447+
void CWallet::Close()
448+
{
449+
database->Close();
445450
}
446451

447452
void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> range)

src/wallet/wallet.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10871087
bool HasWalletSpend(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
10881088

10891089
//! Flush wallet (bitdb flush)
1090-
void Flush(bool shutdown=false);
1090+
void Flush();
1091+
1092+
//! Close wallet database
1093+
void Close();
10911094

10921095
/** Wallet is about to be unloaded */
10931096
boost::signals2::signal<void ()> NotifyUnload;

src/wallet/walletdb.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,20 +1012,20 @@ bool IsWalletLoaded(const fs::path& wallet_path)
10121012
}
10131013

10141014
/** Return object for accessing database at specified path. */
1015-
std::unique_ptr<BerkeleyDatabase> CreateWalletDatabase(const fs::path& path)
1015+
std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path)
10161016
{
10171017
std::string filename;
10181018
return MakeUnique<BerkeleyDatabase>(GetWalletEnv(path, filename), std::move(filename));
10191019
}
10201020

10211021
/** Return object for accessing dummy database with no read/write capabilities. */
1022-
std::unique_ptr<BerkeleyDatabase> CreateDummyWalletDatabase()
1022+
std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase()
10231023
{
10241024
return MakeUnique<BerkeleyDatabase>();
10251025
}
10261026

10271027
/** Return object for accessing temporary in-memory database. */
1028-
std::unique_ptr<BerkeleyDatabase> CreateMockWalletDatabase()
1028+
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase()
10291029
{
10301030
return MakeUnique<BerkeleyDatabase>(std::make_shared<BerkeleyEnvironment>(), "");
10311031
}

src/wallet/walletdb.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ class CWalletTx;
4040
class uint160;
4141
class uint256;
4242

43-
/** Backend-agnostic database type. */
44-
using WalletDatabase = BerkeleyDatabase;
45-
4643
/** Error statuses for the wallet database */
4744
enum class DBErrors
4845
{
@@ -280,7 +277,7 @@ class WalletBatch
280277
//! Abort current transaction
281278
bool TxnAbort();
282279
private:
283-
std::unique_ptr<BerkeleyBatch> m_batch;
280+
std::unique_ptr<DatabaseBatch> m_batch;
284281
WalletDatabase& m_database;
285282
};
286283

@@ -294,12 +291,12 @@ bool ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, st
294291
bool IsWalletLoaded(const fs::path& wallet_path);
295292

296293
/** Return object for accessing database at specified path. */
297-
std::unique_ptr<BerkeleyDatabase> CreateWalletDatabase(const fs::path& path);
294+
std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path);
298295

299296
/** Return object for accessing dummy database with no read/write capabilities. */
300-
std::unique_ptr<BerkeleyDatabase> CreateDummyWalletDatabase();
297+
std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase();
301298

302299
/** Return object for accessing temporary in-memory database. */
303-
std::unique_ptr<BerkeleyDatabase> CreateMockWalletDatabase();
300+
std::unique_ptr<WalletDatabase> CreateMockWalletDatabase();
304301

305302
#endif // BITCOIN_WALLET_WALLETDB_H

0 commit comments

Comments
 (0)