diff --git a/src/evo/creditpool.cpp b/src/evo/creditpool.cpp index 6baa4d7f807a8..9592dfa4927db 100644 --- a/src/evo/creditpool.cpp +++ b/src/evo/creditpool.cpp @@ -7,9 +7,8 @@ #include #include -#include - #include +#include #include #include #include @@ -19,6 +18,11 @@ #include #include +// Forward declaration to prevent a new circular dependencies through masternode/payments.h +namespace MasternodePayments { +CAmount PlatformShare(const CAmount masternodeReward); +} // namespace MasternodePayments + static const std::string DB_CREDITPOOL_SNAPSHOT = "cpm_S"; std::unique_ptr creditPoolManager; @@ -213,11 +217,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)); + platformReward += reward; +} + bool CCreditPoolDiff::SetTarget(const CTransaction& tx, TxValidationState& state) { CCbTx cbTx; @@ -225,9 +235,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; } + platformReward = MasternodePayments::PlatformShare(GetMasternodePayment(cbTx.nHeight, blockReward, params.BRRHeight)); + LogPrintf("CreditPool: set target to %lld with MN reward %lld\n", *targetBalance, platformReward); + return true; } diff --git a/src/evo/creditpool.h b/src/evo/creditpool.h index 9e1bf0ebff710..84efdca1410e7 100644 --- a/src/evo/creditpool.h +++ b/src/evo/creditpool.h @@ -67,11 +67,13 @@ class CCreditPoolDiff { CAmount sessionLocked{0}; CAmount sessionUnlocked{0}; + CAmount platformReward{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 initialization of MasterNode reward + */ + void AddRewardRealloced(const CAmount reward); + CAmount GetTotalLocked() const { - return pool.locked + sessionLocked - sessionUnlocked; + return pool.locked + sessionLocked - sessionUnlocked + platformReward; } const std::optional& GetTargetBalance() const { diff --git a/src/llmq/utils.cpp b/src/llmq/utils.cpp index c0b5a5c3f20fe..d1ebad745a738 100644 --- a/src/llmq/utils.cpp +++ b/src/llmq/utils.cpp @@ -728,6 +728,7 @@ bool IsMNRewardReallocationActive(const CBlockIndex* pindex) { assert(pindex); if (!IsV20Active(pindex)) return false; + if (Params().NetworkIDString() == CBaseChainParams::TESTNET) return IsV20Active(pindex); // TODO remove this before re-hardforking testnet to check EHF LOCK(cs_llmq_vbc); return VersionBitsState(pindex, Params().GetConsensus(), Consensus::DEPLOYMENT_MN_RR, llmq_versionbitscache) == ThresholdState::ACTIVE; diff --git a/src/masternode/payments.cpp b/src/masternode/payments.cpp index fd61a4af05c99..63c83fd1f1cc6 100644 --- a/src/masternode/payments.cpp +++ b/src/masternode/payments.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include