From 57f38e04a53cbf27d2d369ff9f8c76ce498094b5 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Tue, 1 Aug 2023 16:55:09 +0700 Subject: [PATCH 1/4] refactor: move common code to GetBlockSubsidyPrev for sake of simplification --- src/index/coinstatsindex.cpp | 4 ++-- src/miner.cpp | 2 +- src/rpc/blockchain.cpp | 2 +- src/validation.cpp | 11 ++++++++++- src/validation.h | 1 + 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp index 22330661329b..597a51e7223a 100644 --- a/src/index/coinstatsindex.cpp +++ b/src/index/coinstatsindex.cpp @@ -107,7 +107,7 @@ CoinStatsIndex::CoinStatsIndex(size_t n_cache_size, bool f_memory, bool f_wipe) bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex) { CBlockUndo block_undo; - const CAmount block_subsidy{pindex->nHeight > 0 ? GetBlockSubsidy(pindex->pprev->nBits, pindex->pprev->nHeight, Params().GetConsensus()) : Params().GenesisBlock().vtx[0]->GetValueOut()}; + const CAmount block_subsidy{GetBlockSubsidyPrev(pindex->pprev, Params().GetConsensus())}; m_total_subsidy += block_subsidy; // Ignore genesis block @@ -394,7 +394,7 @@ bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex CBlockUndo block_undo; std::pair read_out; - const CAmount block_subsidy{pindex->nHeight > 0 ? GetBlockSubsidy(pindex->pprev->nBits, pindex->pprev->nHeight, Params().GetConsensus()) : Params().GenesisBlock().vtx[0]->GetValueOut()}; + const CAmount block_subsidy{GetBlockSubsidyPrev(pindex->pprev, Params().GetConsensus())}; m_total_subsidy -= block_subsidy; // Ignore genesis block diff --git a/src/miner.cpp b/src/miner.cpp index 208058230b2d..119f2bbad3ed 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -190,7 +190,7 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn; // NOTE: unlike in bitcoin, we need to pass PREVIOUS block height here - CAmount blockReward = nFees + GetBlockSubsidy(pindexPrev->nBits, pindexPrev->nHeight, Params().GetConsensus()); + CAmount blockReward = nFees + GetBlockSubsidyPrev(pindexPrev, Params().GetConsensus()); // Compute regular coinbase transaction. coinbaseTx.vout[0].nValue = blockReward; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index f24e968f240d..08ddce65a1b1 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2386,7 +2386,7 @@ static UniValue getblockstats(const JSONRPCRequest& request) ret_all.pushKV("minfeerate", (minfeerate == MAX_MONEY) ? 0 : minfeerate); ret_all.pushKV("mintxsize", mintxsize == MaxBlockSize() ? 0 : mintxsize); ret_all.pushKV("outs", outputs); - ret_all.pushKV("subsidy", pindex->pprev ? GetBlockSubsidy(pindex->pprev->nBits, pindex->pprev->nHeight, Params().GetConsensus()) : 50 * COIN); + ret_all.pushKV("subsidy", GetBlockSubsidyPrev(pindex->pprev, Params().GetConsensus())); ret_all.pushKV("time", pindex->GetBlockTime()); ret_all.pushKV("total_out", total_out); ret_all.pushKV("total_size", total_size); diff --git a/src/validation.cpp b/src/validation.cpp index 0c39f31dd2c6..b116c96cb21c 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1112,6 +1112,9 @@ double ConvertBitsToDouble(unsigned int nBits) NOTE: unlike bitcoin we are using PREVIOUS block height here, might be a good idea to change this to use prev bits but current height to avoid confusion. + + Due to difference in interface, prefer to use GetBlockSubsidyPrev() + that have a check for nullptr for pIndexPrev inside */ CAmount GetBlockSubsidy(int nPrevBits, int nPrevHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly) { @@ -1163,6 +1166,12 @@ CAmount GetBlockSubsidy(int nPrevBits, int nPrevHeight, const Consensus::Params& return fSuperblockPartOnly ? nSuperblockPart : nSubsidy - nSuperblockPart; } +CAmount GetBlockSubsidyPrev(const CBlockIndex* const pindexPrev, const Consensus::Params& consensusParams) +{ + if (pindexPrev == nullptr) return Params().GenesisBlock().vtx[0]->GetValueOut(); + return GetBlockSubsidy(pindexPrev->nBits, pindexPrev->nHeight, consensusParams); +} + CAmount GetMasternodePayment(int nHeight, CAmount blockValue, int nReallocActivationHeight) { CAmount ret = blockValue/5; // start at 20% @@ -2401,7 +2410,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, // DASH : MODIFIED TO CHECK MASTERNODE PAYMENTS AND SUPERBLOCKS // TODO: resync data (both ways?) and try to reprocess this block later. - CAmount blockReward = nFees + GetBlockSubsidy(pindex->pprev->nBits, pindex->pprev->nHeight, m_params.GetConsensus()); + CAmount blockReward = nFees + GetBlockSubsidyPrev(pindex->pprev, m_params.GetConsensus()); std::string strError = ""; int64_t nTime5_2 = GetTimeMicros(); nTimeSubsidy += nTime5_2 - nTime5_1; diff --git a/src/validation.h b/src/validation.h index 518954a20bb9..ac0bff9e9b60 100644 --- a/src/validation.h +++ b/src/validation.h @@ -207,6 +207,7 @@ CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMe double ConvertBitsToDouble(unsigned int nBits); CAmount GetBlockSubsidy(int nBits, int nHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly = false); +CAmount GetBlockSubsidyPrev(const CBlockIndex* const pindexPrev, const Consensus::Params& consensusParams); CAmount GetMasternodePayment(int nHeight, CAmount blockValue, int nReallocActivationHeight = std::numeric_limits::max() /* not activated */); /** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */ From 4862b5f3ad3ee66264b5a71ec1b2ec84a41a0654 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Thu, 3 Aug 2023 16:56:53 +0300 Subject: [PATCH 2/4] refactor: GetBlockSubsidy->GetBlockSubsidyInner, GetBlockSubsidyPrev(pindexPrev)->GetBlockSubsidy(pindex) --- src/bench/duplicate_inputs.cpp | 2 +- src/governance/classes.cpp | 2 +- src/index/coinstatsindex.cpp | 4 ++-- src/miner.cpp | 2 +- src/rpc/blockchain.cpp | 2 +- src/rpc/masternode.cpp | 2 +- src/test/block_reward_reallocation_tests.cpp | 12 ++++++------ src/test/subsidy_tests.cpp | 16 ++++++++-------- src/validation.cpp | 12 ++++++------ src/validation.h | 4 ++-- 10 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/bench/duplicate_inputs.cpp b/src/bench/duplicate_inputs.cpp index 4604789c5fc3..7e1a18c85ce2 100644 --- a/src/bench/duplicate_inputs.cpp +++ b/src/bench/duplicate_inputs.cpp @@ -38,7 +38,7 @@ static void DuplicateInputs(benchmark::Bench& bench) coinbaseTx.vin[0].prevout.SetNull(); coinbaseTx.vout.resize(1); coinbaseTx.vout[0].scriptPubKey = SCRIPT_PUB; - coinbaseTx.vout[0].nValue = GetBlockSubsidy(block.nBits, nHeight, chainparams.GetConsensus()); + coinbaseTx.vout[0].nValue = GetBlockSubsidyInner(block.nBits, nHeight, chainparams.GetConsensus()); coinbaseTx.vin[0].scriptSig = CScript() << nHeight << OP_0; naughtyTx.vout.resize(1); diff --git a/src/governance/classes.cpp b/src/governance/classes.cpp index 34a3e27294d8..e1e8023210dd 100644 --- a/src/governance/classes.cpp +++ b/src/governance/classes.cpp @@ -486,7 +486,7 @@ CAmount CSuperblock::GetPaymentsLimit(int nBlockHeight) // min subsidy for high diff networks and vice versa int nBits = consensusParams.fPowAllowMinDifficultyBlocks ? UintToArith256(consensusParams.powLimit).GetCompact() : 1; // some part of all blocks issued during the cycle goes to superblock, see GetBlockSubsidy - CAmount nSuperblockPartOfSubsidy = GetBlockSubsidy(nBits, nBlockHeight - 1, consensusParams, true); + CAmount nSuperblockPartOfSubsidy = GetBlockSubsidyInner(nBits, nBlockHeight - 1, consensusParams, true); CAmount nPaymentsLimit = nSuperblockPartOfSubsidy * consensusParams.nSuperblockCycle; LogPrint(BCLog::GOBJECT, "CSuperblock::GetPaymentsLimit -- Valid superblock height %d, payments max %lld\n", nBlockHeight, nPaymentsLimit); diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp index 597a51e7223a..8321b7f17b2b 100644 --- a/src/index/coinstatsindex.cpp +++ b/src/index/coinstatsindex.cpp @@ -107,7 +107,7 @@ CoinStatsIndex::CoinStatsIndex(size_t n_cache_size, bool f_memory, bool f_wipe) bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex) { CBlockUndo block_undo; - const CAmount block_subsidy{GetBlockSubsidyPrev(pindex->pprev, Params().GetConsensus())}; + const CAmount block_subsidy{GetBlockSubsidy(pindex, Params().GetConsensus())}; m_total_subsidy += block_subsidy; // Ignore genesis block @@ -394,7 +394,7 @@ bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex CBlockUndo block_undo; std::pair read_out; - const CAmount block_subsidy{GetBlockSubsidyPrev(pindex->pprev, Params().GetConsensus())}; + const CAmount block_subsidy{GetBlockSubsidy(pindex, Params().GetConsensus())}; m_total_subsidy -= block_subsidy; // Ignore genesis block diff --git a/src/miner.cpp b/src/miner.cpp index 119f2bbad3ed..858d039fe40c 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -190,7 +190,7 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc coinbaseTx.vout[0].scriptPubKey = scriptPubKeyIn; // NOTE: unlike in bitcoin, we need to pass PREVIOUS block height here - CAmount blockReward = nFees + GetBlockSubsidyPrev(pindexPrev, Params().GetConsensus()); + CAmount blockReward = nFees + GetBlockSubsidyInner(pindexPrev->nBits, pindexPrev->nHeight, Params().GetConsensus()); // Compute regular coinbase transaction. coinbaseTx.vout[0].nValue = blockReward; diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 08ddce65a1b1..99c2766744af 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2386,7 +2386,7 @@ static UniValue getblockstats(const JSONRPCRequest& request) ret_all.pushKV("minfeerate", (minfeerate == MAX_MONEY) ? 0 : minfeerate); ret_all.pushKV("mintxsize", mintxsize == MaxBlockSize() ? 0 : mintxsize); ret_all.pushKV("outs", outputs); - ret_all.pushKV("subsidy", GetBlockSubsidyPrev(pindex->pprev, Params().GetConsensus())); + ret_all.pushKV("subsidy", GetBlockSubsidy(pindex, Params().GetConsensus())); ret_all.pushKV("time", pindex->GetBlockTime()); ret_all.pushKV("total_out", total_out); ret_all.pushKV("total_size", total_size); diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 63b89739420a..04adb54bebc2 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -459,7 +459,7 @@ static UniValue masternode_payments(const JSONRPCRequest& request, const Chainst std::vector voutMasternodePayments, voutDummy; CMutableTransaction dummyTx; - CAmount blockReward = nBlockFees + GetBlockSubsidy(pindex->pprev->nBits, pindex->pprev->nHeight, Params().GetConsensus()); + CAmount blockReward = nBlockFees + GetBlockSubsidy(pindex, Params().GetConsensus()); CMasternodePayments::FillBlockPayments(*sporkManager, *governance, dummyTx, pindex->nHeight, blockReward, voutMasternodePayments, voutDummy); UniValue blockObj(UniValue::VOBJ); diff --git a/src/test/block_reward_reallocation_tests.cpp b/src/test/block_reward_reallocation_tests.cpp index 0a209024cc38..13c20230b1df 100644 --- a/src/test/block_reward_reallocation_tests.cpp +++ b/src/test/block_reward_reallocation_tests.cpp @@ -201,7 +201,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS LOCK(cs_main); deterministicMNManager->UpdatedBlockTip(::ChainActive().Tip()); BOOST_ASSERT(deterministicMNManager->GetListAtChainTip().HasMN(tx.GetHash())); - auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); + auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey); BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment); } @@ -212,7 +212,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS { LOCK(cs_main); - auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); + auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); 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(), 13748571607); BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment); @@ -227,7 +227,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS CreateAndProcessBlock({}, coinbaseKey); } LOCK(cs_main); - auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); + auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey); BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment); } @@ -236,7 +236,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS { // Reward split should reach ~60/40 after reallocation is done LOCK(cs_main); - auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); + auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); 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(), 10221599170); BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment); @@ -250,7 +250,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS CreateAndProcessBlock({}, coinbaseKey); } LOCK(cs_main); - auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); + auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); const auto pblocktemplate = BlockAssembler(*sporkManager, *governance, *m_node.llmq_ctx, *m_node.evodb, ::ChainstateActive(), *m_node.mempool, Params()).CreateNewBlock(coinbasePubKey); BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment); } @@ -258,7 +258,7 @@ BOOST_FIXTURE_TEST_CASE(block_reward_reallocation, TestChainBRRBeforeActivationS { // Reward split should reach ~60/40 after reallocation is done LOCK(cs_main); - auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidy(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); + auto masternode_payment = GetMasternodePayment(::ChainActive().Height(), GetBlockSubsidyInner(::ChainActive().Tip()->nBits, ::ChainActive().Height(), consensus_params), 2500); 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); BOOST_CHECK_EQUAL(pblocktemplate->voutMasternodePayments[0].nValue, masternode_payment); diff --git a/src/test/subsidy_tests.cpp b/src/test/subsidy_tests.cpp index b29cea8130db..08d9456a9131 100644 --- a/src/test/subsidy_tests.cpp +++ b/src/test/subsidy_tests.cpp @@ -22,43 +22,43 @@ BOOST_AUTO_TEST_CASE(block_subsidy_test) // details for block 4249 (subsidy returned will be for block 4250) nPrevBits = 0x1c4a47c4; nPrevHeight = 4249; - nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); + nSubsidy = GetBlockSubsidyInner(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); BOOST_CHECK_EQUAL(nSubsidy, 50000000000ULL); // details for block 4501 (subsidy returned will be for block 4502) nPrevBits = 0x1c4a47c4; nPrevHeight = 4501; - nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); + nSubsidy = GetBlockSubsidyInner(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); BOOST_CHECK_EQUAL(nSubsidy, 5600000000ULL); // details for block 5464 (subsidy returned will be for block 5465) nPrevBits = 0x1c29ec00; nPrevHeight = 5464; - nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); + nSubsidy = GetBlockSubsidyInner(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); BOOST_CHECK_EQUAL(nSubsidy, 2100000000ULL); // details for block 5465 (subsidy returned will be for block 5466) nPrevBits = 0x1c29ec00; nPrevHeight = 5465; - nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); + nSubsidy = GetBlockSubsidyInner(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); BOOST_CHECK_EQUAL(nSubsidy, 12200000000ULL); // details for block 17588 (subsidy returned will be for block 17589) nPrevBits = 0x1c08ba34; nPrevHeight = 17588; - nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); + nSubsidy = GetBlockSubsidyInner(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); BOOST_CHECK_EQUAL(nSubsidy, 6100000000ULL); // details for block 99999 (subsidy returned will be for block 100000) nPrevBits = 0x1b10cf42; nPrevHeight = 99999; - nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); + nSubsidy = GetBlockSubsidyInner(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); BOOST_CHECK_EQUAL(nSubsidy, 500000000ULL); // details for block 210239 (subsidy returned will be for block 210240) nPrevBits = 0x1b11548e; nPrevHeight = 210239; - nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); + nSubsidy = GetBlockSubsidyInner(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); BOOST_CHECK_EQUAL(nSubsidy, 500000000ULL); // 1st subsidy reduction happens here @@ -66,7 +66,7 @@ BOOST_AUTO_TEST_CASE(block_subsidy_test) // details for block 210240 (subsidy returned will be for block 210241) nPrevBits = 0x1b10d50b; nPrevHeight = 210240; - nSubsidy = GetBlockSubsidy(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); + nSubsidy = GetBlockSubsidyInner(nPrevBits, nPrevHeight, chainParams->GetConsensus(), false); BOOST_CHECK_EQUAL(nSubsidy, 464285715ULL); } diff --git a/src/validation.cpp b/src/validation.cpp index b116c96cb21c..2e9c42b463a5 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1113,10 +1113,10 @@ NOTE: unlike bitcoin we are using PREVIOUS block height here, might be a good idea to change this to use prev bits but current height to avoid confusion. - Due to difference in interface, prefer to use GetBlockSubsidyPrev() + Due to difference in interface, prefer to use GetBlockSubsidy() that have a check for nullptr for pIndexPrev inside */ -CAmount GetBlockSubsidy(int nPrevBits, int nPrevHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly) +CAmount GetBlockSubsidyInner(int nPrevBits, int nPrevHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly) { double dDiff; CAmount nSubsidyBase; @@ -1166,10 +1166,10 @@ CAmount GetBlockSubsidy(int nPrevBits, int nPrevHeight, const Consensus::Params& return fSuperblockPartOnly ? nSuperblockPart : nSubsidy - nSuperblockPart; } -CAmount GetBlockSubsidyPrev(const CBlockIndex* const pindexPrev, const Consensus::Params& consensusParams) +CAmount GetBlockSubsidy(const CBlockIndex* const pindex, const Consensus::Params& consensusParams) { - if (pindexPrev == nullptr) return Params().GenesisBlock().vtx[0]->GetValueOut(); - return GetBlockSubsidy(pindexPrev->nBits, pindexPrev->nHeight, consensusParams); + if (pindex->pprev == nullptr) return Params().GenesisBlock().vtx[0]->GetValueOut(); + return GetBlockSubsidyInner(pindex->pprev->nBits, pindex->pprev->nHeight, consensusParams); } CAmount GetMasternodePayment(int nHeight, CAmount blockValue, int nReallocActivationHeight) @@ -2410,7 +2410,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, // DASH : MODIFIED TO CHECK MASTERNODE PAYMENTS AND SUPERBLOCKS // TODO: resync data (both ways?) and try to reprocess this block later. - CAmount blockReward = nFees + GetBlockSubsidyPrev(pindex->pprev, m_params.GetConsensus()); + CAmount blockReward = nFees + GetBlockSubsidy(pindex, m_params.GetConsensus()); std::string strError = ""; int64_t nTime5_2 = GetTimeMicros(); nTimeSubsidy += nTime5_2 - nTime5_1; diff --git a/src/validation.h b/src/validation.h index ac0bff9e9b60..697696b1b6f6 100644 --- a/src/validation.h +++ b/src/validation.h @@ -206,8 +206,8 @@ void StopScriptCheckWorkerThreads(); CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock); double ConvertBitsToDouble(unsigned int nBits); -CAmount GetBlockSubsidy(int nBits, int nHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly = false); -CAmount GetBlockSubsidyPrev(const CBlockIndex* const pindexPrev, const Consensus::Params& consensusParams); +CAmount GetBlockSubsidyInner(int nBits, int nHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly = false); +CAmount GetBlockSubsidy(const CBlockIndex* const pindex, const Consensus::Params& consensusParams); CAmount GetMasternodePayment(int nHeight, CAmount blockValue, int nReallocActivationHeight = std::numeric_limits::max() /* not activated */); /** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */ From 8259a6f529a6442d1c2597700f35a7d05f8dc486 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Thu, 3 Aug 2023 22:05:49 +0700 Subject: [PATCH 3/4] docs: make comment for GetBlockSubsidy in doxygen style --- src/validation.cpp | 3 --- src/validation.h | 7 +++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/validation.cpp b/src/validation.cpp index 2e9c42b463a5..5bc1e91996a2 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1112,9 +1112,6 @@ double ConvertBitsToDouble(unsigned int nBits) NOTE: unlike bitcoin we are using PREVIOUS block height here, might be a good idea to change this to use prev bits but current height to avoid confusion. - - Due to difference in interface, prefer to use GetBlockSubsidy() - that have a check for nullptr for pIndexPrev inside */ CAmount GetBlockSubsidyInner(int nPrevBits, int nPrevHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly) { diff --git a/src/validation.h b/src/validation.h index 697696b1b6f6..1f3074d70597 100644 --- a/src/validation.h +++ b/src/validation.h @@ -206,6 +206,13 @@ void StopScriptCheckWorkerThreads(); CTransactionRef GetTransaction(const CBlockIndex* const block_index, const CTxMemPool* const mempool, const uint256& hash, const Consensus::Params& consensusParams, uint256& hashBlock); double ConvertBitsToDouble(unsigned int nBits); +/** + * Due to difference in logic, the GetBlockSubsidy() has also different list of + * arguments. + * + * GetBlockSubsidy() uses a pre-calculated value for genesis block. + * It happens if pIndex->pprev equals to nullptr + */ CAmount GetBlockSubsidyInner(int nBits, int nHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly = false); CAmount GetBlockSubsidy(const CBlockIndex* const pindex, const Consensus::Params& consensusParams); CAmount GetMasternodePayment(int nHeight, CAmount blockValue, int nReallocActivationHeight = std::numeric_limits::max() /* not activated */); From 1530c8753e9416e962f02bda420929b04927c64b Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Fri, 4 Aug 2023 02:29:22 +0700 Subject: [PATCH 4/4] Apply suggestions from code review updated comments Co-authored-by: UdjinM6 --- src/validation.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/validation.h b/src/validation.h index 1f3074d70597..37de5260a8eb 100644 --- a/src/validation.h +++ b/src/validation.h @@ -210,10 +210,10 @@ double ConvertBitsToDouble(unsigned int nBits); * Due to difference in logic, the GetBlockSubsidy() has also different list of * arguments. * - * GetBlockSubsidy() uses a pre-calculated value for genesis block. - * It happens if pIndex->pprev equals to nullptr + * When pindex points to a genesis block GetBlockSubsidy() returns a pre-calculated value. + * For other blocks it calls GetBlockSubsidyInner() using nBits and nHeight of a pindex->pprev block. */ -CAmount GetBlockSubsidyInner(int nBits, int nHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly = false); +CAmount GetBlockSubsidyInner(int nPrevBits, int nPrevHeight, const Consensus::Params& consensusParams, bool fSuperblockPartOnly = false); CAmount GetBlockSubsidy(const CBlockIndex* const pindex, const Consensus::Params& consensusParams); CAmount GetMasternodePayment(int nHeight, CAmount blockValue, int nReallocActivationHeight = std::numeric_limits::max() /* not activated */);