diff --git a/src/evo/cbtx.cpp b/src/evo/cbtx.cpp index 6e44a0dcaaa0a..de68e1a18edcc 100644 --- a/src/evo/cbtx.cpp +++ b/src/evo/cbtx.cpp @@ -111,12 +111,10 @@ bool CheckCbTxMerkleRoots(const CBlock& block, const CBlockIndex* pindex, const bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev, uint256& merkleRootRet, BlockValidationState& state, const CCoinsViewCache& view) { - LOCK(deterministicMNManager->cs); - try { - static int64_t nTimeDMN = 0; - static int64_t nTimeSMNL = 0; - static int64_t nTimeMerkle = 0; + static std::atomic nTimeDMN = 0; + static std::atomic nTimeSMNL = 0; + static std::atomic nTimeMerkle = 0; int64_t nTime1 = GetTimeMicros(); @@ -134,10 +132,12 @@ bool CalcCbTxMerkleRootMNList(const CBlock& block, const CBlockIndex* pindexPrev int64_t nTime3 = GetTimeMicros(); nTimeSMNL += nTime3 - nTime2; LogPrint(BCLog::BENCHMARK, " - CSimplifiedMNList: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeSMNL * 0.000001); - static CSimplifiedMNList smlCached; - static uint256 merkleRootCached; - static bool mutatedCached{false}; + static Mutex cached_mutex; + static CSimplifiedMNList smlCached GUARDED_BY(cached_mutex); + static uint256 merkleRootCached GUARDED_BY(cached_mutex); + static bool mutatedCached GUARDED_BY(cached_mutex) {false}; + LOCK(cached_mutex); if (sml == smlCached) { merkleRootRet = merkleRootCached; if (mutatedCached) { diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp index 12baa3cfcec96..9b86418bcea15 100644 --- a/src/evo/deterministicmns.cpp +++ b/src/evo/deterministicmns.cpp @@ -613,8 +613,6 @@ bool CDeterministicMNManager::ProcessBlock(const CBlock& block, const CBlockInde int nHeight = pindex->nHeight; try { - LOCK(cs); - if (!BuildNewListFromBlock(block, pindex->pprev, state, view, newList, true)) { // pass the state returned by the function above return false; @@ -626,6 +624,8 @@ bool CDeterministicMNManager::ProcessBlock(const CBlock& block, const CBlockInde newList.SetBlockHash(pindex->GetBlockHash()); + LOCK(cs); + oldList = GetListForBlockInternal(pindex->pprev); diff = oldList.BuildDiff(newList); @@ -708,11 +708,9 @@ void CDeterministicMNManager::UpdatedBlockTip(const CBlockIndex* pindex) bool CDeterministicMNManager::BuildNewListFromBlock(const CBlock& block, const CBlockIndex* pindexPrev, BlockValidationState& state, const CCoinsViewCache& view, CDeterministicMNList& mnListRet, bool debugLogs) { - AssertLockHeld(cs); - int nHeight = pindexPrev->nHeight + 1; - CDeterministicMNList oldList = GetListForBlockInternal(pindexPrev); + CDeterministicMNList oldList = GetListForBlock(pindexPrev); CDeterministicMNList newList = oldList; newList.SetBlockHash(uint256()); // we can't know the final block hash, so better not return a (invalid) block hash newList.SetHeight(nHeight); diff --git a/src/evo/deterministicmns.h b/src/evo/deterministicmns.h index 8af688c822897..7d56e3d10331f 100644 --- a/src/evo/deterministicmns.h +++ b/src/evo/deterministicmns.h @@ -574,10 +574,8 @@ class CDeterministicMNManager static constexpr int DISK_SNAPSHOTS = llmq_max_blocks() / DISK_SNAPSHOT_PERIOD + 1; static constexpr int LIST_DIFFS_CACHE_SIZE = DISK_SNAPSHOT_PERIOD * DISK_SNAPSHOTS; -public: - Mutex cs; - private: + Mutex cs; Mutex cs_cleanup; // We have performed CleanupCache() on this height. int did_cleanup GUARDED_BY(cs_cleanup) {0}; @@ -607,7 +605,7 @@ class CDeterministicMNManager // the returned list will not contain the correct block hash (we can't know it yet as the coinbase TX is not updated yet) bool BuildNewListFromBlock(const CBlock& block, const CBlockIndex* pindexPrev, BlockValidationState& state, const CCoinsViewCache& view, - CDeterministicMNList& mnListRet, bool debugLogs) EXCLUSIVE_LOCKS_REQUIRED(cs); + CDeterministicMNList& mnListRet, bool debugLogs) LOCKS_EXCLUDED(cs); static void HandleQuorumCommitment(const llmq::CFinalCommitment& qc, const CBlockIndex* pQuorumBaseBlockIndex, CDeterministicMNList& mnList, bool debugLogs); CDeterministicMNList GetListForBlock(const CBlockIndex* pindex) LOCKS_EXCLUDED(cs) {