Skip to content

Commit 132aded

Browse files
refactor: avoid cs_main (dashpay#5650)
## Issue being fixed or feature implemented Avoid locking cs_main in high volume call locations; I'm not fully sure the removal in signature_shares.cpp is okay; but it compiles. ## What was done? Removed or reduced scope ## How Has This Been Tested? Running with enable-debug ## Breaking Changes Should be done ## Checklist: _Go over all the following points, and put an `x` in all the boxes that apply._ - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_
1 parent 3133be1 commit 132aded

File tree

4 files changed

+9
-13
lines changed

4 files changed

+9
-13
lines changed

src/llmq/chainlocks.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,10 @@ void CChainLocksHandler::UpdatedBlockTip()
219219

220220
void CChainLocksHandler::CheckActiveState()
221221
{
222-
const bool fDIP0008Active = WITH_LOCK(cs_main, return (m_chainstate.m_chain.Tip() != nullptr) && (m_chainstate.m_chain.Tip()->pprev != nullptr) && m_chainstate.m_chain.Tip()->pprev->nHeight >= Params().GetConsensus().DIP0008Height);
223-
224-
bool oldIsEnforced = isEnforced;
222+
bool oldIsEnabled = isEnabled;
225223
isEnabled = AreChainLocksEnabled(spork_manager);
226-
isEnforced = (fDIP0008Active && isEnabled);
227224

228-
if (!oldIsEnforced && isEnforced) {
225+
if (!oldIsEnabled && isEnabled) {
229226
// ChainLocks got activated just recently, but it's possible that it was already running before, leaving
230227
// us with some stale values which we should not try to enforce anymore (there probably was a good reason
231228
// to disable spork19)
@@ -477,7 +474,7 @@ void CChainLocksHandler::EnforceBestChainLock()
477474
{
478475
LOCK(cs);
479476

480-
if (!isEnforced) {
477+
if (!isEnabled) {
481478
return;
482479
}
483480

@@ -563,7 +560,7 @@ bool CChainLocksHandler::InternalHasChainLock(int nHeight, const uint256& blockH
563560
{
564561
AssertLockHeld(cs);
565562

566-
if (!isEnforced) {
563+
if (!isEnabled) {
567564
return false;
568565
}
569566

@@ -593,7 +590,7 @@ bool CChainLocksHandler::InternalHasConflictingChainLock(int nHeight, const uint
593590
{
594591
AssertLockHeld(cs);
595592

596-
if (!isEnforced) {
593+
if (!isEnabled) {
597594
return false;
598595
}
599596

src/llmq/chainlocks.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ class CChainLocksHandler : public CRecoveredSigsListener
5959
mutable Mutex cs;
6060
std::atomic<bool> tryLockChainTipScheduled{false};
6161
std::atomic<bool> isEnabled{false};
62-
std::atomic<bool> isEnforced{false};
6362

6463
uint256 bestChainLockHash GUARDED_BY(cs);
6564
CChainLockSig bestChainLock GUARDED_BY(cs);

src/llmq/signing_shares.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ void CSigSharesManager::RemoveBannedNodeStates()
13871387
{
13881388
// Called regularly to cleanup local node states for banned nodes
13891389

1390-
LOCK2(cs_main, cs);
1390+
LOCK(cs);
13911391
for (auto it = nodeStates.begin(); it != nodeStates.end();) {
13921392
if (m_peerman->IsBanned(it->first)) {
13931393
// re-request sigshares from other nodes

src/masternode/sync.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,7 @@ void CMasternodeSync::UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitia
338338
LogPrint(BCLog::MNSYNC, "CMasternodeSync::UpdatedBlockTip -- pindexNew->nHeight: %d fInitialDownload=%d\n", pindexNew->nHeight, fInitialDownload);
339339
nTimeLastUpdateBlockTip = GetTime<std::chrono::seconds>().count();
340340

341-
CBlockIndex* pindexTip = WITH_LOCK(cs_main, return pindexBestHeader);
342-
343-
if (IsSynced() || !pindexTip)
341+
if (IsSynced())
344342
return;
345343

346344
if (!IsBlockchainSynced()) {
@@ -359,6 +357,8 @@ void CMasternodeSync::UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitia
359357
}
360358

361359
// Note: since we sync headers first, it should be ok to use this
360+
CBlockIndex* pindexTip = WITH_LOCK(cs_main, return pindexBestHeader);
361+
if (pindexTip == nullptr) return;
362362
bool fReachedBestHeaderNew = pindexNew->GetBlockHash() == pindexTip->GetBlockHash();
363363

364364
if (fReachedBestHeader && !fReachedBestHeaderNew) {

0 commit comments

Comments
 (0)