Line data Source code
1 : // Copyright (c) 2018-2025 The Dash Core developers 2 : // Distributed under the MIT/X11 software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_LLMQ_DEBUG_H 6 : #define BITCOIN_LLMQ_DEBUG_H 7 : 8 : #include <consensus/params.h> 9 : #include <sync.h> 10 : #include <univalue.h> 11 : 12 : #include <functional> 13 : #include <unordered_set> 14 : 15 : class CDataStream; 16 : class CDeterministicMNManager; 17 : class ChainstateManager; 18 : class CInv; 19 : class CScheduler; 20 : struct RPCResult; 21 : 22 : namespace llmq 23 : { 24 : class CQuorumSnapshotManager; 25 : 26 : enum class QuorumPhase; 27 : 28 : class CDKGDebugMemberStatus 29 : { 30 : public: 31 : union { 32 : struct 33 : { 34 : // is it locally considered as bad (and thus removed from the validMembers set) 35 : bool bad : 1; 36 : // did we complain about this member 37 : bool weComplain : 1; 38 : 39 : // received message for DKG phases 40 : bool receivedContribution : 1; 41 : bool receivedComplaint : 1; 42 : bool receivedJustification : 1; 43 : bool receivedPrematureCommitment : 1; 44 : } statusBits; 45 : uint8_t statusBitset; 46 : }; 47 : 48 : std::unordered_set<uint16_t> complaintsFromMembers; 49 : 50 : public: 51 0 : CDKGDebugMemberStatus() : statusBitset(0) {} 52 : }; 53 : 54 : class CDKGDebugSessionStatus 55 : { 56 : public: 57 0 : Consensus::LLMQType llmqType{Consensus::LLMQType::LLMQ_NONE}; 58 : uint256 quorumHash; 59 0 : uint32_t quorumHeight{0}; 60 0 : QuorumPhase phase{0}; 61 : 62 : union { 63 : struct 64 : { 65 : // sent messages for DKG phases 66 : bool sentContributions : 1; 67 : bool sentComplaint : 1; 68 : bool sentJustification : 1; 69 : bool sentPrematureCommitment : 1; 70 : 71 : bool aborted : 1; 72 : } statusBits; 73 : uint8_t statusBitset; 74 : }; 75 : 76 : std::vector<CDKGDebugMemberStatus> members; 77 : 78 : public: 79 0 : CDKGDebugSessionStatus() : statusBitset(0) {} 80 : 81 : [[nodiscard]] static RPCResult GetJsonHelp(const std::string& key, bool optional); 82 : [[nodiscard]] UniValue ToJson(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman, 83 : const ChainstateManager& chainman, int quorumIndex, int detailLevel) const; 84 : }; 85 : 86 0 : struct CDKGDebugStatus { 87 0 : int64_t nTime{0}; 88 : std::map<std::pair<Consensus::LLMQType, int>, CDKGDebugSessionStatus> sessions; 89 : }; 90 : 91 : class CDKGDebugManager 92 : { 93 : private: 94 : CDeterministicMNManager& m_dmnman; 95 : CQuorumSnapshotManager& m_qsnapman; 96 : const ChainstateManager& m_chainman; 97 : 98 : private: 99 : mutable Mutex cs_lockStatus; 100 : CDKGDebugStatus localStatus GUARDED_BY(cs_lockStatus); 101 : 102 : public: 103 : CDKGDebugManager(const CDKGDebugManager&) = delete; 104 : CDKGDebugManager& operator=(const CDKGDebugManager&) = delete; 105 : CDKGDebugManager(CDeterministicMNManager& dmnman, CQuorumSnapshotManager& qsnapman, const ChainstateManager& chainman); 106 : ~CDKGDebugManager(); 107 : 108 : void ResetLocalSessionStatus(Consensus::LLMQType llmqType, int quorumIndex) EXCLUSIVE_LOCKS_REQUIRED(!cs_lockStatus); 109 : void InitLocalSessionStatus(const Consensus::LLMQParams& llmqParams, int quorumIndex, const uint256& quorumHash, 110 : int quorumHeight) EXCLUSIVE_LOCKS_REQUIRED(!cs_lockStatus); 111 : 112 : void UpdateLocalSessionStatus(Consensus::LLMQType llmqType, int quorumIndex, 113 : std::function<bool(CDKGDebugSessionStatus& status)>&& func) 114 : EXCLUSIVE_LOCKS_REQUIRED(!cs_lockStatus); 115 : void UpdateLocalMemberStatus(Consensus::LLMQType llmqType, int quorumIndex, size_t memberIdx, 116 : std::function<bool(CDKGDebugMemberStatus& status)>&& func) 117 : EXCLUSIVE_LOCKS_REQUIRED(!cs_lockStatus); 118 : 119 : //! Set the locally tracked phase to @p newPhase if different. 120 : void MarkPhaseAdvanced(Consensus::LLMQType llmqType, int quorumIndex, QuorumPhase newPhase) 121 : EXCLUSIVE_LOCKS_REQUIRED(!cs_lockStatus); 122 : //! Mark the local session as aborted. 123 : void MarkAborted(Consensus::LLMQType llmqType, int quorumIndex) EXCLUSIVE_LOCKS_REQUIRED(!cs_lockStatus); 124 : 125 : size_t GetSessionCount() const 126 : EXCLUSIVE_LOCKS_REQUIRED(!cs_lockStatus); 127 : [[nodiscard]] static RPCResult GetJsonHelp(const std::string& key, bool optional, bool inner_optional = false); 128 : [[nodiscard]] UniValue ToJson(int detailLevel) const 129 : EXCLUSIVE_LOCKS_REQUIRED(!cs_lockStatus); 130 : }; 131 : } // namespace llmq 132 : 133 : #endif // BITCOIN_LLMQ_DEBUG_H