2222
2323#include < string>
2424
25- CMasternodePayments mnpayments;
25+ /* *
26+ * GetMasternodeTxOuts
27+ *
28+ * Get masternode payment tx outputs
29+ */
30+
31+ static bool GetBlockTxOuts (const int nBlockHeight, const CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet)
32+ {
33+ voutMasternodePaymentsRet.clear ();
34+
35+ const CBlockIndex* pindex = WITH_LOCK (cs_main, return ::ChainActive ()[nBlockHeight - 1 ]);
36+ auto dmnPayee = deterministicMNManager->GetListForBlock (pindex).GetMNPayee (pindex);
37+ if (!dmnPayee) {
38+ return false ;
39+ }
40+
41+ CAmount operatorReward = 0 ;
42+ CAmount masternodeReward = GetMasternodePayment (nBlockHeight, blockReward, Params ().GetConsensus ().BRRHeight );
43+
44+ if (dmnPayee->nOperatorReward != 0 && dmnPayee->pdmnState ->scriptOperatorPayout != CScript ()) {
45+ // This calculation might eventually turn out to result in 0 even if an operator reward percentage is given.
46+ // This will however only happen in a few years when the block rewards drops very low.
47+ operatorReward = (masternodeReward * dmnPayee->nOperatorReward ) / 10000 ;
48+ masternodeReward -= operatorReward;
49+ }
50+
51+ if (masternodeReward > 0 ) {
52+ voutMasternodePaymentsRet.emplace_back (masternodeReward, dmnPayee->pdmnState ->scriptPayout );
53+ }
54+ if (operatorReward > 0 ) {
55+ voutMasternodePaymentsRet.emplace_back (operatorReward, dmnPayee->pdmnState ->scriptOperatorPayout );
56+ }
57+
58+ return true ;
59+ }
60+
61+
62+ static bool GetMasternodeTxOuts (const int nBlockHeight, const CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet)
63+ {
64+ // make sure it's not filled yet
65+ voutMasternodePaymentsRet.clear ();
66+
67+ if (!GetBlockTxOuts (nBlockHeight, blockReward, voutMasternodePaymentsRet)) {
68+ LogPrintf (" CMasternodePayments::%s -- no payee (deterministic masternode list empty)\n " , __func__);
69+ return false ;
70+ }
71+
72+ for (const auto & txout : voutMasternodePaymentsRet) {
73+ CTxDestination dest;
74+ ExtractDestination (txout.scriptPubKey , dest);
75+
76+ LogPrintf (" CMasternodePayments::%s -- Masternode payment %lld to %s\n " , __func__, txout.nValue , EncodeDestination (dest));
77+ }
78+
79+ return true ;
80+ }
81+
82+ static bool IsTransactionValid (const CTransaction& txNew, const int nBlockHeight, const CAmount blockReward)
83+ {
84+ if (!deterministicMNManager->IsDIP3Enforced (nBlockHeight)) {
85+ // can't verify historical blocks here
86+ return true ;
87+ }
2688
27- bool IsOldBudgetBlockValueValid (const CMasternodeSync& mn_sync, const CBlock& block, int nBlockHeight, CAmount blockReward, std::string& strErrorRet) {
89+ std::vector<CTxOut> voutMasternodePayments;
90+ if (!GetBlockTxOuts (nBlockHeight, blockReward, voutMasternodePayments)) {
91+ LogPrintf (" CMasternodePayments::%s -- ERROR failed to get payees for block at height %s\n " , __func__, nBlockHeight);
92+ return true ;
93+ }
94+
95+ for (const auto & txout : voutMasternodePayments) {
96+ bool found = ranges::any_of (txNew.vout , [&txout](const auto & txout2) {return txout == txout2;});
97+ if (!found) {
98+ CTxDestination dest;
99+ if (!ExtractDestination (txout.scriptPubKey , dest))
100+ assert (false );
101+ LogPrintf (" CMasternodePayments::%s -- ERROR failed to find expected payee %s in block at height %s\n " , __func__, EncodeDestination (dest), nBlockHeight);
102+ return false ;
103+ }
104+ }
105+ return true ;
106+ }
107+
108+ static bool IsOldBudgetBlockValueValid (const CMasternodeSync& mn_sync, const CBlock& block, const int nBlockHeight, const CAmount blockReward, std::string& strErrorRet) {
28109 const Consensus::Params& consensusParams = Params ().GetConsensus ();
29110 bool isBlockRewardValueMet = (block.vtx [0 ]->GetValueOut () <= blockReward);
30111
@@ -66,6 +147,8 @@ bool IsOldBudgetBlockValueValid(const CMasternodeSync& mn_sync, const CBlock& bl
66147 return isBlockRewardValueMet;
67148}
68149
150+ namespace CMasternodePayments {
151+
69152/* *
70153* IsBlockValueValid
71154*
@@ -76,9 +159,8 @@ bool IsOldBudgetBlockValueValid(const CMasternodeSync& mn_sync, const CBlock& bl
76159* - Other blocks are 10% lower in outgoing value, so in total, no extra coins are created
77160* - When non-superblocks are detected, the normal schedule should be maintained
78161*/
79-
80162bool IsBlockValueValid (const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
81- const CMasternodeSync& mn_sync, const CBlock& block, int nBlockHeight, CAmount blockReward, std::string& strErrorRet)
163+ const CMasternodeSync& mn_sync, const CBlock& block, const int nBlockHeight, const CAmount blockReward, std::string& strErrorRet)
82164{
83165 const Consensus::Params& consensusParams = Params ().GetConsensus ();
84166 bool isBlockRewardValueMet = (block.vtx [0 ]->GetValueOut () <= blockReward);
@@ -164,7 +246,7 @@ bool IsBlockValueValid(const CSporkManager& sporkManager, CGovernanceManager& go
164246}
165247
166248bool IsBlockPayeeValid (const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
167- const CTransaction& txNew, int nBlockHeight, CAmount blockReward)
249+ const CTransaction& txNew, const int nBlockHeight, const CAmount blockReward)
168250{
169251 if (fDisableGovernance ) {
170252 // there is no budget data to use to check anything, let's just accept the longest chain
@@ -207,7 +289,7 @@ bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& go
207289 }
208290
209291 // Check for correct masternode payment
210- if (CMasternodePayments:: IsTransactionValid (txNew, nBlockHeight, blockReward)) {
292+ if (IsTransactionValid (txNew, nBlockHeight, blockReward)) {
211293 LogPrint (BCLog::MNPAYMENTS, " %s -- Valid masternode payment at height %d: %s" , __func__, nBlockHeight, txNew.ToString ()); /* Continued */
212294 return true ;
213295 }
@@ -217,7 +299,8 @@ bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& go
217299}
218300
219301void FillBlockPayments (const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
220- CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet, std::vector<CTxOut>& voutSuperblockPaymentsRet)
302+ CMutableTransaction& txNew, const int nBlockHeight, const CAmount blockReward,
303+ std::vector<CTxOut>& voutMasternodePaymentsRet, std::vector<CTxOut>& voutSuperblockPaymentsRet)
221304{
222305 // only create superblocks if spork is enabled AND if superblock is actually triggered
223306 // (height should be validated inside)
@@ -226,7 +309,7 @@ void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& go
226309 CSuperblockManager::GetSuperblockPayments (governanceManager, nBlockHeight, voutSuperblockPaymentsRet);
227310 }
228311
229- if (!CMasternodePayments:: GetMasternodeTxOuts (nBlockHeight, blockReward, voutMasternodePaymentsRet)) {
312+ if (!GetMasternodeTxOuts (nBlockHeight, blockReward, voutMasternodePaymentsRet)) {
230313 LogPrint (BCLog::MNPAYMENTS, " %s -- no masternode to pay (MN list probably empty)\n " , __func__);
231314 }
232315
@@ -246,84 +329,4 @@ void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& go
246329 nBlockHeight, blockReward, voutMasternodeStr, txNew.ToString ());
247330}
248331
249- /* *
250- * GetMasternodeTxOuts
251- *
252- * Get masternode payment tx outputs
253- */
254-
255- bool CMasternodePayments::GetMasternodeTxOuts (int nBlockHeight, CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet)
256- {
257- // make sure it's not filled yet
258- voutMasternodePaymentsRet.clear ();
259-
260- if (!GetBlockTxOuts (nBlockHeight, blockReward, voutMasternodePaymentsRet)) {
261- LogPrintf (" CMasternodePayments::%s -- no payee (deterministic masternode list empty)\n " , __func__);
262- return false ;
263- }
264-
265- for (const auto & txout : voutMasternodePaymentsRet) {
266- CTxDestination dest;
267- ExtractDestination (txout.scriptPubKey , dest);
268-
269- LogPrintf (" CMasternodePayments::%s -- Masternode payment %lld to %s\n " , __func__, txout.nValue , EncodeDestination (dest));
270- }
271-
272- return true ;
273- }
274-
275- bool CMasternodePayments::GetBlockTxOuts (int nBlockHeight, CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet)
276- {
277- voutMasternodePaymentsRet.clear ();
278-
279- const CBlockIndex* pindex = WITH_LOCK (cs_main, return ::ChainActive ()[nBlockHeight - 1 ]);
280- auto dmnPayee = deterministicMNManager->GetListForBlock (pindex).GetMNPayee (pindex);
281- if (!dmnPayee) {
282- return false ;
283- }
284-
285- CAmount operatorReward = 0 ;
286- CAmount masternodeReward = GetMasternodePayment (nBlockHeight, blockReward, Params ().GetConsensus ().BRRHeight );
287-
288- if (dmnPayee->nOperatorReward != 0 && dmnPayee->pdmnState ->scriptOperatorPayout != CScript ()) {
289- // This calculation might eventually turn out to result in 0 even if an operator reward percentage is given.
290- // This will however only happen in a few years when the block rewards drops very low.
291- operatorReward = (masternodeReward * dmnPayee->nOperatorReward ) / 10000 ;
292- masternodeReward -= operatorReward;
293- }
294-
295- if (masternodeReward > 0 ) {
296- voutMasternodePaymentsRet.emplace_back (masternodeReward, dmnPayee->pdmnState ->scriptPayout );
297- }
298- if (operatorReward > 0 ) {
299- voutMasternodePaymentsRet.emplace_back (operatorReward, dmnPayee->pdmnState ->scriptOperatorPayout );
300- }
301-
302- return true ;
303- }
304-
305- bool CMasternodePayments::IsTransactionValid (const CTransaction& txNew, int nBlockHeight, CAmount blockReward)
306- {
307- if (!deterministicMNManager->IsDIP3Enforced (nBlockHeight)) {
308- // can't verify historical blocks here
309- return true ;
310- }
311-
312- std::vector<CTxOut> voutMasternodePayments;
313- if (!GetBlockTxOuts (nBlockHeight, blockReward, voutMasternodePayments)) {
314- LogPrintf (" CMasternodePayments::%s -- ERROR failed to get payees for block at height %s\n " , __func__, nBlockHeight);
315- return true ;
316- }
317-
318- for (const auto & txout : voutMasternodePayments) {
319- bool found = ranges::any_of (txNew.vout , [&txout](const auto & txout2) {return txout == txout2;});
320- if (!found) {
321- CTxDestination dest;
322- if (!ExtractDestination (txout.scriptPubKey , dest))
323- assert (false );
324- LogPrintf (" CMasternodePayments::%s -- ERROR failed to find expected payee %s in block at height %s\n " , __func__, EncodeDestination (dest), nBlockHeight);
325- return false ;
326- }
327- }
328- return true ;
329- }
332+ } // namespace CMasternodePayments
0 commit comments