diff --git a/src/evo/creditpool.cpp b/src/evo/creditpool.cpp index 6baa4d7f80..9592dfa492 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 9e1bf0ebff..84efdca141 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/masternode/payments.cpp b/src/masternode/payments.cpp index fd61a4af05..22c785811c 100644 --- a/src/masternode/payments.cpp +++ b/src/masternode/payments.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include