Dash Core  0.12.2.1
P2P Digital Currency
masternode.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2017 The Dash Core developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef MASTERNODE_H
6 #define MASTERNODE_H
7 
8 #include "key.h"
9 #include "validation.h"
10 #include "spork.h"
11 
12 class CMasternode;
14 class CConnman;
15 
16 static const int MASTERNODE_CHECK_SECONDS = 5;
17 static const int MASTERNODE_MIN_MNB_SECONDS = 5 * 60;
18 static const int MASTERNODE_MIN_MNP_SECONDS = 10 * 60;
19 static const int MASTERNODE_EXPIRATION_SECONDS = 65 * 60;
20 static const int MASTERNODE_WATCHDOG_MAX_SECONDS = 120 * 60;
21 static const int MASTERNODE_NEW_START_REQUIRED_SECONDS = 180 * 60;
22 
23 static const int MASTERNODE_POSE_BAN_MAX_SCORE = 5;
24 
25 //
26 // The Masternode Ping Class : Contains a different serialize method for sending pings from masternodes throughout the network
27 //
28 
29 // sentinel version before sentinel ping implementation
30 #define DEFAULT_SENTINEL_VERSION 0x010001
31 
33 {
34 public:
37  int64_t sigTime{}; //mnb message times
38  std::vector<unsigned char> vchSig{};
39  bool fSentinelIsCurrent = false; // true if last sentinel ping was actual
40  // MSB is always 0, other 3 bits corresponds to x.x.x version scheme
42 
43  CMasternodePing() = default;
44 
45  CMasternodePing(const COutPoint& outpoint);
46 
48 
49  template <typename Stream, typename Operation>
50  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
51  READWRITE(vin);
55  if(ser_action.ForRead() && (s.size() == 0))
56  {
57  fSentinelIsCurrent = false;
59  return;
60  }
63  }
64 
65  uint256 GetHash() const
66  {
68  ss << vin;
69  ss << sigTime;
70  return ss.GetHash();
71  }
72 
74 
75  bool Sign(const CKey& keyMasternode, const CPubKey& pubKeyMasternode);
76  bool CheckSignature(CPubKey& pubKeyMasternode, int &nDos);
77  bool SimpleCheck(int& nDos);
78  bool CheckAndUpdate(CMasternode* pmn, bool fFromNewBroadcast, int& nDos, CConnman& connman);
79  void Relay(CConnman& connman);
80 };
81 
82 inline bool operator==(const CMasternodePing& a, const CMasternodePing& b)
83 {
84  return a.vin == b.vin && a.blockHash == b.blockHash;
85 }
86 inline bool operator!=(const CMasternodePing& a, const CMasternodePing& b)
87 {
88  return !(a == b);
89 }
90 
92 {
93  // Note: all these constructors can be removed once C++14 is enabled.
94  // (in C++11 the member initializers wrongly disqualify this as an aggregate)
95  masternode_info_t() = default;
96  masternode_info_t(masternode_info_t const&) = default;
97 
98  masternode_info_t(int activeState, int protoVer, int64_t sTime) :
99  nActiveState{activeState}, nProtocolVersion{protoVer}, sigTime{sTime} {}
100 
101  masternode_info_t(int activeState, int protoVer, int64_t sTime,
102  COutPoint const& outpoint, CService const& addr,
103  CPubKey const& pkCollAddr, CPubKey const& pkMN,
104  int64_t tWatchdogV = 0) :
105  nActiveState{activeState}, nProtocolVersion{protoVer}, sigTime{sTime},
106  vin{outpoint}, addr{addr},
107  pubKeyCollateralAddress{pkCollAddr}, pubKeyMasternode{pkMN},
108  nTimeLastWatchdogVote{tWatchdogV} {}
109 
110  int nActiveState = 0;
112  int64_t sigTime = 0; //mnb message time
113 
119 
120  int64_t nLastDsq = 0; //the dsq count from the last dsq broadcast of this node
121  int64_t nTimeLastChecked = 0;
122  int64_t nTimeLastPaid = 0;
123  int64_t nTimeLastPing = 0; //* not in CMN
124  bool fInfoValid = false; //* not in CMN
125 };
126 
127 //
128 // The Masternode Class. For managing the Darksend process. It contains the input of the 1000DRK, signature to prove
129 // it's the one who own that ip address and code for calculating the payment election.
130 //
132 {
133 private:
134  // critical section to protect the inner data structures
136 
137 public:
138  enum state {
147  };
148 
153  };
154 
155 
157  std::vector<unsigned char> vchSig{};
158 
164  bool fUnitTest = false;
165 
166  // KEEP TRACK OF GOVERNANCE ITEMS EACH MASTERNODE HAS VOTE UPON FOR RECALCULATION
167  std::map<uint256, int> mapGovernanceObjectsVotedOn;
168 
169  CMasternode();
170  CMasternode(const CMasternode& other);
171  CMasternode(const CMasternodeBroadcast& mnb);
172  CMasternode(CService addrNew, COutPoint outpointNew, CPubKey pubKeyCollateralAddressNew, CPubKey pubKeyMasternodeNew, int nProtocolVersionIn);
173 
175 
176  template <typename Stream, typename Operation>
177  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
178  LOCK(cs);
179  READWRITE(vin);
180  READWRITE(addr);
184  READWRITE(vchSig);
199  }
200 
201  // CALCULATE A RANK AGAINST OF GIVEN BLOCK
202  arith_uint256 CalculateScore(const uint256& blockHash);
203 
205 
206  static CollateralStatus CheckCollateral(const COutPoint& outpoint);
207  static CollateralStatus CheckCollateral(const COutPoint& outpoint, int& nHeightRet);
208  void Check(bool fForce = false);
209 
210  bool IsBroadcastedWithin(int nSeconds) { return GetAdjustedTime() - sigTime < nSeconds; }
211 
212  bool IsPingedWithin(int nSeconds, int64_t nTimeToCheckAt = -1)
213  {
214  if(lastPing == CMasternodePing()) return false;
215 
216  if(nTimeToCheckAt == -1) {
217  nTimeToCheckAt = GetAdjustedTime();
218  }
219  return nTimeToCheckAt - lastPing.sigTime < nSeconds;
220  }
221 
225  // NOTE: this one relies on nPoSeBanScore, not on nActiveState as everything else here
232 
233  static bool IsValidStateForAutoStart(int nActiveStateIn)
234  {
235  return nActiveStateIn == MASTERNODE_ENABLED ||
236  nActiveStateIn == MASTERNODE_PRE_ENABLED ||
237  nActiveStateIn == MASTERNODE_EXPIRED ||
238  nActiveStateIn == MASTERNODE_WATCHDOG_EXPIRED;
239  }
240 
242  {
244  return true;
245  }
248  return true;
249  }
250 
251  return false;
252  }
253 
256 
257  bool IsValidNetAddr();
258  static bool IsValidNetAddr(CService addrIn);
259 
263 
265 
266  static std::string StateToString(int nStateIn);
267  std::string GetStateString() const;
268  std::string GetStatus() const;
269 
270  int GetLastPaidTime() { return nTimeLastPaid; }
272  void UpdateLastPaid(const CBlockIndex *pindex, int nMaxBlocksToScanBack);
273 
274  // KEEP TRACK OF EACH GOVERNANCE ITEM INCASE THIS NODE GOES OFFLINE, SO WE CAN RECALC THEIR STATUS
275  void AddGovernanceVote(uint256 nGovernanceObjectHash);
276  // RECALCULATE CACHED STATUS FLAGS FOR ALL AFFECTED OBJECTS
278 
279  void RemoveGovernanceObject(uint256 nGovernanceObjectHash);
280 
281  void UpdateWatchdogVoteTime(uint64_t nVoteTime = 0);
282 
284  {
285  static_cast<masternode_info_t&>(*this)=from;
286  lastPing = from.lastPing;
287  vchSig = from.vchSig;
293  fUnitTest = from.fUnitTest;
295  return *this;
296  }
297 };
298 
299 inline bool operator==(const CMasternode& a, const CMasternode& b)
300 {
301  return a.vin == b.vin;
302 }
303 inline bool operator!=(const CMasternode& a, const CMasternode& b)
304 {
305  return !(a.vin == b.vin);
306 }
307 
308 
309 //
310 // The Masternode Broadcast Class : Contains a different serialize method for sending masternodes through the network
311 //
312 
314 {
315 public:
316 
317  bool fRecovery;
318 
321  CMasternodeBroadcast(CService addrNew, COutPoint outpointNew, CPubKey pubKeyCollateralAddressNew, CPubKey pubKeyMasternodeNew, int nProtocolVersionIn) :
322  CMasternode(addrNew, outpointNew, pubKeyCollateralAddressNew, pubKeyMasternodeNew, nProtocolVersionIn), fRecovery(false) {}
323 
325 
326  template <typename Stream, typename Operation>
327  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
328  READWRITE(vin);
329  READWRITE(addr);
332  READWRITE(vchSig);
336  }
337 
338  uint256 GetHash() const
339  {
341  ss << vin;
343  ss << sigTime;
344  return ss.GetHash();
345  }
346 
348  static bool Create(const COutPoint& outpoint, const CService& service, const CKey& keyCollateralAddressNew, const CPubKey& pubKeyCollateralAddressNew, const CKey& keyMasternodeNew, const CPubKey& pubKeyMasternodeNew, std::string &strErrorRet, CMasternodeBroadcast &mnbRet);
349  static bool Create(std::string strService, std::string strKey, std::string strTxHash, std::string strOutputIndex, std::string& strErrorRet, CMasternodeBroadcast &mnbRet, bool fOffline = false);
350 
351  bool SimpleCheck(int& nDos);
352  bool Update(CMasternode* pmn, int& nDos, CConnman& connman);
353  bool CheckOutpoint(int& nDos);
354 
355  bool Sign(const CKey& keyCollateralAddress);
356  bool CheckSignature(int& nDos);
357  void Relay(CConnman& connman);
358 };
359 
361 {
362 public:
366  int nonce{};
368  std::vector<unsigned char> vchSig1{};
369  std::vector<unsigned char> vchSig2{};
370 
371  CMasternodeVerification() = default;
372 
374  addr(addr),
375  nonce(nonce),
377  {}
378 
380 
381  template <typename Stream, typename Operation>
382  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
383  READWRITE(vin1);
384  READWRITE(vin2);
385  READWRITE(addr);
386  READWRITE(nonce);
390  }
391 
392  uint256 GetHash() const
393  {
395  ss << vin1;
396  ss << vin2;
397  ss << addr;
398  ss << nonce;
399  ss << nBlockHeight;
400  return ss.GetHash();
401  }
402 
403  void Relay() const
404  {
406  g_connman->RelayInv(inv);
407  }
408 };
409 
410 #endif
int GetLastPaidTime()
Definition: masternode.h:270
bool CheckOutpoint(int &nDos)
Definition: masternode.cpp:543
uint32_t nSentinelVersion
Definition: masternode.h:41
bool Update(CMasternode *pmn, int &nDos, CConnman &connman)
Definition: masternode.cpp:491
bool IsPoSeBanned()
Definition: masternode.h:224
void UpdateWatchdogVoteTime(uint64_t nVoteTime=0)
Definition: masternode.cpp:848
uint256 GetHash() const
Definition: masternode.h:65
bool IsValidForPayment()
Definition: masternode.h:241
#define READWRITE(obj)
Definition: serialize.h:175
void Relay(CConnman &connman)
Definition: masternode.cpp:665
bool UpdateFromNewBroadcast(CMasternodeBroadcast &mnb, CConnman &connman)
Definition: masternode.cpp:53
void PoSeBan()
Definition: masternode.h:262
CMasternodePing lastPing
Definition: masternode.h:156
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: masternode.h:382
bool SimpleCheck(int &nDos)
Definition: masternode.cpp:718
int64_t nTimeLastWatchdogVote
Definition: masternode.h:118
masternode_info_t(int activeState, int protoVer, int64_t sTime, COutPoint const &outpoint, CService const &addr, CPubKey const &pkCollAddr, CPubKey const &pkMN, int64_t tWatchdogV=0)
Definition: masternode.h:101
std::vector< unsigned char > vchSig
Definition: masternode.h:157
Definition: net.h:108
bool CheckAndUpdate(CMasternode *pmn, bool fFromNewBroadcast, int &nDos, CConnman &connman)
Definition: masternode.cpp:743
void FlagGovernanceItemsAsDirty()
Definition: masternode.cpp:860
CPubKey pubKeyMasternode
Definition: masternode.h:117
int nBlockLastPaid
Definition: masternode.h:160
int GetLastPaidBlock()
Definition: masternode.h:271
uint256 blockHash
Definition: masternode.h:36
bool IsEnabled()
Definition: masternode.h:222
bool Sign(const CKey &keyCollateralAddress)
Definition: masternode.cpp:620
static const int MASTERNODE_NEW_START_REQUIRED_SECONDS
Definition: masternode.h:21
masternode_info_t()=default
bool IsPreEnabled()
Definition: masternode.h:223
#define DEFAULT_SENTINEL_VERSION
Definition: masternode.h:30
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: masternode.h:327
int nPoSeBanHeight
Definition: masternode.h:162
std::vector< unsigned char > vchSig
Definition: masternode.h:38
static const int MASTERNODE_WATCHDOG_MAX_SECONDS
Definition: masternode.h:20
int64_t nTimeLastPing
Definition: masternode.h:123
std::vector< unsigned char > vchSig1
Definition: masternode.h:368
bool IsBroadcastedWithin(int nSeconds)
Definition: masternode.h:210
static CollateralStatus CheckCollateral(const COutPoint &outpoint)
Definition: masternode.cpp:116
std::map< uint256, int > mapGovernanceObjectsVotedOn
Definition: masternode.h:167
std::string GetStateString() const
Definition: masternode.cpp:309
uint256 GetHash()
Definition: hash.h:254
bool IsInputAssociatedWithPubkey()
Is the input associated with collateral public key? (and there is 1000 DASH - checking if valid maste...
Definition: masternode.cpp:258
static const int MASTERNODE_MIN_MNB_SECONDS
Definition: masternode.h:17
std::string GetStatus() const
Definition: masternode.cpp:314
int64_t nTimeLastChecked
Definition: masternode.h:121
bool Sign(const CKey &keyMasternode, const CPubKey &pubKeyMasternode)
Definition: masternode.cpp:681
static const int MASTERNODE_CHECK_SECONDS
Definition: masternode.h:16
arith_uint256 CalculateScore(const uint256 &blockHash)
Definition: masternode.cpp:91
bool fAllowMixingTx
Definition: masternode.h:163
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: masternode.h:50
CMasternodePing()=default
#define LOCK(cs)
Definition: sync.h:168
bool operator==(const CMasternodePing &a, const CMasternodePing &b)
Definition: masternode.h:82
static bool Create(const COutPoint &outpoint, const CService &service, const CKey &keyCollateralAddressNew, const CPubKey &pubKeyCollateralAddressNew, const CKey &keyMasternodeNew, const CPubKey &pubKeyMasternodeNew, std::string &strErrorRet, CMasternodeBroadcast &mnbRet)
Create Masternode broadcast, needs to be relayed manually after that.
Definition: masternode.cpp:397
CMasternodeBroadcast(CService addrNew, COutPoint outpointNew, CPubKey pubKeyCollateralAddressNew, CPubKey pubKeyMasternodeNew, int nProtocolVersionIn)
Definition: masternode.h:321
void RemoveGovernanceObject(uint256 nGovernanceObjectHash)
Definition: masternode.cpp:839
bool operator!=(const CMasternodePing &a, const CMasternodePing &b)
Definition: masternode.h:86
bool IsPoSeVerified()
Definition: masternode.h:226
void UpdateLastPaid(const CBlockIndex *pindex, int nMaxBlocksToScanBack)
Definition: masternode.cpp:320
void Check(bool fForce=false)
Definition: masternode.cpp:139
bool IsExpired()
Definition: masternode.h:227
bool CheckSignature(CPubKey &pubKeyMasternode, int &nDos)
Definition: masternode.cpp:703
bool IsWatchdogExpired()
Definition: masternode.h:230
static const int MASTERNODE_MIN_MNP_SECONDS
Definition: masternode.h:18
CMasternodeBroadcast(const CMasternode &mn)
Definition: masternode.h:320
CSporkManager sporkManager
Definition: spork.cpp:14
static const int SPORK_14_REQUIRE_SENTINEL_FLAG
Definition: spork.h:30
bool IsNewStartRequired()
Definition: masternode.h:231
std::vector< unsigned char > vchSig2
Definition: masternode.h:369
void AddGovernanceVote(uint256 nGovernanceObjectHash)
Definition: masternode.cpp:830
CPubKey pubKeyCollateralAddress
Definition: masternode.h:116
int64_t sigTime
Definition: masternode.h:37
static const int MASTERNODE_EXPIRATION_SECONDS
Definition: masternode.h:19
static const int MASTERNODE_POSE_BAN_MAX_SCORE
Definition: masternode.h:23
uint256 GetHash() const
Definition: masternode.h:338
bool fSentinelIsCurrent
Definition: masternode.h:39
bool CheckSignature(int &nDos)
Definition: masternode.cpp:644
CCriticalSection cs
Definition: masternode.h:135
static const int PROTOCOL_VERSION
Definition: version.h:13
static std::string StateToString(int nStateIn)
Definition: masternode.cpp:294
bool IsValidNetAddr()
Definition: masternode.cpp:273
int64_t GetAdjustedTime()
Definition: timedata.cpp:33
void IncreasePoSeBanScore()
Definition: masternode.h:260
masternode_info_t(int activeState, int protoVer, int64_t sTime)
Definition: masternode.h:98
static bool IsValidStateForAutoStart(int nActiveStateIn)
Definition: masternode.h:233
uint256 GetHash() const
Definition: masternode.h:392
std::unique_ptr< CConnman > g_connman
Definition: init.cpp:103
Definition: protocol.h:314
bool SimpleCheck(int &nDos)
Definition: masternode.cpp:430
bool fUnitTest
Definition: masternode.h:164
masternode_info_t GetInfo()
Definition: masternode.cpp:286
bool IsOutpointSpent()
Definition: masternode.h:228
Definition: pubkey.h:37
bool IsExpired() const
Definition: masternode.h:73
bool IsSporkActive(int nSporkID)
Definition: spork.cpp:120
int nPoSeBanScore
Definition: masternode.h:161
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: masternode.h:177
void Relay(CConnman &connman)
Definition: masternode.cpp:824
bool IsUpdateRequired()
Definition: masternode.h:229
CMasternodeVerification()=default
Definition: key.h:35
void DecreasePoSeBanScore()
Definition: masternode.h:261
uint256 nCollateralMinConfBlockHash
Definition: masternode.h:159
CMasternodeVerification(CService addr, int nonce, int nBlockHeight)
Definition: masternode.h:373
CMasternode & operator=(CMasternode const &from)
Definition: masternode.h:283
int64_t nTimeLastPaid
Definition: masternode.h:122
bool IsPingedWithin(int nSeconds, int64_t nTimeToCheckAt=-1)
Definition: masternode.h:212