@@ -1230,9 +1230,8 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
1230
1230
return result;
1231
1231
}
1232
1232
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)
1234
1234
{
1235
- WalletBatch batch (database);
1236
1235
if (!batch.TxnBegin ()) {
1237
1236
LogPrint (BCLog::WALLETDB, " Error: cannot create db txn for %s\n " , process_desc);
1238
1237
return false ;
@@ -1254,6 +1253,12 @@ bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const
1254
1253
return true ;
1255
1254
}
1256
1255
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
+
1257
1262
void MaybeCompactWalletDB (WalletContext& context)
1258
1263
{
1259
1264
static std::atomic<bool > fOneThread (false );
@@ -1316,47 +1321,11 @@ bool WalletBatch::WriteWalletFlags(const uint64_t flags)
1316
1321
1317
1322
bool WalletBatch::EraseRecords (const std::unordered_set<std::string>& types)
1318
1323
{
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
+ });
1360
1329
}
1361
1330
1362
1331
bool WalletBatch::TxnBegin ()
0 commit comments