aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Chow <achow101-github@achow101.com>2020-06-15 16:54:58 -0400
committerAndrew Chow <achow101-github@achow101.com>2020-07-09 11:43:54 -0400
commitb82f0ca4d5465b36debb6c57f335bdccf4899c49 (patch)
treee4e0912cc3cc9d43de8308964e31b9ad2b18b928 /src
parenteac9200814fa01da6522625be01dded730b26751 (diff)
downloadbitcoin-b82f0ca4d5465b36debb6c57f335bdccf4899c49.tar.xz
walletdb: Add MakeBatch function to BerkeleyDatabase and use it
Instead of having WalletBatch construct the BerkeleyBatch, have BerkeleyDatabase do it and return a std::unique_ptr<BerkeleyBatch>
Diffstat (limited to 'src')
-rw-r--r--src/wallet/bdb.cpp5
-rw-r--r--src/wallet/bdb.h5
-rw-r--r--src/wallet/walletdb.cpp38
-rw-r--r--src/wallet/walletdb.h12
4 files changed, 35 insertions, 25 deletions
diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp
index 5f823d5906..8992031ba0 100644
--- a/src/wallet/bdb.cpp
+++ b/src/wallet/bdb.cpp
@@ -841,3 +841,8 @@ bool BerkeleyBatch::HasKey(CDataStream&& key)
int ret = pdb->exists(activeTxn, datKey, 0);
return ret == 0;
}
+
+std::unique_ptr<BerkeleyBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
+{
+ return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close);
+}
diff --git a/src/wallet/bdb.h b/src/wallet/bdb.h
index f3b1e5d0a3..b565bfc680 100644
--- a/src/wallet/bdb.h
+++ b/src/wallet/bdb.h
@@ -93,6 +93,8 @@ std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& wallet_path, s
/** Return wheter a BDB wallet database is currently loaded. */
bool IsBDBWalletLoaded(const fs::path& wallet_path);
+class BerkeleyBatch;
+
/** An instance of this class represents one database.
* For BerkeleyDB this is just a (env, strFile) tuple.
**/
@@ -161,6 +163,9 @@ public:
/** Database pointer. This is initialized lazily and reset during flushes, so it can be null. */
std::unique_ptr<Db> m_db;
+ /** Make a BerkeleyBatch connected to this database */
+ std::unique_ptr<BerkeleyBatch> MakeBatch(const char* mode, bool flush_on_close);
+
private:
std::string strFile;
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index 7da477d5b7..3a5a2450e1 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -121,7 +121,7 @@ bool WalletBatch::WriteCryptedKey(const CPubKey& vchPubKey,
if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) {
// It may already exist, so try writing just the checksum
std::vector<unsigned char> val;
- if (!m_batch.Read(key, val)) {
+ if (!m_batch->Read(key, val)) {
return false;
}
if (!WriteIC(key, std::make_pair(val, checksum), true)) {
@@ -166,8 +166,8 @@ bool WalletBatch::WriteBestBlock(const CBlockLocator& locator)
bool WalletBatch::ReadBestBlock(CBlockLocator& locator)
{
- if (m_batch.Read(DBKeys::BESTBLOCK, locator) && !locator.vHave.empty()) return true;
- return m_batch.Read(DBKeys::BESTBLOCK_NOMERKLE, locator);
+ if (m_batch->Read(DBKeys::BESTBLOCK, locator) && !locator.vHave.empty()) return true;
+ return m_batch->Read(DBKeys::BESTBLOCK_NOMERKLE, locator);
}
bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)
@@ -177,7 +177,7 @@ bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)
bool WalletBatch::ReadPool(int64_t nPool, CKeyPool& keypool)
{
- return m_batch.Read(std::make_pair(DBKeys::POOL, nPool), keypool);
+ return m_batch->Read(std::make_pair(DBKeys::POOL, nPool), keypool);
}
bool WalletBatch::WritePool(int64_t nPool, const CKeyPool& keypool)
@@ -693,14 +693,14 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
LOCK(pwallet->cs_wallet);
try {
int nMinVersion = 0;
- if (m_batch.Read(DBKeys::MINVERSION, nMinVersion)) {
+ if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
if (nMinVersion > FEATURE_LATEST)
return DBErrors::TOO_NEW;
pwallet->LoadMinVersion(nMinVersion);
}
// Get cursor
- if (!m_batch.StartCursor())
+ if (!m_batch->StartCursor())
{
pwallet->WalletLogPrintf("Error getting wallet database cursor\n");
return DBErrors::CORRUPT;
@@ -712,13 +712,13 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
bool complete;
- bool ret = m_batch.ReadAtCursor(ssKey, ssValue, complete);
+ bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
if (complete) {
break;
}
else if (!ret)
{
- m_batch.CloseCursor();
+ m_batch->CloseCursor();
pwallet->WalletLogPrintf("Error reading next record from wallet database\n");
return DBErrors::CORRUPT;
}
@@ -748,7 +748,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
} catch (...) {
result = DBErrors::CORRUPT;
}
- m_batch.CloseCursor();
+ m_batch->CloseCursor();
// Set the active ScriptPubKeyMans
for (auto spk_man_pair : wss.m_active_external_spks) {
@@ -785,7 +785,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
// Last client version to open this wallet, was previously the file version number
int last_client = CLIENT_VERSION;
- m_batch.Read(DBKeys::VERSION, last_client);
+ m_batch->Read(DBKeys::VERSION, last_client);
int wallet_version = pwallet->GetVersion();
pwallet->WalletLogPrintf("Wallet File Version = %d\n", wallet_version > 0 ? wallet_version : last_client);
@@ -810,7 +810,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
return DBErrors::NEED_REWRITE;
if (last_client < CLIENT_VERSION) // Update
- m_batch.Write(DBKeys::VERSION, CLIENT_VERSION);
+ m_batch->Write(DBKeys::VERSION, CLIENT_VERSION);
if (wss.fAnyUnordered)
result = pwallet->ReorderTransactions();
@@ -846,13 +846,13 @@ DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWal
try {
int nMinVersion = 0;
- if (m_batch.Read(DBKeys::MINVERSION, nMinVersion)) {
+ if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
if (nMinVersion > FEATURE_LATEST)
return DBErrors::TOO_NEW;
}
// Get cursor
- if (!m_batch.StartCursor())
+ if (!m_batch->StartCursor())
{
LogPrintf("Error getting wallet database cursor\n");
return DBErrors::CORRUPT;
@@ -864,11 +864,11 @@ DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWal
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
bool complete;
- bool ret = m_batch.ReadAtCursor(ssKey, ssValue, complete);
+ bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
if (complete) {
break;
} else if (!ret) {
- m_batch.CloseCursor();
+ m_batch->CloseCursor();
LogPrintf("Error reading next record from wallet database\n");
return DBErrors::CORRUPT;
}
@@ -886,7 +886,7 @@ DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWal
} catch (...) {
result = DBErrors::CORRUPT;
}
- m_batch.CloseCursor();
+ m_batch->CloseCursor();
return result;
}
@@ -999,17 +999,17 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags)
bool WalletBatch::TxnBegin()
{
- return m_batch.TxnBegin();
+ return m_batch->TxnBegin();
}
bool WalletBatch::TxnCommit()
{
- return m_batch.TxnCommit();
+ return m_batch->TxnCommit();
}
bool WalletBatch::TxnAbort()
{
- return m_batch.TxnAbort();
+ return m_batch->TxnAbort();
}
bool IsWalletLoaded(const fs::path& wallet_path)
diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h
index 61e0f19e56..6b55361c07 100644
--- a/src/wallet/walletdb.h
+++ b/src/wallet/walletdb.h
@@ -183,12 +183,12 @@ private:
template <typename K, typename T>
bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
{
- if (!m_batch.Write(key, value, fOverwrite)) {
+ if (!m_batch->Write(key, value, fOverwrite)) {
return false;
}
m_database.IncrementUpdateCounter();
if (m_database.nUpdateCounter % 1000 == 0) {
- m_batch.Flush();
+ m_batch->Flush();
}
return true;
}
@@ -196,19 +196,19 @@ private:
template <typename K>
bool EraseIC(const K& key)
{
- if (!m_batch.Erase(key)) {
+ if (!m_batch->Erase(key)) {
return false;
}
m_database.IncrementUpdateCounter();
if (m_database.nUpdateCounter % 1000 == 0) {
- m_batch.Flush();
+ m_batch->Flush();
}
return true;
}
public:
explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
- m_batch(database, pszMode, _fFlushOnClose),
+ m_batch(database.MakeBatch(pszMode, _fFlushOnClose)),
m_database(database)
{
}
@@ -280,7 +280,7 @@ public:
//! Abort current transaction
bool TxnAbort();
private:
- BerkeleyBatch m_batch;
+ std::unique_ptr<BerkeleyBatch> m_batch;
WalletDatabase& m_database;
};