refactor: removing usage of ::ChainActive::Tip() form masternode/payments

This commit is contained in:
Konstantin Akimov 2023-10-16 16:32:46 +07:00 committed by PastaPastaPasta
parent 4cf13d77c3
commit a699ad187d
5 changed files with 23 additions and 17 deletions

View File

@ -24,12 +24,13 @@
#include <cassert>
#include <string>
[[nodiscard]] static bool GetBlockTxOuts(const int nBlockHeight, const CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet)
[[nodiscard]] static bool GetBlockTxOuts(const CBlockIndex* const pindexPrev, const CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet)
{
voutMasternodePaymentsRet.clear();
const CBlockIndex* pindex = WITH_LOCK(cs_main, return ::ChainActive()[nBlockHeight - 1]);
bool fMNRewardReallocated = llmq::utils::IsMNRewardReallocationActive(pindex);
const int nBlockHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
bool fMNRewardReallocated = llmq::utils::IsMNRewardReallocationActive(pindexPrev);
CAmount masternodeReward = GetMasternodePayment(nBlockHeight, blockReward, fMNRewardReallocated);
if (fMNRewardReallocated) {
@ -42,7 +43,7 @@
voutMasternodePaymentsRet.emplace_back(platformReward, CScript() << OP_RETURN);
}
auto dmnPayee = deterministicMNManager->GetListForBlock(pindex).GetMNPayee(pindex);
auto dmnPayee = deterministicMNManager->GetListForBlock(pindexPrev).GetMNPayee(pindexPrev);
if (!dmnPayee) {
return false;
}
@ -72,12 +73,12 @@
*
* Get masternode payment tx outputs
*/
[[nodiscard]] static bool GetMasternodeTxOuts(const int nBlockHeight, const CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet)
[[nodiscard]] static bool GetMasternodeTxOuts(const CBlockIndex* const pindexPrev, const CAmount blockReward, std::vector<CTxOut>& voutMasternodePaymentsRet)
{
// make sure it's not filled yet
voutMasternodePaymentsRet.clear();
if(!GetBlockTxOuts(nBlockHeight, blockReward, voutMasternodePaymentsRet)) {
if(!GetBlockTxOuts(pindexPrev, blockReward, voutMasternodePaymentsRet)) {
LogPrintf("MasternodePayments::%s -- no payee (deterministic masternode list empty)\n", __func__);
return false;
}
@ -92,15 +93,16 @@
return true;
}
[[nodiscard]] static bool IsTransactionValid(const CTransaction& txNew, const int nBlockHeight, const CAmount blockReward)
[[nodiscard]] static bool IsTransactionValid(const CTransaction& txNew, const CBlockIndex* const pindexPrev, const CAmount blockReward)
{
const int nBlockHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
if (!deterministicMNManager->IsDIP3Enforced(nBlockHeight)) {
// can't verify historical blocks here
return true;
}
std::vector<CTxOut> voutMasternodePayments;
if (!GetBlockTxOuts(nBlockHeight, blockReward, voutMasternodePayments)) {
if (!GetBlockTxOuts(pindexPrev, blockReward, voutMasternodePayments)) {
LogPrintf("MasternodePayments::%s -- ERROR failed to get payees for block at height %s\n", __func__, nBlockHeight);
return true;
}
@ -259,7 +261,7 @@ bool IsBlockValueValid(const CSporkManager& sporkManager, CGovernanceManager& go
}
bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
const CTransaction& txNew, const int nBlockHeight, const CAmount blockReward)
const CTransaction& txNew, const CBlockIndex* const pindexPrev, const CAmount blockReward)
{
if(fDisableGovernance) {
//there is no budget data to use to check anything, let's just accept the longest chain
@ -267,6 +269,7 @@ bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& go
return true;
}
const int nBlockHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
// we are still using budgets, but we have no data about them anymore,
// we can only check masternode payments
@ -302,7 +305,7 @@ bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& go
}
// Check for correct masternode payment
if(IsTransactionValid(txNew, nBlockHeight, blockReward)) {
if(IsTransactionValid(txNew, pindexPrev, blockReward)) {
LogPrint(BCLog::MNPAYMENTS, "%s -- Valid masternode payment at height %d: %s", __func__, nBlockHeight, txNew.ToString()); /* Continued */
return true;
}
@ -312,9 +315,11 @@ bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& go
}
void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
CMutableTransaction& txNew, const int nBlockHeight, const CAmount blockReward,
CMutableTransaction& txNew, const CBlockIndex* const pindexPrev, const CAmount blockReward,
std::vector<CTxOut>& voutMasternodePaymentsRet, std::vector<CTxOut>& voutSuperblockPaymentsRet)
{
int nBlockHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
// only create superblocks if spork is enabled AND if superblock is actually triggered
// (height should be validated inside)
if(AreSuperblocksEnabled(sporkManager) && CSuperblockManager::IsSuperblockTriggered(governanceManager, nBlockHeight)) {
@ -322,7 +327,7 @@ void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& go
CSuperblockManager::GetSuperblockPayments(governanceManager, nBlockHeight, voutSuperblockPaymentsRet);
}
if (!GetMasternodeTxOuts(nBlockHeight, blockReward, voutMasternodePaymentsRet)) {
if (!GetMasternodeTxOuts(pindexPrev, blockReward, voutMasternodePaymentsRet)) {
LogPrint(BCLog::MNPAYMENTS, "%s -- no masternode to pay (MN list probably empty)\n", __func__);
}

View File

@ -12,6 +12,7 @@
class CGovernanceManager;
class CBlock;
class CBlockIndex;
class CTransaction;
struct CMutableTransaction;
class CSporkManager;
@ -28,9 +29,9 @@ 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);
bool IsBlockPayeeValid(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
const CTransaction& txNew, const int nBlockHeight, const CAmount blockReward);
const CTransaction& txNew, const CBlockIndex* const pindexPrev, const CAmount blockReward);
void FillBlockPayments(const CSporkManager& sporkManager, CGovernanceManager& governanceManager,
CMutableTransaction& txNew, const int nBlockHeight, const CAmount blockReward,
CMutableTransaction& txNew, const CBlockIndex* const pindexPrev, const CAmount blockReward,
std::vector<CTxOut>& voutMasternodePaymentsRet, std::vector<CTxOut>& voutSuperblockPaymentsRet);
/**

View File

@ -250,7 +250,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
// Update coinbase transaction with additional info about masternode and governance payments,
// get some info back to pass to getblocktemplate
MasternodePayments::FillBlockPayments(spork_manager, governance_manager, coinbaseTx, nHeight, blockReward, pblocktemplate->voutMasternodePayments, pblocktemplate->voutSuperblockPayments);
MasternodePayments::FillBlockPayments(spork_manager, governance_manager, coinbaseTx, pindexPrev, blockReward, pblocktemplate->voutMasternodePayments, pblocktemplate->voutSuperblockPayments);
pblock->vtx[0] = MakeTransactionRef(std::move(coinbaseTx));
pblocktemplate->vTxFees[0] = -nFees;

View File

@ -458,7 +458,7 @@ static UniValue masternode_payments(const JSONRPCRequest& request, const Chainst
std::vector<CTxOut> voutMasternodePayments, voutDummy;
CMutableTransaction dummyTx;
CAmount blockReward = nBlockFees + GetBlockSubsidy(pindex, Params().GetConsensus());
MasternodePayments::FillBlockPayments(*sporkManager, *governance, dummyTx, pindex->nHeight, blockReward, voutMasternodePayments, voutDummy);
MasternodePayments::FillBlockPayments(*sporkManager, *governance, dummyTx, pindex->pprev, blockReward, voutMasternodePayments, voutDummy);
UniValue blockObj(UniValue::VOBJ);
CAmount payedPerBlock{0};

View File

@ -2399,7 +2399,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
int64_t nTime5_4 = GetTimeMicros(); nTimeValueValid += nTime5_4 - nTime5_3;
LogPrint(BCLog::BENCHMARK, " - IsBlockValueValid: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5_4 - nTime5_3), nTimeValueValid * MICRO, nTimeValueValid * MILLI / nBlocksTotal);
if (!MasternodePayments::IsBlockPayeeValid(*sporkManager, *governance, *block.vtx[0], pindex->nHeight, blockReward)) {
if (!MasternodePayments::IsBlockPayeeValid(*sporkManager, *governance, *block.vtx[0], pindex->pprev, 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");