Skip to content

Commit 6604599

Browse files
committed
fix: resolve lock order issues
1 parent 327f135 commit 6604599

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/governance/governance.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ MessageProcessingResult CGovernanceManager::ProcessMessage(CNode& peer, CConnman
320320
return ret;
321321
}
322322

323-
AddGovernanceObject(govobj, &peer);
323+
InternalAddGovernanceObject(govobj, &peer);
324324
return ret;
325325
}
326326

@@ -398,9 +398,12 @@ void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj)
398398
}
399399
}
400400

401-
void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, const CNode* pfrom)
401+
void CGovernanceManager::InternalAddGovernanceObject(CGovernanceObject& govobj, const CNode* pfrom)
402402
{
403+
AssertLockHeld(::cs_main);
404+
AssertLockHeld(cs_store);
403405
AssertLockNotHeld(cs_relay);
406+
404407
uint256 nHash = govobj.GetHash();
405408
std::string strHash = nHash.ToString();
406409

@@ -410,7 +413,6 @@ void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, const CN
410413

411414
govobj.UpdateSentinelVariables(tip_mn_list); //this sets local vars in object
412415

413-
LOCK2(::cs_main, cs_store);
414416
std::string strError;
415417

416418
// MAKE SURE THIS OBJECT IS OK
@@ -459,6 +461,15 @@ void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, const CN
459461
GetMainSignals().NotifyGovernanceObject(std::make_shared<const Governance::Object>(govobj.Object()), nHash.ToString());
460462
}
461463

464+
void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, const CNode* pfrom)
465+
{
466+
AssertLockNotHeld(cs_store);
467+
AssertLockNotHeld(cs_relay);
468+
469+
LOCK2(::cs_main, cs_store);
470+
InternalAddGovernanceObject(govobj, pfrom);
471+
}
472+
462473
void CGovernanceManager::CheckAndRemove()
463474
{
464475
AssertLockNotHeld(cs_store);
@@ -989,12 +1000,11 @@ bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote,
9891000

9901001
void CGovernanceManager::CheckPostponedObjects()
9911002
{
1003+
AssertLockHeld(::cs_main);
9921004
AssertLockHeld(cs_store);
9931005
AssertLockNotHeld(cs_relay);
9941006
if (!m_mn_sync.IsSynced()) return;
9951007

996-
LOCK(::cs_main);
997-
9981008
// Check postponed proposals
9991009
for (auto it = mapPostponedObjects.begin(); it != mapPostponedObjects.end();) {
10001010
const uint256& nHash = it->first;
@@ -1006,7 +1016,7 @@ void CGovernanceManager::CheckPostponedObjects()
10061016
bool fMissingConfirmations;
10071017
if (govobj.IsCollateralValid(m_chainman, strError, fMissingConfirmations)) {
10081018
if (govobj.IsValidLocally(Assert(m_dmnman)->GetListAtChainTip(), m_chainman, strError, false)) {
1009-
AddGovernanceObject(govobj);
1019+
InternalAddGovernanceObject(govobj);
10101020
} else {
10111021
LogPrint(BCLog::GOBJECT, "CGovernanceManager::CheckPostponedObjects -- %s invalid\n", nHash.ToString());
10121022
}
@@ -1379,7 +1389,7 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex)
13791389
nCachedBlockHeight = pindex->nHeight;
13801390
LogPrint(BCLog::GOBJECT, "CGovernanceManager::UpdatedBlockTip -- nCachedBlockHeight: %d\n", nCachedBlockHeight);
13811391

1382-
LOCK(cs_store);
1392+
LOCK2(::cs_main, cs_store);
13831393
if (DeploymentDIP0003Enforced(pindex->nHeight, Params().GetConsensus())) {
13841394
RemoveInvalidVotes();
13851395
}

src/governance/governance.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ using vote_time_pair_t = std::pair<CGovernanceVote, int64_t>;
5454
static constexpr int RATE_BUFFER_SIZE = 5;
5555
static constexpr bool DEFAULT_GOVERNANCE_ENABLE{true};
5656

57+
extern RecursiveMutex cs_main;
58+
5759
class CRateCheckBuffer
5860
{
5961
private:
@@ -340,7 +342,7 @@ class CGovernanceManager : public GovernanceStore, public GovernanceSignerParent
340342
std::vector<std::shared_ptr<const CGovernanceObject>> GetApprovedProposals(const CDeterministicMNList& tip_mn_list) override
341343
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
342344
void AddGovernanceObject(CGovernanceObject& govobj, const CNode* pfrom = nullptr) override
343-
EXCLUSIVE_LOCKS_REQUIRED(!cs_relay);
345+
EXCLUSIVE_LOCKS_REQUIRED(!cs_store, !cs_relay);
344346

345347
// Superblocks
346348
bool GetSuperblockPayments(const CDeterministicMNList& tip_mn_list, int nBlockHeight,
@@ -398,14 +400,19 @@ class CGovernanceManager : public GovernanceStore, public GovernanceSignerParent
398400
const CGovernanceObject* InternalFindConstGovernanceObject(const uint256& nHash) const
399401
EXCLUSIVE_LOCKS_REQUIRED(cs_store);
400402

403+
// Internal counterpart to "Signer interface"
404+
void InternalAddGovernanceObject(CGovernanceObject& govobj, const CNode* pfrom = nullptr)
405+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, cs_store, !cs_relay);
406+
407+
// ...
401408
void MasternodeRateUpdate(const CGovernanceObject& govobj)
402409
EXCLUSIVE_LOCKS_REQUIRED(cs_store);
403410

404411
bool MasternodeRateCheck(const CGovernanceObject& govobj, bool fUpdateFailStatus, bool fForce, bool& fRateCheckBypassed)
405412
EXCLUSIVE_LOCKS_REQUIRED(cs_store);
406413

407414
void CheckPostponedObjects()
408-
EXCLUSIVE_LOCKS_REQUIRED(cs_store, !cs_relay);
415+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, cs_store, !cs_relay);
409416

410417
void InitOnLoad()
411418
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);

0 commit comments

Comments
 (0)