Line data Source code
1 : // Copyright (c) 2014-2021 The Dash Core developers 2 : // Copyright (c) 2015-2022 The PIVX Core developers 3 : // Distributed under the MIT/X11 software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef PIVX_ACTIVEMASTERNODE_H 7 : #define PIVX_ACTIVEMASTERNODE_H 8 : 9 : #include "key.h" 10 : #include "evo/deterministicmns.h" 11 : #include "operationresult.h" 12 : #include "sync.h" 13 : #include "validationinterface.h" 14 : 15 : class CActiveDeterministicMasternodeManager; 16 : class CBLSPublicKey; 17 : class CBLSSecretKey; 18 : 19 : #define ACTIVE_MASTERNODE_INITIAL 0 // initial state 20 : #define ACTIVE_MASTERNODE_SYNC_IN_PROCESS 1 21 : #define ACTIVE_MASTERNODE_NOT_CAPABLE 3 22 : #define ACTIVE_MASTERNODE_STARTED 4 23 : 24 : extern CActiveDeterministicMasternodeManager* activeMasternodeManager; 25 : 26 : struct CActiveMasternodeInfo 27 : { 28 : // Keys for the active Masternode 29 : CBLSPublicKey pubKeyOperator; 30 : CBLSSecretKey keyOperator; 31 : // Initialized while registering Masternode 32 : uint256 proTxHash{UINT256_ZERO}; 33 : CService service; 34 : }; 35 : 36 : class CActiveDeterministicMasternodeManager : public CValidationInterface 37 : { 38 : public: 39 : enum masternode_state_t { 40 : MASTERNODE_WAITING_FOR_PROTX, 41 : MASTERNODE_POSE_BANNED, 42 : MASTERNODE_REMOVED, 43 : MASTERNODE_OPERATOR_KEY_CHANGED, 44 : MASTERNODE_PROTX_IP_CHANGED, 45 : MASTERNODE_READY, 46 : MASTERNODE_ERROR, 47 : }; 48 : 49 : private: 50 : masternode_state_t state{MASTERNODE_WAITING_FOR_PROTX}; 51 : std::string strError; 52 : CActiveMasternodeInfo info; 53 : 54 : public: 55 82 : ~CActiveDeterministicMasternodeManager() override = default; 56 : void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) override; 57 : 58 : void Init(const CBlockIndex* pindexTip); 59 : void Reset(masternode_state_t _state, const CBlockIndex* pindexTip); 60 : // Sets the Deterministic Masternode Operator's private/public key 61 : OperationResult SetOperatorKey(const std::string& strMNOperatorPrivKey); 62 : // If the active masternode is ready, and the keyID matches with the registered one, 63 : // return private key, keyID, and pointer to dmn. 64 : OperationResult GetOperatorKey(CBLSSecretKey& key, CDeterministicMNCPtr& dmn) const; 65 : // Directly return the operator secret key saved in the manager, without performing any validation 66 334 : const CBLSSecretKey* OperatorKey() const { return &info.keyOperator; } 67 6 : void SetNullProTx() { info.proTxHash = UINT256_ZERO; } 68 9972 : const uint256 GetProTx() const { return info.proTxHash; } 69 : 70 328 : const CActiveMasternodeInfo* GetInfo() const { return &info; } 71 41 : masternode_state_t GetState() const { return state; } 72 : std::string GetStatus() const; 73 425 : bool IsReady() const { return state == MASTERNODE_READY; } 74 : 75 : static bool IsValidNetAddr(const CService& addrIn); 76 : }; 77 : 78 : // Responsible for initializing the masternode 79 : OperationResult initMasternode(const std::string& strMasterNodePrivKey, const std::string& strMasterNodeAddr, bool isFromInit); 80 : 81 : 82 : // Responsible for activating the Masternode and pinging the network (legacy MN list) 83 : class CActiveMasternode 84 : { 85 : private: 86 : int status{ACTIVE_MASTERNODE_INITIAL}; 87 : std::string notCapableReason; 88 : 89 : public: 90 479 : CActiveMasternode() = default; 91 : 92 : // Initialized by init.cpp 93 : // Keys for the main Masternode 94 : CPubKey pubKeyMasternode; 95 : CKey privKeyMasternode; 96 : 97 : // Initialized while registering Masternode 98 : Optional<CTxIn> vin{nullopt}; 99 : CService service; 100 : 101 : /// Manage status of main Masternode 102 : void ManageStatus(); 103 : void ResetStatus(); 104 : std::string GetStatusMessage() const; 105 662 : int GetStatus() const { return status; } 106 : 107 : /// Ping Masternode 108 : bool SendMasternodePing(std::string& errorMessage); 109 : /// Enable cold wallet mode (run a Masternode with no funds) 110 : bool EnableHotColdMasterNode(CTxIn& vin, CService& addr); 111 : 112 : void GetKeys(CKey& privKeyMasternode, CPubKey& pubKeyMasternode) const; 113 : }; 114 : 115 : // Compatibility code: get vin and keys for either legacy or deterministic masternode 116 : bool GetActiveMasternodeKeys(CTxIn& vin, Optional<CKey>& key, CBLSSecretKey& blsKey); 117 : // Get active masternode BLS operator keys for DMN 118 : bool GetActiveDMNKeys(CBLSSecretKey& key, CTxIn& vin); 119 : 120 : #endif // PIVX_ACTIVEMASTERNODE_H