Skip to content

Commit b440b16

Browse files
Merge #6954: feat: convert CSigSharesManager to use Mutex instead of RecursiveMutex
e98ba12 refactor: streamline signature recovery process in CSigSharesManager (pasta) 0a08b2a refactor: enhance CSigSharesManager for single-member quorum handling (pasta) 582e6e4 chore: run clang-format (pasta) 8832775 feat: convert CSigSharesManager to use Mutex instead of RecursiveMutex (pasta) Pull request description: update lock requirements for various methods. This change enhances thread safety and simplifies locking semantics throughout the class. ## Issue being fixed or feature implemented After other recent lock contention improvements, this RecursiveMutex is now the source of the most contention during high load. Convert the mutex from Recursive to non-recursive. Surprisingly compiler is happy with this and we don't need more invasive changes ## What was done? ## How Has This Been Tested? Builds ## Breaking Changes None ## 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)_ ACKs for top commit: knst: utACK e98ba12 UdjinM6: utACK e98ba12 Tree-SHA512: ac9a9df73ed099e8518954b80cc83cf5e92122162ecb26d2a80ace65c7ef7c65dcd4e07fddbd60fece549dcfbcf42b2698cb60b4149aea9720f9be3dc1bbcff5
2 parents cdde1b2 + e98ba12 commit b440b16

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed

src/llmq/signing_shares.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,7 @@ void CSigSharesManager::TryRecoverSig(const CQuorum& quorum, const uint256& id,
783783

784784
std::vector<CBLSSignature> sigSharesForRecovery;
785785
std::vector<CBLSId> idsForRecovery;
786+
std::shared_ptr<CRecoveredSig> singleMemberRecoveredSig;
786787
{
787788
LOCK(cs);
788789

@@ -805,10 +806,8 @@ void CSigSharesManager::TryRecoverSig(const CQuorum& quorum, const uint256& id,
805806
LogPrint(BCLog::LLMQ_SIGS, "CSigSharesManager::%s -- recover single-node signature. id=%s, msgHash=%s\n",
806807
__func__, id.ToString(), msgHash.ToString());
807808

808-
auto rs = std::make_shared<CRecoveredSig>(quorum.params.type, quorum.qc->quorumHash, id, msgHash,
809+
singleMemberRecoveredSig = std::make_shared<CRecoveredSig>(quorum.params.type, quorum.qc->quorumHash, id, msgHash,
809810
recoveredSig);
810-
sigman.ProcessRecoveredSig(rs, m_peerman);
811-
return; // end of single-quorum processing
812811
}
813812

814813
sigSharesForRecovery.reserve((size_t) quorum.params.threshold);
@@ -825,6 +824,12 @@ void CSigSharesManager::TryRecoverSig(const CQuorum& quorum, const uint256& id,
825824
}
826825
}
827826

827+
// Handle single-member quorum case after releasing the lock
828+
if (singleMemberRecoveredSig) {
829+
sigman.ProcessRecoveredSig(singleMemberRecoveredSig, m_peerman);
830+
return; // end of single-quorum processing
831+
}
832+
828833
// now recover it
829834
cxxtimer::Timer t(true);
830835
CBLSSignature recoveredSig;

src/llmq/signing_shares.h

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ class CSigSharesManager : public CRecoveredSigsListener
376376
static constexpr int64_t MAX_SEND_FOR_RECOVERY_TIMEOUT{10000};
377377
static constexpr size_t MAX_MSGS_SIG_SHARES{32};
378378

379-
RecursiveMutex cs;
379+
Mutex cs;
380380

381381
std::thread workThread;
382382
CThreadInterrupt workInterrupt;
@@ -424,73 +424,79 @@ class CSigSharesManager : public CRecoveredSigsListener
424424
const CQuorumManager& _qman, const CSporkManager& sporkman);
425425
~CSigSharesManager() override;
426426

427-
void StartWorkerThread();
428-
void StopWorkerThread();
429-
void RegisterAsRecoveredSigsListener();
430-
void UnregisterAsRecoveredSigsListener();
431-
void InterruptWorkerThread();
427+
void StartWorkerThread() EXCLUSIVE_LOCKS_REQUIRED(!cs);
428+
void StopWorkerThread() EXCLUSIVE_LOCKS_REQUIRED(!cs);
429+
void RegisterAsRecoveredSigsListener() EXCLUSIVE_LOCKS_REQUIRED(!cs);
430+
void UnregisterAsRecoveredSigsListener() EXCLUSIVE_LOCKS_REQUIRED(!cs);
431+
void InterruptWorkerThread() EXCLUSIVE_LOCKS_REQUIRED(!cs);
432432

433-
void ProcessMessage(const CNode& pnode, const std::string& msg_type, CDataStream& vRecv);
433+
void ProcessMessage(const CNode& pnode, const std::string& msg_type, CDataStream& vRecv) EXCLUSIVE_LOCKS_REQUIRED(!cs);
434434

435435
void AsyncSign(CQuorumCPtr quorum, const uint256& id, const uint256& msgHash)
436-
EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingSigns);
437-
std::optional<CSigShare> CreateSigShare(const CQuorum& quorum, const uint256& id, const uint256& msgHash) const;
438-
void ForceReAnnouncement(const CQuorum& quorum, Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash);
436+
EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingSigns, !cs);
437+
std::optional<CSigShare> CreateSigShare(const CQuorum& quorum, const uint256& id, const uint256& msgHash) const
438+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
439+
void ForceReAnnouncement(const CQuorum& quorum, Consensus::LLMQType llmqType, const uint256& id,
440+
const uint256& msgHash) EXCLUSIVE_LOCKS_REQUIRED(!cs);
439441

440-
[[nodiscard]] MessageProcessingResult HandleNewRecoveredSig(const CRecoveredSig& recoveredSig) override;
442+
[[nodiscard]] MessageProcessingResult HandleNewRecoveredSig(const CRecoveredSig& recoveredSig) override
443+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
441444

442445
static CDeterministicMNCPtr SelectMemberForRecovery(const CQuorum& quorum, const uint256& id, int attempt);
443446

444447
bool AsyncSignIfMember(Consensus::LLMQType llmqType, CSigningManager& sigman, const uint256& id,
445448
const uint256& msgHash, const uint256& quorumHash = uint256(), bool allowReSign = false,
446-
bool allowDiffMsgHashSigning = false)
447-
EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingSigns);
449+
bool allowDiffMsgHashSigning = false) EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingSigns, !cs);
448450

449-
void NotifyRecoveredSig(const std::shared_ptr<const CRecoveredSig>& sig) const;
451+
void NotifyRecoveredSig(const std::shared_ptr<const CRecoveredSig>& sig) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
450452

451453
private:
452454
// all of these return false when the currently processed message should be aborted (as each message actually contains multiple messages)
453-
bool ProcessMessageSigSesAnn(const CNode& pfrom, const CSigSesAnn& ann);
454-
bool ProcessMessageSigSharesInv(const CNode& pfrom, const CSigSharesInv& inv);
455-
bool ProcessMessageGetSigShares(const CNode& pfrom, const CSigSharesInv& inv);
456-
bool ProcessMessageBatchedSigShares(const CNode& pfrom, const CBatchedSigShares& batchedSigShares);
457-
void ProcessMessageSigShare(NodeId fromId, const CSigShare& sigShare);
455+
bool ProcessMessageSigSesAnn(const CNode& pfrom, const CSigSesAnn& ann) EXCLUSIVE_LOCKS_REQUIRED(!cs);
456+
bool ProcessMessageSigSharesInv(const CNode& pfrom, const CSigSharesInv& inv) EXCLUSIVE_LOCKS_REQUIRED(!cs);
457+
bool ProcessMessageGetSigShares(const CNode& pfrom, const CSigSharesInv& inv) EXCLUSIVE_LOCKS_REQUIRED(!cs);
458+
bool ProcessMessageBatchedSigShares(const CNode& pfrom, const CBatchedSigShares& batchedSigShares)
459+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
460+
void ProcessMessageSigShare(NodeId fromId, const CSigShare& sigShare) EXCLUSIVE_LOCKS_REQUIRED(!cs);
458461

459462
static bool VerifySigSharesInv(Consensus::LLMQType llmqType, const CSigSharesInv& inv);
460463
static bool PreVerifyBatchedSigShares(const CActiveMasternodeManager& mn_activeman, const CQuorumManager& quorum_manager,
461464
const CSigSharesNodeState::SessionInfo& session, const CBatchedSigShares& batchedSigShares, bool& retBan);
462465

463466
bool CollectPendingSigSharesToVerify(
464467
size_t maxUniqueSessions, std::unordered_map<NodeId, std::vector<CSigShare>>& retSigShares,
465-
std::unordered_map<std::pair<Consensus::LLMQType, uint256>, CQuorumCPtr, StaticSaltedHasher>& retQuorums);
466-
bool ProcessPendingSigShares();
468+
std::unordered_map<std::pair<Consensus::LLMQType, uint256>, CQuorumCPtr, StaticSaltedHasher>& retQuorums)
469+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
470+
bool ProcessPendingSigShares() EXCLUSIVE_LOCKS_REQUIRED(!cs);
467471

468472
void ProcessPendingSigShares(
469473
const std::vector<CSigShare>& sigSharesToProcess,
470-
const std::unordered_map<std::pair<Consensus::LLMQType, uint256>, CQuorumCPtr, StaticSaltedHasher>& quorums);
474+
const std::unordered_map<std::pair<Consensus::LLMQType, uint256>, CQuorumCPtr, StaticSaltedHasher>& quorums)
475+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
471476

472-
void ProcessSigShare(const CSigShare& sigShare, const CQuorumCPtr& quorum);
473-
void TryRecoverSig(const CQuorum& quorum, const uint256& id, const uint256& msgHash);
477+
void ProcessSigShare(const CSigShare& sigShare, const CQuorumCPtr& quorum) EXCLUSIVE_LOCKS_REQUIRED(!cs);
478+
void TryRecoverSig(const CQuorum& quorum, const uint256& id, const uint256& msgHash) EXCLUSIVE_LOCKS_REQUIRED(!cs);
474479

475-
bool GetSessionInfoByRecvId(NodeId nodeId, uint32_t sessionId, CSigSharesNodeState::SessionInfo& retInfo);
480+
bool GetSessionInfoByRecvId(NodeId nodeId, uint32_t sessionId, CSigSharesNodeState::SessionInfo& retInfo)
481+
EXCLUSIVE_LOCKS_REQUIRED(!cs);
476482
static CSigShare RebuildSigShare(const CSigSharesNodeState::SessionInfo& session, const std::pair<uint16_t, CBLSLazySignature>& in);
477483

478-
void Cleanup();
484+
void Cleanup() EXCLUSIVE_LOCKS_REQUIRED(!cs);
479485
void RemoveSigSharesForSession(const uint256& signHash) EXCLUSIVE_LOCKS_REQUIRED(cs);
480-
void RemoveBannedNodeStates();
486+
void RemoveBannedNodeStates() EXCLUSIVE_LOCKS_REQUIRED(!cs);
481487

482-
void BanNode(NodeId nodeId);
488+
void BanNode(NodeId nodeId) EXCLUSIVE_LOCKS_REQUIRED(!cs);
483489

484-
bool SendMessages();
490+
bool SendMessages() EXCLUSIVE_LOCKS_REQUIRED(!cs);
485491
void CollectSigSharesToRequest(std::unordered_map<NodeId, Uint256HashMap<CSigSharesInv>>& sigSharesToRequest)
486492
EXCLUSIVE_LOCKS_REQUIRED(cs);
487493
void CollectSigSharesToSend(std::unordered_map<NodeId, Uint256HashMap<CBatchedSigShares>>& sigSharesToSend)
488494
EXCLUSIVE_LOCKS_REQUIRED(cs);
489495
void CollectSigSharesToSendConcentrated(std::unordered_map<NodeId, std::vector<CSigShare>>& sigSharesToSend, const std::vector<CNode*>& vNodes) EXCLUSIVE_LOCKS_REQUIRED(cs);
490496
void CollectSigSharesToAnnounce(std::unordered_map<NodeId, Uint256HashMap<CSigSharesInv>>& sigSharesToAnnounce)
491497
EXCLUSIVE_LOCKS_REQUIRED(cs);
492-
void SignPendingSigShares() EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingSigns);
493-
void WorkThreadMain() EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingSigns);
498+
void SignPendingSigShares() EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingSigns, !cs);
499+
void WorkThreadMain() EXCLUSIVE_LOCKS_REQUIRED(!cs_pendingSigns, !cs);
494500
};
495501
} // namespace llmq
496502

0 commit comments

Comments
 (0)