Skip to content

Commit 77331aa

Browse files
committed
wallet: simplify EraseRecords by using 'ErasePrefix'
1 parent 3375781 commit 77331aa

File tree

1 file changed

+12
-43
lines changed

1 file changed

+12
-43
lines changed

src/wallet/walletdb.cpp

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,9 +1230,8 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
12301230
return result;
12311231
}
12321232

1233-
bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func)
1233+
static bool RunWithinTxn(WalletBatch& batch, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func)
12341234
{
1235-
WalletBatch batch(database);
12361235
if (!batch.TxnBegin()) {
12371236
LogPrint(BCLog::WALLETDB, "Error: cannot create db txn for %s\n", process_desc);
12381237
return false;
@@ -1254,6 +1253,12 @@ bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const
12541253
return true;
12551254
}
12561255

1256+
bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func)
1257+
{
1258+
WalletBatch batch(database);
1259+
return RunWithinTxn(batch, process_desc, func);
1260+
}
1261+
12571262
void MaybeCompactWalletDB(WalletContext& context)
12581263
{
12591264
static std::atomic<bool> fOneThread(false);
@@ -1316,47 +1321,11 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags)
13161321

13171322
bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
13181323
{
1319-
// Begin db txn
1320-
if (!m_batch->TxnBegin()) return false;
1321-
1322-
// Get cursor
1323-
std::unique_ptr<DatabaseCursor> cursor = m_batch->GetNewCursor();
1324-
if (!cursor)
1325-
{
1326-
return false;
1327-
}
1328-
1329-
// Iterate the DB and look for any records that have the type prefixes
1330-
while (true) {
1331-
// Read next record
1332-
DataStream key{};
1333-
DataStream value{};
1334-
DatabaseCursor::Status status = cursor->Next(key, value);
1335-
if (status == DatabaseCursor::Status::DONE) {
1336-
break;
1337-
} else if (status == DatabaseCursor::Status::FAIL) {
1338-
cursor.reset(nullptr);
1339-
m_batch->TxnAbort(); // abort db txn
1340-
return false;
1341-
}
1342-
1343-
// Make a copy of key to avoid data being deleted by the following read of the type
1344-
const SerializeData key_data{key.begin(), key.end()};
1345-
1346-
std::string type;
1347-
key >> type;
1348-
1349-
if (types.count(type) > 0) {
1350-
if (!m_batch->Erase(Span{key_data})) {
1351-
cursor.reset(nullptr);
1352-
m_batch->TxnAbort();
1353-
return false; // erase failed
1354-
}
1355-
}
1356-
}
1357-
// Finish db txn
1358-
cursor.reset(nullptr);
1359-
return m_batch->TxnCommit();
1324+
return RunWithinTxn(*this, "erase records", [&types](WalletBatch& self) {
1325+
return std::all_of(types.begin(), types.end(), [&self](const std::string& type) {
1326+
return self.m_batch->ErasePrefix(DataStream() << type);
1327+
});
1328+
});
13601329
}
13611330

13621331
bool WalletBatch::TxnBegin()

0 commit comments

Comments
 (0)