Skip to content

Commit

Permalink
refactor: rid-of magic constant by introducing MasternodePayments::Pl…
Browse files Browse the repository at this point in the history
…atformShare
  • Loading branch information
knst committed Aug 28, 2023
1 parent 2444540 commit fadd425
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/masternode/payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <validation.h>
#include <evo/creditpool.h>

#include <cassert>
#include <string>

/**
Expand All @@ -40,10 +41,9 @@ static bool GetBlockTxOuts(const int nBlockHeight, const CAmount blockReward, st
bool fMNRewardReallocated = llmq::utils::IsMNRewardReallocationActive(pindex);

if (fMNRewardReallocated) {
const CAmount platformReward = masternodeReward * 0.375;
const CAmount platformReward = MasternodePayments::PlatformShare(masternodeReward);
masternodeReward -= platformReward;

assert(MoneyRange(platformReward));
assert(MoneyRange(masternodeReward));

LogPrint(BCLog::MNPAYMENTS, "CMasternodePayments::%s -- MN reward %lld reallocated to credit pool\n", __func__, platformReward);
Expand Down Expand Up @@ -345,4 +345,13 @@ void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& go
nBlockHeight, blockReward, voutMasternodeStr, txNew.ToString());
}

CAmount PlatformShare(const CAmount reward)
{
constexpr double platformShare = 0.375;
const CAmount platformReward = reward * platformShare;
assert(MoneyRange(platformReward));

return platformReward;
}

} // namespace MasternodePayments
7 changes: 7 additions & 0 deletions src/masternode/payments.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& go
void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
CMutableTransaction& txNew, const int nBlockHeight, const CAmount blockReward,
std::vector<CTxOut>& voutMasternodePaymentsRet, std::vector<CTxOut>& voutSuperblockPaymentsRet);

/**
* this helper returns amount that should be reallocated to platform
* it is calculated based on total amount of Masternode rewards (not block reward)
*/
CAmount PlatformShare(const CAmount masternodeReward);

} // namespace MasternodePayments

#endif // BITCOIN_MASTERNODE_PAYMENTS_H
2 changes: 1 addition & 1 deletion src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
bool fMNRewardReallocated = llmq::utils::IsMNRewardReallocationActive(pindexPrev);
if (fMNRewardReallocated) {
const CAmount masternodeReward = GetMasternodePayment(nHeight, blockReward, Params().GetConsensus().BRRHeight);
const CAmount reallocedReward = masternodeReward * 0.375;
const CAmount reallocedReward = MasternodePayments::PlatformShare(masternodeReward);
LogPrint(BCLog::MNPAYMENTS, "%s: add MN reward %lld (%lld) to credit pool\n", __func__, masternodeReward, reallocedReward);
creditPoolDiff->AddRewardRealloced(reallocedReward);
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/block_reward_reallocation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <llmq/context.h>
#include <llmq/instantsend.h>
#include <llmq/utils.h>
#include <masternode/payments.h>
#include <util/enumerate.h>
#include <util/irange.h>

Expand Down Expand Up @@ -257,7 +258,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS

bool isMNRewardReallocated = llmq::utils::IsMNRewardReallocationActive(::ChainActive().Tip());
if (isMNRewardReallocated) {
CAmount platform_payment = 0.375 * masternode_payment;
const CAmount platform_payment = MasternodePayments::PlatformShare(masternode_payment);
masternode_payment -= platform_payment;
}
size_t payment_index = isMNRewardReallocated ? 1 : 0;
Expand All @@ -269,7 +270,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS
// Reward split should reach ~60/40 after reallocation is done
LOCK(cs_main);
CAmount masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500);
CAmount platform_payment = 0.375 * masternode_payment;
const CAmount platform_payment = MasternodePayments::PlatformShare(masternode_payment);
masternode_payment -= platform_payment;
const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey);
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx[0]->GetValueOut(), 9491484944);
Expand Down

0 comments on commit fadd425

Please sign in to comment.