aboutsummaryrefslogtreecommitdiff
path: root/src/wallet/walletdb.cpp
diff options
context:
space:
mode:
authorfurszy <matiasfurszyfer@protonmail.com>2024-02-08 12:34:26 -0300
committerfurszy <matiasfurszyfer@protonmail.com>2024-02-12 16:05:14 -0300
commitcf4d72a75e9603e947b3d47e1f3ac7c7f37bb401 (patch)
tree7ba3c8cd493d1d523d647cf44fabe439ee29b4b8 /src/wallet/walletdb.cpp
parent6ff0aa089c01ff3e610ecb47814ed739d685a14c (diff)
downloadbitcoin-cf4d72a75e9603e947b3d47e1f3ac7c7f37bb401.tar.xz
wallet: db, introduce 'RunWithinTxn()' helper function
'RunWithinTxn()' provides a way to execute db operations within a transactional context. It avoids writing repetitive boilerplate code for starting and committing the database transaction.
Diffstat (limited to 'src/wallet/walletdb.cpp')
-rw-r--r--src/wallet/walletdb.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp
index 6e37bc2930..c66b09628e 100644
--- a/src/wallet/walletdb.cpp
+++ b/src/wallet/walletdb.cpp
@@ -1230,6 +1230,30 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
return result;
}
+bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const std::function<bool(WalletBatch&)>& func)
+{
+ WalletBatch batch(database);
+ if (!batch.TxnBegin()) {
+ LogPrint(BCLog::WALLETDB, "Error: cannot create db txn for %s\n", process_desc);
+ return false;
+ }
+
+ // Run procedure
+ if (!func(batch)) {
+ LogPrint(BCLog::WALLETDB, "Error: %s failed\n", process_desc);
+ batch.TxnAbort();
+ return false;
+ }
+
+ if (!batch.TxnCommit()) {
+ LogPrint(BCLog::WALLETDB, "Error: cannot commit db txn for %s\n", process_desc);
+ return false;
+ }
+
+ // All good
+ return true;
+}
+
void MaybeCompactWalletDB(WalletContext& context)
{
static std::atomic<bool> fOneThread(false);