Skip to content

Commit be1ca25

Browse files
authored
refactor: hide some payments methods, replace a class to namespace (#5494)
## Issue being fixed or feature implemented Here's TODO that seems out-dated ``` /// TODO: all 4 functions do not belong here really, they should be refactored/moved somewhere (main.cpp ?) ``` This changes are extracted from this PR: #5342 ## What was done? This changes hides some methods from global namespace (making local static function), hiding other functions to the namespace ## How Has This Been Tested? Run unit/functional tests ## Breaking Changes N/A ## Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone
1 parent e0252bb commit be1ca25

File tree

5 files changed

+105
-111
lines changed

5 files changed

+105
-111
lines changed

src/masternode/payments.cpp

Lines changed: 92 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,90 @@
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-
80162
bool 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

166248
bool 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

219301
void 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

src/masternode/payments.h

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,27 @@
1111
#include <vector>
1212

1313
class CGovernanceManager;
14-
class CMasternodePayments;
1514
class CBlock;
1615
class CTransaction;
1716
struct CMutableTransaction;
1817
class CSporkManager;
1918
class CTxOut;
2019
class CMasternodeSync;
2120

22-
/// TODO: all 4 functions do not belong here really, they should be refactored/moved somewhere (main.cpp ?)
23-
bool IsBlockValueValid(const CSporkManager& sporkManager, CGovernanceManager& governanceManager, const CMasternodeSync& mn_sync,
24-
const CBlock& block, int nBlockHeight, CAmount blockReward, std::string& strErrorRet);
25-
bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
26-
const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
27-
void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
28-
CMutableTransaction& txNew, int nBlockHeight, CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet, std::vector<CTxOut>& voutSuperblockPaymentsRet);
29-
30-
extern CMasternodePayments mnpayments;
31-
3221
//
3322
// Masternode Payments Class
3423
// Keeps track of who should get paid for which blocks
3524
//
3625

37-
class CMasternodePayments
26+
namespace CMasternodePayments
3827
{
39-
public:
40-
static bool GetBlockTxOuts(int nBlockHeight, CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet);
41-
static bool IsTransactionValid(const CTransaction& txNew, int nBlockHeight, CAmount blockReward);
42-
43-
static bool GetMasternodeTxOuts(int nBlockHeight, CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet);
44-
};
28+
bool IsBlockValueValid(const CSporkManager& sporkManager, CGovernanceManager& governanceManager, const CMasternodeSync& mn_sync,
29+
const CBlock& block, const int nBlockHeight, const CAmount blockReward, std::string& strErrorRet);
30+
bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
31+
const CTransaction& txNew, const int nBlockHeight, const CAmount blockReward);
32+
void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
33+
CMutableTransaction& txNew, const int nBlockHeight, const CAmount blockReward,
34+
std::vector<CTxOut>& voutMasternodePaymentsRet, std::vector<CTxOut>& voutSuperblockPaymentsRet);
35+
} // namespace CMasternodePayments
4536

4637
#endif // BITCOIN_MASTERNODE_PAYMENTS_H

src/miner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
231231

232232
// Update coinbase transaction with additional info about masternode and governance payments,
233233
// get some info back to pass to getblocktemplate
234-
FillBlockPayments(spork_manager, governance_manager, coinbaseTx, nHeight, blockReward, pblocktemplate->voutMasternodePayments, pblocktemplate->voutSuperblockPayments);
234+
CMasternodePayments::FillBlockPayments(spork_manager, governance_manager, coinbaseTx, nHeight, blockReward, pblocktemplate->voutMasternodePayments, pblocktemplate->voutSuperblockPayments);
235235

236236
pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
237237
pblocktemplate->vTxFees[0] = -nFees;

src/rpc/masternode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ static UniValue masternode_payments(const JSONRPCRequest& request)
459459
std::vector<CTxOut> voutMasternodePayments, voutDummy;
460460
CMutableTransaction dummyTx;
461461
CAmount blockReward = nBlockFees + GetBlockSubsidy(pindex->pprev->nBits, pindex->pprev->nHeight, Params().GetConsensus());
462-
FillBlockPayments(*sporkManager, *governance, dummyTx, pindex->nHeight, blockReward, voutMasternodePayments, voutDummy);
462+
CMasternodePayments::FillBlockPayments(*sporkManager, *governance, dummyTx, pindex->nHeight, blockReward, voutMasternodePayments, voutDummy);
463463

464464
UniValue blockObj(UniValue::VOBJ);
465465
CAmount payedPerBlock{0};

src/validation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,7 +2463,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
24632463
int64_t nTime5_2 = GetTimeMicros(); nTimeSubsidy += nTime5_2 - nTime5_1;
24642464
LogPrint(BCLog::BENCHMARK, " - GetBlockSubsidy: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5_2 - nTime5_1), nTimeSubsidy * MICRO, nTimeSubsidy * MILLI / nBlocksTotal);
24652465

2466-
if (!IsBlockValueValid(*sporkManager, *governance, *::masternodeSync, block, pindex->nHeight, blockReward, strError)) {
2466+
if (!CMasternodePayments::IsBlockValueValid(*sporkManager, *governance, *::masternodeSync, block, pindex->nHeight, blockReward, strError)) {
24672467
// NOTE: Do not punish, the node might be missing governance data
24682468
LogPrintf("ERROR: ConnectBlock(DASH): %s\n", strError);
24692469
return state.Invalid(BlockValidationResult::BLOCK_RESULT_UNSET, "bad-cb-amount");
@@ -2472,7 +2472,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
24722472
int64_t nTime5_3 = GetTimeMicros(); nTimeValueValid += nTime5_3 - nTime5_2;
24732473
LogPrint(BCLog::BENCHMARK, " - IsBlockValueValid: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5_3 - nTime5_2), nTimeValueValid * MICRO, nTimeValueValid * MILLI / nBlocksTotal);
24742474

2475-
if (!IsBlockPayeeValid(*sporkManager, *governance, *block.vtx[0], pindex->nHeight, blockReward)) {
2475+
if (!CMasternodePayments::IsBlockPayeeValid(*sporkManager, *governance, *block.vtx[0], pindex->nHeight, blockReward)) {
24762476
// NOTE: Do not punish, the node might be missing governance data
24772477
LogPrintf("ERROR: ConnectBlock(DASH): couldn't find masternode or superblock payments\n");
24782478
return state.Invalid(BlockValidationResult::BLOCK_RESULT_UNSET, "bad-cb-payee");

0 commit comments

Comments
 (0)