diff --git a/src/wallet/bdb.cpp b/src/wallet/bdb.cpp index edfd443b7d1..62b7d7bc16e 100644 --- a/src/wallet/bdb.cpp +++ b/src/wallet/bdb.cpp @@ -399,7 +399,6 @@ void BerkeleyBatch::Close() activeTxn->abort(); activeTxn = nullptr; pdb = nullptr; - CloseCursor(); if (fFlushOnClose) Flush(); @@ -477,12 +476,13 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip) fSuccess = false; } - if (db.StartCursor()) { + std::unique_ptr cursor = db.GetNewCursor(); + if (cursor) { while (fSuccess) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); bool complete; - bool ret1 = db.ReadAtCursor(ssKey, ssValue, complete); + bool ret1 = cursor->Next(ssKey, ssValue, complete); if (complete) { break; } else if (!ret1) { @@ -503,7 +503,7 @@ bool BerkeleyDatabase::Rewrite(const char* pszSkip) if (ret2 > 0) fSuccess = false; } - db.CloseCursor(); + cursor.reset(); } if (fSuccess) { db.Close(); diff --git a/src/wallet/db.h b/src/wallet/db.h index aa1377ccef3..3373bd1de2d 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -38,8 +38,6 @@ public: class DatabaseBatch { private: - std::unique_ptr m_cursor; - virtual bool ReadKey(CDataStream&& key, CDataStream& value) = 0; virtual bool WriteKey(CDataStream&& key, CDataStream&& value, bool overwrite=true) = 0; virtual bool EraseKey(CDataStream&& key) = 0; @@ -107,20 +105,6 @@ public: } virtual std::unique_ptr GetNewCursor() = 0; - bool StartCursor() - { - m_cursor = GetNewCursor(); - return m_cursor != nullptr; - } - bool ReadAtCursor(CDataStream& ssKey, CDataStream& ssValue, bool& complete) - { - if (!m_cursor) return false; - return m_cursor->Next(ssKey, ssValue, complete); - } - void CloseCursor() - { - m_cursor.reset(); - } virtual bool TxnBegin() = 0; virtual bool TxnCommit() = 0; virtual bool TxnAbort() = 0; diff --git a/src/wallet/dump.cpp b/src/wallet/dump.cpp index 2e46cf54543..ed3b05f1187 100644 --- a/src/wallet/dump.cpp +++ b/src/wallet/dump.cpp @@ -47,7 +47,8 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error) std::unique_ptr batch = db.MakeBatch(); bool ret = true; - if (!batch->StartCursor()) { + std::unique_ptr cursor = batch->GetNewCursor(); + if (!cursor) { error = _("Error: Couldn't create cursor into database"); ret = false; } @@ -69,7 +70,7 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error) CDataStream ss_key(SER_DISK, CLIENT_VERSION); CDataStream ss_value(SER_DISK, CLIENT_VERSION); bool complete; - ret = batch->ReadAtCursor(ss_key, ss_value, complete); + ret = cursor->Next(ss_key, ss_value, complete); if (complete) { ret = true; break; @@ -85,7 +86,7 @@ bool DumpWallet(const ArgsManager& args, CWallet& wallet, bilingual_str& error) } } - batch->CloseCursor(); + cursor.reset(); batch.reset(); // Close the wallet after we're done with it. The caller won't be doing this diff --git a/src/wallet/test/util.cpp b/src/wallet/test/util.cpp index f6c7ecb598b..da744809eda 100644 --- a/src/wallet/test/util.cpp +++ b/src/wallet/test/util.cpp @@ -50,7 +50,7 @@ std::unique_ptr DuplicateMockDatabase(WalletDatabase& database, // Get a cursor to the original database auto batch = database.MakeBatch(); - batch->StartCursor(); + std::unique_ptr cursor = batch->GetNewCursor(); // Get a batch for the new database auto new_batch = new_database->MakeBatch(); @@ -60,7 +60,7 @@ std::unique_ptr DuplicateMockDatabase(WalletDatabase& database, CDataStream key(SER_DISK, CLIENT_VERSION); CDataStream value(SER_DISK, CLIENT_VERSION); bool complete; - batch->ReadAtCursor(key, value, complete); + cursor->Next(key, value, complete); if (complete) break; new_batch->Write(key, value); } diff --git a/src/wallet/test/walletload_tests.cpp b/src/wallet/test/walletload_tests.cpp index 24d21c2f220..5af5ceb682d 100644 --- a/src/wallet/test/walletload_tests.cpp +++ b/src/wallet/test/walletload_tests.cpp @@ -54,12 +54,14 @@ BOOST_FIXTURE_TEST_CASE(wallet_load_unknown_descriptor, TestingSetup) bool HasAnyRecordOfType(WalletDatabase& db, const std::string& key) { std::unique_ptr batch = db.MakeBatch(false); - BOOST_CHECK(batch->StartCursor()); + BOOST_CHECK(batch); + std::unique_ptr cursor = batch->GetNewCursor(); + BOOST_CHECK(cursor); while (true) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); bool complete; - BOOST_CHECK(batch->ReadAtCursor(ssKey, ssValue, complete)); + BOOST_CHECK(cursor->Next(ssKey, ssValue, complete)); if (complete) break; std::string type; ssKey >> type; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 2c0ce89929c..bbf1df64c33 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3770,8 +3770,9 @@ bool CWallet::MigrateToSQLite(bilingual_str& error) // Get all of the records for DB type migration std::unique_ptr batch = m_database->MakeBatch(); + std::unique_ptr cursor = batch->GetNewCursor(); std::vector> records; - if (!batch->StartCursor()) { + if (!cursor) { error = _("Error: Unable to begin reading all records in the database"); return false; } @@ -3779,15 +3780,15 @@ bool CWallet::MigrateToSQLite(bilingual_str& error) while (true) { CDataStream ss_key(SER_DISK, CLIENT_VERSION); CDataStream ss_value(SER_DISK, CLIENT_VERSION); - bool ret = batch->ReadAtCursor(ss_key, ss_value, complete); - if (!ret) { + bool ret = cursor->Next(ss_key, ss_value, complete); + if (complete || !ret) { break; } SerializeData key(ss_key.begin(), ss_key.end()); SerializeData value(ss_value.begin(), ss_value.end()); records.emplace_back(key, value); } - batch->CloseCursor(); + cursor.reset(); batch.reset(); if (!complete) { error = _("Error: Unable to read all records in the database"); diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index 826cecfb6f0..1272b5378af 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -812,7 +812,8 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) #endif // Get cursor - if (!m_batch->StartCursor()) + std::unique_ptr cursor = m_batch->GetNewCursor(); + if (!cursor) { pwallet->WalletLogPrintf("Error getting wallet database cursor\n"); return DBErrors::CORRUPT; @@ -824,13 +825,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 = cursor->Next(ssKey, ssValue, complete); if (complete) { break; } else if (!ret) { - m_batch->CloseCursor(); + cursor.reset(); pwallet->WalletLogPrintf("Error reading next record from wallet database\n"); return DBErrors::CORRUPT; } @@ -878,7 +879,6 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet) } catch (...) { result = DBErrors::CORRUPT; } - m_batch->CloseCursor(); // Set the active ScriptPubKeyMans for (auto spk_man_pair : wss.m_active_external_spks) { @@ -986,7 +986,8 @@ DBErrors WalletBatch::FindWalletTx(std::vector& vTxHash, std::listStartCursor()) + std::unique_ptr cursor = m_batch->GetNewCursor(); + if (!cursor) { LogPrintf("Error getting wallet database cursor\n"); return DBErrors::CORRUPT; @@ -998,11 +999,10 @@ DBErrors WalletBatch::FindWalletTx(std::vector& vTxHash, std::listReadAtCursor(ssKey, ssValue, complete); + bool ret = cursor->Next(ssKey, ssValue, complete); if (complete) { break; } else if (!ret) { - m_batch->CloseCursor(); LogPrintf("Error reading next record from wallet database\n"); return DBErrors::CORRUPT; } @@ -1020,7 +1020,6 @@ DBErrors WalletBatch::FindWalletTx(std::vector& vTxHash, std::listCloseCursor(); return result; } @@ -1114,7 +1113,8 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags) bool WalletBatch::EraseRecords(const std::unordered_set& types) { // Get cursor - if (!m_batch->StartCursor()) + std::unique_ptr cursor = m_batch->GetNewCursor(); + if (!cursor) { return false; } @@ -1126,13 +1126,12 @@ bool WalletBatch::EraseRecords(const std::unordered_set& types) CDataStream key(SER_DISK, CLIENT_VERSION); CDataStream value(SER_DISK, CLIENT_VERSION); bool complete; - bool ret = m_batch->ReadAtCursor(key, value, complete); + bool ret = cursor->Next(key, value, complete); if (complete) { break; } else if (!ret) { - m_batch->CloseCursor(); return false; } @@ -1146,7 +1145,6 @@ bool WalletBatch::EraseRecords(const std::unordered_set& types) m_batch->Erase(key_data); } } - m_batch->CloseCursor(); return true; }