From fadd425687fa2597784a09352be6ab66b755d630 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Thu, 24 Aug 2023 18:05:42 +0700 Subject: [PATCH] refactor: rid-of magic constant by introducing MasternodePayments::PlatformShare --- src/masternode/payments.cpp | 13 +++++++++++-- src/masternode/payments.h | 7 +++++++ src/miner.cpp | 2 +- src/test/block_reward_reallocation_tests.cpp | 5 +++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/masternode/payments.cpp b/src/masternode/payments.cpp index 4203ccc23049e..7da6cea8a9900 100644 --- a/src/masternode/payments.cpp +++ b/src/masternode/payments.cpp @@ -22,6 +22,7 @@ #include #include +#include #include /** @@ -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); @@ -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 diff --git a/src/masternode/payments.h b/src/masternode/payments.h index 576851fbde311..73f5fd367e1cc 100644 --- a/src/masternode/payments.h +++ b/src/masternode/payments.h @@ -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& voutMasternodePaymentsRet, std::vector& 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 diff --git a/src/miner.cpp b/src/miner.cpp index a29a3f6bccbd4..4043ff44d9069 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -235,7 +235,7 @@ std::unique_ptr 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); } diff --git a/src/test/block_reward_reallocation_tests.cpp b/src/test/block_reward_reallocation_tests.cpp index b4ec14b5eeea4..2b587acf4bf6f 100644 --- a/src/test/block_reward_reallocation_tests.cpp +++ b/src/test/block_reward_reallocation_tests.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -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; @@ -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);