From f0e6b25ed2182f0075915a806c087da890de1ccf Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Wed, 24 Nov 2021 14:29:57 +0200 Subject: [PATCH] Simplify writers flush / discard Signed-off-by: Anthony Fieroni --- src/masternodes/accountshistory.h | 24 ++++++++++++++++++ src/validation.cpp | 42 ++++--------------------------- 2 files changed, 29 insertions(+), 37 deletions(-) diff --git a/src/masternodes/accountshistory.h b/src/masternodes/accountshistory.h index 20469cd8cbd..53cd278f2ff 100644 --- a/src/masternodes/accountshistory.h +++ b/src/masternodes/accountshistory.h @@ -126,6 +126,30 @@ class CAccountsHistoryWriter : public CCustomCSView bool Flush(); }; +template +inline void FlushWriters(std::unique_ptr& writer, Args&... args) +{ + static_assert(std::is_base_of::value, "T should inherit CStorageView"); + if (writer) { + writer->Flush(); + } + if constexpr (sizeof...(Args) != 0) { + FlushWriters(args...); + } +} + +template +inline void DiscardWriters(std::unique_ptr& writer, Args&... args) +{ + static_assert(std::is_base_of::value, "T should inherit CStorageView"); + if (writer) { + writer->Discard(); + } + if constexpr (sizeof...(Args) != 0) { + DiscardWriters(args...); + } +} + extern std::unique_ptr paccountHistoryDB; extern std::unique_ptr pburnHistoryDB; diff --git a/src/validation.cpp b/src/validation.cpp index 45d21252e8a..e150e89b6da 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3538,32 +3538,16 @@ bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& cha assert(view.GetBestBlock() == pindexDelete->GetBlockHash()); std::vector disconnectedConfirms; if (DisconnectBlock(block, pindexDelete, view, mnview, disconnectedConfirms) != DISCONNECT_OK) { - // no usable history - if (paccountHistoryDB) { - paccountHistoryDB->Discard(); - } - if (pburnHistoryDB) { - pburnHistoryDB->Discard(); - } - if (pvaultHistoryDB) { - pvaultHistoryDB->Discard(); - } m_disconnectTip = false; + // no usable history + DiscardWriters(paccountHistoryDB, pburnHistoryDB, pvaultHistoryDB); return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString()); } bool flushed = view.Flush() && mnview.Flush(); assert(flushed); // flush history - if (paccountHistoryDB) { - paccountHistoryDB->Flush(); - } - if (pburnHistoryDB) { - pburnHistoryDB->Flush(); - } - if (pvaultHistoryDB) { - pvaultHistoryDB->Flush(); - } + FlushWriters(paccountHistoryDB, pburnHistoryDB, pvaultHistoryDB); if (!disconnectedConfirms.empty()) { for (auto const & confirm : disconnectedConfirms) { @@ -3709,15 +3693,7 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp InvalidBlockFound(pindexNew, state); } // no usable history - if (paccountHistoryDB) { - paccountHistoryDB->Discard(); - } - if (pburnHistoryDB) { - pburnHistoryDB->Discard(); - } - if (pvaultHistoryDB) { - pvaultHistoryDB->Discard(); - } + DiscardWriters(paccountHistoryDB, pburnHistoryDB, pvaultHistoryDB); return error("%s: ConnectBlock %s failed, %s", __func__, pindexNew->GetBlockHash().ToString(), FormatStateMessage(state)); } nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2; @@ -3726,15 +3702,7 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp assert(flushed); // flush history - if (paccountHistoryDB) { - paccountHistoryDB->Flush(); - } - if (pburnHistoryDB) { - pburnHistoryDB->Flush(); - } - if (pvaultHistoryDB) { - pvaultHistoryDB->Flush(); - } + FlushWriters(paccountHistoryDB, pburnHistoryDB, pvaultHistoryDB); // anchor rewards re-voting etc... if (!rewardedAnchors.empty()) {