wallet: introduce active db txn listeners

Useful to ensure that the in-memory state is updated only
after successfully committing the data to disk.
This commit is contained in:
furszy
2024-03-26 10:48:30 -03:00
parent 91e065ec17
commit 57249ff669
8 changed files with 44 additions and 2 deletions

View File

@@ -1347,12 +1347,34 @@ bool WalletBatch::TxnBegin()
bool WalletBatch::TxnCommit()
{
return m_batch->TxnCommit();
bool res = m_batch->TxnCommit();
if (res) {
for (const auto& listener : m_txn_listeners) {
listener.on_commit();
}
// txn finished, clear listeners
m_txn_listeners.clear();
}
return res;
}
bool WalletBatch::TxnAbort()
{
return m_batch->TxnAbort();
bool res = m_batch->TxnAbort();
if (res) {
for (const auto& listener : m_txn_listeners) {
listener.on_abort();
}
// txn finished, clear listeners
m_txn_listeners.clear();
}
return res;
}
void WalletBatch::RegisterTxnListener(const DbTxnListener& l)
{
assert(m_batch->HasActiveTxn());
m_txn_listeners.emplace_back(l);
}
std::unique_ptr<WalletDatabase> MakeDatabase(const fs::path& path, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error)