2016-12-20 14:26:45 +01:00
|
|
|
// Copyright (c) 2014-2017 The Dash Core 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 "masternode.h"
|
2016-10-20 23:11:30 +02:00
|
|
|
#include "sync.h"
|
2015-02-23 21:01:21 +01:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
class CMasternodeMan;
|
2017-09-19 16:51:38 +02:00
|
|
|
class CConnman;
|
2016-10-20 23:11:30 +02:00
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
extern CMasternodeMan mnodeman;
|
|
|
|
|
|
|
|
class CMasternodeMan
|
|
|
|
{
|
2016-11-13 18:52:34 +01:00
|
|
|
public:
|
2017-09-14 15:58:29 +02:00
|
|
|
typedef std::pair<arith_uint256, CMasternode*> score_pair_t;
|
|
|
|
typedef std::vector<score_pair_t> score_pair_vec_t;
|
|
|
|
typedef std::pair<int, CMasternode> rank_pair_t;
|
|
|
|
typedef std::vector<rank_pair_t> rank_pair_vec_t;
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
private:
|
2016-10-17 20:54:28 +02:00
|
|
|
static const std::string SERIALIZATION_VERSION_STRING;
|
|
|
|
|
2016-10-20 23:11:30 +02:00
|
|
|
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;
|
2017-02-17 21:08:29 +01:00
|
|
|
static const int MAX_POSE_CONNECTIONS = 10;
|
2016-10-20 23:11:30 +02:00
|
|
|
static const int MAX_POSE_RANK = 10;
|
|
|
|
static const int MAX_POSE_BLOCKS = 10;
|
|
|
|
|
2017-01-01 18:48:53 +01:00
|
|
|
static const int MNB_RECOVERY_QUORUM_TOTAL = 10;
|
|
|
|
static const int MNB_RECOVERY_QUORUM_REQUIRED = 6;
|
2017-01-31 16:29:27 +01:00
|
|
|
static const int MNB_RECOVERY_MAX_ASK_ENTRIES = 10;
|
2017-01-01 18:48:53 +01:00
|
|
|
static const int MNB_RECOVERY_WAIT_SECONDS = 60;
|
|
|
|
static const int MNB_RECOVERY_RETRY_SECONDS = 3 * 60 * 60;
|
|
|
|
|
2016-09-13 01:21:59 +02:00
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
// critical section to protect the inner data structures
|
|
|
|
mutable CCriticalSection cs;
|
|
|
|
|
2017-08-25 14:57:05 +02:00
|
|
|
// Keep track of current block height
|
|
|
|
int nCachedBlockHeight;
|
2015-07-14 07:25:07 +02:00
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
// map to hold all MNs
|
2017-09-11 16:13:48 +02:00
|
|
|
std::map<COutPoint, CMasternode> mapMasternodes;
|
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
|
2016-12-21 00:55:55 +01:00
|
|
|
std::map<COutPoint, std::map<CNetAddr, int64_t> > mWeAskedForMasternodeListEntry;
|
2016-10-20 23:11:30 +02:00
|
|
|
// who we asked for the masternode verification
|
|
|
|
std::map<CNetAddr, CMasternodeVerification> mWeAskedForVerification;
|
2015-02-24 11:39:29 +01:00
|
|
|
|
2017-01-01 18:48:53 +01:00
|
|
|
// these maps are used for masternode recovery from MASTERNODE_NEW_START_REQUIRED state
|
|
|
|
std::map<uint256, std::pair< int64_t, std::set<CNetAddr> > > mMnbRecoveryRequests;
|
|
|
|
std::map<uint256, std::vector<CMasternodeBroadcast> > mMnbRecoveryGoodReplies;
|
2017-01-21 20:03:55 +01:00
|
|
|
std::list< std::pair<CService, uint256> > listScheduledMnbRequestConnections;
|
2017-01-01 18:48:53 +01:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
/// Set when masternodes are added, cleared when CGovernanceManager is notified
|
|
|
|
bool fMasternodesAdded;
|
|
|
|
|
|
|
|
/// Set when masternodes are removed, cleared when CGovernanceManager is notified
|
|
|
|
bool fMasternodesRemoved;
|
|
|
|
|
2016-10-17 20:54:28 +02:00
|
|
|
std::vector<uint256> vecDirtyGovernanceObjectHashes;
|
|
|
|
|
|
|
|
int64_t nLastWatchdogVoteTime;
|
|
|
|
|
2016-10-30 20:40:30 +01:00
|
|
|
friend class CMasternodeSync;
|
2017-09-11 16:13:48 +02:00
|
|
|
/// Find an entry
|
|
|
|
CMasternode* Find(const COutPoint& outpoint);
|
2016-10-30 20:40:30 +01:00
|
|
|
|
2017-09-14 15:58:29 +02:00
|
|
|
bool GetMasternodeScores(const uint256& nBlockHash, score_pair_vec_t& vecMasternodeScoresRet, int nMinProtocol = 0);
|
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
public:
|
2015-07-25 21:07:30 +02:00
|
|
|
// Keep track of all broadcasts I've seen
|
2016-12-26 07:44:36 +01:00
|
|
|
std::map<uint256, std::pair<int64_t, CMasternodeBroadcast> > mapSeenMasternodeBroadcast;
|
2015-07-25 21:07:30 +02:00
|
|
|
// Keep track of all pings I've seen
|
2016-10-20 23:11:30 +02:00
|
|
|
std::map<uint256, CMasternodePing> mapSeenMasternodePing;
|
|
|
|
// Keep track of all verifications I've seen
|
|
|
|
std::map<uint256, CMasternodeVerification> mapSeenMasternodeVerification;
|
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
|
|
|
|
2016-05-30 08:22:08 +02: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) {
|
2015-07-25 21:07:30 +02:00
|
|
|
LOCK(cs);
|
2016-10-17 20:54:28 +02:00
|
|
|
std::string strVersion;
|
|
|
|
if(ser_action.ForRead()) {
|
|
|
|
READWRITE(strVersion);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strVersion = SERIALIZATION_VERSION_STRING;
|
|
|
|
READWRITE(strVersion);
|
|
|
|
}
|
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
READWRITE(mapMasternodes);
|
2015-07-25 21:07:30 +02:00
|
|
|
READWRITE(mAskedUsForMasternodeList);
|
|
|
|
READWRITE(mWeAskedForMasternodeList);
|
|
|
|
READWRITE(mWeAskedForMasternodeListEntry);
|
2017-01-01 18:48:53 +01:00
|
|
|
READWRITE(mMnbRecoveryRequests);
|
|
|
|
READWRITE(mMnbRecoveryGoodReplies);
|
2016-10-17 20:54:28 +02:00
|
|
|
READWRITE(nLastWatchdogVoteTime);
|
2015-07-25 21:07:30 +02:00
|
|
|
READWRITE(nDsqCount);
|
|
|
|
|
|
|
|
READWRITE(mapSeenMasternodeBroadcast);
|
|
|
|
READWRITE(mapSeenMasternodePing);
|
2016-10-17 20:54:28 +02:00
|
|
|
if(ser_action.ForRead() && (strVersion != SERIALIZATION_VERSION_STRING)) {
|
|
|
|
Clear();
|
|
|
|
}
|
2015-04-03 00:51:08 +02:00
|
|
|
}
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2016-11-13 18:52:34 +01:00
|
|
|
CMasternodeMan();
|
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Add an entry
|
2015-02-23 21:01:21 +01:00
|
|
|
bool Add(CMasternode &mn);
|
|
|
|
|
2015-08-07 05:07:40 +02:00
|
|
|
/// Ask (source) node for mnb
|
2017-09-19 16:51:38 +02:00
|
|
|
void AskForMN(CNode *pnode, const COutPoint& outpoint, CConnman& connman);
|
2017-01-21 20:03:55 +01:00
|
|
|
void AskForMnb(CNode *pnode, const uint256 &hash);
|
2015-08-07 05:07:40 +02:00
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
bool PoSeBan(const COutPoint &outpoint);
|
|
|
|
bool AllowMixing(const COutPoint &outpoint);
|
|
|
|
bool DisallowMixing(const COutPoint &outpoint);
|
|
|
|
|
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
|
2017-09-19 16:51:38 +02:00
|
|
|
void CheckAndRemove(CConnman& connman);
|
|
|
|
/// This is dummy overload to be used for dumping/loading mncache.dat
|
|
|
|
void CheckAndRemove() {}
|
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
|
|
|
|
2016-10-22 18:52:14 +02:00
|
|
|
/// 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);
|
2016-08-12 07:58:55 +02:00
|
|
|
|
|
|
|
/// Count Masternodes by network type - NET_IPV4, NET_IPV6, NET_TOR
|
2016-10-22 18:52:14 +02:00
|
|
|
// int CountByIP(int nNetworkType);
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void DsegUpdate(CNode* pnode, CConnman& connman);
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2016-09-15 08:49:24 +02:00
|
|
|
/// Versions of Find that are safe to use from outside the class
|
2017-09-11 16:13:48 +02:00
|
|
|
bool Get(const COutPoint& outpoint, CMasternode& masternodeRet);
|
|
|
|
bool Has(const COutPoint& outpoint);
|
2016-10-17 20:54:28 +02:00
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
bool GetMasternodeInfo(const COutPoint& outpoint, masternode_info_t& mnInfoRet);
|
|
|
|
bool GetMasternodeInfo(const CPubKey& pubKeyMasternode, masternode_info_t& mnInfoRet);
|
2017-09-20 22:31:23 +02:00
|
|
|
bool GetMasternodeInfo(const CScript& payee, masternode_info_t& mnInfoRet);
|
2016-10-17 20:54:28 +02:00
|
|
|
|
2015-05-04 17:04:09 +02:00
|
|
|
/// Find an entry in the masternode list that is next to be paid
|
2017-10-17 18:40:25 +02:00
|
|
|
bool GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime, int& nCountRet, masternode_info_t& mnInfoRet);
|
2016-11-28 15:21:50 +01:00
|
|
|
/// Same as above but use current block height
|
2017-10-17 18:40:25 +02:00
|
|
|
bool GetNextMasternodeInQueueForPayment(bool fFilterSigTime, int& nCountRet, masternode_info_t& mnInfoRet);
|
2015-02-26 00:21:28 +01:00
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Find a random entry
|
2017-09-11 16:13:48 +02:00
|
|
|
masternode_info_t FindRandomNotInVec(const std::vector<COutPoint> &vecToExclude, int nProtocolVersion = -1);
|
2015-02-26 00:21:28 +01:00
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
std::map<COutPoint, CMasternode> GetFullMasternodeMap() { return mapMasternodes; }
|
2015-02-23 21:01:21 +01:00
|
|
|
|
2017-09-14 15:58:29 +02:00
|
|
|
bool GetMasternodeRanks(rank_pair_vec_t& vecMasternodeRanksRet, int nBlockHeight = -1, int nMinProtocol = 0);
|
|
|
|
bool GetMasternodeRank(const COutPoint &outpoint, int& nRankRet, int nBlockHeight = -1, int nMinProtocol = 0);
|
2016-05-30 08:22:08 +02:00
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void ProcessMasternodeConnections(CConnman& connman);
|
2017-01-31 16:29:27 +01:00
|
|
|
std::pair<CService, std::set<uint256> > PopScheduledMnbRequestConnection();
|
2015-03-02 00:09:33 +01:00
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv, CConnman& connman);
|
2015-04-08 04:07:25 +02:00
|
|
|
|
2017-09-19 16:51:38 +02:00
|
|
|
void DoFullVerificationStep(CConnman& connman);
|
2016-10-20 23:11:30 +02:00
|
|
|
void CheckSameAddr();
|
2017-09-19 16:51:38 +02:00
|
|
|
bool SendVerifyRequest(const CAddress& addr, const std::vector<CMasternode*>& vSortedByAddr, CConnman& connman);
|
|
|
|
void SendVerifyReply(CNode* pnode, CMasternodeVerification& mnv, CConnman& connman);
|
2016-10-20 23:11:30 +02:00
|
|
|
void ProcessVerifyReply(CNode* pnode, CMasternodeVerification& mnv);
|
|
|
|
void ProcessVerifyBroadcast(CNode* pnode, const CMasternodeVerification& mnv);
|
|
|
|
|
2015-03-05 09:10:15 +01:00
|
|
|
/// Return the number of (unique) Masternodes
|
2017-09-11 16:13:48 +02:00
|
|
|
int size() { return mapMasternodes.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
|
|
|
|
2016-03-16 16:30:22 +01:00
|
|
|
/// Update masternode list and maps using provided CMasternodeBroadcast
|
2017-09-19 16:51:38 +02:00
|
|
|
void UpdateMasternodeList(CMasternodeBroadcast mnb, CConnman& connman);
|
2016-03-18 00:37:48 +01:00
|
|
|
/// Perform complete check and only then update list and maps
|
2017-09-19 16:51:38 +02:00
|
|
|
bool CheckMnbAndUpdateMasternodeList(CNode* pfrom, CMasternodeBroadcast mnb, int& nDos, CConnman& connman);
|
2017-01-01 18:48:53 +01:00
|
|
|
bool IsMnbRecoveryRequested(const uint256& hash) { return mMnbRecoveryRequests.count(hash); }
|
2016-03-16 16:30:22 +01:00
|
|
|
|
2017-08-25 14:57:05 +02:00
|
|
|
void UpdateLastPaid(const CBlockIndex* pindex);
|
2016-10-17 20:54:28 +02:00
|
|
|
|
|
|
|
void AddDirtyGovernanceObjectHash(const uint256& nHash)
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
vecDirtyGovernanceObjectHashes.push_back(nHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint256> GetAndClearDirtyGovernanceObjectHashes()
|
|
|
|
{
|
|
|
|
LOCK(cs);
|
|
|
|
std::vector<uint256> vecTmp = vecDirtyGovernanceObjectHashes;
|
|
|
|
vecDirtyGovernanceObjectHashes.clear();
|
|
|
|
return vecTmp;;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsWatchdogActive();
|
2017-10-12 14:05:17 +02:00
|
|
|
void UpdateWatchdogVoteTime(const COutPoint& outpoint, uint64_t nVoteTime = 0);
|
2017-09-11 16:13:48 +02:00
|
|
|
bool AddGovernanceVote(const COutPoint& outpoint, uint256 nGovernanceObjectHash);
|
2016-10-17 20:54:28 +02:00
|
|
|
void RemoveGovernanceObject(uint256 nGovernanceObjectHash);
|
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
void CheckMasternode(const CPubKey& pubKeyMasternode, bool fForce);
|
2016-10-17 20:54:28 +02:00
|
|
|
|
2017-09-11 16:13:48 +02:00
|
|
|
bool IsMasternodePingedWithin(const COutPoint& outpoint, int nSeconds, int64_t nTimeToCheckAt = -1);
|
|
|
|
void SetMasternodeLastPing(const COutPoint& outpoint, const CMasternodePing& mnp);
|
2016-10-20 23:11:30 +02:00
|
|
|
|
|
|
|
void UpdatedBlockTip(const CBlockIndex *pindex);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called to notify CGovernanceManager that the masternode index has been updated.
|
|
|
|
* Must be called while not holding the CMasternodeMan::cs mutex
|
|
|
|
*/
|
2017-09-19 16:51:38 +02:00
|
|
|
void NotifyMasternodeUpdates(CConnman& connman);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2015-02-23 21:01:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|