Dash Core  0.12.2.1
P2P Digital Currency
masternode-payments.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_PAYMENTS_H
6 #define MASTERNODE_PAYMENTS_H
7 
8 #include "util.h"
9 #include "core_io.h"
10 #include "key.h"
11 #include "masternode.h"
12 #include "net_processing.h"
13 #include "utilstrencodings.h"
14 
18 
19 static const int MNPAYMENTS_SIGNATURES_REQUIRED = 6;
20 static const int MNPAYMENTS_SIGNATURES_TOTAL = 10;
21 
23 // vote for masternode and be elected as a payment winner
24 // V1 - Last protocol version before update
25 // V2 - Newest protocol version
26 static const int MIN_MASTERNODE_PAYMENT_PROTO_VERSION_1 = 70206;
27 static const int MIN_MASTERNODE_PAYMENT_PROTO_VERSION_2 = 70208;
28 
32 
34 
36 bool IsBlockValueValid(const CBlock& block, int nBlockHeight, CAmount blockReward, std::string &strErrorRet);
37 bool IsBlockPayeeValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
38 void FillBlockPayments(CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, CTxOut& txoutMasternodeRet, std::vector<CTxOut>& voutSuperblockRet);
40 
42 {
43 private:
45  std::vector<uint256> vecVoteHashes;
46 
47 public:
49  scriptPubKey(),
51  {}
52 
53  CMasternodePayee(CScript payee, uint256 hashIn) :
54  scriptPubKey(payee),
56  {
57  vecVoteHashes.push_back(hashIn);
58  }
59 
61 
62  template <typename Stream, typename Operation>
63  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
66  }
67 
69 
70  void AddVoteHash(uint256 hashIn) { vecVoteHashes.push_back(hashIn); }
71  std::vector<uint256> GetVoteHashes() { return vecVoteHashes; }
72  int GetVoteCount() { return vecVoteHashes.size(); }
73 };
74 
75 // Keep track of votes for payees from masternodes
77 {
78 public:
80  std::vector<CMasternodePayee> vecPayees;
81 
83  nBlockHeight(0),
84  vecPayees()
85  {}
86  CMasternodeBlockPayees(int nBlockHeightIn) :
87  nBlockHeight(nBlockHeightIn),
88  vecPayees()
89  {}
90 
92 
93  template <typename Stream, typename Operation>
94  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
97  }
98 
99  void AddPayee(const CMasternodePaymentVote& vote);
100  bool GetBestPayee(CScript& payeeRet);
101  bool HasPayeeWithVotes(const CScript& payeeIn, int nVotesReq);
102 
103  bool IsTransactionValid(const CTransaction& txNew);
104 
105  std::string GetRequiredPaymentsString();
106 };
107 
108 // vote for the winning payment
110 {
111 public:
113 
116  std::vector<unsigned char> vchSig;
117 
119  vinMasternode(),
120  nBlockHeight(0),
121  payee(),
122  vchSig()
123  {}
124 
126  vinMasternode(outpointMasternode),
128  payee(payee),
129  vchSig()
130  {}
131 
133 
134  template <typename Stream, typename Operation>
135  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
138  READWRITE(*(CScriptBase*)(&payee));
139  READWRITE(vchSig);
140  }
141 
142  uint256 GetHash() const {
144  ss << *(CScriptBase*)(&payee);
145  ss << nBlockHeight;
146  ss << vinMasternode.prevout;
147  return ss.GetHash();
148  }
149 
150  bool Sign();
151  bool CheckSignature(const CPubKey& pubKeyMasternode, int nValidationHeight, int &nDos);
152 
153  bool IsValid(CNode* pnode, int nValidationHeight, std::string& strError, CConnman& connman);
154  void Relay(CConnman& connman);
155 
156  bool IsVerified() { return !vchSig.empty(); }
157  void MarkAsNotVerified() { vchSig.clear(); }
158 
159  std::string ToString() const;
160 };
161 
162 //
163 // Masternode Payments Class
164 // Keeps track of who should get paid for which blocks
165 //
166 
168 {
169 private:
170  // masternode count times nStorageCoeff payments blocks should be stored ...
171  const float nStorageCoeff;
172  // ... but at least nMinBlocksToStore (payments blocks)
173  const int nMinBlocksToStore;
174 
175  // Keep track of current block height
177 
178 public:
179  std::map<uint256, CMasternodePaymentVote> mapMasternodePaymentVotes;
180  std::map<int, CMasternodeBlockPayees> mapMasternodeBlocks;
181  std::map<COutPoint, int> mapMasternodesLastVote;
182  std::map<COutPoint, int> mapMasternodesDidNotVote;
183 
185 
187 
188  template <typename Stream, typename Operation>
189  inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
192  }
193 
194  void Clear();
195 
196  bool AddPaymentVote(const CMasternodePaymentVote& vote);
197  bool HasVerifiedPaymentVote(uint256 hashIn);
198  bool ProcessBlock(int nBlockHeight, CConnman& connman);
199  void CheckPreviousBlockVotes(int nPrevBlockHeight);
200 
201  void Sync(CNode* node, CConnman& connman);
202  void RequestLowDataPaymentBlocks(CNode* pnode, CConnman& connman);
203  void CheckAndRemove();
204 
205  bool GetBlockPayee(int nBlockHeight, CScript& payee);
206  bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
207  bool IsScheduled(CMasternode& mn, int nNotBlockHeight);
208 
209  bool CanVote(COutPoint outMasternode, int nBlockHeight);
210 
212  void ProcessMessage(CNode* pfrom, std::string& strCommand, CDataStream& vRecv, CConnman& connman);
213  std::string GetRequiredPaymentsString(int nBlockHeight);
214  void FillBlockPayee(CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, CTxOut& txoutMasternodeRet);
215  std::string ToString() const;
216 
217  int GetBlockCount() { return mapMasternodeBlocks.size(); }
218  int GetVoteCount() { return mapMasternodePaymentVotes.size(); }
219 
220  bool IsEnoughData();
221  int GetStorageLimit();
222 
223  void UpdatedBlockTip(const CBlockIndex *pindex, CConnman& connman);
224 };
225 
226 #endif
bool AddPaymentVote(const CMasternodePaymentVote &vote)
void FillBlockPayee(CMutableTransaction &txNew, int nBlockHeight, CAmount blockReward, CTxOut &txoutMasternodeRet)
std::string ToString() const
#define READWRITE(obj)
Definition: serialize.h:175
CMasternodePaymentVote(COutPoint outpointMasternode, int nBlockHeight, CScript payee)
CMasternodePayee(CScript payee, uint256 hashIn)
bool GetBestPayee(CScript &payeeRet)
std::string ToString() const
bool CheckSignature(const CPubKey &pubKeyMasternode, int nValidationHeight, int &nDos)
CCriticalSection cs_vecPayees
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
Definition: net.h:108
void ProcessMessage(CNode *pfrom, std::string &strCommand, CDataStream &vRecv, CConnman &connman)
static const int MNPAYMENTS_SIGNATURES_REQUIRED
void RequestLowDataPaymentBlocks(CNode *pnode, CConnman &connman)
bool IsValid(CNode *pnode, int nValidationHeight, std::string &strError, CConnman &connman)
void CheckPreviousBlockVotes(int nPrevBlockHeight)
void AddVoteHash(uint256 hashIn)
void UpdatedBlockTip(const CBlockIndex *pindex, CConnman &connman)
std::string GetRequiredPaymentsString(int nBlockHeight)
int64_t CAmount
Definition: amount.h:14
uint256 GetHash()
Definition: hash.h:254
bool IsBlockPayeeValid(const CTransaction &txNew, int nBlockHeight, CAmount blockReward)
static const int MNPAYMENTS_SIGNATURES_TOTAL
COutPoint prevout
Definition: transaction.h:61
std::vector< CMasternodePayee > vecPayees
std::map< COutPoint, int > mapMasternodesLastVote
Definition: net.h:661
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
std::vector< uint256 > GetVoteHashes()
bool IsScheduled(CMasternode &mn, int nNotBlockHeight)
bool IsTransactionValid(const CTransaction &txNew)
bool IsTransactionValid(const CTransaction &txNew, int nBlockHeight)
CMasternodePayments mnpayments
CMasternodeBlockPayees(int nBlockHeightIn)
std::string GetRequiredPaymentsString(int nBlockHeight)
static const int MIN_MASTERNODE_PAYMENT_PROTO_VERSION_1
minimum peer version that can receive and send masternode payment messages,
CCriticalSection cs_mapMasternodeBlocks
void FillBlockPayments(CMutableTransaction &txNew, int nBlockHeight, CAmount blockReward, CTxOut &txoutMasternodeRet, std::vector< CTxOut > &voutSuperblockRet)
static const int MIN_MASTERNODE_PAYMENT_PROTO_VERSION_2
std::vector< unsigned char > vchSig
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
bool HasPayeeWithVotes(const CScript &payeeIn, int nVotesReq)
bool ProcessBlock(int nBlockHeight, CConnman &connman)
std::string GetRequiredPaymentsString()
static const int PROTOCOL_VERSION
Definition: version.h:13
void SerializationOp(Stream &s, Operation ser_action, int nType, int nVersion)
bool IsBlockValueValid(const CBlock &block, int nBlockHeight, CAmount blockReward, std::string &strErrorRet)
TODO: all 4 functions do not belong here really, they should be refactored/moved somewhere (main...
CCriticalSection cs_mapMasternodePayeeVotes
Definition: pubkey.h:37
std::map< COutPoint, int > mapMasternodesDidNotVote
void Sync(CNode *node, CConnman &connman)
std::map< uint256, CMasternodePaymentVote > mapMasternodePaymentVotes
Definition: block.h:73
void Relay(CConnman &connman)
std::vector< uint256 > vecVoteHashes
bool CanVote(COutPoint outMasternode, int nBlockHeight)
bool GetBlockPayee(int nBlockHeight, CScript &payee)
std::map< int, CMasternodeBlockPayees > mapMasternodeBlocks
void AddPayee(const CMasternodePaymentVote &vote)
bool HasVerifiedPaymentVote(uint256 hashIn)