Skip to content

Commit 37b765b

Browse files
author
MarcoFalke
committed
Merge #19102: wallet: Introduce and use DummyDatabase instead of dummy BerkeleyDatabase
0fcff54 walletdb: Ensure that having no database handle is a failure (Andrew Chow) da039d2 Remove BDB dummy databases (Andrew Chow) 0103d64 Introduce DummyDatabase and use it in the tests (Andrew Chow) Pull request description: In the unit tests, we use a dummy `WalletDatabase` which does nothing and always returns true. This is currently implemented by creating a `BerkeleyDatabase` in dummy mode. This PR instead adds a `DummyDatabase` class which does nothing and never fails for use in the tests. `CreateDummyWalletDatabase` is changed to return this `DummyDatabase` and `BerkeleyDatabase` is cleaned up to remove all of the checks for `IsDummy`. Based on `WalletDatabase` abstract class introduced in #19334 ACKs for top commit: instagibbs: utACK bitcoin/bitcoin@0fcff54 MarcoFalke: crACK 0fcff54 🚈 Tree-SHA512: 05fbf32e078753e9a55a05f4c080b6d365b909a2a3a8e571b7e64b59ebbe53da49394f70419cc793192ade79f312f5e0422ca7c261ba81bae5912671c5ff6402
2 parents 17de75b + 0fcff54 commit 37b765b

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

src/wallet/bdb.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,6 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo
337337

338338
void BerkeleyDatabase::Open(const char* pszMode)
339339
{
340-
if (IsDummy()){
341-
return;
342-
}
343-
344340
bool fCreate = strchr(pszMode, 'c') != nullptr;
345341
unsigned int nFlags = DB_THREAD;
346342
if (fCreate)
@@ -472,9 +468,6 @@ void BerkeleyEnvironment::ReloadDbEnv()
472468

473469
bool BerkeleyDatabase::Rewrite(const char* pszSkip)
474470
{
475-
if (IsDummy()) {
476-
return true;
477-
}
478471
while (true) {
479472
{
480473
LOCK(cs_db);
@@ -602,9 +595,6 @@ void BerkeleyEnvironment::Flush(bool fShutdown)
602595

603596
bool BerkeleyDatabase::PeriodicFlush()
604597
{
605-
// There's nothing to do for dummy databases. Return true.
606-
if (IsDummy()) return true;
607-
608598
// Don't flush if we can't acquire the lock.
609599
TRY_LOCK(cs_db, lockDb);
610600
if (!lockDb) return false;
@@ -632,9 +622,6 @@ bool BerkeleyDatabase::PeriodicFlush()
632622

633623
bool BerkeleyDatabase::Backup(const std::string& strDest) const
634624
{
635-
if (IsDummy()) {
636-
return false;
637-
}
638625
while (true)
639626
{
640627
{
@@ -672,23 +659,17 @@ bool BerkeleyDatabase::Backup(const std::string& strDest) const
672659

673660
void BerkeleyDatabase::Flush()
674661
{
675-
if (!IsDummy()) {
676-
env->Flush(false);
677-
}
662+
env->Flush(false);
678663
}
679664

680665
void BerkeleyDatabase::Close()
681666
{
682-
if (!IsDummy()) {
683-
env->Flush(true);
684-
}
667+
env->Flush(true);
685668
}
686669

687670
void BerkeleyDatabase::ReloadDbEnv()
688671
{
689-
if (!IsDummy()) {
690-
env->ReloadDbEnv();
691-
}
672+
env->ReloadDbEnv();
692673
}
693674

694675
bool BerkeleyBatch::StartCursor()
@@ -786,7 +767,7 @@ bool BerkeleyBatch::ReadKey(CDataStream&& key, CDataStream& value)
786767
bool BerkeleyBatch::WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite)
787768
{
788769
if (!pdb)
789-
return true;
770+
return false;
790771
if (fReadOnly)
791772
assert(!"Write called on database in read-only mode");
792773

src/wallet/bdb.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ class BerkeleyBatch;
9898
class BerkeleyDatabase : public WalletDatabase
9999
{
100100
public:
101-
/** Create dummy DB handle */
102-
BerkeleyDatabase() : WalletDatabase(), env(nullptr)
103-
{
104-
}
101+
BerkeleyDatabase() = delete;
105102

106103
/** Create DB handle to real database */
107104
BerkeleyDatabase(std::shared_ptr<BerkeleyEnvironment> env, std::string filename) :
@@ -166,14 +163,6 @@ class BerkeleyDatabase : public WalletDatabase
166163

167164
/** Make a BerkeleyBatch connected to this database */
168165
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override;
169-
170-
private:
171-
172-
/** Return whether this database handle is a dummy for testing.
173-
* Only to be used at a low level, application should ideally not care
174-
* about this.
175-
*/
176-
bool IsDummy() const { return env == nullptr; }
177166
};
178167

179168
/** RAII class that provides access to a Berkeley database */

src/wallet/db.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <clientversion.h>
1010
#include <fs.h>
1111
#include <streams.h>
12+
#include <util/memory.h>
1213

1314
#include <atomic>
1415
#include <memory>
@@ -154,4 +155,44 @@ class WalletDatabase
154155
virtual std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0;
155156
};
156157

158+
/** RAII class that provides access to a DummyDatabase. Never fails. */
159+
class DummyBatch : public DatabaseBatch
160+
{
161+
private:
162+
bool ReadKey(CDataStream&& key, CDataStream& value) override { return true; }
163+
bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) override { return true; }
164+
bool EraseKey(CDataStream&& key) override { return true; }
165+
bool HasKey(CDataStream&& key) override { return true; }
166+
167+
public:
168+
void Flush() override {}
169+
void Close() override {}
170+
171+
bool StartCursor() override { return true; }
172+
bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) override { return true; }
173+
void CloseCursor() override {}
174+
bool TxnBegin() override { return true; }
175+
bool TxnCommit() override { return true; }
176+
bool TxnAbort() override { return true; }
177+
};
178+
179+
/** A dummy WalletDatabase that does nothing and never fails. Only used by unit tests.
180+
**/
181+
class DummyDatabase : public WalletDatabase
182+
{
183+
public:
184+
void Open(const char* mode) override {};
185+
void AddRef() override {}
186+
void RemoveRef() override {}
187+
bool Rewrite(const char* pszSkip=nullptr) override { return true; }
188+
bool Backup(const std::string& strDest) const override { return true; }
189+
void Close() override {}
190+
void Flush() override {}
191+
bool PeriodicFlush() override { return true; }
192+
void IncrementUpdateCounter() override { ++nUpdateCounter; }
193+
void ReloadDbEnv() override {}
194+
bool Verify(bilingual_str& errorStr) override { return true; }
195+
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override { return MakeUnique<DummyBatch>(); }
196+
};
197+
157198
#endif // BITCOIN_WALLET_DB_H

src/wallet/walletdb.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ std::unique_ptr<WalletDatabase> CreateWalletDatabase(const fs::path& path)
10211021
/** Return object for accessing dummy database with no read/write capabilities. */
10221022
std::unique_ptr<WalletDatabase> CreateDummyWalletDatabase()
10231023
{
1024-
return MakeUnique<BerkeleyDatabase>();
1024+
return MakeUnique<DummyDatabase>();
10251025
}
10261026

10271027
/** Return object for accessing temporary in-memory database. */

0 commit comments

Comments
 (0)