// Copyright (c) 2014-2016 The Dash Core developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef MASTERNODEMAN_H #define MASTERNODEMAN_H #include "masternode.h" #include "sync.h" using namespace std; class CMasternodeMan; extern CMasternodeMan mnodeman; class CMasternodeMan { private: static const std::string SERIALIZATION_VERSION_STRING; static const int DSEG_UPDATE_SECONDS = 3 * 60 * 60; static const int LAST_PAID_SCAN_BLOCKS = 100; static const int MIN_POSE_PROTO_VERSION = 70203; static const int MAX_POSE_RANK = 10; static const int MAX_POSE_BLOCKS = 10; // critical section to protect the inner data structures mutable CCriticalSection cs; // Keep track of current block index const CBlockIndex *pCurrentBlockIndex; // map to hold all MNs std::vector vMasternodes; // who's asked for the Masternode list and the last time std::map mAskedUsForMasternodeList; // who we asked for the Masternode list and the last time std::map mWeAskedForMasternodeList; // which Masternodes we've asked for std::map mWeAskedForMasternodeListEntry; // who we asked for the masternode verification std::map mWeAskedForVerification; std::vector vecDirtyGovernanceObjectHashes; int64_t nLastWatchdogVoteTime; public: // Keep track of all broadcasts I've seen std::map mapSeenMasternodeBroadcast; // Keep track of all pings I've seen std::map mapSeenMasternodePing; // Keep track of all verifications I've seen std::map mapSeenMasternodeVerification; // keep track of dsq count to prevent masternodes from gaming darksend queue int64_t nDsqCount; CMasternodeMan() : nLastWatchdogVoteTime(0), nDsqCount(0) {} ADD_SERIALIZE_METHODS; template inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { LOCK(cs); std::string strVersion; if(ser_action.ForRead()) { READWRITE(strVersion); } else { strVersion = SERIALIZATION_VERSION_STRING; READWRITE(strVersion); } READWRITE(vMasternodes); READWRITE(mAskedUsForMasternodeList); READWRITE(mWeAskedForMasternodeList); READWRITE(mWeAskedForMasternodeListEntry); READWRITE(nLastWatchdogVoteTime); READWRITE(nDsqCount); READWRITE(mapSeenMasternodeBroadcast); READWRITE(mapSeenMasternodePing); if(ser_action.ForRead() && (strVersion != SERIALIZATION_VERSION_STRING)) { Clear(); } } /// Add an entry bool Add(CMasternode &mn); /// Ask (source) node for mnb void AskForMN(CNode *pnode, CTxIn &vin); /// Check all Masternodes void Check(); /// Check all Masternodes and remove inactive void CheckAndRemove(bool fForceExpiredRemoval = false); /// Clear Masternode vector void Clear(); /// Count Masternodes filtered by nProtocolVersion. /// Masternode nProtocolVersion should match or be above the one specified in param here. int CountMasternodes(int nProtocolVersion = -1); /// Count enabled Masternodes filtered by nProtocolVersion. /// Masternode nProtocolVersion should match or be above the one specified in param here. int CountEnabled(int nProtocolVersion = -1); /// Count Masternodes by network type - NET_IPV4, NET_IPV6, NET_TOR // int CountByIP(int nNetworkType); void DsegUpdate(CNode* pnode); /// Find an entry CMasternode* Find(const CScript &payee); CMasternode* Find(const CTxIn& vin); CMasternode* Find(const CPubKey& pubKeyMasternode); /// Versions of Find that are safe to use from outside the class bool Get(const CPubKey& pubKeyMasternode, CMasternode& masternode); bool Get(const CTxIn& vin, CMasternode& masternode); bool Has(const CTxIn& vin); masternode_info_t GetMasternodeInfo(const CTxIn& vin); masternode_info_t GetMasternodeInfo(const CPubKey& pubKeyMasternode); /// Find an entry in the masternode list that is next to be paid CMasternode* GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime, int& nCount); /// Find a random entry CMasternode* FindRandomNotInVec(const std::vector &vecToExclude, int nProtocolVersion = -1); std::vector GetFullMasternodeVector() { Check(); return vMasternodes; } std::vector > GetMasternodeRanks(int nBlockHeight = -1, int nMinProtocol=0); int GetMasternodeRank(const CTxIn &vin, int nBlockHeight, int nMinProtocol=0, bool fOnlyActive=true); CMasternode* GetMasternodeByRank(int nRank, int nBlockHeight, int nMinProtocol=0, bool fOnlyActive=true); void ProcessMasternodeConnections(); void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv); void DoFullVerificationStep(); void CheckSameAddr(); bool SendVerifyRequest(const CAddress& addr, const std::vector& vSortedByAddr); void SendVerifyReply(CNode* pnode, CMasternodeVerification& mnv); void ProcessVerifyReply(CNode* pnode, CMasternodeVerification& mnv); void ProcessVerifyBroadcast(CNode* pnode, const CMasternodeVerification& mnv); /// Return the number of (unique) Masternodes int size() { return vMasternodes.size(); } std::string ToString() const; void Remove(CTxIn vin); int GetEstimatedMasternodes(int nBlock); /// Update masternode list and maps using provided CMasternodeBroadcast void UpdateMasternodeList(CMasternodeBroadcast mnb); /// Perform complete check and only then update list and maps bool CheckMnbAndUpdateMasternodeList(CMasternodeBroadcast mnb, int& nDos); void UpdateLastPaid(const CBlockIndex *pindex); void AddDirtyGovernanceObjectHash(const uint256& nHash) { LOCK(cs); vecDirtyGovernanceObjectHashes.push_back(nHash); } std::vector GetAndClearDirtyGovernanceObjectHashes() { LOCK(cs); std::vector vecTmp = vecDirtyGovernanceObjectHashes; vecDirtyGovernanceObjectHashes.clear(); return vecTmp;; } bool IsWatchdogActive(); void UpdateWatchdogVoteTime(const CTxIn& vin); void AddGovernanceVote(const CTxIn& vin, uint256 nGovernanceObjectHash); void RemoveGovernanceObject(uint256 nGovernanceObjectHash); void CheckMasternode(const CTxIn& vin, bool fForce = false); void CheckMasternode(const CPubKey& pubKeyMasternode, bool fForce = false); int GetMasternodeState(const CTxIn& vin); int GetMasternodeState(const CPubKey& pubKeyMasternode); bool IsMasternodePingedWithin(const CTxIn& vin, int nSeconds, int64_t nTimeToCheckAt = -1); void SetMasternodeLastPing(const CTxIn& vin, const CMasternodePing& mnp); void UpdatedBlockTip(const CBlockIndex *pindex); }; #endif