PIVX Core  5.6.99
P2P Digital Currency
masternode-payments.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2015 The Dash developers
2 // Copyright (c) 2015-2022 The PIVX Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef PIVX_MASTERNODE_PAYMENTS_H
7 #define PIVX_MASTERNODE_PAYMENTS_H
8 
9 #include "key.h"
10 #include "masternode.h"
11 #include "validationinterface.h"
12 
13 
17 
21 class CValidationState;
22 
24 
25 #define MNPAYMENTS_SIGNATURES_REQUIRED 6
26 #define MNPAYMENTS_SIGNATURES_TOTAL 10
27 
28 bool IsBlockPayeeValid(const CBlock& block, const CBlockIndex* pindexPrev);
29 std::string GetRequiredPaymentsString(int nBlockHeight);
30 bool IsBlockValueValid(int nHeight, CAmount& nExpectedValue, CAmount nMinted, CAmount& nBudgetAmt);
31 void FillBlockPayee(CMutableTransaction& txCoinbase, CMutableTransaction& txCoinstake, const CBlockIndex* pindexPrev, bool fProofOfStake);
32 
37 bool IsCoinbaseValueValid(const CTransactionRef& tx, CAmount nBudgetAmt, CValidationState& _state);
38 
40 
44 {
45 private:
46  fs::path pathDB;
47  std::string strMagicMessage;
48 
49 public:
50  enum ReadResult {
51  Ok,
58  };
59 
61  bool Write(const CMasternodePayments& objToSave);
63 };
64 
66 {
67 public:
69  int nVotes;
70 
72  {
74  nVotes = 0;
75  }
76 
77  CMasternodePayee(CScript payee, int nVotesIn)
78  {
79  scriptPubKey = payee;
80  nVotes = nVotesIn;
81  }
82 
83  SERIALIZE_METHODS(CMasternodePayee, obj) { READWRITE(obj.scriptPubKey, obj.nVotes); }
84 };
85 
86 // Keep track of votes for payees from masternodes
88 {
89 public:
91  std::vector<CMasternodePayee> vecPayments;
92 
94  {
95  nBlockHeight = 0;
96  vecPayments.clear();
97  }
98  explicit CMasternodeBlockPayees(int nBlockHeightIn)
99  {
100  nBlockHeight = nBlockHeightIn;
101  vecPayments.clear();
102  }
103 
104  void AddPayee(const CScript& payeeIn, int nIncrement)
105  {
107 
108  for (CMasternodePayee& payee : vecPayments) {
109  if (payee.scriptPubKey == payeeIn) {
110  payee.nVotes += nIncrement;
111  return;
112  }
113  }
114 
115  CMasternodePayee c(payeeIn, nIncrement);
116  vecPayments.push_back(c);
117  }
118 
119  bool GetPayee(CScript& payee) const
120  {
122 
123  int nVotes = -1;
124  for (const CMasternodePayee& p : vecPayments) {
125  if (p.nVotes > nVotes) {
126  payee = p.scriptPubKey;
127  nVotes = p.nVotes;
128  }
129  }
130 
131  return (nVotes > -1);
132  }
133 
134  bool HasPayeeWithVotes(const CScript& payee, int nVotesReq)
135  {
137 
138  for (CMasternodePayee& p : vecPayments) {
139  if (p.nVotes >= nVotesReq && p.scriptPubKey == payee) return true;
140  }
141 
142  return false;
143  }
144 
145  bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
146  std::string GetRequiredPaymentsString();
147 
148  SERIALIZE_METHODS(CMasternodeBlockPayees, obj) { READWRITE(obj.nBlockHeight, obj.vecPayments); }
149 };
150 
151 // for storing the winning payments
153 {
154 public:
158 
160  CSignedMessage(),
161  vinMasternode(),
162  nBlockHeight(0),
163  payee()
164  {}
165 
166  CMasternodePaymentWinner(const CTxIn& vinIn, int nHeight):
167  CSignedMessage(),
168  vinMasternode(vinIn),
169  nBlockHeight(nHeight),
170  payee()
171  {}
172 
173  uint256 GetHash() const;
174 
175  // override CSignedMessage functions
176  uint256 GetSignatureHash() const override { return GetHash(); }
177  std::string GetStrMessage() const override;
178  CTxIn GetVin() const { return vinMasternode; };
179 
180  bool IsValid(CNode* pnode, CValidationState& state, int chainHeight);
181  void Relay();
182 
183  void AddPayee(const CScript& payeeIn)
184  {
185  payee = payeeIn;
186  }
187 
188  SERIALIZE_METHODS(CMasternodePaymentWinner, obj) { READWRITE(obj.vinMasternode, obj.nBlockHeight, obj.payee, obj.vchSig, obj.nMessVersion); }
189 
190  std::string ToString()
191  {
192  std::string ret = "";
193  ret += vinMasternode.ToString();
194  ret += ", " + std::to_string(nBlockHeight);
195  ret += ", " + HexStr(payee);
196  ret += ", " + std::to_string((int)vchSig.size());
197  return ret;
198  }
199 };
200 
201 //
202 // Masternode Payments Class
203 // Keeps track of who should get paid for which blocks
204 //
205 
207 {
208 private:
210 
211 public:
212  std::map<uint256, CMasternodePaymentWinner> mapMasternodePayeeVotes;
213  std::map<int, CMasternodeBlockPayees> mapMasternodeBlocks;
214 
216  {
217  nLastBlockHeight = 0;
218  }
219 
220  void Clear()
221  {
223  mapMasternodeBlocks.clear();
224  mapMasternodePayeeVotes.clear();
225  }
226 
227  void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
228 
230  void ProcessBlock(int nBlockHeight);
231 
232  void Sync(CNode* node, int nCountNeeded);
233  void CleanPaymentList(int mnCount, int nHeight);
234 
235  // get the masternode payment outs for block built on top of pindexPrev
236  bool GetMasternodeTxOuts(const CBlockIndex* pindexPrev, std::vector<CTxOut>& voutMasternodePaymentsRet) const;
237 
238  // can be removed after transition to DMN
239  bool GetLegacyMasternodeTxOut(int nHeight, std::vector<CTxOut>& voutMasternodePaymentsRet) const;
240  bool GetBlockPayee(int nBlockHeight, CScript& payee) const;
241 
242  bool IsTransactionValid(const CTransaction& txNew, const CBlockIndex* pindexPrev);
243  bool IsScheduled(const CMasternode& mn, int nNotBlockHeight);
244 
245  bool ProcessMNWinner(CMasternodePaymentWinner& winner, CNode* pfrom, CValidationState& state);
246  bool ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv, CValidationState& state);
247  std::string GetRequiredPaymentsString(int nBlockHeight);
248  void FillBlockPayee(CMutableTransaction& txCoinbase, CMutableTransaction& txCoinstake, const CBlockIndex* pindexPrev, bool fProofOfStake) const;
249  std::string ToString() const;
250 
251  SERIALIZE_METHODS(CMasternodePayments, obj) { READWRITE(obj.mapMasternodePayeeVotes, obj.mapMasternodeBlocks); }
252 
253 private:
254  // keep track of last voted height for mnw signers
255  std::map<COutPoint, int> mapMasternodesLastVote; //prevout, nBlockHeight
256 
257  bool CanVote(const COutPoint& outMasternode, int nBlockHeight) const;
258  void RecordWinnerVote(const COutPoint& outMasternode, int nBlockHeight);
259 };
260 
261 #endif // PIVX_MASTERNODE_PAYMENTS_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
Definition: block.h:80
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
CMasternodeBlockPayees(int nBlockHeightIn)
std::string GetRequiredPaymentsString()
bool HasPayeeWithVotes(const CScript &payee, int nVotesReq)
SERIALIZE_METHODS(CMasternodeBlockPayees, obj)
void AddPayee(const CScript &payeeIn, int nIncrement)
std::vector< CMasternodePayee > vecPayments
bool GetPayee(CScript &payee) const
bool IsTransactionValid(const CTransaction &txNew, int nBlockHeight)
CMasternodePayee(CScript payee, int nVotesIn)
SERIALIZE_METHODS(CMasternodePayee, obj)
Save Masternode Payment Data (mnpayments.dat)
bool Write(const CMasternodePayments &objToSave)
ReadResult Read(CMasternodePayments &objToLoad)
void AddPayee(const CScript &payeeIn)
bool IsValid(CNode *pnode, CValidationState &state, int chainHeight)
std::string GetStrMessage() const override
uint256 GetSignatureHash() const override
CMasternodePaymentWinner(const CTxIn &vinIn, int nHeight)
SERIALIZE_METHODS(CMasternodePaymentWinner, obj)
SERIALIZE_METHODS(CMasternodePayments, obj)
bool CanVote(const COutPoint &outMasternode, int nBlockHeight) const
void ProcessBlock(int nBlockHeight)
void AddWinningMasternode(CMasternodePaymentWinner &winner)
void CleanPaymentList(int mnCount, int nHeight)
bool GetMasternodeTxOuts(const CBlockIndex *pindexPrev, std::vector< CTxOut > &voutMasternodePaymentsRet) const
bool IsTransactionValid(const CTransaction &txNew, const CBlockIndex *pindexPrev)
std::string GetRequiredPaymentsString(int nBlockHeight)
void RecordWinnerVote(const COutPoint &outMasternode, int nBlockHeight)
std::map< int, CMasternodeBlockPayees > mapMasternodeBlocks
void Sync(CNode *node, int nCountNeeded)
bool IsScheduled(const CMasternode &mn, int nNotBlockHeight)
std::map< COutPoint, int > mapMasternodesLastVote
bool GetBlockPayee(int nBlockHeight, CScript &payee) const
void FillBlockPayee(CMutableTransaction &txCoinbase, CMutableTransaction &txCoinstake, const CBlockIndex *pindexPrev, bool fProofOfStake) const
std::map< uint256, CMasternodePaymentWinner > mapMasternodePayeeVotes
std::string ToString() const
bool ProcessMNWinner(CMasternodePaymentWinner &winner, CNode *pfrom, CValidationState &state)
bool ProcessMessageMasternodePayments(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, CValidationState &state)
bool GetLegacyMasternodeTxOut(int nHeight, std::vector< CTxOut > &voutMasternodePaymentsRet) const
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Notifies listeners when the block chain tip advances.
Information about a peer.
Definition: net.h:669
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:72
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
Base Class for all signed messages on the network.
Definition: messagesigner.h:63
std::vector< unsigned char > vchSig
Definition: messagesigner.h:65
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
An input of a transaction.
Definition: transaction.h:94
std::string ToString() const
Definition: transaction.cpp:53
Implement this to subscribe to events generated in validation.
Capture information about block/transaction validation.
Definition: validation.h:24
256-bit opaque blob.
Definition: uint256.h:138
@ LOCK
Definition: lockunlock.h:16
RecursiveMutex cs_mapMasternodePayeeVotes
bool IsBlockValueValid(int nHeight, CAmount &nExpectedValue, CAmount nMinted, CAmount &nBudgetAmt)
std::string GetRequiredPaymentsString(int nBlockHeight)
RecursiveMutex cs_mapMasternodeBlocks
CMasternodePayments masternodePayments
Object for who's going to get paid on which blocks.
void DumpMasternodePayments()
bool IsCoinbaseValueValid(const CTransactionRef &tx, CAmount nBudgetAmt, CValidationState &_state)
Check coinbase output value for blocks after v6.0 enforcement.
bool IsBlockPayeeValid(const CBlock &block, const CBlockIndex *pindexPrev)
RecursiveMutex cs_vecPayments
void FillBlockPayee(CMutableTransaction &txCoinbase, CMutableTransaction &txCoinstake, const CBlockIndex *pindexPrev, bool fProofOfStake)
#define READWRITE(...)
Definition: serialize.h:183
A mutable version of CTransaction.
Definition: transaction.h:409
#define LOCK2(cs1, cs2)
Definition: sync.h:221
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:456
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.