From 64d6e5720f7d7bd24548f5471bc17d847bd5059f Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Wed, 23 Jun 2021 11:11:33 +0300 Subject: [PATCH] Fix locks in CMasternodeSync --- src/masternode/masternode-sync.cpp | 36 +++++++++++++++++------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/masternode/masternode-sync.cpp b/src/masternode/masternode-sync.cpp index d236376782..bb962b6461 100644 --- a/src/masternode/masternode-sync.cpp +++ b/src/masternode/masternode-sync.cpp @@ -23,20 +23,24 @@ CMasternodeSync::CMasternodeSync() void CMasternodeSync::Reset(bool fForce, bool fNotifyReset) { // Avoid resetting the sync process if we just "recently" received a new block - if (fForce || (GetTime() - nTimeLastUpdateBlockTip > MASTERNODE_SYNC_RESET_SECONDS)) { - { - LOCK(cs); - nCurrentAsset = MASTERNODE_SYNC_BLOCKCHAIN; - nTriedPeerCount = 0; - nTimeAssetSyncStarted = GetTime(); - nTimeLastBumped = GetTime(); - nTimeLastUpdateBlockTip = 0; - fReachedBestHeader = false; - } - if (fNotifyReset) { - uiInterface.NotifyAdditionalDataSyncProgressChanged(-1); + if (!fForce) { + LOCK(cs); + if (GetTime() - nTimeLastUpdateBlockTip < MASTERNODE_SYNC_RESET_SECONDS) { + return; } } + { + LOCK(cs); + nCurrentAsset = MASTERNODE_SYNC_BLOCKCHAIN; + nTriedPeerCount = 0; + nTimeAssetSyncStarted = GetTime(); + nTimeLastBumped = GetTime(); + nTimeLastUpdateBlockTip = 0; + fReachedBestHeader = false; + } + if (fNotifyReset) { + uiInterface.NotifyAdditionalDataSyncProgressChanged(-1); + } } void CMasternodeSync::BumpAssetLastTime(const std::string& strFuncName) @@ -88,6 +92,7 @@ void CMasternodeSync::SwitchToNextAsset(CConnman& connman) std::string CMasternodeSync::GetSyncStatus() const { + LOCK(cs); switch (nCurrentAsset) { case MASTERNODE_SYNC_BLOCKCHAIN: return _("Synchronizing blockchain..."); case MASTERNODE_SYNC_GOVERNANCE: return _("Synchronizing governance objects..."); @@ -143,6 +148,7 @@ void CMasternodeSync::ProcessTick(CConnman& connman) return; } + LOCK(cs); // Calculate "progress" for LOG reporting / GUI notification double nSyncProgress = double(nTriedPeerCount + (nCurrentAsset - 1) * 8) / (8*4); LogPrint(BCLog::MNSYNC, "CMasternodeSync::ProcessTick -- nTick %d nCurrentAsset %d nTriedPeerCount %d nSyncProgress %f\n", nTick, nCurrentAsset, nTriedPeerCount, nSyncProgress); @@ -371,6 +377,7 @@ void CMasternodeSync::UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitia // Note: since we sync headers first, it should be ok to use this bool fReachedBestHeaderNew = pindexNew->GetBlockHash() == pindexTip->GetBlockHash(); + LOCK(cs); if (fReachedBestHeader && !fReachedBestHeaderNew) { // Switching from true to false means that we previously stuck syncing headers for some reason, // probably initial timeout was not enough, @@ -378,10 +385,7 @@ void CMasternodeSync::UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitia Reset(true); } - { - LOCK(cs); - fReachedBestHeader = fReachedBestHeaderNew; - } + fReachedBestHeader = fReachedBestHeaderNew; LogPrint(BCLog::MNSYNC, "CMasternodeSync::UpdatedBlockTip -- pindexNew->nHeight: %d pindexTip->nHeight: %d fInitialDownload=%d fReachedBestHeader=%d\n", pindexNew->nHeight, pindexTip->nHeight, fInitialDownload, fReachedBestHeader); }