Fix locks in CMasternodeSync

This commit is contained in:
UdjinM6 2021-06-23 11:11:33 +03:00
parent cc601e80c5
commit 64d6e5720f
No known key found for this signature in database
GPG Key ID: 83592BD1400D58D9

View File

@ -23,7 +23,12 @@ CMasternodeSync::CMasternodeSync()
void CMasternodeSync::Reset(bool fForce, bool fNotifyReset) void CMasternodeSync::Reset(bool fForce, bool fNotifyReset)
{ {
// Avoid resetting the sync process if we just "recently" received a new block // Avoid resetting the sync process if we just "recently" received a new block
if (fForce || (GetTime() - nTimeLastUpdateBlockTip > MASTERNODE_SYNC_RESET_SECONDS)) { if (!fForce) {
LOCK(cs);
if (GetTime() - nTimeLastUpdateBlockTip < MASTERNODE_SYNC_RESET_SECONDS) {
return;
}
}
{ {
LOCK(cs); LOCK(cs);
nCurrentAsset = MASTERNODE_SYNC_BLOCKCHAIN; nCurrentAsset = MASTERNODE_SYNC_BLOCKCHAIN;
@ -36,7 +41,6 @@ void CMasternodeSync::Reset(bool fForce, bool fNotifyReset)
if (fNotifyReset) { if (fNotifyReset) {
uiInterface.NotifyAdditionalDataSyncProgressChanged(-1); uiInterface.NotifyAdditionalDataSyncProgressChanged(-1);
} }
}
} }
void CMasternodeSync::BumpAssetLastTime(const std::string& strFuncName) void CMasternodeSync::BumpAssetLastTime(const std::string& strFuncName)
@ -88,6 +92,7 @@ void CMasternodeSync::SwitchToNextAsset(CConnman& connman)
std::string CMasternodeSync::GetSyncStatus() const std::string CMasternodeSync::GetSyncStatus() const
{ {
LOCK(cs);
switch (nCurrentAsset) { switch (nCurrentAsset) {
case MASTERNODE_SYNC_BLOCKCHAIN: return _("Synchronizing blockchain..."); case MASTERNODE_SYNC_BLOCKCHAIN: return _("Synchronizing blockchain...");
case MASTERNODE_SYNC_GOVERNANCE: return _("Synchronizing governance objects..."); case MASTERNODE_SYNC_GOVERNANCE: return _("Synchronizing governance objects...");
@ -143,6 +148,7 @@ void CMasternodeSync::ProcessTick(CConnman& connman)
return; return;
} }
LOCK(cs);
// Calculate "progress" for LOG reporting / GUI notification // Calculate "progress" for LOG reporting / GUI notification
double nSyncProgress = double(nTriedPeerCount + (nCurrentAsset - 1) * 8) / (8*4); 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); 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 // Note: since we sync headers first, it should be ok to use this
bool fReachedBestHeaderNew = pindexNew->GetBlockHash() == pindexTip->GetBlockHash(); bool fReachedBestHeaderNew = pindexNew->GetBlockHash() == pindexTip->GetBlockHash();
LOCK(cs);
if (fReachedBestHeader && !fReachedBestHeaderNew) { if (fReachedBestHeader && !fReachedBestHeaderNew) {
// Switching from true to false means that we previously stuck syncing headers for some reason, // Switching from true to false means that we previously stuck syncing headers for some reason,
// probably initial timeout was not enough, // probably initial timeout was not enough,
@ -378,10 +385,7 @@ void CMasternodeSync::UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitia
Reset(true); Reset(true);
} }
{
LOCK(cs);
fReachedBestHeader = fReachedBestHeaderNew; fReachedBestHeader = fReachedBestHeaderNew;
}
LogPrint(BCLog::MNSYNC, "CMasternodeSync::UpdatedBlockTip -- pindexNew->nHeight: %d pindexTip->nHeight: %d fInitialDownload=%d fReachedBestHeader=%d\n", LogPrint(BCLog::MNSYNC, "CMasternodeSync::UpdatedBlockTip -- pindexNew->nHeight: %d pindexTip->nHeight: %d fInitialDownload=%d fReachedBestHeader=%d\n",
pindexNew->nHeight, pindexTip->nHeight, fInitialDownload, fReachedBestHeader); pindexNew->nHeight, pindexTip->nHeight, fInitialDownload, fReachedBestHeader);
} }