Skip to content

Commit b82f0ca

Browse files
committed
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>
1 parent eac9200 commit b82f0ca

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

src/wallet/bdb.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,3 +841,8 @@ bool BerkeleyBatch::HasKey(CDataStream&& key)
841841
int ret = pdb->exists(activeTxn, datKey, 0);
842842
return ret == 0;
843843
}
844+
845+
std::unique_ptr<BerkeleyBatch> BerkeleyDatabase::MakeBatch(const char* mode, bool flush_on_close)
846+
{
847+
return MakeUnique<BerkeleyBatch>(*this, mode, flush_on_close);
848+
}

src/wallet/bdb.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& wallet_path, s
9393
/** Return wheter a BDB wallet database is currently loaded. */
9494
bool IsBDBWalletLoaded(const fs::path& wallet_path);
9595

96+
class BerkeleyBatch;
97+
9698
/** An instance of this class represents one database.
9799
* For BerkeleyDB this is just a (env, strFile) tuple.
98100
**/
@@ -161,6 +163,9 @@ class BerkeleyDatabase
161163
/** Database pointer. This is initialized lazily and reset during flushes, so it can be null. */
162164
std::unique_ptr<Db> m_db;
163165

166+
/** Make a BerkeleyBatch connected to this database */
167+
std::unique_ptr<BerkeleyBatch> MakeBatch(const char* mode, bool flush_on_close);
168+
164169
private:
165170
std::string strFile;
166171

src/wallet/walletdb.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ bool WalletBatch::WriteCryptedKey(const CPubKey& vchPubKey,
121121
if (!WriteIC(key, std::make_pair(vchCryptedSecret, checksum), false)) {
122122
// It may already exist, so try writing just the checksum
123123
std::vector<unsigned char> val;
124-
if (!m_batch.Read(key, val)) {
124+
if (!m_batch->Read(key, val)) {
125125
return false;
126126
}
127127
if (!WriteIC(key, std::make_pair(val, checksum), true)) {
@@ -166,8 +166,8 @@ bool WalletBatch::WriteBestBlock(const CBlockLocator& locator)
166166

167167
bool WalletBatch::ReadBestBlock(CBlockLocator& locator)
168168
{
169-
if (m_batch.Read(DBKeys::BESTBLOCK, locator) && !locator.vHave.empty()) return true;
170-
return m_batch.Read(DBKeys::BESTBLOCK_NOMERKLE, locator);
169+
if (m_batch->Read(DBKeys::BESTBLOCK, locator) && !locator.vHave.empty()) return true;
170+
return m_batch->Read(DBKeys::BESTBLOCK_NOMERKLE, locator);
171171
}
172172

173173
bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)
@@ -177,7 +177,7 @@ bool WalletBatch::WriteOrderPosNext(int64_t nOrderPosNext)
177177

178178
bool WalletBatch::ReadPool(int64_t nPool, CKeyPool& keypool)
179179
{
180-
return m_batch.Read(std::make_pair(DBKeys::POOL, nPool), keypool);
180+
return m_batch->Read(std::make_pair(DBKeys::POOL, nPool), keypool);
181181
}
182182

183183
bool WalletBatch::WritePool(int64_t nPool, const CKeyPool& keypool)
@@ -693,14 +693,14 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
693693
LOCK(pwallet->cs_wallet);
694694
try {
695695
int nMinVersion = 0;
696-
if (m_batch.Read(DBKeys::MINVERSION, nMinVersion)) {
696+
if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
697697
if (nMinVersion > FEATURE_LATEST)
698698
return DBErrors::TOO_NEW;
699699
pwallet->LoadMinVersion(nMinVersion);
700700
}
701701

702702
// Get cursor
703-
if (!m_batch.StartCursor())
703+
if (!m_batch->StartCursor())
704704
{
705705
pwallet->WalletLogPrintf("Error getting wallet database cursor\n");
706706
return DBErrors::CORRUPT;
@@ -712,13 +712,13 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
712712
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
713713
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
714714
bool complete;
715-
bool ret = m_batch.ReadAtCursor(ssKey, ssValue, complete);
715+
bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
716716
if (complete) {
717717
break;
718718
}
719719
else if (!ret)
720720
{
721-
m_batch.CloseCursor();
721+
m_batch->CloseCursor();
722722
pwallet->WalletLogPrintf("Error reading next record from wallet database\n");
723723
return DBErrors::CORRUPT;
724724
}
@@ -748,7 +748,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
748748
} catch (...) {
749749
result = DBErrors::CORRUPT;
750750
}
751-
m_batch.CloseCursor();
751+
m_batch->CloseCursor();
752752

753753
// Set the active ScriptPubKeyMans
754754
for (auto spk_man_pair : wss.m_active_external_spks) {
@@ -785,7 +785,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
785785

786786
// Last client version to open this wallet, was previously the file version number
787787
int last_client = CLIENT_VERSION;
788-
m_batch.Read(DBKeys::VERSION, last_client);
788+
m_batch->Read(DBKeys::VERSION, last_client);
789789

790790
int wallet_version = pwallet->GetVersion();
791791
pwallet->WalletLogPrintf("Wallet File Version = %d\n", wallet_version > 0 ? wallet_version : last_client);
@@ -810,7 +810,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
810810
return DBErrors::NEED_REWRITE;
811811

812812
if (last_client < CLIENT_VERSION) // Update
813-
m_batch.Write(DBKeys::VERSION, CLIENT_VERSION);
813+
m_batch->Write(DBKeys::VERSION, CLIENT_VERSION);
814814

815815
if (wss.fAnyUnordered)
816816
result = pwallet->ReorderTransactions();
@@ -846,13 +846,13 @@ DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWal
846846

847847
try {
848848
int nMinVersion = 0;
849-
if (m_batch.Read(DBKeys::MINVERSION, nMinVersion)) {
849+
if (m_batch->Read(DBKeys::MINVERSION, nMinVersion)) {
850850
if (nMinVersion > FEATURE_LATEST)
851851
return DBErrors::TOO_NEW;
852852
}
853853

854854
// Get cursor
855-
if (!m_batch.StartCursor())
855+
if (!m_batch->StartCursor())
856856
{
857857
LogPrintf("Error getting wallet database cursor\n");
858858
return DBErrors::CORRUPT;
@@ -864,11 +864,11 @@ DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWal
864864
CDataStream ssKey(SER_DISK, CLIENT_VERSION);
865865
CDataStream ssValue(SER_DISK, CLIENT_VERSION);
866866
bool complete;
867-
bool ret = m_batch.ReadAtCursor(ssKey, ssValue, complete);
867+
bool ret = m_batch->ReadAtCursor(ssKey, ssValue, complete);
868868
if (complete) {
869869
break;
870870
} else if (!ret) {
871-
m_batch.CloseCursor();
871+
m_batch->CloseCursor();
872872
LogPrintf("Error reading next record from wallet database\n");
873873
return DBErrors::CORRUPT;
874874
}
@@ -886,7 +886,7 @@ DBErrors WalletBatch::FindWalletTx(std::vector<uint256>& vTxHash, std::list<CWal
886886
} catch (...) {
887887
result = DBErrors::CORRUPT;
888888
}
889-
m_batch.CloseCursor();
889+
m_batch->CloseCursor();
890890

891891
return result;
892892
}
@@ -999,17 +999,17 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags)
999999

10001000
bool WalletBatch::TxnBegin()
10011001
{
1002-
return m_batch.TxnBegin();
1002+
return m_batch->TxnBegin();
10031003
}
10041004

10051005
bool WalletBatch::TxnCommit()
10061006
{
1007-
return m_batch.TxnCommit();
1007+
return m_batch->TxnCommit();
10081008
}
10091009

10101010
bool WalletBatch::TxnAbort()
10111011
{
1012-
return m_batch.TxnAbort();
1012+
return m_batch->TxnAbort();
10131013
}
10141014

10151015
bool IsWalletLoaded(const fs::path& wallet_path)

src/wallet/walletdb.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,32 +183,32 @@ class WalletBatch
183183
template <typename K, typename T>
184184
bool WriteIC(const K& key, const T& value, bool fOverwrite = true)
185185
{
186-
if (!m_batch.Write(key, value, fOverwrite)) {
186+
if (!m_batch->Write(key, value, fOverwrite)) {
187187
return false;
188188
}
189189
m_database.IncrementUpdateCounter();
190190
if (m_database.nUpdateCounter % 1000 == 0) {
191-
m_batch.Flush();
191+
m_batch->Flush();
192192
}
193193
return true;
194194
}
195195

196196
template <typename K>
197197
bool EraseIC(const K& key)
198198
{
199-
if (!m_batch.Erase(key)) {
199+
if (!m_batch->Erase(key)) {
200200
return false;
201201
}
202202
m_database.IncrementUpdateCounter();
203203
if (m_database.nUpdateCounter % 1000 == 0) {
204-
m_batch.Flush();
204+
m_batch->Flush();
205205
}
206206
return true;
207207
}
208208

209209
public:
210210
explicit WalletBatch(WalletDatabase& database, const char* pszMode = "r+", bool _fFlushOnClose = true) :
211-
m_batch(database, pszMode, _fFlushOnClose),
211+
m_batch(database.MakeBatch(pszMode, _fFlushOnClose)),
212212
m_database(database)
213213
{
214214
}
@@ -280,7 +280,7 @@ class WalletBatch
280280
//! Abort current transaction
281281
bool TxnAbort();
282282
private:
283-
BerkeleyBatch m_batch;
283+
std::unique_ptr<BerkeleyBatch> m_batch;
284284
WalletDatabase& m_database;
285285
};
286286

0 commit comments

Comments
 (0)