From a02f8302d953164685b3185aae5792982e6b5f3f Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Tue, 18 May 2021 23:29:51 +0300 Subject: [PATCH] instantsend: Postpone mempool related cleanup fixes until dip0020 activation (#4159) * instantsend: Upgrade IS db on dip0020 activation * instantsend: Do not remove islocks for txes removed from mempool until dip0020 is activated * refactor: introduce fUpgradedDB to avoid excessive locking and checking the state * llmq: Decouple `fUpgradedDB` from `cs` * Update src/llmq/quorums_instantsend.cpp Co-authored-by: dustinface <35775977+xdustinface@users.noreply.github.com> * Fix deadlock Upgrade locks cs_main via GetTransaction Co-authored-by: xdustinface Co-authored-by: dustinface <35775977+xdustinface@users.noreply.github.com> --- src/llmq/quorums_instantsend.cpp | 11 +++++++++-- src/llmq/quorums_instantsend.h | 6 ++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/llmq/quorums_instantsend.cpp b/src/llmq/quorums_instantsend.cpp index 2d1e26557b..a6e96aca84 100644 --- a/src/llmq/quorums_instantsend.cpp +++ b/src/llmq/quorums_instantsend.cpp @@ -52,7 +52,6 @@ uint256 CInstantSendLock::GetRequestId() const CInstantSendDb::CInstantSendDb(CDBWrapper& _db) : db(_db) { - Upgrade(); } void CInstantSendDb::Upgrade() @@ -1072,7 +1071,7 @@ void CInstantSendManager::TransactionAddedToMempool(const CTransactionRef& tx) void CInstantSendManager::TransactionRemovedFromMempool(const CTransactionRef& tx) { - if (tx->vin.empty()) { + if (tx->vin.empty() || !fUpgradedDB) { return; } @@ -1217,6 +1216,14 @@ void CInstantSendManager::NotifyChainLock(const CBlockIndex* pindexChainLock) void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew) { + if (!fUpgradedDB) { + LOCK(cs_main); + if (VersionBitsState(pindexNew, Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0020, versionbitscache) == ThresholdState::ACTIVE) { + db.Upgrade(); + fUpgradedDB = true; + } + } + bool fDIP0008Active = pindexNew->pprev && pindexNew->pprev->nHeight >= Params().GetConsensus().DIP0008Height; if (AreChainLocksEnabled() && fDIP0008Active) { diff --git a/src/llmq/quorums_instantsend.h b/src/llmq/quorums_instantsend.h index f7850a582d..7b963ab36d 100644 --- a/src/llmq/quorums_instantsend.h +++ b/src/llmq/quorums_instantsend.h @@ -55,11 +55,11 @@ private: void WriteInstantSendLockMined(CDBBatch& batch, const uint256& hash, int nHeight); void RemoveInstantSendLockMined(CDBBatch& batch, const uint256& hash, int nHeight); - void Upgrade(); - public: explicit CInstantSendDb(CDBWrapper& _db); + void Upgrade(); + void WriteNewInstantSendLock(const uint256& hash, const CInstantSendLock& islock); void RemoveInstantSendLock(CDBBatch& batch, const uint256& hash, CInstantSendLockPtr islock, bool keep_cache = true); @@ -87,6 +87,8 @@ private: mutable CCriticalSection cs; CInstantSendDb db; + std::atomic fUpgradedDB{false}; + std::thread workThread; CThreadInterrupt workInterrupt;