mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-09-29 13:44:21 +02:00
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.
This commit is contained in:
@@ -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.AddRef();
|
||||||
database.Open(pszMode);
|
database.Open();
|
||||||
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
|
fReadOnly = read_only;
|
||||||
fFlushOnClose = fFlushOnCloseIn;
|
fFlushOnClose = fFlushOnCloseIn;
|
||||||
env = database.env.get();
|
env = database.env.get();
|
||||||
pdb = database.m_db.get();
|
pdb = database.m_db.get();
|
||||||
strFile = database.strFile;
|
strFile = database.strFile;
|
||||||
bool fCreate = strchr(pszMode, 'c') != nullptr;
|
if (!Exists(std::string("version"))) {
|
||||||
if (fCreate && !Exists(std::string("version"))) {
|
|
||||||
bool fTmp = fReadOnly;
|
bool fTmp = fReadOnly;
|
||||||
fReadOnly = false;
|
fReadOnly = false;
|
||||||
Write(std::string("version"), CLIENT_VERSION);
|
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 | DB_CREATE;
|
||||||
unsigned int nFlags = DB_THREAD;
|
|
||||||
if (fCreate)
|
|
||||||
nFlags |= DB_CREATE;
|
|
||||||
|
|
||||||
{
|
{
|
||||||
LOCK(cs_db);
|
LOCK(cs_db);
|
||||||
@@ -468,7 +464,7 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip)
|
|||||||
LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile);
|
LogPrintf("BerkeleyBatch::Rewrite: Rewriting %s...\n", strFile);
|
||||||
std::string strFileRes = strFile + ".rewrite";
|
std::string strFileRes = strFile + ".rewrite";
|
||||||
{ // surround usage of db with extra {}
|
{ // surround usage of db with extra {}
|
||||||
BerkeleyBatch db(*this, "r");
|
BerkeleyBatch db(*this, true);
|
||||||
std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0);
|
std::unique_ptr<Db> pdbCopy = MakeUnique<Db>(env->dbenv.get(), 0);
|
||||||
|
|
||||||
int ret = pdbCopy->open(nullptr, // Txn pointer
|
int ret = pdbCopy->open(nullptr, // Txn pointer
|
||||||
@@ -807,9 +803,9 @@ void BerkeleyDatabase::RemoveRef()
|
|||||||
if (env) env->m_db_in_use.notify_all();
|
if (env) env->m_db_in_use.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
|
std::unique_ptr<DatabaseBatch> BerkeleyDatabase::MakeBatch(bool flush_on_close)
|
||||||
{
|
{
|
||||||
return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close);
|
return MakeUnique<BerkeleyBatch>(*this, false, flush_on_close);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExistsBerkeleyDatabase(const fs::path& path)
|
bool ExistsBerkeleyDatabase(const fs::path& path)
|
||||||
|
@@ -109,9 +109,8 @@ public:
|
|||||||
|
|
||||||
~BerkeleyDatabase() override;
|
~BerkeleyDatabase() override;
|
||||||
|
|
||||||
/** Open the database if it is not already opened.
|
/** 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() override;
|
||||||
void Open(const char* mode) override;
|
|
||||||
|
|
||||||
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
|
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
|
||||||
*/
|
*/
|
||||||
@@ -164,7 +163,7 @@ public:
|
|||||||
std::string strFile;
|
std::string strFile;
|
||||||
|
|
||||||
/** Make a BerkeleyBatch connected to this database */
|
/** Make a BerkeleyBatch connected to this database */
|
||||||
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override;
|
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** RAII class that provides access to a Berkeley database */
|
/** RAII class that provides access to a Berkeley database */
|
||||||
@@ -207,7 +206,7 @@ protected:
|
|||||||
BerkeleyDatabase& m_database;
|
BerkeleyDatabase& m_database;
|
||||||
|
|
||||||
public:
|
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() override;
|
||||||
|
|
||||||
BerkeleyBatch(const BerkeleyBatch&) = delete;
|
BerkeleyBatch(const BerkeleyBatch&) = delete;
|
||||||
|
@@ -108,7 +108,7 @@ public:
|
|||||||
virtual ~WalletDatabase() {};
|
virtual ~WalletDatabase() {};
|
||||||
|
|
||||||
/** Open the database if it is not already opened. */
|
/** 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
|
//! Counts the number of active database users to be sure that the database is not closed while someone is using it
|
||||||
std::atomic<int> m_refcount{0};
|
std::atomic<int> m_refcount{0};
|
||||||
@@ -149,7 +149,7 @@ public:
|
|||||||
int64_t nLastWalletUpdate;
|
int64_t nLastWalletUpdate;
|
||||||
|
|
||||||
/** Make a DatabaseBatch connected to this database */
|
/** Make a DatabaseBatch connected to this database */
|
||||||
virtual std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) = 0;
|
virtual std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** RAII class that provides access to a DummyDatabase. Never fails. */
|
/** RAII class that provides access to a DummyDatabase. Never fails. */
|
||||||
@@ -178,7 +178,7 @@ public:
|
|||||||
class DummyDatabase : public WalletDatabase
|
class DummyDatabase : public WalletDatabase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void Open(const char* mode) override {};
|
void Open() override {};
|
||||||
void AddRef() override {}
|
void AddRef() override {}
|
||||||
void RemoveRef() override {}
|
void RemoveRef() override {}
|
||||||
bool Rewrite(const char* pszSkip=nullptr) override { return true; }
|
bool Rewrite(const char* pszSkip=nullptr) override { return true; }
|
||||||
@@ -189,7 +189,7 @@ public:
|
|||||||
void IncrementUpdateCounter() override { ++nUpdateCounter; }
|
void IncrementUpdateCounter() override { ++nUpdateCounter; }
|
||||||
void ReloadDbEnv() override {}
|
void ReloadDbEnv() override {}
|
||||||
std::string Filename() override { return "dummy"; }
|
std::string Filename() override { return "dummy"; }
|
||||||
std::unique_ptr<DatabaseBatch> MakeBatch(const char* mode = "r+", bool flush_on_close = true) override { return MakeUnique<DummyBatch>(); }
|
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return MakeUnique<DummyBatch>(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class DatabaseFormat {
|
enum class DatabaseFormat {
|
||||||
|
@@ -791,7 +791,7 @@ bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
|
|||||||
|
|
||||||
wtx.mapValue["replaced_by_txid"] = newHash.ToString();
|
wtx.mapValue["replaced_by_txid"] = newHash.ToString();
|
||||||
|
|
||||||
WalletBatch batch(*database, "r+");
|
WalletBatch batch(*database);
|
||||||
|
|
||||||
bool success = true;
|
bool success = true;
|
||||||
if (!batch.WriteTx(wtx)) {
|
if (!batch.WriteTx(wtx)) {
|
||||||
@@ -863,7 +863,7 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
|
|||||||
{
|
{
|
||||||
LOCK(cs_wallet);
|
LOCK(cs_wallet);
|
||||||
|
|
||||||
WalletBatch batch(*database, "r+", fFlushOnClose);
|
WalletBatch batch(*database, fFlushOnClose);
|
||||||
|
|
||||||
uint256 hash = tx->GetHash();
|
uint256 hash = tx->GetHash();
|
||||||
|
|
||||||
@@ -1062,7 +1062,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
|
|||||||
{
|
{
|
||||||
LOCK(cs_wallet);
|
LOCK(cs_wallet);
|
||||||
|
|
||||||
WalletBatch batch(*database, "r+");
|
WalletBatch batch(*database);
|
||||||
|
|
||||||
std::set<uint256> todo;
|
std::set<uint256> todo;
|
||||||
std::set<uint256> done;
|
std::set<uint256> done;
|
||||||
@@ -1125,7 +1125,7 @@ void CWallet::MarkConflicted(const uint256& hashBlock, int conflicting_height, c
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
// Do not flush the wallet here for performance reasons
|
// Do not flush the wallet here for performance reasons
|
||||||
WalletBatch batch(*database, "r+", false);
|
WalletBatch batch(*database, false);
|
||||||
|
|
||||||
std::set<uint256> todo;
|
std::set<uint256> todo;
|
||||||
std::set<uint256> done;
|
std::set<uint256> done;
|
||||||
@@ -3190,7 +3190,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
|
|||||||
LOCK(cs_wallet);
|
LOCK(cs_wallet);
|
||||||
|
|
||||||
fFirstRunRet = false;
|
fFirstRunRet = false;
|
||||||
DBErrors nLoadWalletRet = WalletBatch(*database,"cr+").LoadWallet(this);
|
DBErrors nLoadWalletRet = WalletBatch(*database).LoadWallet(this);
|
||||||
if (nLoadWalletRet == DBErrors::NEED_REWRITE)
|
if (nLoadWalletRet == DBErrors::NEED_REWRITE)
|
||||||
{
|
{
|
||||||
if (database->Rewrite("\x04pool"))
|
if (database->Rewrite("\x04pool"))
|
||||||
@@ -3217,7 +3217,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
|
|||||||
DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
|
DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_wallet);
|
AssertLockHeld(cs_wallet);
|
||||||
DBErrors nZapSelectTxRet = WalletBatch(*database, "cr+").ZapSelectTx(vHashIn, vHashOut);
|
DBErrors nZapSelectTxRet = WalletBatch(*database).ZapSelectTx(vHashIn, vHashOut);
|
||||||
for (const uint256& hash : vHashOut) {
|
for (const uint256& hash : vHashOut) {
|
||||||
const auto& it = mapWallet.find(hash);
|
const auto& it = mapWallet.find(hash);
|
||||||
wtxOrdered.erase(it->second.m_it_wtxOrdered);
|
wtxOrdered.erase(it->second.m_it_wtxOrdered);
|
||||||
|
@@ -204,8 +204,8 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
|
explicit WalletBatch(WalletDatabase &database, bool _fFlushOnClose = true) :
|
||||||
m_batch(database.MakeBatch(pszMode, _fFlushOnClose)),
|
m_batch(database.MakeBatch(_fFlushOnClose)),
|
||||||
m_database(database)
|
m_database(database)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user