dash/src/masternodeman.h

141 lines
4.5 KiB
C
Raw Normal View History

// 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.
2015-02-24 15:02:22 +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)
#define MASTERNODES_DSEG_SECONDS (3*60*60)
using namespace std;
class CMasternodeMan;
extern CMasternodeMan mnodeman;
class CMasternodeMan
{
private:
static const int MASTERNODES_LAST_PAID_SCAN_BLOCKS = 100;
// critical section to protect the inner data structures
mutable CCriticalSection cs;
// critical section to protect the inner data structures specifically on messaging
mutable CCriticalSection cs_process_message;
// 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:
// Keep track of all broadcasts I've seen
map<uint256, CMasternodeBroadcast> mapSeenMasternodeBroadcast;
// Keep track of all pings I've seen
map<uint256, CMasternodePing> mapSeenMasternodePing;
2015-03-06 18:25:48 +01:00
// keep track of dsq count to prevent masternodes from gaming darksend queue
int64_t nDsqCount;
// dummy script pubkey to test masternodes' vins against mempool
CScript dummyScriptPubkey;
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);
READWRITE(mapSeenMasternodeBroadcast);
READWRITE(mapSeenMasternodePing);
2015-04-03 00:51:08 +02:00
}
CMasternodeMan();
CMasternodeMan(CMasternodeMan& other);
2015-03-05 09:10:15 +01:00
/// Add an entry
bool Add(CMasternode &mn);
2015-08-07 05:07:40 +02:00
/// Ask (source) node for mnb
void AskForMN(CNode *pnode, CTxIn &vin);
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(bool fForceExpiredRemoval = false);
2015-03-05 09:10:15 +01:00
/// Clear Masternode vector
2015-03-01 01:04:17 +01:00
void Clear();
int CountEnabled(int protocolVersion = -1);
/// Count Masternodes by network type - NET_IPV4, NET_IPV6, NET_TOR
int CountByIP(int nNetworkType);
void DsegUpdate(CNode* pnode);
2015-03-05 09:10:15 +01:00
/// Find an entry
CMasternode* Find(const CScript &payee);
CMasternode* Find(const CTxIn& vin);
2015-04-07 21:59:30 +02:00
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);
/// Find an entry in the masternode list that is next to be paid
CMasternode* GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime, int& nCount);
2015-03-05 09:10:15 +01:00
/// Find a random entry
CMasternode* FindRandomNotInVec(std::vector<CTxIn> &vecToExclude, int nProtocolVersion = -1);
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 InitDummyScriptPubkey();
2015-03-02 00:09:33 +01:00
void ProcessMasternodeConnections();
void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv);
2015-03-05 09:10:15 +01:00
/// Return the number of (unique) Masternodes
int size() { return vMasternodes.size(); }
std::string ToString() const;
void Remove(CTxIn vin);
2015-03-02 00:09:33 +01:00
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);
};
#endif