Skip to content

Commit

Permalink
drop pCurrentBlockIndex and use cached block height instead (nCachedB…
Browse files Browse the repository at this point in the history
…lockHeight) (#1579)
  • Loading branch information
UdjinM6 authored Aug 25, 2017
1 parent 23582ae commit fe81d64
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 104 deletions.
31 changes: 4 additions & 27 deletions src/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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();
}
Expand Down
5 changes: 2 additions & 3 deletions src/governance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 4 additions & 4 deletions src/instantx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ int64_t CInstantSend::GetAverageMasternodeOrphanVoteTime()

void CInstantSend::CheckAndRemove()
{
if(!pCurrentBlockIndex) return;
if(!masternodeSync.IsMasternodeListSynced()) return;

LOCK(cs_instantsend);

Expand All @@ -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<COutPoint, COutPointLock>::iterator itOutpointLock = txLockCandidate.mapOutPointLocks.begin();
while(itOutpointLock != txLockCandidate.mapOutPointLocks.end()) {
Expand All @@ -634,7 +634,7 @@ void CInstantSend::CheckAndRemove()
// remove expired votes
std::map<uint256, CTxLockVote>::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++);
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/instantx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint256, CTxLockRequest> mapLockRequestAccepted; // tx hash - tx
Expand Down
42 changes: 21 additions & 21 deletions src/masternode-payments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,14 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand,

if(pfrom->nVersion < GetMinMasternodePaymentsProto()) return;

if(!pCurrentBlockIndex) return;

uint256 nHash = vote.GetHash();

pfrom->setAskFor.erase(nHash);

{
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;
}

Expand All @@ -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;
}
Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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<uint256> vecVoteHashes = payee.GetVoteHashes();
Expand All @@ -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<CInv> 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()));
Expand Down Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions src/masternode-payments.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint256, CMasternodePaymentVote> mapMasternodePaymentVotes;
Expand Down
4 changes: 1 addition & 3 deletions src/masternode-sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions src/masternode-sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ class CMasternodeSync
// ... or failed
int64_t nTimeLastFailure;

// Keep track of current block index
const CBlockIndex *pCurrentBlockIndex;

void Fail();
void ClearFulfilledRequests();

Expand Down
Loading

0 comments on commit fe81d64

Please sign in to comment.