Skip to content

Commit

Permalink
wallet: simplify EraseRecords by using 'ErasePrefix'
Browse files Browse the repository at this point in the history
  • Loading branch information
furszy committed Feb 7, 2024
1 parent d8265e5 commit b7b5e5b
Showing 1 changed file with 2 additions and 38 deletions.
40 changes: 2 additions & 38 deletions src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,46 +1376,10 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags)

bool WalletBatch::EraseRecords(const std::unordered_set<std::string>& types)
{
// Begin db txn
if (!m_batch->TxnBegin()) return false;

// Get cursor
std::unique_ptr<DatabaseCursor> cursor = m_batch->GetNewCursor();
if (!cursor)
{
return false;
}

// Iterate the DB and look for any records that have the type prefixes
while (true) {
// Read next record
DataStream key{};
DataStream value{};
DatabaseCursor::Status status = cursor->Next(key, value);
if (status == DatabaseCursor::Status::DONE) {
break;
} else if (status == DatabaseCursor::Status::FAIL) {
cursor.reset(nullptr);
m_batch->TxnAbort(); // abort db txn
return false;
}

// Make a copy of key to avoid data being deleted by the following read of the type
const SerializeData key_data{key.begin(), key.end()};

std::string type;
key >> type;

if (types.count(type) > 0) {
if (!m_batch->Erase(Span{key_data})) {
cursor.reset(nullptr);
m_batch->TxnAbort();
return false; // erase failed
}
}
for (const auto& type : types) {
if (!m_batch->ErasePrefix(DataStream() << type)) return false;
}
// Finish db txn
cursor.reset(nullptr);
return m_batch->TxnCommit();
}

Expand Down

0 comments on commit b7b5e5b

Please sign in to comment.