From 0fa0991e6eccce0cf274eb98e7a3917e951f5f9c Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Thu, 3 Nov 2022 21:22:28 +0530 Subject: [PATCH] merge bitcoin#20130: remove db mode string --- src/coinjoin/util.cpp | 2 +- src/wallet/bdb.cpp | 22 +++++++++------------- src/wallet/bdb.h | 9 ++++----- src/wallet/db.h | 8 ++++---- src/wallet/wallet.cpp | 10 +++++----- src/wallet/walletdb.h | 4 ++-- 6 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/coinjoin/util.cpp b/src/coinjoin/util.cpp index e589780e54..c787fb5511 100644 --- a/src/coinjoin/util.cpp +++ b/src/coinjoin/util.cpp @@ -131,7 +131,7 @@ CTransactionBuilder::CTransactionBuilder(std::shared_ptr pwalletIn, con CScript dummyScript; { LOCK(pwallet->cs_wallet); - WalletBatch dummyBatch(pwallet->GetDBHandle(), "r+", false); + WalletBatch dummyBatch(pwallet->GetDBHandle(), false); dummyBatch.TxnBegin(); CKey secret; secret.MakeNewKey(pwallet->CanSupportFeature(FEATURE_COMPRPUBKEY)); diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index d5adaa5f23..3e5bd57dd8 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -305,17 +305,16 @@ BerkeleyDatabase::~BerkeleyDatabase() } } -BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database) +BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const bool read_only, bool fFlushOnCloseIn) : pdb(nullptr), activeTxn(nullptr), m_cursor(nullptr), m_database(database) { database.AddRef(); - database.Open(pszMode); - fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w')); + database.Open(); + fReadOnly = read_only; fFlushOnClose = fFlushOnCloseIn; env = database.env.get(); pdb = database.m_db.get(); strFile = database.strFile; - bool fCreate = strchr(pszMode, 'c') != nullptr; - if (fCreate && !Exists(std::string("version"))) { + if (!Exists(std::string("version"))) { bool fTmp = fReadOnly; fReadOnly = false; Write(std::string("version"), CLIENT_VERSION); @@ -323,12 +322,9 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode, bo } } -void BerkeleyDatabase::Open(const char* pszMode) +void BerkeleyDatabase::Open() { - bool fCreate = strchr(pszMode, 'c') != nullptr; - unsigned int nFlags = DB_THREAD; - if (fCreate) - nFlags |= DB_CREATE; + unsigned int nFlags = DB_THREAD | DB_CREATE; { LOCK(cs_db); @@ -468,7 +464,7 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip) LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile); std::string strFileRes = strFile + ".rewrite"; { // surround usage of db with extra {} - BerkeleyBatch db(*this, "r"); + BerkeleyBatch db(*this, true); std::unique_ptr pdbCopy = std::make_unique(env->dbenv.get(), 0); int ret = pdbCopy->open(nullptr, // Txn pointer @@ -807,9 +803,9 @@ void BerkeleyDatabase::RemoveRef() if (env) env->m_db_in_use.notify_all(); } -std::unique_ptr BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close) +std::unique_ptr BerkeleyDatabase::MakeBatch(bool flush_on_close) { - return std::make_unique(*this, mode, flush_on_close); + return std::make_unique(*this, false, flush_on_close); } bool ExistsBerkeleyDatabase(const fs::path& path) diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h index 12d50f5187..cce4ab9afd 100644 --- a/src/wallet/bdb.h +++ b/src/wallet/bdb.h @@ -109,9 +109,8 @@ public: ~BerkeleyDatabase() override; - /** Open the database if it is not already opened. - * Dummy function, doesn't do anything right now, but is needed for class abstraction */ - void Open(const char* mode) override; + /** Open the database if it is not already opened. */ + void Open() override; /** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero */ @@ -164,7 +163,7 @@ public: std::string strFile; /** Make a BerkeleyBatch connected to this database */ - std::unique_ptr MakeBatch(const char* mode = "r+", bool flush_on_close = true) override; + std::unique_ptr MakeBatch(bool flush_on_close = true) override; }; /** RAII class that provides access to a Berkeley database */ @@ -207,7 +206,7 @@ protected: BerkeleyDatabase& m_database; public: - explicit BerkeleyBatch(BerkeleyDatabase& database, const char* pszMode = "r+", bool fFlushOnCloseIn=true); + explicit BerkeleyBatch(BerkeleyDatabase& database, const bool fReadOnly, bool fFlushOnCloseIn=true); ~BerkeleyBatch() override; BerkeleyBatch(const BerkeleyBatch&) = delete; diff --git a/src/wallet/db.h b/src/wallet/db.h index 3432b87e0a..1b5f4d9a13 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -108,7 +108,7 @@ public: virtual ~WalletDatabase() {}; /** Open the database if it is not already opened. */ - virtual void Open(const char* mode) = 0; + virtual void Open() = 0; //! Counts the number of active database users to be sure that the database is not closed while someone is using it std::atomic m_refcount{0}; @@ -149,7 +149,7 @@ public: int64_t nLastWalletUpdate; /** Make a DatabaseBatch connected to this database */ - virtual std::unique_ptr MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0; + virtual std::unique_ptr MakeBatch(bool flush_on_close = true) = 0; }; /** RAII class that provides access to a DummyDatabase. Never fails. */ @@ -178,7 +178,7 @@ public: class DummyDatabase : public WalletDatabase { public: - void Open(const char* mode) override {}; + void Open() override {}; void AddRef() override {} void RemoveRef() override {} bool Rewrite(const char* pszSkip=nullptr) override { return true; } @@ -189,7 +189,7 @@ public: void IncrementUpdateCounter() override { ++nUpdateCounter; } void ReloadDbEnv() override {} std::string Filename() override { return "dummy"; } - std::unique_ptr MakeBatch(const char* mode = "r+", bool flush_on_close = true) override { return std::make_unique(); } + std::unique_ptr MakeBatch(bool flush_on_close = true) override { return std::make_unique(); } }; enum class DatabaseFormat { diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 450582a154..e0324b3d0b 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -822,7 +822,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose) { LOCK(cs_wallet); - WalletBatch batch(*database, "r+", fFlushOnClose); + WalletBatch batch(*database, fFlushOnClose); uint256 hash = wtxIn.GetHash(); @@ -1051,7 +1051,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx) { LOCK(cs_wallet); - WalletBatch batch(*database, "r+"); + WalletBatch batch(*database); std::set todo; std::set done; @@ -1129,7 +1129,7 @@ void CWallet::MarkConflicted(const uint256& hashBlock, int conflicting_height, c return; // Do not flush the wallet here for performance reasons - WalletBatch batch(*database, "r+", false); + WalletBatch batch(*database, false); std::set todo; std::set done; @@ -3600,7 +3600,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet) LOCK(cs_wallet); fFirstRunRet = false; - DBErrors nLoadWalletRet = WalletBatch(*database,"cr+").LoadWallet(this); + DBErrors nLoadWalletRet = WalletBatch(*database).LoadWallet(this); if (nLoadWalletRet == DBErrors::NEED_REWRITE) { if (database->Rewrite("\x04pool")) @@ -3661,7 +3661,7 @@ void CWallet::AutoLockMasternodeCollaterals() DBErrors CWallet::ZapSelectTx(std::vector& vHashIn, std::vector& vHashOut) { AssertLockHeld(cs_wallet); - DBErrors nZapSelectTxRet = WalletBatch(*database, "cr+").ZapSelectTx(vHashIn, vHashOut); + DBErrors nZapSelectTxRet = WalletBatch(*database).ZapSelectTx(vHashIn, vHashOut); for (uint256 hash : vHashOut) { const auto& it = mapWallet.find(hash); wtxOrdered.erase(it->second.m_it_wtxOrdered); diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 5c37590342..5e79e453d6 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -158,8 +158,8 @@ private: } public: - explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) : - m_batch(database.MakeBatch(pszMode, _fFlushOnClose)), + explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true) : + m_batch(database.MakeBatch(_fFlushOnClose)), m_database(database) { }