Line data Source code
1 : // Copyright (c) 2018-2025 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_LLMQ_NET_DKG_H 6 : #define BITCOIN_LLMQ_NET_DKG_H 7 : 8 : #include <consensus/params.h> 9 : #include <net_processing.h> 10 : #include <sync.h> 11 : #include <uint256.h> 12 : #include <unordered_lru_cache.h> 13 : 14 : #include <map> 15 : #include <memory> 16 : #include <thread> 17 : #include <vector> 18 : 19 : class CActiveMasternodeManager; 20 : class CBLSWorker; 21 : class CConnman; 22 : class CDeterministicMNManager; 23 : class ChainstateManager; 24 : class CMasternodeMetaMan; 25 : class CSporkManager; 26 : namespace llmq { 27 : class ActiveDKGSessionHandler; 28 : class CDKGDebugManager; 29 : class CDKGSessionManager; 30 : class CQuorumBlockProcessor; 31 : class CQuorumManager; 32 : class CQuorumSnapshotManager; 33 : class QuorumRole; 34 : } // namespace llmq 35 : 36 : namespace llmq { 37 : /** 38 : * NetHandler responsible for DKG networking: 39 : * - QCONTRIB / QCOMPLAINT / QJUSTIFICATION / QPCOMMITMENT / QWATCH ProcessMessage 40 : * routing into CDKGSessionManager. The resulting MessageProcessingResult is 41 : * consumed locally via PeerManagerInternal and never propagated up. 42 : * - AlreadyHave for the four MSG_QUORUM_* DKG inv types. 43 : * - ProcessGetData for the four MSG_QUORUM_* DKG inv types (active mode only; 44 : * in observer mode the underlying Get* calls return false by construction). 45 : * 46 : * Active-mode-only deps live in @ref ActiveDKG; @ref m_active is null in 47 : * observer mode and non-null in active mode (all-or-none). 48 : * 49 : * On nodes that run neither active nor observer mode, register @ref NetDKGStub 50 : * instead. 51 : */ 52 : class NetDKG final : public NetHandler 53 : { 54 : public: 55 : //! Observer-mode constructor. 56 : NetDKG(PeerManagerInternal* peer_manager, const CSporkManager& sporkman, CDKGSessionManager& qdkgsman, 57 : const ChainstateManager& chainman, CQuorumManager& qman, QuorumRole& role); 58 : 59 : //! Active-mode constructor: takes the masternode-only dep bundle as required references. 60 : NetDKG(PeerManagerInternal* peer_manager, const CSporkManager& sporkman, CDKGSessionManager& qdkgsman, 61 : const ChainstateManager& chainman, bool quorums_watch, CQuorumManager& qman, QuorumRole& role, 62 : CBLSWorker& bls_worker, CDeterministicMNManager& dmnman, CMasternodeMetaMan& mn_metaman, 63 : CDKGDebugManager& dkgdbgman, CQuorumBlockProcessor& qblockman, CQuorumSnapshotManager& qsnapman, 64 : const CActiveMasternodeManager& mn_activeman, CConnman& connman); 65 : 66 : ~NetDKG(); 67 : 68 : // NetHandler 69 : void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv) override 70 : EXCLUSIVE_LOCKS_REQUIRED(!cs_indexed_quorums_cache); 71 : bool AlreadyHave(const CInv& inv) override; 72 : bool ProcessGetData(CNode& pfrom, const CInv& inv, CConnman& connman, const CNetMsgMaker& msgMaker) override; 73 : /** 74 : * Drives one phase-handler thread per ActiveDKGSessionHandler in active mode; 75 : * no-op in observer mode (no curSession to drive). 76 : */ 77 : void Start() override; 78 : void Stop() override; 79 : void Interrupt() override; 80 : 81 : private: 82 : //! Bundle of refs that exist only in active masternode mode. 83 : struct ActiveDKG { 84 : CDeterministicMNManager& dmnman; 85 : CMasternodeMetaMan& mn_metaman; 86 : CDKGDebugManager& dkgdbgman; 87 : CQuorumBlockProcessor& qblockman; 88 : CQuorumSnapshotManager& qsnapman; 89 : CConnman& connman; 90 : }; 91 : 92 : void PhaseHandlerThread(ActiveDKGSessionHandler& handler); 93 : void HandleDKGRound(ActiveDKGSessionHandler& handler); 94 : 95 : CDKGSessionManager& m_qdkgsman; 96 : CQuorumManager& m_qman; 97 : const CSporkManager& m_sporkman; 98 : const ChainstateManager& m_chainman; 99 : const std::unique_ptr<ActiveDKG> m_active; //!< null in observer mode, non-null in active mode 100 : 101 : /** Cache: quorum hash → quorum index, populated lazily by ProcessMessage. */ 102 : mutable Mutex cs_indexed_quorums_cache; 103 : mutable std::map<Consensus::LLMQType, Uint256LruHashMap<int>> indexed_quorums_cache GUARDED_BY(cs_indexed_quorums_cache); 104 : 105 : std::vector<std::thread> m_phase_threads; 106 : }; 107 : 108 : /** 109 : * Minimal NetHandler installed on nodes that run neither active nor observer 110 : * DKG mode. Just punishes peers that push DKG messages we cannot serve. 111 : */ 112 : class NetDKGStub final : public NetHandler 113 : { 114 : public: 115 0 : explicit NetDKGStub(PeerManagerInternal* peer_manager) : 116 0 : NetHandler(peer_manager) 117 0 : { 118 0 : } 119 : 120 : // NetHandler 121 : void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv) override; 122 : }; 123 : } // namespace llmq 124 : 125 : #endif // BITCOIN_LLMQ_NET_DKG_H