dash/src/masternodeman.h

145 lines
4.1 KiB
C
Raw Normal View History

2015-02-24 15:02:22 +01:00
// 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.
2015-02-24 15:02:22 +01:00
#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 (mncache.dat)
2015-03-05 09:10:15 +01:00
*/
class CMasternodeDB
{
private:
boost::filesystem::path pathMN;
public:
enum ReadResult {
Ok,
FileError,
HashReadError,
IncorrectHash,
IncorrectMagic,
IncorrectFormat
};
CMasternodeDB();
bool Write(const CMasternodeMan &mnodemanToSave);
ReadResult Read(CMasternodeMan& mnodemanToLoad);
};
class CMasternodeMan
{
private:
// critical section to protect the inner data structures
mutable CCriticalSection cs;
// map to hold all MNs
std::vector<CMasternode> vMasternodes;
2015-03-05 09:10:15 +01:00
// who's asked for the Masternode list and the last time
std::map<CNetAddr, int64_t> mAskedUsForMasternodeList;
2015-03-05 09:10:15 +01:00
// who we asked for the Masternode list and the last time
std::map<CNetAddr, int64_t> mWeAskedForMasternodeList;
2015-03-05 09:10:15 +01:00
// which Masternodes we've asked for
std::map<COutPoint, int64_t> mWeAskedForMasternodeListEntry;
public:
2015-03-06 18:25:48 +01:00
// keep track of dsq count to prevent masternodes from gaming darksend queue
int64_t nDsqCount;
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);
2015-03-06 18:25:48 +01:00
READWRITE(nDsqCount);
}
)
CMasternodeMan();
CMasternodeMan(CMasternodeMan& other);
2015-03-05 09:10:15 +01:00
/// Add an entry
bool Add(CMasternode &mn);
2015-03-05 09:10:15 +01:00
/// Check all Masternodes
void Check();
2015-03-05 09:10:15 +01:00
/// Check all Masternodes and remove inactive
void CheckAndRemove();
2015-03-05 09:10:15 +01:00
/// Clear Masternode vector
2015-03-01 01:04:17 +01:00
void Clear();
int CountEnabled();
int CountMasternodesAboveProtocol(int protocolVersion);
void DsegUpdate(CNode* pnode);
2015-03-05 09:10:15 +01:00
/// Find an entry
CMasternode* Find(const CTxIn& vin);
2015-03-05 09:10:15 +01:00
/// Find an entry thta do not match every entry provided vector
CMasternode* FindOldestNotInVec(const std::vector<CTxIn> &vVins);
2015-03-05 09:10:15 +01:00
/// Find a random entry
CMasternode* FindRandom();
2015-03-05 09:10:15 +01:00
/// Get the current winner for this block
CMasternode* GetCurrentMasterNode(int mod=1, int64_t nBlockHeight=0, int minProtocol=0);
std::vector<CMasternode> GetFullMasternodeVector() { Check(); return vMasternodes; }
std::vector<pair<int, CMasternode> > GetMasternodeRanks(int64_t nBlockHeight, int minProtocol=0);
int GetMasternodeRank(const CTxIn &vin, int64_t nBlockHeight, int minProtocol=0, bool fOnlyActive=true);
CMasternode* GetMasternodeByRank(int nRank, int64_t nBlockHeight, int minProtocol=0, bool fOnlyActive=true);
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
2015-03-02 00:09:33 +01:00
void ProcessMasternodeConnections();
2015-03-05 09:10:15 +01:00
/// Return the number of (unique) Masternodes
int size() { return vMasternodes.size(); }
std::string ToString() const;
2015-03-02 00:09:33 +01:00
//
// Relay Masternode Messages
//
void RelayMasternodeEntry(const CTxIn vin, const CService addr, const std::vector<unsigned char> vchSig, const int64_t nNow, const CPubKey pubkey, const CPubKey pubkey2, const int count, const int current, const int64_t lastUpdated, const int protocolVersion, CScript donationAddress, int donationPercentage);
2015-03-02 00:09:33 +01:00
void RelayMasternodeEntryPing(const CTxIn vin, const std::vector<unsigned char> vchSig, const int64_t nNow, const bool stop);
};
#endif