2015-03-18 00:06:58 +01:00
|
|
|
// Copyright (c) 2014-2015 The Dash developers
|
2015-02-23 21:01:21 +01:00
|
|
|
// 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
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
#ifndef MASTERNODEMAN_H
|
|
|
|
#define MASTERNODEMAN_H
|
|
|
|
|
|
|
|
#include "sync.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "key.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "base58.h"
|
|
|
|
#include "main.h"
|
|
|
|
#include "masternode.h"
|
|
|
|
|
|
|
|
#define MASTERNODES_DUMP_SECONDS (15*60)
|
2015-02-26 00:21:28 +01:00
|
|
|
#define MASTERNODES_DSEG_SECONDS (3*60*60)
|
2015-02-23 21:01:21 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class CMasternodeMan;
|
|
|
|
|
2015-04-22 16:33:44 +02:00
|
|
|
// Keep track of all broadcasts I've seen
|
|
|
|
extern map<uint256, CMasternodeBroadcast> mapSeenMasternodeBroadcast;
|
|
|
|
|
|
|
|
// Keep track of all pings I've seen
|
|
|
|
extern map<uint256, CMasternodePing> mapSeenMasternodePing;
|
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
extern CMasternodeMan mnodeman;
|
|
|
|
void DumpMasternodes();
|
|
|
|
|
2015-03-05 18:39:47 +01:00
|
|
|
/** Access to the MN database (mncache.dat)
|
2015-03-05 09:10:15 +01:00
|
|
|
*/
|
2015-02-23 21:01:21 +01:00
|
|
|
class CMasternodeDB
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
boost::filesystem::path pathMN;
|
2015-03-24 01:21:07 +01:00
|
|
|
std::string strMagicMessage;
|
2015-02-23 21:01:21 +01:00
|
|
|
public:
|
2015-03-05 00:46:50 +01:00
|
|
|
enum ReadResult {
|
|
|
|
Ok,
|
|
|
|
FileError,
|
|
|
|
HashReadError,
|
|
|
|
IncorrectHash,
|
2015-03-24 01:21:07 +01:00
|
|
|
IncorrectMagicMessage,
|
|
|
|
IncorrectMagicNumber,
|
2015-03-05 00:46:50 +01:00
|
|
|
IncorrectFormat
|
|
|
|
};
|
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
CMasternodeDB();
|
|
|
|
bool Write(const CMasternodeMan &mnodemanToSave);
|
2015-07-14 07:25:07 +02:00
|
|
|
ReadResult Read(CMasternodeMan& mnodemanToLoad, bool fDryRun = false);
|
2015-02-23 21:01:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class CMasternodeMan
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
// critical section to protect the inner data structures
|
|
|
|
mutable CCriticalSection cs;
|
|
|
|
|
2015-07-14 07:25:07 +02:00
|
|
|
// critical section to protect the inner data structures specifically on messaging
|
|
|
|
mutable CCriticalSection cs_process_message;
|
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
// 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
|
2015-02-26 00:21:28 +01:00
|
|
|
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
|
2015-02-26 00:21:28 +01:00
|
|
|
std::map<CNetAddr, int64_t> mWeAskedForMasternodeList;
|
2015-03-05 09:10:15 +01:00
|
|
|
// which Masternodes we've asked for
|
2015-02-26 00:21:28 +01:00
|
|
|
std::map<COutPoint, int64_t> mWeAskedForMasternodeListEntry;
|
2015-02-24 11:39:29 +01:00
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
public:
|
2015-03-06 18:25:48 +01:00
|
|
|
// keep track of dsq count to prevent masternodes from gaming darksend queue
|
|
|
|
int64_t nDsqCount;
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2015-04-03 00:51:08 +02:00
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
|
|
LOCK(cs);
|
|
|
|
READWRITE(vMasternodes);
|
|
|
|
READWRITE(mAskedUsForMasternodeList);
|
|
|
|
READWRITE(mWeAskedForMasternodeList);
|
|
|
|
READWRITE(mWeAskedForMasternodeListEntry);
|
|
|
|
READWRITE(nDsqCount);
|
|
|
|
}
|
2015-02-23 21:01:21 +01:00
|
|
|
|
|
|
|
CMasternodeMan();
|
|
|
|
CMasternodeMan(CMasternodeMan& other);
|
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Add an entry
|
2015-02-23 21:01:21 +01:00
|
|
|
bool Add(CMasternode &mn);
|
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Check all Masternodes
|
2015-02-23 21:01:21 +01:00
|
|
|
void Check();
|
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Check all Masternodes and remove inactive
|
2015-07-14 07:25:07 +02:00
|
|
|
void CheckAndRemove(bool forceExpiredRemoval = false);
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Clear Masternode vector
|
2015-03-01 01:04:17 +01:00
|
|
|
void Clear();
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2015-07-20 07:03:36 +02:00
|
|
|
int CountEnabled(int protocolVersion = -1);
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2015-02-26 00:21:28 +01:00
|
|
|
void DsegUpdate(CNode* pnode);
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Find an entry
|
2015-05-28 19:45:31 +02:00
|
|
|
CMasternode* Find(const CScript &payee);
|
2015-02-26 00:21:28 +01:00
|
|
|
CMasternode* Find(const CTxIn& vin);
|
2015-04-07 21:59:30 +02:00
|
|
|
CMasternode* Find(const CPubKey& pubKeyMasternode);
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2015-05-04 17:04:09 +02:00
|
|
|
/// Find an entry in the masternode list that is next to be paid
|
2015-07-25 01:10:44 +02:00
|
|
|
CMasternode* GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime=true);
|
2015-02-26 00:21:28 +01:00
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Find a random entry
|
2015-02-26 00:21:28 +01:00
|
|
|
CMasternode* FindRandom();
|
|
|
|
|
2015-04-22 16:33:44 +02:00
|
|
|
/// Decrement all masternode nVotedTimes, called 1/6 blocks (allowing for 100 votes each day)
|
|
|
|
void DecrementVotedTimes();
|
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Get the current winner for this block
|
2015-02-26 00:21:28 +01:00
|
|
|
CMasternode* GetCurrentMasterNode(int mod=1, int64_t nBlockHeight=0, int minProtocol=0);
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2015-02-24 11:39:29 +01:00
|
|
|
std::vector<CMasternode> GetFullMasternodeVector() { Check(); return vMasternodes; }
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2015-03-14 19:34:51 +01:00
|
|
|
std::vector<pair<int, CMasternode> > GetMasternodeRanks(int64_t nBlockHeight, int minProtocol=0);
|
2015-03-13 10:28:20 +01:00
|
|
|
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);
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2015-03-02 00:09:33 +01:00
|
|
|
void ProcessMasternodeConnections();
|
|
|
|
|
2015-04-08 04:07:25 +02:00
|
|
|
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
|
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Return the number of (unique) Masternodes
|
2015-02-26 00:21:28 +01:00
|
|
|
int size() { return vMasternodes.size(); }
|
2015-02-24 11:39:29 +01:00
|
|
|
|
2015-03-05 00:46:50 +01:00
|
|
|
std::string ToString() const;
|
2015-03-01 00:56:52 +01:00
|
|
|
|
2015-04-08 04:07:25 +02:00
|
|
|
void Remove(CTxIn vin);
|
2015-03-02 00:09:33 +01:00
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|