From a77dc95b7870b17ba89ad45b7c3079cf54909322 Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Thu, 24 Aug 2023 17:49:07 +0700 Subject: [PATCH 01/11] refactor: fix bad naming of namespace CMasternodePayments -> MasternodePayments --- src/masternode/payments.cpp | 12 ++++++------ src/masternode/payments.h | 12 ++++++------ src/miner.cpp | 2 +- src/rpc/masternode.cpp | 2 +- src/validation.cpp | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/masternode/payments.cpp b/src/masternode/payments.cpp index fd61a4af05c99..146cd13e5445a 100644 --- a/src/masternode/payments.cpp +++ b/src/masternode/payments.cpp @@ -65,7 +65,7 @@ static bool GetMasternodeTxOuts(const int nBlockHeight, const CAmount blockRewar voutMasternodePaymentsRet.clear(); if(!GetBlockTxOuts(nBlockHeight, blockReward, voutMasternodePaymentsRet)) { - LogPrintf("CMasternodePayments::%s -- no payee (deterministic masternode list empty)\n", __func__); + LogPrintf("MasternodePayments::%s -- no payee (deterministic masternode list empty)\n", __func__); return false; } @@ -73,7 +73,7 @@ static bool GetMasternodeTxOuts(const int nBlockHeight, const CAmount blockRewar CTxDestination dest; ExtractDestination(txout.scriptPubKey, dest); - LogPrintf("CMasternodePayments::%s -- Masternode payment %lld to %s\n", __func__, txout.nValue, EncodeDestination(dest)); + LogPrintf("MasternodePayments::%s -- Masternode payment %lld to %s\n", __func__, txout.nValue, EncodeDestination(dest)); } return true; @@ -88,7 +88,7 @@ static bool IsTransactionValid(const CTransaction& txNew, const int nBlockHeight std::vector voutMasternodePayments; if (!GetBlockTxOuts(nBlockHeight, blockReward, voutMasternodePayments)) { - LogPrintf("CMasternodePayments::%s -- ERROR failed to get payees for block at height %s\n", __func__, nBlockHeight); + LogPrintf("MasternodePayments::%s -- ERROR failed to get payees for block at height %s\n", __func__, nBlockHeight); return true; } @@ -98,7 +98,7 @@ static bool IsTransactionValid(const CTransaction& txNew, const int nBlockHeight CTxDestination dest; if (!ExtractDestination(txout.scriptPubKey, dest)) assert(false); - LogPrintf("CMasternodePayments::%s -- ERROR failed to find expected payee %s in block at height %s\n", __func__, EncodeDestination(dest), nBlockHeight); + LogPrintf("MasternodePayments::%s -- ERROR failed to find expected payee %s in block at height %s\n", __func__, EncodeDestination(dest), nBlockHeight); return false; } } @@ -147,7 +147,7 @@ static bool IsOldBudgetBlockValueValid(const CMasternodeSync& mn_sync, const CBl return isBlockRewardValueMet; } -namespace CMasternodePayments { +namespace MasternodePayments { /** * IsBlockValueValid @@ -329,4 +329,4 @@ void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& go nBlockHeight, blockReward, voutMasternodeStr, txNew.ToString()); } -} // namespace CMasternodePayments +} // namespace MasternodePayments diff --git a/src/masternode/payments.h b/src/masternode/payments.h index f8888d68f998a..576851fbde311 100644 --- a/src/masternode/payments.h +++ b/src/masternode/payments.h @@ -18,12 +18,12 @@ class CSporkManager; class CTxOut; class CMasternodeSync; -// -// Masternode Payments Class -// Keeps track of who should get paid for which blocks -// +/** + * Masternode Payments Namespace + * Helpers to kees track of who should get paid for which blocks + */ -namespace CMasternodePayments +namespace MasternodePayments { bool IsBlockValueValid(const CSporkManager& sporkManager, CGovernanceManager& governanceManager, const CMasternodeSync& mn_sync, const CBlock& block, const int nBlockHeight, const CAmount blockReward, std::string& strErrorRet); @@ -32,6 +32,6 @@ 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); -} // namespace CMasternodePayments +} // namespace MasternodePayments #endif // BITCOIN_MASTERNODE_PAYMENTS_H diff --git a/src/miner.cpp b/src/miner.cpp index 72b86e938695e..480b1d974bae2 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -240,7 +240,7 @@ std::unique_ptr BlockAssembler::CreateNewBlock(const CScript& sc // Update coinbase transaction with additional info about masternode and governance payments, // get some info back to pass to getblocktemplate - CMasternodePayments::FillBlockPayments(spork_manager, governance_manager, coinbaseTx, nHeight, blockReward, pblocktemplate->voutMasternodePayments, pblocktemplate->voutSuperblockPayments); + MasternodePayments::FillBlockPayments(spork_manager, governance_manager, coinbaseTx, nHeight, blockReward, pblocktemplate->voutMasternodePayments, pblocktemplate->voutSuperblockPayments); pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx)); pblocktemplate->vTxFees[0] = -nFees; diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 94e401a230fa4..2317411a0f89b 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -460,7 +460,7 @@ static UniValue masternode_payments(const JSONRPCRequest& request, const Chainst std::vector voutMasternodePayments, voutDummy; CMutableTransaction dummyTx; CAmount blockReward = nBlockFees + GetBlockSubsidy(pindex, Params().GetConsensus()); - CMasternodePayments::FillBlockPayments(*sporkManager, *governance, dummyTx, pindex->nHeight, blockReward, voutMasternodePayments, voutDummy); + MasternodePayments::FillBlockPayments(*sporkManager, *governance, dummyTx, pindex->nHeight, blockReward, voutMasternodePayments, voutDummy); UniValue blockObj(UniValue::VOBJ); CAmount payedPerBlock{0}; diff --git a/src/validation.cpp b/src/validation.cpp index a6b74a3e71bd4..7c0d0a53cc1f5 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2413,7 +2413,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, int64_t nTime5_2 = GetTimeMicros(); nTimeSubsidy += nTime5_2 - nTime5_1; LogPrint(BCLog::BENCHMARK, " - GetBlockSubsidy: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5_2 - nTime5_1), nTimeSubsidy * MICRO, nTimeSubsidy * MILLI / nBlocksTotal); - if (!CMasternodePayments::IsBlockValueValid(*sporkManager, *governance, *::masternodeSync, block, pindex->nHeight, blockReward, strError)) { + if (!MasternodePayments::IsBlockValueValid(*sporkManager, *governance, *::masternodeSync, block, pindex->nHeight, blockReward, strError)) { // NOTE: Do not punish, the node might be missing governance data LogPrintf("ERROR: ConnectBlock(DASH): %s\n", strError); return state.Invalid(BlockValidationResult::BLOCK_RESULT_UNSET, "bad-cb-amount"); @@ -2422,7 +2422,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, int64_t nTime5_3 = GetTimeMicros(); nTimeValueValid += nTime5_3 - nTime5_2; LogPrint(BCLog::BENCHMARK, " - IsBlockValueValid: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5_3 - nTime5_2), nTimeValueValid * MICRO, nTimeValueValid * MILLI / nBlocksTotal); - if (!CMasternodePayments::IsBlockPayeeValid(*sporkManager, *governance, *block.vtx[0], pindex->nHeight, blockReward)) { + if (!MasternodePayments::IsBlockPayeeValid(*sporkManager, *governance, *block.vtx[0], pindex->nHeight, blockReward)) { // NOTE: Do not punish, the node might be missing governance data LogPrintf("ERROR: ConnectBlock(DASH): couldn't find masternode or superblock payments\n"); return state.Invalid(BlockValidationResult::BLOCK_RESULT_UNSET, "bad-cb-payee"); From 5583890689c2933629835bf7a50eb20f34a1d67c Mon Sep 17 00:00:00 2001 From: Konstantin Akimov Date: Thu, 8 Dec 2022 16:10:26 +0700 Subject: [PATCH 02/11] feat: masternode payment reallocation from coin base to platform Move funds from the coinbase, into the Asset Lock Pool. This is to incentivize MNs to upgrade to platform, because only MNs running platform will get these migrated rewards --- src/evo/creditpool.cpp | 26 +++++++++++++++---- src/evo/creditpool.h | 9 ++++++- src/masternode/payments.cpp | 13 +++++++++- src/miner.cpp | 7 +++++ test/functional/feature_asset_locks.py | 20 ++++++++++++++ .../test_framework/test_framework.py | 1 + 6 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/evo/creditpool.cpp b/src/evo/creditpool.cpp index 6baa4d7f807a8..85176301d184c 100644 --- a/src/evo/creditpool.cpp +++ b/src/evo/creditpool.cpp @@ -7,9 +7,8 @@ #include #include -#include - #include +#include #include #include #include @@ -213,11 +212,17 @@ CCreditPoolManager::CCreditPoolManager(CEvoDB& _evoDb) CCreditPoolDiff::CCreditPoolDiff(CCreditPool starter, const CBlockIndex *pindex, const Consensus::Params& consensusParams) : pool(std::move(starter)), - pindex(pindex) + pindex(pindex), + params(consensusParams) { assert(pindex); } +void CCreditPoolDiff::AddRewardRealloced(const CAmount reward) { + assert(MoneyRange(reward)); + masternodeReward += reward; +} + bool CCreditPoolDiff::SetTarget(const CTransaction& tx, TxValidationState& state) { CCbTx cbTx; @@ -225,9 +230,20 @@ bool CCreditPoolDiff::SetTarget(const CTransaction& tx, TxValidationState& state return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-cbtx-payload"); } - if (cbTx.nVersion == 3) { - targetBalance = cbTx.creditPoolBalance; + if (cbTx.nVersion != 3) return true; + + targetBalance = cbTx.creditPoolBalance; + + + if (!llmq::utils::IsMNRewardReallocationActive(pindex)) return true; + + CAmount blockReward = 0; + for (const CTxOut& txout : tx.vout) { + blockReward += txout.nValue; } + masternodeReward = GetMasternodePayment(cbTx.nHeight, blockReward, params.BRRHeight); + LogPrintf("CreditPool: set target to %lld with MN reward %lld\n", *targetBalance, masternodeReward); + return true; } diff --git a/src/evo/creditpool.h b/src/evo/creditpool.h index 9e1bf0ebff710..30a46c68e852d 100644 --- a/src/evo/creditpool.h +++ b/src/evo/creditpool.h @@ -67,11 +67,13 @@ class CCreditPoolDiff { CAmount sessionLocked{0}; CAmount sessionUnlocked{0}; + CAmount masternodeReward{0}; // target value is used to validate CbTx. If values mismatched, block is invalid std::optional targetBalance; const CBlockIndex *pindex{nullptr}; + const Consensus::Params& params; public: explicit CCreditPoolDiff(CCreditPool starter, const CBlockIndex *pindex, const Consensus::Params& consensusParams); @@ -82,8 +84,13 @@ class CCreditPoolDiff { */ bool ProcessTransaction(const CTransaction& tx, TxValidationState& state); + /** + * This function should be called by miner for initalization of MasterNode reward + */ + void AddRewardRealloced(const CAmount reward); + CAmount GetTotalLocked() const { - return pool.locked + sessionLocked - sessionUnlocked; + return pool.locked + sessionLocked - sessionUnlocked + masternodeReward; } const std::optional& GetTargetBalance() const { diff --git a/src/masternode/payments.cpp b/src/masternode/payments.cpp index 146cd13e5445a..f80d59c925bab 100644 --- a/src/masternode/payments.cpp +++ b/src/masternode/payments.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include