Line data Source code
1 : // Copyright (c) 2020 The Dash developers 2 : // Copyright (c) 2021-2022 The PIVX Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or https://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef PIVX_TIERTWO_NET_MASTERNODES_H 7 : #define PIVX_TIERTWO_NET_MASTERNODES_H 8 : 9 : #include "consensus/params.h" 10 : #include "net.h" 11 : #include "sync.h" 12 : #include "threadinterrupt.h" 13 : #include "uint256.h" 14 : 15 : #include <thread> 16 : 17 : class CAddress; 18 : class CConnman; 19 : class CChainParams; 20 : class CNode; 21 : class CScheduler; 22 : 23 : class TierTwoConnMan 24 : { 25 : public: 26 : struct Options { 27 : bool m_has_specified_outgoing; 28 : }; 29 : 30 : explicit TierTwoConnMan(CConnman* _connman); 31 : ~TierTwoConnMan(); 32 : 33 : // Add or update quorum nodes 34 : void setQuorumNodes(Consensus::LLMQType llmqType, 35 : const uint256& quorumHash, 36 : const std::set<uint256>& proTxHashes); 37 : 38 : // Return quorum nodes for a given llmqType 39 : std::set<uint256> getQuorumNodes(Consensus::LLMQType llmqType); 40 : 41 : // Return quorum nodes for a given llmqType and hash 42 : std::set<NodeId> getQuorumNodes(Consensus::LLMQType llmqType, uint256 quorumHash); 43 : 44 : // Return true if the quorum was already registered 45 : bool hasQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash); 46 : 47 : // Remove the registered quorum from the pending/protected MN connections 48 : void removeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash); 49 : 50 : // Add MNs to the active quorum relay members map and push QSENDRECSIGS to the verified connected peers that are part of this new quorum. 51 : void setMasternodeQuorumRelayMembers(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set<uint256>& proTxHashes); 52 : 53 : // Returns true if the node has the same address as a MN. 54 : bool isMasternodeQuorumNode(const CNode* pnode); 55 : 56 : // Whether protxHash an active quorum relay member 57 : bool isMasternodeQuorumRelayMember(const uint256& protxHash); 58 : 59 : // Add DMN to the pending connection list 60 : bool addPendingMasternode(const uint256& proTxHash); 61 : 62 : // Adds the DMNs to the pending to probe list 63 : void addPendingProbeConnections(const std::set<uint256>& proTxHashes); 64 : 65 : // Set the local DMN so the node does not try to connect to himself 66 129 : void setLocalDMN(const uint256& pro_tx_hash) { WITH_LOCK(cs_vPendingMasternodes, local_dmn_pro_tx_hash = pro_tx_hash;); } 67 : 68 : // Clear connections cache 69 : void clear(); 70 : 71 : // Manages the MN connections 72 : void ThreadOpenMasternodeConnections(); 73 : void start(CScheduler& scheduler, const TierTwoConnMan::Options& options); 74 : void stop(); 75 : void interrupt(); 76 : 77 : private: 78 : CThreadInterrupt interruptNet; 79 : std::thread threadOpenMasternodeConnections; 80 : 81 : mutable RecursiveMutex cs_vPendingMasternodes; 82 : std::vector<uint256> vPendingMasternodes GUARDED_BY(cs_vPendingMasternodes); 83 : typedef std::pair<Consensus::LLMQType, uint256> QuorumTypeAndHash; 84 : std::map<QuorumTypeAndHash, std::set<uint256>> masternodeQuorumNodes GUARDED_BY(cs_vPendingMasternodes); 85 : std::map<QuorumTypeAndHash, std::set<uint256>> masternodeQuorumRelayMembers GUARDED_BY(cs_vPendingMasternodes); 86 : std::set<uint256> masternodePendingProbes GUARDED_BY(cs_vPendingMasternodes); 87 : 88 : // The local DMN 89 : Optional<uint256> local_dmn_pro_tx_hash GUARDED_BY(cs_vPendingMasternodes){nullopt}; 90 : 91 : // parent connections manager 92 : CConnman* connman; 93 : 94 : void openConnection(const CAddress& addrConnect, bool isProbe); 95 : void doMaintenance(); 96 : }; 97 : 98 : #endif // PIVX_TIERTWO_NET_MASTERNODES_H