diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h --- a/src/wallet/bdb.h +++ b/src/wallet/bdb.h @@ -106,12 +106,8 @@ ~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 @@ -183,7 +179,7 @@ /** Make a BerkeleyBatch connected to this database */ std::unique_ptr - MakeBatch(const char *mode = "r+", bool flush_on_close = true) override; + MakeBatch(bool flush_on_close = true) override; }; /** RAII class that provides access to a Berkeley database */ @@ -225,8 +221,7 @@ BerkeleyDatabase &m_database; public: - explicit BerkeleyBatch(BerkeleyDatabase &database, - const char *pszMode = "r+", + explicit BerkeleyBatch(BerkeleyDatabase &database, const bool fReadOnly, bool fFlushOnCloseIn = true); ~BerkeleyBatch() override; diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -325,19 +325,18 @@ } } -BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase &database, const char *pszMode, +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); @@ -345,12 +344,8 @@ } } -void BerkeleyDatabase::Open(const char *pszMode) { - bool fCreate = strchr(pszMode, 'c') != nullptr; - unsigned int nFlags = DB_THREAD; - if (fCreate) { - nFlags |= DB_CREATE; - } +void BerkeleyDatabase::Open() { + unsigned int nFlags = DB_THREAD | DB_CREATE; { LOCK(cs_db); @@ -504,7 +499,7 @@ 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); @@ -890,8 +885,8 @@ } std::unique_ptr -BerkeleyDatabase::MakeBatch(const char *mode, bool flush_on_close) { - return std::make_unique(*this, mode, flush_on_close); +BerkeleyDatabase::MakeBatch(bool flush_on_close) { + return std::make_unique(*this, false, flush_on_close); } bool ExistsBerkeleyDatabase(const fs::path &path) { diff --git a/src/wallet/db.h b/src/wallet/db.h --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -106,7 +106,7 @@ 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 @@ -160,7 +160,7 @@ /** Make a DatabaseBatch connected to this database */ virtual std::unique_ptr - MakeBatch(const char *mode = "r+", bool flush_on_close = true) = 0; + MakeBatch(bool flush_on_close = true) = 0; }; /** RAII class that provides access to a DummyDatabase. Never fails. */ @@ -197,7 +197,7 @@ */ 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; } @@ -209,7 +209,7 @@ void ReloadDbEnv() override {} std::string Filename() override { return "dummy"; } std::unique_ptr - MakeBatch(const char *mode = "r+", bool flush_on_close = true) override { + MakeBatch(bool flush_on_close = true) override { return std::make_unique(); } }; diff --git a/src/wallet/test/walletdb_tests.cpp b/src/wallet/test/walletdb_tests.cpp --- a/src/wallet/test/walletdb_tests.cpp +++ b/src/wallet/test/walletdb_tests.cpp @@ -31,7 +31,7 @@ BOOST_FIXTURE_TEST_SUITE(walletdb_tests, WalletTestingSetup) BOOST_AUTO_TEST_CASE(write_erase_name) { - WalletBatch batch(m_wallet.GetDBHandle(), "cr+"); + WalletBatch batch(m_wallet.GetDBHandle()); CTxDestination dst1 = PKHash(uint160S("c0ffee")); CTxDestination dst2 = PKHash(uint160S("f00d")); @@ -57,7 +57,7 @@ } BOOST_AUTO_TEST_CASE(write_erase_purpose) { - WalletBatch batch(m_wallet.GetDBHandle(), "cr+"); + WalletBatch batch(m_wallet.GetDBHandle()); CTxDestination dst1 = PKHash(uint160S("c0ffee")); CTxDestination dst2 = PKHash(uint160S("f00d")); @@ -83,7 +83,7 @@ } BOOST_AUTO_TEST_CASE(write_erase_destdata) { - WalletBatch batch(m_wallet.GetDBHandle(), "cr+"); + WalletBatch batch(m_wallet.GetDBHandle()); CTxDestination dst1 = PKHash(uint160S("c0ffee")); CTxDestination dst2 = PKHash(uint160S("f00d")); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -956,7 +956,7 @@ bool fFlushOnClose) { LOCK(cs_wallet); - WalletBatch batch(*database, "r+", fFlushOnClose); + WalletBatch batch(*database, fFlushOnClose); const TxId &txid = tx->GetId(); @@ -1167,7 +1167,7 @@ bool CWallet::AbandonTransaction(const TxId &txid) { LOCK(cs_wallet); - WalletBatch batch(*database, "r+"); + WalletBatch batch(*database); std::set todo; std::set done; @@ -1238,7 +1238,7 @@ } // Do not flush the wallet here for performance reasons. - WalletBatch batch(*database, "r+", false); + WalletBatch batch(*database, false); std::set todo; std::set done; @@ -3577,7 +3577,7 @@ 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")) { for (const auto &spk_man_pair : m_spk_managers) { @@ -3607,7 +3607,7 @@ std::vector &txIdsOut) { AssertLockHeld(cs_wallet); DBErrors nZapSelectTxRet = - WalletBatch(*database, "cr+").ZapSelectTx(txIdsIn, txIdsOut); + WalletBatch(*database).ZapSelectTx(txIdsIn, txIdsOut); for (const TxId &txid : txIdsOut) { const auto &it = mapWallet.find(txid); wtxOrdered.erase(it->second.m_it_wtxOrdered); diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -199,10 +199,8 @@ } public: - explicit WalletBatch(WalletDatabase &database, const char *pszMode = "r+", - bool _fFlushOnClose = true) - : m_batch(database.MakeBatch(pszMode, _fFlushOnClose)), - m_database(database) {} + explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true) + : m_batch(database.MakeBatch(_fFlushOnClose)), m_database(database) {} WalletBatch(const WalletBatch &) = delete; WalletBatch &operator=(const WalletBatch &) = delete;