Skip to content

Commit 135afa7

Browse files
committed
wallet: remove db mode string
We never need to open database in read-only mode as it's controlled separately for every batch. Also we can safely create database if it doesn't exist already because require_existing option is verified in MakeDatabase before creating a new WalletDatabase instance.
1 parent af22322 commit 135afa7

File tree

5 files changed

+25
-30
lines changed

5 files changed

+25
-30
lines changed

src/wallet/bdb.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,30 +305,26 @@ BerkeleyDatabase::~BerkeleyDatabase()
305305
}
306306
}
307307

308-
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database)
308+
BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const bool read_only, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database)
309309
{
310310
database.AddRef();
311-
database.Open(pszMode);
312-
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
311+
database.Open();
312+
fReadOnly = read_only;
313313
fFlushOnClose = fFlushOnCloseIn;
314314
env = database.env.get();
315315
pdb = database.m_db.get();
316316
strFile = database.strFile;
317-
bool fCreate = strchr(pszMode, 'c') != nullptr;
318-
if (fCreate && !Exists(std::string("version"))) {
317+
if (!Exists(std::string("version"))) {
319318
bool fTmp = fReadOnly;
320319
fReadOnly = false;
321320
Write(std::string("version"), CLIENT_VERSION);
322321
fReadOnly = fTmp;
323322
}
324323
}
325324

326-
void BerkeleyDatabase::Open(const char* pszMode)
325+
void BerkeleyDatabase::Open()
327326
{
328-
bool fCreate = strchr(pszMode, 'c') != nullptr;
329-
unsigned int nFlags = DB_THREAD;
330-
if (fCreate)
331-
nFlags |= DB_CREATE;
327+
unsigned int nFlags = DB_THREAD | DB_CREATE;
332328

333329
{
334330
LOCK(cs_db);
@@ -468,7 +464,7 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip)
468464
LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile);
469465
std::string strFileRes = strFile + ".rewrite";
470466
{ // surround usage of db with extra {}
471-
BerkeleyBatch db(*this, "r");
467+
BerkeleyBatch db(*this, true);
472468
std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0);
473469

474470
int ret = pdbCopy->open(nullptr, // Txn pointer
@@ -807,9 +803,9 @@ void BerkeleyDatabase::RemoveRef()
807803
if (env) env->m_db_in_use.notify_all();
808804
}
809805

810-
std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
806+
std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(bool flush_on_close)
811807
{
812-
return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close);
808+
return MakeUnique<BerkeleyBatch>(*this, false, flush_on_close);
813809
}
814810

815811
bool ExistsBerkeleyDatabase(const fs::path& path)

src/wallet/bdb.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@ class BerkeleyDatabase : public WalletDatabase
109109

110110
~BerkeleyDatabase() override;
111111

112-
/** Open the database if it is not already opened.
113-
* Dummy function, doesn't do anything right now, but is needed for class abstraction */
114-
void Open(const char* mode) override;
112+
/** Open the database if it is not already opened. */
113+
void Open() override;
115114

116115
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
117116
*/
@@ -164,7 +163,7 @@ class BerkeleyDatabase : public WalletDatabase
164163
std::string strFile;
165164

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

170169
/** RAII class that provides access to a Berkeley database */
@@ -207,7 +206,7 @@ class BerkeleyBatch : public DatabaseBatch
207206
BerkeleyDatabase& m_database;
208207

209208
public:
210-
explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true);
209+
explicit BerkeleyBatch(BerkeleyDatabase& database, const bool fReadOnly, bool fFlushOnCloseIn=true);
211210
~BerkeleyBatch() override;
212211

213212
BerkeleyBatch(const BerkeleyBatch&) = delete;

src/wallet/db.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class WalletDatabase
108108
virtual ~WalletDatabase() {};
109109

110110
/** Open the database if it is not already opened. */
111-
virtual void Open(const char* mode) = 0;
111+
virtual void Open() = 0;
112112

113113
//! Counts the number of active database users to be sure that the database is not closed while someone is using it
114114
std::atomic<int> m_refcount{0};
@@ -149,7 +149,7 @@ class WalletDatabase
149149
int64_t nLastWalletUpdate;
150150

151151
/** Make a DatabaseBatch connected to this database */
152-
virtual std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0;
152+
virtual std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) = 0;
153153
};
154154

155155
/** RAII class that provides access to a DummyDatabase. Never fails. */
@@ -178,7 +178,7 @@ class DummyBatch : public DatabaseBatch
178178
class DummyDatabase : public WalletDatabase
179179
{
180180
public:
181-
void Open(const char* mode) override {};
181+
void Open() override {};
182182
void AddRef() override {}
183183
void RemoveRef() override {}
184184
bool Rewrite(const char* pszSkip=nullptr) override { return true; }
@@ -189,7 +189,7 @@ class DummyDatabase : public WalletDatabase
189189
void IncrementUpdateCounter() override { ++nUpdateCounter; }
190190
void ReloadDbEnv() override {}
191191
std::string Filename() override { return "dummy"; }
192-
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override { return MakeUnique<DummyBatch>(); }
192+
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return MakeUnique<DummyBatch>(); }
193193
};
194194

195195
enum class DatabaseFormat {

src/wallet/wallet.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
791791

792792
wtx.mapValue["replaced_by_txid"] = newHash.ToString();
793793

794-
WalletBatch batch(*database, "r+");
794+
WalletBatch batch(*database);
795795

796796
bool success = true;
797797
if (!batch.WriteTx(wtx)) {
@@ -863,7 +863,7 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
863863
{
864864
LOCK(cs_wallet);
865865

866-
WalletBatch batch(*database, "r+", fFlushOnClose);
866+
WalletBatch batch(*database, fFlushOnClose);
867867

868868
uint256 hash = tx->GetHash();
869869

@@ -1062,7 +1062,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
10621062
{
10631063
LOCK(cs_wallet);
10641064

1065-
WalletBatch batch(*database, "r+");
1065+
WalletBatch batch(*database);
10661066

10671067
std::set<uint256> todo;
10681068
std::set<uint256> done;
@@ -1125,7 +1125,7 @@ void CWallet::MarkConflicted(const uint256& hashBlock, int conflicting_height, c
11251125
return;
11261126

11271127
// Do not flush the wallet here for performance reasons
1128-
WalletBatch batch(*database, "r+", false);
1128+
WalletBatch batch(*database, false);
11291129

11301130
std::set<uint256> todo;
11311131
std::set<uint256> done;
@@ -3190,7 +3190,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
31903190
LOCK(cs_wallet);
31913191

31923192
fFirstRunRet = false;
3193-
DBErrors nLoadWalletRet = WalletBatch(*database,"cr+").LoadWallet(this);
3193+
DBErrors nLoadWalletRet = WalletBatch(*database).LoadWallet(this);
31943194
if (nLoadWalletRet == DBErrors::NEED_REWRITE)
31953195
{
31963196
if (database->Rewrite("\x04pool"))
@@ -3217,7 +3217,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
32173217
DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
32183218
{
32193219
AssertLockHeld(cs_wallet);
3220-
DBErrors nZapSelectTxRet = WalletBatch(*database, "cr+").ZapSelectTx(vHashIn, vHashOut);
3220+
DBErrors nZapSelectTxRet = WalletBatch(*database).ZapSelectTx(vHashIn, vHashOut);
32213221
for (const uint256& hash : vHashOut) {
32223222
const auto& it = mapWallet.find(hash);
32233223
wtxOrdered.erase(it->second.m_it_wtxOrdered);

src/wallet/walletdb.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ class WalletBatch
204204
}
205205

206206
public:
207-
explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
208-
m_batch(database.MakeBatch(pszMode, _fFlushOnClose)),
207+
explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true) :
208+
m_batch(database.MakeBatch(_fFlushOnClose)),
209209
m_database(database)
210210
{
211211
}

0 commit comments

Comments
 (0)