From 6fc05e7fa8e4c0e5f5634edfdb4b4bfe5f883e5f Mon Sep 17 00:00:00 2001 From: Evan Duffield Date: Sat, 4 Jul 2015 07:49:49 -0700 Subject: [PATCH] nHeight calculation for BlockValue checking --- src/chain.cpp | 2 +- src/main.cpp | 2 +- src/masternode-payments.cpp | 28 ++++++++++++++++++++++------ src/masternode-payments.h | 2 +- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/chain.cpp b/src/chain.cpp index 222d98981..e0acb42df 100644 --- a/src/chain.cpp +++ b/src/chain.cpp @@ -25,7 +25,7 @@ void CChain::SetTip(CBlockIndex *pindex) { } //recalculate the coinbase payee cache if needed - if(nChainSwitch > 0) { + if(nChainSwitch > 1) { coinbasePayee.BuildIndex(true); } } diff --git a/src/main.cpp b/src/main.cpp index 77e09c338..ff21ffc52 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2097,7 +2097,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin int64_t nTime1 = GetTimeMicros(); nTimeConnect += nTime1 - nTimeStart; LogPrint("bench", " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime1 - nTimeStart), 0.001 * (nTime1 - nTimeStart) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime1 - nTimeStart) / (nInputs-1), nTimeConnect * 0.000001); - if(!IsBlockValueValid(block.vtx[0].GetValueOut(), GetBlockValue(pindex->pprev->nBits, pindex->pprev->nHeight, nFees))){ + if(!IsBlockValueValid(block, GetBlockValue(pindex->pprev->nBits, pindex->pprev->nHeight, nFees))){ return state.DoS(100, error("ConnectBlock() : coinbase pays too much (actual=%d vs limit=%d)", block.vtx[0].GetValueOut(), GetBlockValue(pindex->pprev->nBits, pindex->pprev->nHeight, nFees)), diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index eec1fb811..21e697b29 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -19,23 +19,39 @@ CMasternodePayments masternodePayments; map mapMasternodePayeeVotes; map mapMasternodeBlocks; -bool IsBlockValueValid(int64_t nBlockValue, int64_t nExpectedValue){ +bool IsBlockValueValid(const CBlock& block, int64_t nExpectedValue){ CBlockIndex* pindexPrev = chainActive.Tip(); if(pindexPrev == NULL) return true; - if(budget.sizeFinalized() == 0 && budget.sizeProposals() == 0) { //there is no budget data to use to check anything + int nHeight = 0; + if(pindexPrev->GetBlockHash() == block.hashPrevBlock) + { + nHeight = pindexPrev->nHeight+1; + } else { //out of order + BOOST_FOREACH(const PAIRTYPE(const uint256, CBlockIndex*)& item, mapBlockIndex){ + if(item.first == block.hashPrevBlock) { + nHeight = item.second->nHeight+1; + } + } + } + + if(nHeight == 0){ + LogPrintf("IsBlockValueValid() : WARNING: Couldn't find previous block"); + } + + if((budget.sizeFinalized() == 0 && budget.sizeProposals() == 0) || nHeight == 0) { //there is no budget data to use to check anything //super blocks will always be on these blocks, max 100 per budgeting - if((pindexPrev->nHeight+1) % GetBudgetPaymentCycleBlocks() < 100){ + if(nHeight % GetBudgetPaymentCycleBlocks() < 100){ return true; } else { - if(nBlockValue > nExpectedValue) return false; + if(block.vtx[0].GetValueOut() > nExpectedValue) return false; } } else { // we're synced and have data so check the budget schedule - if(budget.IsBudgetPaymentBlock(pindexPrev->nHeight+1)){ + if(budget.IsBudgetPaymentBlock(nHeight)){ //the value of the block is evaluated in CheckBlock return true; } else { - if(nBlockValue > nExpectedValue) return false; + if(block.vtx[0].GetValueOut() > nExpectedValue) return false; } } diff --git a/src/masternode-payments.h b/src/masternode-payments.h index d0104d39d..378abedf9 100644 --- a/src/masternode-payments.h +++ b/src/masternode-payments.h @@ -29,7 +29,7 @@ void ProcessMessageMasternodePayments(CNode* pfrom, std::string& strCommand, CDa bool IsReferenceNode(CTxIn& vin); bool IsBlockPayeeValid(const CTransaction& txNew, int64_t nBlockHeight); std::string GetRequiredPaymentsString(int64_t nBlockHeight); -bool IsBlockValueValid(int64_t nBlockValue, int64_t nExpectedValue); +bool IsBlockValueValid(const CBlock& block, int64_t nExpectedValue); void FillBlockPayee(CMutableTransaction& txNew, int64_t nFees); class CMasternodePayee