forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 717
/
Copy pathmasternode-payments.h
261 lines (206 loc) · 7.42 KB
/
masternode-payments.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) 2014-2015 The Dash developers
// Copyright (c) 2015-2022 The PIVX Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef PIVX_MASTERNODE_PAYMENTS_H
#define PIVX_MASTERNODE_PAYMENTS_H
#include "key.h"
#include "masternode.h"
#include "validationinterface.h"
extern RecursiveMutex cs_vecPayments;
extern RecursiveMutex cs_mapMasternodeBlocks;
extern RecursiveMutex cs_mapMasternodePayeeVotes;
class CMasternodePayments;
class CMasternodePaymentWinner;
class CMasternodeBlockPayees;
class CValidationState;
extern CMasternodePayments masternodePayments;
#define MNPAYMENTS_SIGNATURES_REQUIRED 6
#define MNPAYMENTS_SIGNATURES_TOTAL 10
bool IsBlockPayeeValid(const CBlock& block, const CBlockIndex* pindexPrev);
std::string GetRequiredPaymentsString(int nBlockHeight);
bool IsBlockValueValid(int nHeight, CAmount& nExpectedValue, CAmount nMinted, CAmount& nBudgetAmt);
void FillBlockPayee(CMutableTransaction& txCoinbase, CMutableTransaction& txCoinstake, const CBlockIndex* pindexPrev, bool fProofOfStake);
/**
* Check coinbase output value for blocks after v6.0 enforcement.
* It must pay the masternode for regular blocks and a proposal during superblocks.
*/
bool IsCoinbaseValueValid(const CTransactionRef& tx, CAmount nBudgetAmt, CValidationState& _state);
void DumpMasternodePayments();
/** Save Masternode Payment Data (mnpayments.dat)
*/
class CMasternodePaymentDB
{
private:
fs::path pathDB;
std::string strMagicMessage;
public:
enum ReadResult {
Ok,
FileError,
HashReadError,
IncorrectHash,
IncorrectMagicMessage,
IncorrectMagicNumber,
IncorrectFormat
};
CMasternodePaymentDB();
bool Write(const CMasternodePayments& objToSave);
ReadResult Read(CMasternodePayments& objToLoad);
};
class CMasternodePayee
{
public:
CScript scriptPubKey;
int nVotes;
CMasternodePayee()
{
scriptPubKey = CScript();
nVotes = 0;
}
CMasternodePayee(CScript payee, int nVotesIn)
{
scriptPubKey = payee;
nVotes = nVotesIn;
}
SERIALIZE_METHODS(CMasternodePayee, obj) { READWRITE(obj.scriptPubKey, obj.nVotes); }
};
// Keep track of votes for payees from masternodes
class CMasternodeBlockPayees
{
public:
int nBlockHeight;
std::vector<CMasternodePayee> vecPayments;
CMasternodeBlockPayees()
{
nBlockHeight = 0;
vecPayments.clear();
}
explicit CMasternodeBlockPayees(int nBlockHeightIn)
{
nBlockHeight = nBlockHeightIn;
vecPayments.clear();
}
void AddPayee(const CScript& payeeIn, int nIncrement)
{
LOCK(cs_vecPayments);
for (CMasternodePayee& payee : vecPayments) {
if (payee.scriptPubKey == payeeIn) {
payee.nVotes += nIncrement;
return;
}
}
CMasternodePayee c(payeeIn, nIncrement);
vecPayments.push_back(c);
}
bool GetPayee(CScript& payee) const
{
LOCK(cs_vecPayments);
int nVotes = -1;
for (const CMasternodePayee& p : vecPayments) {
if (p.nVotes > nVotes) {
payee = p.scriptPubKey;
nVotes = p.nVotes;
}
}
return (nVotes > -1);
}
bool HasPayeeWithVotes(const CScript& payee, int nVotesReq)
{
LOCK(cs_vecPayments);
for (CMasternodePayee& p : vecPayments) {
if (p.nVotes >= nVotesReq && p.scriptPubKey == payee) return true;
}
return false;
}
bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight);
std::string GetRequiredPaymentsString();
SERIALIZE_METHODS(CMasternodeBlockPayees, obj) { READWRITE(obj.nBlockHeight, obj.vecPayments); }
};
// for storing the winning payments
class CMasternodePaymentWinner : public CSignedMessage
{
public:
CTxIn vinMasternode;
int nBlockHeight;
CScript payee;
CMasternodePaymentWinner() :
CSignedMessage(),
vinMasternode(),
nBlockHeight(0),
payee()
{}
CMasternodePaymentWinner(const CTxIn& vinIn, int nHeight):
CSignedMessage(),
vinMasternode(vinIn),
nBlockHeight(nHeight),
payee()
{}
uint256 GetHash() const;
// override CSignedMessage functions
uint256 GetSignatureHash() const override { return GetHash(); }
std::string GetStrMessage() const override;
CTxIn GetVin() const { return vinMasternode; };
bool IsValid(CNode* pnode, CValidationState& state, int chainHeight);
void Relay();
void AddPayee(const CScript& payeeIn)
{
payee = payeeIn;
}
SERIALIZE_METHODS(CMasternodePaymentWinner, obj) { READWRITE(obj.vinMasternode, obj.nBlockHeight, obj.payee, obj.vchSig, obj.nMessVersion); }
std::string ToString()
{
std::string ret = "";
ret += vinMasternode.ToString();
ret += ", " + std::to_string(nBlockHeight);
ret += ", " + HexStr(payee);
ret += ", " + std::to_string((int)vchSig.size());
return ret;
}
};
//
// Masternode Payments Class
// Keeps track of who should get paid for which blocks
//
class CMasternodePayments : public CValidationInterface
{
private:
int nLastBlockHeight;
public:
std::map<uint256, CMasternodePaymentWinner> mapMasternodePayeeVotes;
std::map<int, CMasternodeBlockPayees> mapMasternodeBlocks;
CMasternodePayments()
{
nLastBlockHeight = 0;
}
void Clear()
{
LOCK2(cs_mapMasternodeBlocks, cs_mapMasternodePayeeVotes);
mapMasternodeBlocks.clear();
mapMasternodePayeeVotes.clear();
}
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
void AddWinningMasternode(CMasternodePaymentWinner& winner);
void ProcessBlock(int nBlockHeight);
void Sync(CNode* node, int nCountNeeded);
void CleanPaymentList(int mnCount, int nHeight);
// get the masternode payment outs for block built on top of pindexPrev
bool GetMasternodeTxOuts(const CBlockIndex* pindexPrev, std::vector<CTxOut>& voutMasternodePaymentsRet) const;
// can be removed after transition to DMN
bool GetLegacyMasternodeTxOut(int nHeight, std::vector<CTxOut>& voutMasternodePaymentsRet) const;
bool GetBlockPayee(int nBlockHeight, CScript& payee) const;
bool IsTransactionValid(const CTransaction& txNew, const CBlockIndex* pindexPrev);
bool IsScheduled(const CMasternode& mn, int nNotBlockHeight);
bool ProcessMNWinner(CMasternodePaymentWinner& winner, CNode* pfrom, CValidationState& state);
bool ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDataStream& vRecv, CValidationState& state);
std::string GetRequiredPaymentsString(int nBlockHeight);
void FillBlockPayee(CMutableTransaction& txCoinbase, CMutableTransaction& txCoinstake, const CBlockIndex* pindexPrev, bool fProofOfStake) const;
std::string ToString() const;
SERIALIZE_METHODS(CMasternodePayments, obj) { READWRITE(obj.mapMasternodePayeeVotes, obj.mapMasternodeBlocks); }
private:
// keep track of last voted height for mnw signers
std::map<COutPoint, int> mapMasternodesLastVote; //prevout, nBlockHeight
bool CanVote(const COutPoint& outMasternode, int nBlockHeight) const;
void RecordWinnerVote(const COutPoint& outMasternode, int nBlockHeight);
};
#endif // PIVX_MASTERNODE_PAYMENTS_H