// Copyright (c) 2014-2015 The Darkcoin 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 "bignum.h" #include "sync.h" #include "net.h" #include "key.h" #include "core.h" #include "util.h" #include "script.h" #include "base58.h" #include "main.h" #include "masternode.h" #define MASTERNODES_DUMP_SECONDS (15*60) #define MASTERNODES_DSEG_SECONDS (3*60*60) using namespace std; class CMasternodeMan; extern CMasternodeMan mnodeman; void DumpMasternodes(); /** Access to the MN database (masternodes.dat) */ class CMasternodeDB { private: boost::filesystem::path pathMN; public: CMasternodeDB(); bool Write(const CMasternodeMan &mnodemanToSave); bool Read(CMasternodeMan& mnodemanToLoad); }; class CMasternodeMan { private: // critical section to protect the inner data structures mutable CCriticalSection cs; // 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; public: IMPLEMENT_SERIALIZE ( // serialized format: // * version byte (currently 0) // * masternodes vector { LOCK(cs); unsigned char nVersion = 0; READWRITE(nVersion); READWRITE(vMasternodes); READWRITE(mAskedUsForMasternodeList); READWRITE(mWeAskedForMasternodeList); READWRITE(mWeAskedForMasternodeListEntry); } ) CMasternodeMan(); CMasternodeMan(CMasternodeMan& other); // Add an entry bool Add(CMasternode &mn); // Check all masternodes void Check(); // Check all masternodes and remove inactive void CheckAndRemove(); // Clear masternode vector void Clear(); int CountEnabled(); int CountMasternodesAboveProtocol(int protocolVersion); void DsegUpdate(CNode* pnode); // Find an entry CMasternode* Find(const CTxIn& vin); //Find an entry thta do not match every entry provided vector CMasternode* FindOldestNotInVec(const std::vector &vVins); // Find a random entry CMasternode* FindRandom(); // Get the current winner for this block CMasternode* GetCurrentMasterNode(int mod=1, int64_t nBlockHeight=0, int minProtocol=0); std::vector GetFullMasternodeVector() { Check(); return vMasternodes; } int GetMasternodeRank(const CTxIn &vin, int64_t nBlockHeight, int minProtocol=0); CMasternode* GetMasternodeByRank(int nRank, int64_t nBlockHeight, int minProtocol=0); void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv); void ProcessMasternodeConnections(); // Return the number of (unique) masternodes int size() { return vMasternodes.size(); } std::string ToString(); // // Relay Masternode Messages // void RelayMasternodeEntry(const CTxIn vin, const CService addr, const std::vector vchSig, const int64_t nNow, const CPubKey pubkey, const CPubKey pubkey2, const int count, const int current, const int64_t lastUpdated, const int protocolVersion); void RelayMasternodeEntryPing(const CTxIn vin, const std::vector vchSig, const int64_t nNow, const bool stop); }; #endif