From fe81d641dad80c60ff04c400f03fda0ab9cc074e Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Fri, 25 Aug 2017 15:57:05 +0300 Subject: [PATCH] drop pCurrentBlockIndex and use cached block height instead (nCachedBlockHeight) (#1579) --- src/governance.cpp | 31 ++++---------------------- src/governance.h | 5 ++--- src/init.cpp | 2 +- src/instantx.cpp | 8 +++---- src/instantx.h | 4 ++-- src/masternode-payments.cpp | 42 +++++++++++++++++------------------ src/masternode-payments.h | 4 ++-- src/masternode-sync.cpp | 4 +--- src/masternode-sync.h | 3 --- src/masternodeman.cpp | 44 ++++++++++++++++++------------------- src/masternodeman.h | 6 ++--- src/privatesend-client.cpp | 17 +++++++------- src/privatesend-client.h | 4 +++- src/rpc/masternode.cpp | 13 ++++++++--- src/test/miner_tests.cpp | 2 +- 15 files changed, 85 insertions(+), 104 deletions(-) diff --git a/src/governance.cpp b/src/governance.cpp index 90967f7cfc277..93cb95f3ed647 100644 --- a/src/governance.cpp +++ b/src/governance.cpp @@ -23,8 +23,7 @@ const int CGovernanceManager::MAX_TIME_FUTURE_DEVIATION = 60*60; const int CGovernanceManager::RELIABLE_PROPAGATION_TIME = 60; CGovernanceManager::CGovernanceManager() - : pCurrentBlockIndex(NULL), - nTimeLastDiff(0), + : nTimeLastDiff(0), nCachedBlockHeight(0), mapObjects(), mapErasedGovernanceObjects(), @@ -149,10 +148,6 @@ void CGovernanceManager::ProcessMessage(CNode* pfrom, std::string& strCommand, C { // MAKE SURE WE HAVE A VALID REFERENCE TO THE TIP BEFORE CONTINUING - if(!pCurrentBlockIndex) { - LogPrintf("MNGOVERNANCEOBJECT -- pCurrentBlockIndex is NULL\n"); - return; - } if(!masternodeSync.IsMasternodeListSynced()) { LogPrint("gobject", "MNGOVERNANCEOBJECT -- masternode list not synced\n"); @@ -472,14 +467,8 @@ void CGovernanceManager::UpdateCachesAndClean() it->second.fDirtyCache = true; } - // DOUBLE CHECK THAT WE HAVE A VALID POINTER TO TIP - - if(!pCurrentBlockIndex) return; - CRateChecksGuard guard(false, *this); - LogPrint("gobject", "CGovernanceManager::UpdateCachesAndClean -- After pCurrentBlockIndex (not NULL)\n"); - // UPDATE CACHE FOR EACH OBJECT THAT IS FLAGGED DIRTYCACHE=TRUE object_m_it it = mapObjects.begin(); @@ -681,15 +670,7 @@ struct sortProposalsByVotes { void CGovernanceManager::DoMaintenance() { - // NOTHING TO DO IN LITEMODE - if(fLiteMode) { - return; - } - - // IF WE'RE NOT SYNCED, EXIT - if(!masternodeSync.IsSynced()) return; - - if(!pCurrentBlockIndex) return; + if(fLiteMode || !masternodeSync.IsSynced()) return; // CHECK OBJECTS WE'VE ASKED FOR, REMOVE OLD ENTRIES @@ -1400,12 +1381,8 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex *pindex) return; } - { - LOCK(cs); - pCurrentBlockIndex = pindex; - nCachedBlockHeight = pCurrentBlockIndex->nHeight; - LogPrint("gobject", "CGovernanceManager::UpdatedBlockTip pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight); - } + nCachedBlockHeight = pindex->nHeight; + LogPrint("gobject", "CGovernanceManager::UpdatedBlockTip -- nCachedBlockHeight: %d\n", nCachedBlockHeight); CheckPostponedObjects(); } diff --git a/src/governance.h b/src/governance.h index a85d47425715b..50dc9629c57e9 100644 --- a/src/governance.h +++ b/src/governance.h @@ -226,10 +226,9 @@ class CGovernanceManager static const int MAX_TIME_FUTURE_DEVIATION; static const int RELIABLE_PROPAGATION_TIME; - // Keep track of current block index - const CBlockIndex *pCurrentBlockIndex; - int64_t nTimeLastDiff; + + // keep track of current block height int nCachedBlockHeight; // keep track of the scanning errors diff --git a/src/init.cpp b/src/init.cpp index ae9c62c2beb6a..16bea9fd35217 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1972,7 +1972,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) // ********************************************************* Step 11c: update block tip in Dash modules - // force UpdatedBlockTip to initialize pCurrentBlockIndex for DS, MN payments and budgets + // force UpdatedBlockTip to initialize nCachedBlockHeight for DS, MN payments and budgets // but don't call it directly to prevent triggering of other listeners like zmq etc. // GetMainSignals().UpdatedBlockTip(chainActive.Tip()); mnodeman.UpdatedBlockTip(chainActive.Tip()); diff --git a/src/instantx.cpp b/src/instantx.cpp index aa9ef35b909ec..24a328e5c6d7f 100644 --- a/src/instantx.cpp +++ b/src/instantx.cpp @@ -605,7 +605,7 @@ int64_t CInstantSend::GetAverageMasternodeOrphanVoteTime() void CInstantSend::CheckAndRemove() { - if(!pCurrentBlockIndex) return; + if(!masternodeSync.IsMasternodeListSynced()) return; LOCK(cs_instantsend); @@ -615,7 +615,7 @@ void CInstantSend::CheckAndRemove() while(itLockCandidate != mapTxLockCandidates.end()) { CTxLockCandidate &txLockCandidate = itLockCandidate->second; uint256 txHash = txLockCandidate.GetHash(); - if(txLockCandidate.IsExpired(pCurrentBlockIndex->nHeight)) { + if(txLockCandidate.IsExpired(nCachedBlockHeight)) { LogPrintf("CInstantSend::CheckAndRemove -- Removing expired Transaction Lock Candidate: txid=%s\n", txHash.ToString()); std::map::iterator itOutpointLock = txLockCandidate.mapOutPointLocks.begin(); while(itOutpointLock != txLockCandidate.mapOutPointLocks.end()) { @@ -634,7 +634,7 @@ void CInstantSend::CheckAndRemove() // remove expired votes std::map::iterator itVote = mapTxLockVotes.begin(); while(itVote != mapTxLockVotes.end()) { - if(itVote->second.IsExpired(pCurrentBlockIndex->nHeight)) { + if(itVote->second.IsExpired(nCachedBlockHeight)) { LogPrint("instantsend", "CInstantSend::CheckAndRemove -- Removing expired vote: txid=%s masternode=%s\n", itVote->second.GetTxHash().ToString(), itVote->second.GetMasternodeOutpoint().ToStringShort()); mapTxLockVotes.erase(itVote++); @@ -803,7 +803,7 @@ void CInstantSend::Relay(const uint256& txHash) void CInstantSend::UpdatedBlockTip(const CBlockIndex *pindex) { - pCurrentBlockIndex = pindex; + nCachedBlockHeight = pindex->nHeight; } void CInstantSend::SyncTransaction(const CTransaction& tx, const CBlock* pblock) diff --git a/src/instantx.h b/src/instantx.h index 987ed258324d5..c508939813ee7 100644 --- a/src/instantx.h +++ b/src/instantx.h @@ -39,8 +39,8 @@ class CInstantSend private: static const int ORPHAN_VOTE_SECONDS = 60; - // Keep track of current block index - const CBlockIndex *pCurrentBlockIndex; + // Keep track of current block height + int nCachedBlockHeight; // maps for AlreadyHave std::map mapLockRequestAccepted; // tx hash - tx diff --git a/src/masternode-payments.cpp b/src/masternode-payments.cpp index a86e13dbb3460..ba1f899d2ce19 100644 --- a/src/masternode-payments.cpp +++ b/src/masternode-payments.cpp @@ -338,8 +338,6 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand, if(pfrom->nVersion < GetMinMasternodePaymentsProto()) return; - if(!pCurrentBlockIndex) return; - uint256 nHash = vote.GetHash(); pfrom->setAskFor.erase(nHash); @@ -347,7 +345,7 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand, { LOCK(cs_mapMasternodePaymentVotes); if(mapMasternodePaymentVotes.count(nHash)) { - LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- hash=%s, nHeight=%d seen\n", nHash.ToString(), pCurrentBlockIndex->nHeight); + LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- hash=%s, nHeight=%d seen\n", nHash.ToString(), nCachedBlockHeight); return; } @@ -358,14 +356,14 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand, mapMasternodePaymentVotes[nHash].MarkAsNotVerified(); } - int nFirstBlock = pCurrentBlockIndex->nHeight - GetStorageLimit(); - if(vote.nBlockHeight < nFirstBlock || vote.nBlockHeight > pCurrentBlockIndex->nHeight+20) { - LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- vote out of range: nFirstBlock=%d, nBlockHeight=%d, nHeight=%d\n", nFirstBlock, vote.nBlockHeight, pCurrentBlockIndex->nHeight); + int nFirstBlock = nCachedBlockHeight - GetStorageLimit(); + if(vote.nBlockHeight < nFirstBlock || vote.nBlockHeight > nCachedBlockHeight+20) { + LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- vote out of range: nFirstBlock=%d, nBlockHeight=%d, nHeight=%d\n", nFirstBlock, vote.nBlockHeight, nCachedBlockHeight); return; } std::string strError = ""; - if(!vote.IsValid(pfrom, pCurrentBlockIndex->nHeight, strError)) { + if(!vote.IsValid(pfrom, nCachedBlockHeight, strError)) { LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- invalid message, error: %s\n", strError); return; } @@ -384,7 +382,7 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand, } int nDos = 0; - if(!vote.CheckSignature(mnInfo.pubKeyMasternode, pCurrentBlockIndex->nHeight, nDos)) { + if(!vote.CheckSignature(mnInfo.pubKeyMasternode, nCachedBlockHeight, nDos)) { if(nDos) { LogPrintf("MASTERNODEPAYMENTVOTE -- ERROR: invalid signature\n"); Misbehaving(pfrom->GetId(), nDos); @@ -406,7 +404,7 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand, CBitcoinAddress address2(address1); LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- vote: address=%s, nBlockHeight=%d, nHeight=%d, prevout=%s, hash=%s new\n", - address2.ToString(), vote.nBlockHeight, pCurrentBlockIndex->nHeight, vote.vinMasternode.prevout.ToStringShort(), nHash.ToString()); + address2.ToString(), vote.nBlockHeight, nCachedBlockHeight, vote.vinMasternode.prevout.ToStringShort(), nHash.ToString()); if(AddPaymentVote(vote)){ vote.Relay(); @@ -450,13 +448,13 @@ bool CMasternodePayments::IsScheduled(CMasternode& mn, int nNotBlockHeight) { LOCK(cs_mapMasternodeBlocks); - if(!pCurrentBlockIndex) return false; + if(!masternodeSync.IsMasternodeListSynced()) return false; CScript mnpayee; mnpayee = GetScriptForDestination(mn.pubKeyCollateralAddress.GetID()); CScript payee; - for(int64_t h = pCurrentBlockIndex->nHeight; h <= pCurrentBlockIndex->nHeight + 8; h++){ + for(int64_t h = nCachedBlockHeight; h <= nCachedBlockHeight + 8; h++){ if(h == nNotBlockHeight) continue; if(mapMasternodeBlocks.count(h) && mapMasternodeBlocks[h].GetBestPayee(payee) && mnpayee == payee) { return true; @@ -633,7 +631,7 @@ bool CMasternodePayments::IsTransactionValid(const CTransaction& txNew, int nBlo void CMasternodePayments::CheckAndRemove() { - if(!pCurrentBlockIndex) return; + if(!masternodeSync.IsBlockchainSynced()) return; LOCK2(cs_mapMasternodeBlocks, cs_mapMasternodePaymentVotes); @@ -643,7 +641,7 @@ void CMasternodePayments::CheckAndRemove() while(it != mapMasternodePaymentVotes.end()) { CMasternodePaymentVote vote = (*it).second; - if(pCurrentBlockIndex->nHeight - vote.nBlockHeight > nLimit) { + if(nCachedBlockHeight - vote.nBlockHeight > nLimit) { LogPrint("mnpayments", "CMasternodePayments::CheckAndRemove -- Removing old Masternode payment: nBlockHeight=%d\n", vote.nBlockHeight); mapMasternodePaymentVotes.erase(it++); mapMasternodeBlocks.erase(vote.nBlockHeight); @@ -824,11 +822,11 @@ void CMasternodePayments::Sync(CNode* pnode) { LOCK(cs_mapMasternodeBlocks); - if(!pCurrentBlockIndex) return; + if(!masternodeSync.IsWinnersListSynced()) return; int nInvCount = 0; - for(int h = pCurrentBlockIndex->nHeight; h < pCurrentBlockIndex->nHeight + 20; h++) { + for(int h = nCachedBlockHeight; h < nCachedBlockHeight + 20; h++) { if(mapMasternodeBlocks.count(h)) { BOOST_FOREACH(CMasternodePayee& payee, mapMasternodeBlocks[h].vecPayees) { std::vector vecVoteHashes = payee.GetVoteHashes(); @@ -848,16 +846,16 @@ void CMasternodePayments::Sync(CNode* pnode) // Request low data/unknown payment blocks in batches directly from some node instead of/after preliminary Sync. void CMasternodePayments::RequestLowDataPaymentBlocks(CNode* pnode) { - if(!pCurrentBlockIndex) return; + if(!masternodeSync.IsMasternodeListSynced()) return; LOCK2(cs_main, cs_mapMasternodeBlocks); std::vector vToFetch; int nLimit = GetStorageLimit(); - const CBlockIndex *pindex = pCurrentBlockIndex; + const CBlockIndex *pindex = chainActive.Tip(); - while(pCurrentBlockIndex->nHeight - pindex->nHeight < nLimit) { + while(nCachedBlockHeight - pindex->nHeight < nLimit) { if(!mapMasternodeBlocks.count(pindex->nHeight)) { // We have no idea about this block height, let's ask vToFetch.push_back(CInv(MSG_MASTERNODE_PAYMENT_BLOCK, pindex->GetBlockHash())); @@ -949,8 +947,10 @@ int CMasternodePayments::GetStorageLimit() void CMasternodePayments::UpdatedBlockTip(const CBlockIndex *pindex) { - pCurrentBlockIndex = pindex; - LogPrint("mnpayments", "CMasternodePayments::UpdatedBlockTip -- pCurrentBlockIndex->nHeight=%d\n", pCurrentBlockIndex->nHeight); + if(!pindex) return; + + nCachedBlockHeight = pindex->nHeight; + LogPrint("mnpayments", "CMasternodePayments::UpdatedBlockTip -- nCachedBlockHeight=%d\n", nCachedBlockHeight); - ProcessBlock(pindex->nHeight + 10); + ProcessBlock(nCachedBlockHeight + 10); } diff --git a/src/masternode-payments.h b/src/masternode-payments.h index b4d2914184547..bb441740622de 100644 --- a/src/masternode-payments.h +++ b/src/masternode-payments.h @@ -172,8 +172,8 @@ class CMasternodePayments // ... but at least nMinBlocksToStore (payments blocks) const int nMinBlocksToStore; - // Keep track of current block index - const CBlockIndex *pCurrentBlockIndex; + // Keep track of current block height + int nCachedBlockHeight; public: std::map mapMasternodePaymentVotes; diff --git a/src/masternode-sync.cpp b/src/masternode-sync.cpp index d5c3322864b61..5db6301023bd0 100644 --- a/src/masternode-sync.cpp +++ b/src/masternode-sync.cpp @@ -145,7 +145,6 @@ void CMasternodeSync::ProcessTick() { static int nTick = 0; if(nTick++ % MASTERNODE_SYNC_TICK_SECONDS != 0) return; - if(!pCurrentBlockIndex) return; // reset the sync process if the last call to this function was more than 60 minutes ago (client was in sleep mode) static int64_t nTimeLastProcess = GetTime(); @@ -389,8 +388,7 @@ void CMasternodeSync::SendGovernanceSyncRequest(CNode* pnode) void CMasternodeSync::UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitialDownload) { - pCurrentBlockIndex = pindexNew; - if(fDebug) LogPrintf("CMasternodeSync::UpdatedBlockTip -- pCurrentBlockIndex->nHeight: %d fInitialDownload=%d\n", pCurrentBlockIndex->nHeight, fInitialDownload); + if(fDebug) LogPrintf("CMasternodeSync::UpdatedBlockTip -- pindexNew->nHeight: %d fInitialDownload=%d\n", pindexNew->nHeight, fInitialDownload); // nothing to do here if we failed to sync previousely, // just wait till status reset after a cooldown (see ProcessTick) if(IsFailed()) return; diff --git a/src/masternode-sync.h b/src/masternode-sync.h index f0c457b878617..2feae5b1dfcb0 100644 --- a/src/masternode-sync.h +++ b/src/masternode-sync.h @@ -46,9 +46,6 @@ class CMasternodeSync // ... or failed int64_t nTimeLastFailure; - // Keep track of current block index - const CBlockIndex *pCurrentBlockIndex; - void Fail(); void ClearFulfilledRequests(); diff --git a/src/masternodeman.cpp b/src/masternodeman.cpp index f92ef92f33c90..728202f3e4d35 100644 --- a/src/masternodeman.cpp +++ b/src/masternodeman.cpp @@ -210,8 +210,7 @@ void CMasternodeMan::CheckAndRemove() it = vMasternodes.erase(it); fMasternodesRemoved = true; } else { - bool fAsk = pCurrentBlockIndex && - (nAskForMnbRecovery > 0) && + bool fAsk = (nAskForMnbRecovery > 0) && masternodeSync.IsSynced() && it->IsNewStartRequired() && !IsMnbRecoveryRequested(hash); @@ -220,7 +219,7 @@ void CMasternodeMan::CheckAndRemove() std::set setRequested; // calulate only once and only when it's needed if(vecMasternodeRanks.empty()) { - int nRandomBlockHeight = GetRandInt(pCurrentBlockIndex->nHeight); + int nRandomBlockHeight = GetRandInt(nCachedBlockHeight); vecMasternodeRanks = GetMasternodeRanks(nRandomBlockHeight); } bool fAskedForMnbRecovery = false; @@ -321,7 +320,7 @@ void CMasternodeMan::CheckAndRemove() std::map::iterator it3 = mWeAskedForVerification.begin(); while(it3 != mWeAskedForVerification.end()){ - if(it3->second.nBlockHeight < pCurrentBlockIndex->nHeight - MAX_POSE_BLOCKS) { + if(it3->second.nBlockHeight < nCachedBlockHeight - MAX_POSE_BLOCKS) { mWeAskedForVerification.erase(it3++); } else { ++it3; @@ -344,7 +343,7 @@ void CMasternodeMan::CheckAndRemove() // remove expired mapSeenMasternodeVerification std::map::iterator itv2 = mapSeenMasternodeVerification.begin(); while(itv2 != mapSeenMasternodeVerification.end()){ - if((*itv2).second.nBlockHeight < pCurrentBlockIndex->nHeight - MAX_POSE_BLOCKS){ + if((*itv2).second.nBlockHeight < nCachedBlockHeight - MAX_POSE_BLOCKS){ LogPrint("masternode", "CMasternodeMan::CheckAndRemove -- Removing expired Masternode verification: hash=%s\n", (*itv2).first.ToString()); mapSeenMasternodeVerification.erase(itv2++); } else { @@ -541,15 +540,17 @@ bool CMasternodeMan::Has(const CTxIn& vin) // CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(bool fFilterSigTime, int& nCount) { - if(!pCurrentBlockIndex) { - nCount = 0; - return NULL; - } - return GetNextMasternodeInQueueForPayment(pCurrentBlockIndex->nHeight, fFilterSigTime, nCount); + return GetNextMasternodeInQueueForPayment(nCachedBlockHeight, fFilterSigTime, nCount); } CMasternode* CMasternodeMan::GetNextMasternodeInQueueForPayment(int nBlockHeight, bool fFilterSigTime, int& nCount) { + if (!masternodeSync.IsWinnersListSynced()) { + // without winner list we can't reliably find the next winner anyway + nCount = 0; + return NULL; + } + // Need LOCK2 here to ensure consistent locking order because the GetBlockHash call below locks cs_main LOCK2(cs_main,cs); @@ -961,7 +962,7 @@ void CMasternodeMan::DoFullVerificationStep() if(activeMasternode.vin == CTxIn()) return; if(!masternodeSync.IsSynced()) return; - std::vector > vecMasternodeRanks = GetMasternodeRanks(pCurrentBlockIndex->nHeight - 1, MIN_POSE_PROTO_VERSION); + std::vector > vecMasternodeRanks = GetMasternodeRanks(nCachedBlockHeight - 1, MIN_POSE_PROTO_VERSION); // Need LOCK2 here to ensure consistent locking order because the SendVerifyRequest call below locks cs_main // through GetHeight() signal in ConnectNode @@ -1106,7 +1107,7 @@ bool CMasternodeMan::SendVerifyRequest(const CAddress& addr, const std::vectornHeight - 1); + CMasternodeVerification mnv(addr, GetRandInt(999999), nCachedBlockHeight - 1); mWeAskedForVerification[addr] = mnv; LogPrintf("CMasternodeMan::SendVerifyRequest -- verifying node using nonce %d addr=%s\n", mnv.nonce, addr.ToString()); g_connman->PushMessage(pnode, NetMsgType::MNVERIFY, mnv); @@ -1274,9 +1275,9 @@ void CMasternodeMan::ProcessVerifyBroadcast(CNode* pnode, const CMasternodeVerif mapSeenMasternodeVerification[mnv.GetHash()] = mnv; // we don't care about history - if(mnv.nBlockHeight < pCurrentBlockIndex->nHeight - MAX_POSE_BLOCKS) { + if(mnv.nBlockHeight < nCachedBlockHeight - MAX_POSE_BLOCKS) { LogPrint("masternode", "CMasternodeMan::ProcessVerifyBroadcast -- Outdated: current block %d, verification block %d, peer=%d\n", - pCurrentBlockIndex->nHeight, mnv.nBlockHeight, pnode->id); + nCachedBlockHeight, mnv.nBlockHeight, pnode->id); return; } @@ -1495,12 +1496,11 @@ bool CMasternodeMan::CheckMnbAndUpdateMasternodeList(CNode* pfrom, CMasternodeBr return true; } -void CMasternodeMan::UpdateLastPaid() +void CMasternodeMan::UpdateLastPaid(const CBlockIndex* pindex) { LOCK(cs); - if(fLiteMode || !pCurrentBlockIndex) return; - if(!masternodeSync.IsWinnersListSynced() || vMasternodes.empty()) return; + if(fLiteMode || !masternodeSync.IsWinnersListSynced() || vMasternodes.empty()) return; static bool IsFirstRun = true; // Do full scan on first run or if we are not a masternode @@ -1508,10 +1508,10 @@ void CMasternodeMan::UpdateLastPaid() int nMaxBlocksToScanBack = (IsFirstRun || !fMasterNode) ? mnpayments.GetStorageLimit() : LAST_PAID_SCAN_BLOCKS; // LogPrint("mnpayments", "CMasternodeMan::UpdateLastPaid -- nHeight=%d, nMaxBlocksToScanBack=%d, IsFirstRun=%s\n", - // pCurrentBlockIndex->nHeight, nMaxBlocksToScanBack, IsFirstRun ? "true" : "false"); + // nCachedBlockHeight, nMaxBlocksToScanBack, IsFirstRun ? "true" : "false"); BOOST_FOREACH(CMasternode& mn, vMasternodes) { - mn.UpdateLastPaid(pCurrentBlockIndex, nMaxBlocksToScanBack); + mn.UpdateLastPaid(pindex, nMaxBlocksToScanBack); } IsFirstRun = false; @@ -1666,14 +1666,14 @@ void CMasternodeMan::SetMasternodeLastPing(const CTxIn& vin, const CMasternodePi void CMasternodeMan::UpdatedBlockTip(const CBlockIndex *pindex) { - pCurrentBlockIndex = pindex; - LogPrint("masternode", "CMasternodeMan::UpdatedBlockTip -- pCurrentBlockIndex->nHeight=%d\n", pCurrentBlockIndex->nHeight); + nCachedBlockHeight = pindex->nHeight; + LogPrint("masternode", "CMasternodeMan::UpdatedBlockTip -- nCachedBlockHeight=%d\n", nCachedBlockHeight); CheckSameAddr(); if(fMasterNode) { // normal wallet does not need to update this every block, doing update on rpc call should be enough - UpdateLastPaid(); + UpdateLastPaid(pindex); } } diff --git a/src/masternodeman.h b/src/masternodeman.h index 9e1f7d0934a98..10432e2bfeff4 100644 --- a/src/masternodeman.h +++ b/src/masternodeman.h @@ -114,8 +114,8 @@ class CMasternodeMan // critical section to protect the inner data structures mutable CCriticalSection cs; - // Keep track of current block index - const CBlockIndex *pCurrentBlockIndex; + // Keep track of current block height + int nCachedBlockHeight; // map to hold all MNs std::vector vMasternodes; @@ -321,7 +321,7 @@ class CMasternodeMan bool CheckMnbAndUpdateMasternodeList(CNode* pfrom, CMasternodeBroadcast mnb, int& nDos); bool IsMnbRecoveryRequested(const uint256& hash) { return mMnbRecoveryRequests.count(hash); } - void UpdateLastPaid(); + void UpdateLastPaid(const CBlockIndex* pindex); bool UpdateLastDsq(const CTxIn& vin); void CheckAndRebuildMasternodeIndex(); diff --git a/src/privatesend-client.cpp b/src/privatesend-client.cpp index bb9ecc2f9544b..2e07db73269c1 100644 --- a/src/privatesend-client.cpp +++ b/src/privatesend-client.cpp @@ -242,7 +242,7 @@ std::string CPrivateSendClient::GetStatus() nStatusMessageProgress += 10; std::string strSuffix = ""; - if((pCurrentBlockIndex && pCurrentBlockIndex->nHeight - nCachedLastSuccessBlock < nMinBlockSpacing) || !masternodeSync.IsBlockchainSynced()) + if((nCachedBlockHeight - nCachedLastSuccessBlock < nMinBlockSpacing) || !masternodeSync.IsBlockchainSynced()) return strAutoDenomResult; switch(nState) { @@ -578,7 +578,7 @@ void CPrivateSendClient::CompletedTransaction(PoolMessage nMessageID) if(nMessageID == MSG_SUCCESS) { LogPrintf("CompletedTransaction -- success\n"); - nCachedLastSuccessBlock = pCurrentBlockIndex->nHeight; + nCachedLastSuccessBlock = nCachedBlockHeight; } else { LogPrintf("CompletedTransaction -- error\n"); } @@ -658,7 +658,8 @@ bool CPrivateSendClient::CheckAutomaticBackup() // bool CPrivateSendClient::DoAutomaticDenominating(CConnman& connman, bool fDryRun) { - if(!fEnablePrivateSend || fMasterNode || !pCurrentBlockIndex) return false; + if(fMasterNode) return false; // no client-side mixing on masternodes + if(!fEnablePrivateSend) return false; if(!pwalletMain || pwalletMain->IsLocked(true)) return false; if(nState != POOL_STATE_IDLE) return false; @@ -686,7 +687,7 @@ bool CPrivateSendClient::DoAutomaticDenominating(CConnman& connman, bool fDryRun return false; } - if(!fPrivateSendMultiSession && pCurrentBlockIndex->nHeight - nCachedLastSuccessBlock < nMinBlockSpacing) { + if(!fPrivateSendMultiSession && nCachedBlockHeight - nCachedLastSuccessBlock < nMinBlockSpacing) { LogPrintf("CPrivateSendClient::DoAutomaticDenominating -- Last successful PrivateSend action was too recent\n"); strAutoDenomResult = _("Last successful PrivateSend action was too recent."); return false; @@ -1214,7 +1215,7 @@ bool CPrivateSendClient::MakeCollateralAmounts(const CompactTallyItem& tallyItem return false; } - nCachedLastSuccessBlock = pCurrentBlockIndex->nHeight; + nCachedLastSuccessBlock = nCachedBlockHeight; return true; } @@ -1362,7 +1363,7 @@ bool CPrivateSendClient::CreateDenominated(const CompactTallyItem& tallyItem, bo } // use the same nCachedLastSuccessBlock as for DS mixing to prevent race - nCachedLastSuccessBlock = pCurrentBlockIndex->nHeight; + nCachedLastSuccessBlock = nCachedBlockHeight; LogPrintf("CPrivateSendClient::CreateDenominated -- txid=%s\n", wtx.GetHash().GetHex()); return true; @@ -1387,8 +1388,8 @@ void CPrivateSendClient::SetState(PoolState nStateNew) void CPrivateSendClient::UpdatedBlockTip(const CBlockIndex *pindex) { - pCurrentBlockIndex = pindex; - LogPrint("privatesend", "CPrivateSendClient::UpdatedBlockTip -- pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight); + nCachedBlockHeight = pindex->nHeight; + LogPrint("privatesend", "CPrivateSendClient::UpdatedBlockTip -- nCachedBlockHeight: %d\n", nCachedBlockHeight); if(!fLiteMode && masternodeSync.IsMasternodeListSynced()) { NewBlock(); diff --git a/src/privatesend-client.h b/src/privatesend-client.h index 10a152e8b1510..056bfbbe00173 100644 --- a/src/privatesend-client.h +++ b/src/privatesend-client.h @@ -43,7 +43,9 @@ class CPrivateSendClient : public CPrivateSendBase int nCachedLastSuccessBlock; int nMinBlockSpacing; //required blocks between mixes - const CBlockIndex *pCurrentBlockIndex; // Keep track of current block index + + // Keep track of current block height + int nCachedBlockHeight; int nEntriesCount; bool fLastEntryAccepted; diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 611c2b95387c1..06e17357e9ab8 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -188,11 +188,13 @@ UniValue masternode(const UniValue& params, bool fHelp) int nCount; int nHeight; CMasternode* winner = NULL; + CBlockIndex* pindex = NULL; { LOCK(cs_main); - nHeight = chainActive.Height() + (strCommand == "current" ? 1 : 10); + pindex = chainActive.Tip(); } - mnodeman.UpdateLastPaid(); + nHeight = pindex->nHeight + (strCommand == "current" ? 1 : 10); + mnodeman.UpdateLastPaid(pindex); winner = mnodeman.GetNextMasternodeInQueueForPayment(nHeight, true, nCount); if(!winner) return "unknown"; @@ -487,7 +489,12 @@ UniValue masternodelist(const UniValue& params, bool fHelp) } if (strMode == "full" || strMode == "lastpaidtime" || strMode == "lastpaidblock") { - mnodeman.UpdateLastPaid(); + CBlockIndex* pindex = NULL; + { + LOCK(cs_main); + pindex = chainActive.Tip(); + } + mnodeman.UpdateLastPaid(pindex); } UniValue obj(UniValue::VOBJ); diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp index 272c83f119e4d..e84562a6eae3e 100644 --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity) LOCK(cs_main); fCheckpointsEnabled = false; - // force UpdatedBlockTip to initialize pCurrentBlockIndex + // force UpdatedBlockTip to initialize nCachedBlockHeight mnpayments.UpdatedBlockTip(chainActive.Tip()); // Simple block creation, nothing special yet: