PRCYCoin  2.0.0.7rc1
P2P Digital Currency
masternode.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2015 The Dash developers
2 // Copyright (c) 2015-2018 The PIVX developers
3 // Copyright (c) 2018-2020 The DAPS Project developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef MASTERNODE_H
8 #define MASTERNODE_H
9 
10 #include "base58.h"
11 #include "key.h"
12 #include "main.h"
13 #include "net.h"
14 #include "sync.h"
15 #include "timedata.h"
16 #include "util.h"
17 
18 #define MASTERNODE_MIN_CONFIRMATIONS 15
19 #define MASTERNODE_MIN_MNP_SECONDS (10 * 60)
20 #define MASTERNODE_MIN_MNB_SECONDS (5 * 60)
21 #define MASTERNODE_PING_SECONDS (5 * 60)
22 #define MASTERNODE_EXPIRATION_SECONDS (120 * 60)
23 #define MASTERNODE_REMOVAL_SECONDS (130 * 60)
24 #define MASTERNODE_CHECK_SECONDS 5
25 
26 
27 class CMasternode;
29 class CMasternodePing;
30 extern std::map<int64_t, uint256> mapCacheBlockHashes;
31 
32 bool GetBlockHash(uint256& hash, int nBlockHeight);
33 
34 
35 //
36 // The Masternode Ping Class : Contains a different serialize method for sending pings from masternodes throughout the network
37 //
38 
40 {
41 public:
44  int64_t sigTime; //mnb message times
45  std::vector<unsigned char> vchSig;
46  //removed stop
47 
49  CMasternodePing(CTxIn& newVin);
50 
52 
53  template <typename Stream, typename Operation>
54  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
55  {
56  READWRITE(vin);
60  }
61 
62  bool CheckAndUpdate(int& nDos, bool fRequireEnabled = true, bool fCheckSigTimeOnly = false);
63  bool Sign(CKey& keyMasternode, CPubKey& pubKeyMasternode);
64  bool VerifySignature(CPubKey& pubKeyMasternode, int &nDos);
65  void Relay();
66 
68  {
69  CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
70  ss << vin;
71  ss << sigTime;
72  return ss.GetHash();
73  }
74 
75  void swap(CMasternodePing& first, CMasternodePing& second) // nothrow
76  {
77  // enable ADL (not necessary in our case, but good practice)
78  using std::swap;
79 
80  // by swapping the members of two classes,
81  // the two classes are effectively swapped
82  swap(first.vin, second.vin);
83  swap(first.blockHash, second.blockHash);
84  swap(first.sigTime, second.sigTime);
85  swap(first.vchSig, second.vchSig);
86  }
87 
89  {
90  swap(*this, from);
91  return *this;
92  }
93  friend bool operator==(const CMasternodePing& a, const CMasternodePing& b)
94  {
95  return a.vin == b.vin && a.blockHash == b.blockHash;
96  }
97  friend bool operator!=(const CMasternodePing& a, const CMasternodePing& b)
98  {
99  return !(a == b);
100  }
101 };
102 
103 //
104 // The Masternode Class. It contains the input of the 5000 PRCY, signature to prove
105 // it's the one who own that ip address and code for calculating the payment election.
106 //
108 {
109 private:
110  // critical section to protect the inner data structures
113 
114 public:
115  enum state {
125  };
126 
133  std::vector<unsigned char> sig;
135  int64_t sigTime; //mnb message time
138  bool unitTest;
142  int64_t nLastDsq; //the dsq count from the last dsq broadcast of this node
146 
147  CMasternode();
148  CMasternode(const CMasternode& other);
149  CMasternode(const CMasternodeBroadcast& mnb);
150 
151 
152  void swap(CMasternode& first, CMasternode& second) // nothrow
153  {
154  // enable ADL (not necessary in our case, but good practice)
155  using std::swap;
156 
157  // by swapping the members of two classes,
158  // the two classes are effectively swapped
159  swap(first.vin, second.vin);
160  swap(first.addr, second.addr);
162  swap(first.pubKeyMasternode, second.pubKeyMasternode);
163  swap(first.sig, second.sig);
164  swap(first.activeState, second.activeState);
165  swap(first.sigTime, second.sigTime);
166  swap(first.lastPing, second.lastPing);
167  swap(first.cacheInputAge, second.cacheInputAge);
169  swap(first.unitTest, second.unitTest);
170  swap(first.allowFreeTx, second.allowFreeTx);
171  swap(first.protocolVersion, second.protocolVersion);
172  swap(first.nLastDsq, second.nLastDsq);
175  }
176 
178  {
179  swap(*this, from);
180  return *this;
181  }
182  friend bool operator==(const CMasternode& a, const CMasternode& b)
183  {
184  return a.vin == b.vin;
185  }
186  friend bool operator!=(const CMasternode& a, const CMasternode& b)
187  {
188  return !(a.vin == b.vin);
189  }
190 
191  uint256 CalculateScore(int mod = 1, int64_t nBlockHeight = 0);
192 
194 
195  template <typename Stream, typename Operation>
196  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
197  {
198  LOCK(cs);
199 
200  READWRITE(vin);
201  READWRITE(addr);
204  READWRITE(sig);
216  }
217 
218  int64_t SecondsSincePayment();
219 
221 
222  void Check(bool forceCheck = false);
223 
224  bool IsBroadcastedWithin(int seconds)
225  {
226  return (GetAdjustedTime() - sigTime) < seconds;
227  }
228 
229  bool IsPingedWithin(int seconds, int64_t now = -1)
230  {
231  now == -1 ? now = GetAdjustedTime() : now;
232 
233  return (lastPing == CMasternodePing()) ? false : now - lastPing.sigTime < seconds;
234  }
235 
236  void Disable()
237  {
238  sigTime = 0;
240  }
241 
242  bool IsEnabled()
243  {
245  }
246 
248  {
249  int tipHeight;
250  {
251  LOCK(cs_main);
252  CBlockIndex *pindex = chainActive.Tip();
253  if (!pindex) return 0;
254  tipHeight = pindex->nHeight;
255  }
256 
257  if (cacheInputAge == 0) {
259  cacheInputAgeBlock = tipHeight;
260  }
261 
262  return cacheInputAge + (tipHeight - cacheInputAgeBlock);
263  }
264 
265  std::string GetStatus();
266 
267  std::string Status()
268  {
269  std::string strStatus = "ACTIVE";
270 
271  if (activeState == CMasternode::MASTERNODE_ENABLED) strStatus = "ENABLED";
272  if (activeState == CMasternode::MASTERNODE_EXPIRED) strStatus = "EXPIRED";
273  if (activeState == CMasternode::MASTERNODE_VIN_SPENT) strStatus = "VIN_SPENT";
274  if (activeState == CMasternode::MASTERNODE_REMOVE) strStatus = "REMOVE";
275  if (activeState == CMasternode::MASTERNODE_POS_ERROR) strStatus = "POS_ERROR";
276 
277  return strStatus;
278  }
279 
280  int64_t GetLastPaid();
281  bool IsValidNetAddr();
282 
284  bool IsInputAssociatedWithPubkey(CTxIn& vin, CPubKey& pubkey) const;
285 };
286 
287 
288 //
289 // The Masternode Broadcast Class : Contains a different serialize method for sending masternodes through the network
290 //
291 
293 {
294 public:
296  CMasternodeBroadcast(CService newAddr, CTxIn newVin, CPubKey newPubkey, CPubKey newPubkey2, int protocolVersionIn);
298 
299  bool CheckAndUpdate(int& nDoS);
300  bool CheckInputsAndAdd(int& nDos);
301  bool Sign(CKey& keyCollateralAddress);
302  bool VerifySignature();
303  void Relay();
304  std::string GetStrMessage();
305 
307 
308  template <typename Stream, typename Operation>
309  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
310  {
311  READWRITE(vin);
312  READWRITE(addr);
315  READWRITE(sig);
320  }
321 
323  {
324  CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
325  ss << sigTime;
327  return ss.GetHash();
328  }
329 
331  static bool Create(CTxIn vin, CService service, CKey keyCollateralAddressNew, CPubKey pubKeyCollateralAddressNew, CKey keyMasternodeNew, CPubKey pubKeyMasternodeNew, std::string& strErrorRet, CMasternodeBroadcast& mnbRet);
332  static bool Create(std::string strService, std::string strKey, std::string strTxHash, std::string strOutputIndex, std::string& strErrorRet, CMasternodeBroadcast& mnbRet, bool fOffline = false);
333  static bool CheckDefaultPort(CService service, std::string& strErrorRet, const std::string& strContext);
334 };
335 
336 #endif
CTxIn
An input of a transaction.
Definition: transaction.h:83
CMasternode::nLastDsq
int64_t nLastDsq
Definition: masternode.h:142
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:133
CMasternodeBroadcast::Sign
bool Sign(CKey &keyCollateralAddress)
Definition: masternode.cpp:700
CMasternode::MASTERNODE_EXPIRED
@ MASTERNODE_EXPIRED
Definition: masternode.h:118
CMasternode::pubKeyCollateralAddress1
CPubKey pubKeyCollateralAddress1
Definition: masternode.h:131
CMasternode::cacheInputAgeBlock
int cacheInputAgeBlock
Definition: masternode.h:137
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:161
CMasternodeBroadcast::CheckDefaultPort
static bool CheckDefaultPort(CService service, std::string &strErrorRet, const std::string &strContext)
Definition: masternode.cpp:505
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
CMasternode::operator!=
friend bool operator!=(const CMasternode &a, const CMasternode &b)
Definition: masternode.h:186
CMasternode::MASTERNODE_ENABLED
@ MASTERNODE_ENABLED
Definition: masternode.h:117
CMasternode::allowFreeTx
bool allowFreeTx
Definition: masternode.h:139
CMasternode::MASTERNODE_WATCHDOG_EXPIRED
@ MASTERNODE_WATCHDOG_EXPIRED
Definition: masternode.h:121
CMasternodeBroadcast::Create
static bool Create(CTxIn vin, CService service, CKey keyCollateralAddressNew, CPubKey pubKeyCollateralAddressNew, CKey keyMasternodeNew, CPubKey pubKeyMasternodeNew, std::string &strErrorRet, CMasternodeBroadcast &mnbRet)
Create Masternode broadcast, needs to be relayed manually after that.
Definition: masternode.cpp:467
CMasternodeBroadcast::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: masternode.h:309
sync.h
chainActive
CChain chainActive
The currently-connected chain of blocks.
Definition: main.cpp:70
timedata.h
CMasternode::nScanningErrorCount
int nScanningErrorCount
Definition: masternode.h:143
CMasternodePing::CheckAndUpdate
bool CheckAndUpdate(int &nDos, bool fRequireEnabled=true, bool fCheckSigTimeOnly=false)
Definition: masternode.cpp:789
CMasternode::state
state
Definition: masternode.h:115
CMasternodePing::Sign
bool Sign(CKey &keyMasternode, CPubKey &pubKeyMasternode)
Definition: masternode.cpp:755
CMasternodeBroadcast::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: masternode.h:306
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:181
CMasternodeBroadcast::CheckAndUpdate
bool CheckAndUpdate(int &nDoS)
Definition: masternode.cpp:519
CMasternode::UpdateFromNewBroadcast
bool UpdateFromNewBroadcast(CMasternodeBroadcast &mnb)
Definition: masternode.cpp:146
CMasternode::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: masternode.h:196
CMasternode::SecondsSincePayment
int64_t SecondsSincePayment()
Definition: masternode.cpp:241
CMasternodePing::operator=
CMasternodePing & operator=(CMasternodePing from)
Definition: masternode.h:88
CMasternode::IsValidNetAddr
bool IsValidNetAddr()
Definition: masternode.cpp:329
CMasternodeBroadcast::Relay
void Relay()
Definition: masternode.cpp:694
AnnotatedMixin< std::recursive_mutex >
CMasternode::activeState
int activeState
Definition: masternode.h:134
CMasternodePing::operator==
friend bool operator==(const CMasternodePing &a, const CMasternodePing &b)
Definition: masternode.h:93
CMasternodePing::sigTime
int64_t sigTime
Definition: masternode.h:44
CMasternode::cs
RecursiveMutex cs
Definition: masternode.h:111
mapCacheBlockHashes
std::map< int64_t, uint256 > mapCacheBlockHashes
Definition: masternode.cpp:23
CMasternode::operator==
friend bool operator==(const CMasternode &a, const CMasternode &b)
Definition: masternode.h:182
CMasternode::GetMasternodeInputAge
int GetMasternodeInputAge()
Definition: masternode.h:247
CMasternode::vin
CTxIn vin
Definition: masternode.h:127
CMasternode::sigTime
int64_t sigTime
Definition: masternode.h:135
cs_main
RecursiveMutex cs_main
Global state.
Definition: main.cpp:65
CMasternode::MASTERNODE_POS_ERROR
@ MASTERNODE_POS_ERROR
Definition: masternode.h:124
CMasternode::Status
std::string Status()
Definition: masternode.h:267
CMasternode::Disable
void Disable()
Definition: masternode.h:236
CMasternode::pubKeyMasternode1
CPubKey pubKeyMasternode1
Definition: masternode.h:132
CMasternode::MASTERNODE_VIN_SPENT
@ MASTERNODE_VIN_SPENT
Definition: masternode.h:123
CMasternodePing::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: masternode.h:51
CMasternode::swap
void swap(CMasternode &first, CMasternode &second)
Definition: masternode.h:152
CMasternode::MASTERNODE_POSE_BAN
@ MASTERNODE_POSE_BAN
Definition: masternode.h:122
GetBlockHash
bool GetBlockHash(uint256 &hash, int nBlockHeight)
Definition: masternode.cpp:26
CMasternode::lastTimeChecked
int64_t lastTimeChecked
Definition: masternode.h:112
CMasternodeBroadcast::CheckInputsAndAdd
bool CheckInputsAndAdd(int &nDos)
Definition: masternode.cpp:612
CMasternode::ADD_SERIALIZE_METHODS
ADD_SERIALIZE_METHODS
Definition: masternode.h:193
CMasternode::CalculateScore
uint256 CalculateScore(int mod=1, int64_t nBlockHeight=0)
Definition: masternode.cpp:171
GetInputAge
int GetInputAge(CTxIn &vin)
Definition: main.cpp:1373
CMasternodePing::operator!=
friend bool operator!=(const CMasternodePing &a, const CMasternodePing &b)
Definition: masternode.h:97
CMasternodePing::blockHash
uint256 blockHash
Definition: masternode.h:43
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CMasternode::CMasternode
CMasternode()
Definition: masternode.cpp:74
CMasternode::pubKeyMasternode
CPubKey pubKeyMasternode
Definition: masternode.h:130
CMasternodeBroadcast::CMasternodeBroadcast
CMasternodeBroadcast()
Definition: masternode.cpp:365
CMasternode::addr
CService addr
Definition: masternode.h:128
CMasternodeBroadcast::GetHash
uint256 GetHash()
Definition: masternode.h:322
CMasternode::operator=
CMasternode & operator=(CMasternode from)
Definition: masternode.h:177
CMasternode::sig
std::vector< unsigned char > sig
Definition: masternode.h:133
key.h
CMasternode
Definition: masternode.h:107
CMasternodePing::CMasternodePing
CMasternodePing()
Definition: masternode.cpp:738
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
GetAdjustedTime
int64_t GetAdjustedTime()
Definition: timedata.cpp:30
READWRITE
#define READWRITE(obj)
Definition: serialize.h:164
CMasternode::nLastScanningErrorBlockHeight
int nLastScanningErrorBlockHeight
Definition: masternode.h:144
CMasternodePing::Relay
void Relay()
Definition: masternode.cpp:872
CMasternode::GetStatus
std::string GetStatus()
Definition: masternode.cpp:307
CKey
An encapsulated private key.
Definition: key.h:39
CMasternode::MASTERNODE_OUTPOINT_SPENT
@ MASTERNODE_OUTPOINT_SPENT
Definition: masternode.h:119
main.h
CMasternode::GetLastPaid
int64_t GetLastPaid()
Definition: masternode.cpp:259
LOCK
#define LOCK(cs)
Definition: sync.h:182
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:259
CMasternode::IsPingedWithin
bool IsPingedWithin(int seconds, int64_t now=-1)
Definition: masternode.h:229
CMasternode::MASTERNODE_REMOVE
@ MASTERNODE_REMOVE
Definition: masternode.h:120
base58.h
CMasternode::IsEnabled
bool IsEnabled()
Definition: masternode.h:242
CMasternodePing::SerializationOp
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: masternode.h:54
CMasternode::IsBroadcastedWithin
bool IsBroadcastedWithin(int seconds)
Definition: masternode.h:224
CMasternode::cacheInputAge
int cacheInputAge
Definition: masternode.h:136
CChain::Tip
CBlockIndex * Tip(bool fProofOfStake=false) const
Returns the index entry for the tip of this chain, or NULL if none.
Definition: chain.h:596
CMasternodePing::vchSig
std::vector< unsigned char > vchSig
Definition: masternode.h:45
CHashWriter::GetHash
uint256 GetHash()
Definition: hash.h:277
net.h
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:162
CMasternodeBroadcast::GetStrMessage
std::string GetStrMessage()
Definition: masternode.cpp:730
CMasternode::nActiveState
int nActiveState
Definition: masternode.h:141
CMasternodeBroadcast
Definition: masternode.h:292
CMasternodePing::swap
void swap(CMasternodePing &first, CMasternodePing &second)
Definition: masternode.h:75
CMasternode::MASTERNODE_PRE_ENABLED
@ MASTERNODE_PRE_ENABLED
Definition: masternode.h:116
CMasternodeBroadcast::VerifySignature
bool VerifySignature()
Definition: masternode.cpp:720
CMasternode::protocolVersion
int protocolVersion
Definition: masternode.h:140
CMasternode::unitTest
bool unitTest
Definition: masternode.h:138
util.h
CMasternodePing
Definition: masternode.h:39
CMasternodePing::GetHash
uint256 GetHash()
Definition: masternode.h:67
CMasternode::Check
void Check(bool forceCheck=false)
Definition: masternode.cpp:200
CMasternodePing::VerifySignature
bool VerifySignature(CPubKey &pubKeyMasternode, int &nDos)
Definition: masternode.cpp:777
CMasternode::pubKeyCollateralAddress
CPubKey pubKeyCollateralAddress
Definition: masternode.h:129
CMasternode::lastPing
CMasternodePing lastPing
Definition: masternode.h:145
CMasternode::IsInputAssociatedWithPubkey
bool IsInputAssociatedWithPubkey(CTxIn &vin, CPubKey &pubkey) const
Is the input associated with collateral public key? (and there is 5000 PRCY - checking if valid maste...
Definition: masternode.cpp:337
CMasternodePing::vin
CTxIn vin
Definition: masternode.h:42