Skip to content

Commit fe81d64

Browse files
authored
drop pCurrentBlockIndex and use cached block height instead (nCachedBlockHeight) (#1579)
1 parent 23582ae commit fe81d64

15 files changed

+85
-104
lines changed

src/governance.cpp

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ const int CGovernanceManager::MAX_TIME_FUTURE_DEVIATION = 60*60;
2323
const int CGovernanceManager::RELIABLE_PROPAGATION_TIME = 60;
2424

2525
CGovernanceManager::CGovernanceManager()
26-
: pCurrentBlockIndex(NULL),
27-
nTimeLastDiff(0),
26+
: nTimeLastDiff(0),
2827
nCachedBlockHeight(0),
2928
mapObjects(),
3029
mapErasedGovernanceObjects(),
@@ -149,10 +148,6 @@ void CGovernanceManager::ProcessMessage(CNode* pfrom, std::string& strCommand, C
149148
{
150149
// MAKE SURE WE HAVE A VALID REFERENCE TO THE TIP BEFORE CONTINUING
151150

152-
if(!pCurrentBlockIndex) {
153-
LogPrintf("MNGOVERNANCEOBJECT -- pCurrentBlockIndex is NULL\n");
154-
return;
155-
}
156151

157152
if(!masternodeSync.IsMasternodeListSynced()) {
158153
LogPrint("gobject", "MNGOVERNANCEOBJECT -- masternode list not synced\n");
@@ -472,14 +467,8 @@ void CGovernanceManager::UpdateCachesAndClean()
472467
it->second.fDirtyCache = true;
473468
}
474469

475-
// DOUBLE CHECK THAT WE HAVE A VALID POINTER TO TIP
476-
477-
if(!pCurrentBlockIndex) return;
478-
479470
CRateChecksGuard guard(false, *this);
480471

481-
LogPrint("gobject", "CGovernanceManager::UpdateCachesAndClean -- After pCurrentBlockIndex (not NULL)\n");
482-
483472
// UPDATE CACHE FOR EACH OBJECT THAT IS FLAGGED DIRTYCACHE=TRUE
484473

485474
object_m_it it = mapObjects.begin();
@@ -681,15 +670,7 @@ struct sortProposalsByVotes {
681670

682671
void CGovernanceManager::DoMaintenance()
683672
{
684-
// NOTHING TO DO IN LITEMODE
685-
if(fLiteMode) {
686-
return;
687-
}
688-
689-
// IF WE'RE NOT SYNCED, EXIT
690-
if(!masternodeSync.IsSynced()) return;
691-
692-
if(!pCurrentBlockIndex) return;
673+
if(fLiteMode || !masternodeSync.IsSynced()) return;
693674

694675
// CHECK OBJECTS WE'VE ASKED FOR, REMOVE OLD ENTRIES
695676

@@ -1400,12 +1381,8 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex *pindex)
14001381
return;
14011382
}
14021383

1403-
{
1404-
LOCK(cs);
1405-
pCurrentBlockIndex = pindex;
1406-
nCachedBlockHeight = pCurrentBlockIndex->nHeight;
1407-
LogPrint("gobject", "CGovernanceManager::UpdatedBlockTip pCurrentBlockIndex->nHeight: %d\n", pCurrentBlockIndex->nHeight);
1408-
}
1384+
nCachedBlockHeight = pindex->nHeight;
1385+
LogPrint("gobject", "CGovernanceManager::UpdatedBlockTip -- nCachedBlockHeight: %d\n", nCachedBlockHeight);
14091386

14101387
CheckPostponedObjects();
14111388
}

src/governance.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,9 @@ class CGovernanceManager
226226
static const int MAX_TIME_FUTURE_DEVIATION;
227227
static const int RELIABLE_PROPAGATION_TIME;
228228

229-
// Keep track of current block index
230-
const CBlockIndex *pCurrentBlockIndex;
231-
232229
int64_t nTimeLastDiff;
230+
231+
// keep track of current block height
233232
int nCachedBlockHeight;
234233

235234
// keep track of the scanning errors

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1972,7 +1972,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
19721972

19731973
// ********************************************************* Step 11c: update block tip in Dash modules
19741974

1975-
// force UpdatedBlockTip to initialize pCurrentBlockIndex for DS, MN payments and budgets
1975+
// force UpdatedBlockTip to initialize nCachedBlockHeight for DS, MN payments and budgets
19761976
// but don't call it directly to prevent triggering of other listeners like zmq etc.
19771977
// GetMainSignals().UpdatedBlockTip(chainActive.Tip());
19781978
mnodeman.UpdatedBlockTip(chainActive.Tip());

src/instantx.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ int64_t CInstantSend::GetAverageMasternodeOrphanVoteTime()
605605

606606
void CInstantSend::CheckAndRemove()
607607
{
608-
if(!pCurrentBlockIndex) return;
608+
if(!masternodeSync.IsMasternodeListSynced()) return;
609609

610610
LOCK(cs_instantsend);
611611

@@ -615,7 +615,7 @@ void CInstantSend::CheckAndRemove()
615615
while(itLockCandidate != mapTxLockCandidates.end()) {
616616
CTxLockCandidate &txLockCandidate = itLockCandidate->second;
617617
uint256 txHash = txLockCandidate.GetHash();
618-
if(txLockCandidate.IsExpired(pCurrentBlockIndex->nHeight)) {
618+
if(txLockCandidate.IsExpired(nCachedBlockHeight)) {
619619
LogPrintf("CInstantSend::CheckAndRemove -- Removing expired Transaction Lock Candidate: txid=%s\n", txHash.ToString());
620620
std::map<COutPoint, COutPointLock>::iterator itOutpointLock = txLockCandidate.mapOutPointLocks.begin();
621621
while(itOutpointLock != txLockCandidate.mapOutPointLocks.end()) {
@@ -634,7 +634,7 @@ void CInstantSend::CheckAndRemove()
634634
// remove expired votes
635635
std::map<uint256, CTxLockVote>::iterator itVote = mapTxLockVotes.begin();
636636
while(itVote != mapTxLockVotes.end()) {
637-
if(itVote->second.IsExpired(pCurrentBlockIndex->nHeight)) {
637+
if(itVote->second.IsExpired(nCachedBlockHeight)) {
638638
LogPrint("instantsend", "CInstantSend::CheckAndRemove -- Removing expired vote: txid=%s masternode=%s\n",
639639
itVote->second.GetTxHash().ToString(), itVote->second.GetMasternodeOutpoint().ToStringShort());
640640
mapTxLockVotes.erase(itVote++);
@@ -803,7 +803,7 @@ void CInstantSend::Relay(const uint256& txHash)
803803

804804
void CInstantSend::UpdatedBlockTip(const CBlockIndex *pindex)
805805
{
806-
pCurrentBlockIndex = pindex;
806+
nCachedBlockHeight = pindex->nHeight;
807807
}
808808

809809
void CInstantSend::SyncTransaction(const CTransaction& tx, const CBlock* pblock)

src/instantx.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class CInstantSend
3939
private:
4040
static const int ORPHAN_VOTE_SECONDS = 60;
4141

42-
// Keep track of current block index
43-
const CBlockIndex *pCurrentBlockIndex;
42+
// Keep track of current block height
43+
int nCachedBlockHeight;
4444

4545
// maps for AlreadyHave
4646
std::map<uint256, CTxLockRequest> mapLockRequestAccepted; // tx hash - tx

src/masternode-payments.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,14 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand,
338338

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

341-
if(!pCurrentBlockIndex) return;
342-
343341
uint256 nHash = vote.GetHash();
344342

345343
pfrom->setAskFor.erase(nHash);
346344

347345
{
348346
LOCK(cs_mapMasternodePaymentVotes);
349347
if(mapMasternodePaymentVotes.count(nHash)) {
350-
LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- hash=%s, nHeight=%d seen\n", nHash.ToString(), pCurrentBlockIndex->nHeight);
348+
LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- hash=%s, nHeight=%d seen\n", nHash.ToString(), nCachedBlockHeight);
351349
return;
352350
}
353351

@@ -358,14 +356,14 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand,
358356
mapMasternodePaymentVotes[nHash].MarkAsNotVerified();
359357
}
360358

361-
int nFirstBlock = pCurrentBlockIndex->nHeight - GetStorageLimit();
362-
if(vote.nBlockHeight < nFirstBlock || vote.nBlockHeight > pCurrentBlockIndex->nHeight+20) {
363-
LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- vote out of range: nFirstBlock=%d, nBlockHeight=%d, nHeight=%d\n", nFirstBlock, vote.nBlockHeight, pCurrentBlockIndex->nHeight);
359+
int nFirstBlock = nCachedBlockHeight - GetStorageLimit();
360+
if(vote.nBlockHeight < nFirstBlock || vote.nBlockHeight > nCachedBlockHeight+20) {
361+
LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- vote out of range: nFirstBlock=%d, nBlockHeight=%d, nHeight=%d\n", nFirstBlock, vote.nBlockHeight, nCachedBlockHeight);
364362
return;
365363
}
366364

367365
std::string strError = "";
368-
if(!vote.IsValid(pfrom, pCurrentBlockIndex->nHeight, strError)) {
366+
if(!vote.IsValid(pfrom, nCachedBlockHeight, strError)) {
369367
LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- invalid message, error: %s\n", strError);
370368
return;
371369
}
@@ -384,7 +382,7 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand,
384382
}
385383

386384
int nDos = 0;
387-
if(!vote.CheckSignature(mnInfo.pubKeyMasternode, pCurrentBlockIndex->nHeight, nDos)) {
385+
if(!vote.CheckSignature(mnInfo.pubKeyMasternode, nCachedBlockHeight, nDos)) {
388386
if(nDos) {
389387
LogPrintf("MASTERNODEPAYMENTVOTE -- ERROR: invalid signature\n");
390388
Misbehaving(pfrom->GetId(), nDos);
@@ -406,7 +404,7 @@ void CMasternodePayments::ProcessMessage(CNode* pfrom, std::string& strCommand,
406404
CBitcoinAddress address2(address1);
407405

408406
LogPrint("mnpayments", "MASTERNODEPAYMENTVOTE -- vote: address=%s, nBlockHeight=%d, nHeight=%d, prevout=%s, hash=%s new\n",
409-
address2.ToString(), vote.nBlockHeight, pCurrentBlockIndex->nHeight, vote.vinMasternode.prevout.ToStringShort(), nHash.ToString());
407+
address2.ToString(), vote.nBlockHeight, nCachedBlockHeight, vote.vinMasternode.prevout.ToStringShort(), nHash.ToString());
410408

411409
if(AddPaymentVote(vote)){
412410
vote.Relay();
@@ -450,13 +448,13 @@ bool CMasternodePayments::IsScheduled(CMasternode& mn, int nNotBlockHeight)
450448
{
451449
LOCK(cs_mapMasternodeBlocks);
452450

453-
if(!pCurrentBlockIndex) return false;
451+
if(!masternodeSync.IsMasternodeListSynced()) return false;
454452

455453
CScript mnpayee;
456454
mnpayee = GetScriptForDestination(mn.pubKeyCollateralAddress.GetID());
457455

458456
CScript payee;
459-
for(int64_t h = pCurrentBlockIndex->nHeight; h <= pCurrentBlockIndex->nHeight + 8; h++){
457+
for(int64_t h = nCachedBlockHeight; h <= nCachedBlockHeight + 8; h++){
460458
if(h == nNotBlockHeight) continue;
461459
if(mapMasternodeBlocks.count(h) && mapMasternodeBlocks[h].GetBestPayee(payee) && mnpayee == payee) {
462460
return true;
@@ -633,7 +631,7 @@ bool CMasternodePayments::IsTransactionValid(const CTransaction& txNew, int nBlo
633631

634632
void CMasternodePayments::CheckAndRemove()
635633
{
636-
if(!pCurrentBlockIndex) return;
634+
if(!masternodeSync.IsBlockchainSynced()) return;
637635

638636
LOCK2(cs_mapMasternodeBlocks, cs_mapMasternodePaymentVotes);
639637

@@ -643,7 +641,7 @@ void CMasternodePayments::CheckAndRemove()
643641
while(it != mapMasternodePaymentVotes.end()) {
644642
CMasternodePaymentVote vote = (*it).second;
645643

646-
if(pCurrentBlockIndex->nHeight - vote.nBlockHeight > nLimit) {
644+
if(nCachedBlockHeight - vote.nBlockHeight > nLimit) {
647645
LogPrint("mnpayments", "CMasternodePayments::CheckAndRemove -- Removing old Masternode payment: nBlockHeight=%d\n", vote.nBlockHeight);
648646
mapMasternodePaymentVotes.erase(it++);
649647
mapMasternodeBlocks.erase(vote.nBlockHeight);
@@ -824,11 +822,11 @@ void CMasternodePayments::Sync(CNode* pnode)
824822
{
825823
LOCK(cs_mapMasternodeBlocks);
826824

827-
if(!pCurrentBlockIndex) return;
825+
if(!masternodeSync.IsWinnersListSynced()) return;
828826

829827
int nInvCount = 0;
830828

831-
for(int h = pCurrentBlockIndex->nHeight; h < pCurrentBlockIndex->nHeight + 20; h++) {
829+
for(int h = nCachedBlockHeight; h < nCachedBlockHeight + 20; h++) {
832830
if(mapMasternodeBlocks.count(h)) {
833831
BOOST_FOREACH(CMasternodePayee& payee, mapMasternodeBlocks[h].vecPayees) {
834832
std::vector<uint256> vecVoteHashes = payee.GetVoteHashes();
@@ -848,16 +846,16 @@ void CMasternodePayments::Sync(CNode* pnode)
848846
// Request low data/unknown payment blocks in batches directly from some node instead of/after preliminary Sync.
849847
void CMasternodePayments::RequestLowDataPaymentBlocks(CNode* pnode)
850848
{
851-
if(!pCurrentBlockIndex) return;
849+
if(!masternodeSync.IsMasternodeListSynced()) return;
852850

853851
LOCK2(cs_main, cs_mapMasternodeBlocks);
854852

855853
std::vector<CInv> vToFetch;
856854
int nLimit = GetStorageLimit();
857855

858-
const CBlockIndex *pindex = pCurrentBlockIndex;
856+
const CBlockIndex *pindex = chainActive.Tip();
859857

860-
while(pCurrentBlockIndex->nHeight - pindex->nHeight < nLimit) {
858+
while(nCachedBlockHeight - pindex->nHeight < nLimit) {
861859
if(!mapMasternodeBlocks.count(pindex->nHeight)) {
862860
// We have no idea about this block height, let's ask
863861
vToFetch.push_back(CInv(MSG_MASTERNODE_PAYMENT_BLOCK, pindex->GetBlockHash()));
@@ -949,8 +947,10 @@ int CMasternodePayments::GetStorageLimit()
949947

950948
void CMasternodePayments::UpdatedBlockTip(const CBlockIndex *pindex)
951949
{
952-
pCurrentBlockIndex = pindex;
953-
LogPrint("mnpayments", "CMasternodePayments::UpdatedBlockTip -- pCurrentBlockIndex->nHeight=%d\n", pCurrentBlockIndex->nHeight);
950+
if(!pindex) return;
951+
952+
nCachedBlockHeight = pindex->nHeight;
953+
LogPrint("mnpayments", "CMasternodePayments::UpdatedBlockTip -- nCachedBlockHeight=%d\n", nCachedBlockHeight);
954954

955-
ProcessBlock(pindex->nHeight + 10);
955+
ProcessBlock(nCachedBlockHeight + 10);
956956
}

src/masternode-payments.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ class CMasternodePayments
172172
// ... but at least nMinBlocksToStore (payments blocks)
173173
const int nMinBlocksToStore;
174174

175-
// Keep track of current block index
176-
const CBlockIndex *pCurrentBlockIndex;
175+
// Keep track of current block height
176+
int nCachedBlockHeight;
177177

178178
public:
179179
std::map<uint256, CMasternodePaymentVote> mapMasternodePaymentVotes;

src/masternode-sync.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ void CMasternodeSync::ProcessTick()
145145
{
146146
static int nTick = 0;
147147
if(nTick++ % MASTERNODE_SYNC_TICK_SECONDS != 0) return;
148-
if(!pCurrentBlockIndex) return;
149148

150149
// reset the sync process if the last call to this function was more than 60 minutes ago (client was in sleep mode)
151150
static int64_t nTimeLastProcess = GetTime();
@@ -389,8 +388,7 @@ void CMasternodeSync::SendGovernanceSyncRequest(CNode* pnode)
389388

390389
void CMasternodeSync::UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitialDownload)
391390
{
392-
pCurrentBlockIndex = pindexNew;
393-
if(fDebug) LogPrintf("CMasternodeSync::UpdatedBlockTip -- pCurrentBlockIndex->nHeight: %d fInitialDownload=%d\n", pCurrentBlockIndex->nHeight, fInitialDownload);
391+
if(fDebug) LogPrintf("CMasternodeSync::UpdatedBlockTip -- pindexNew->nHeight: %d fInitialDownload=%d\n", pindexNew->nHeight, fInitialDownload);
394392
// nothing to do here if we failed to sync previousely,
395393
// just wait till status reset after a cooldown (see ProcessTick)
396394
if(IsFailed()) return;

src/masternode-sync.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ class CMasternodeSync
4646
// ... or failed
4747
int64_t nTimeLastFailure;
4848

49-
// Keep track of current block index
50-
const CBlockIndex *pCurrentBlockIndex;
51-
5249
void Fail();
5350
void ClearFulfilledRequests();
5451

0 commit comments

Comments
 (0)