Skip to content

Commit def064f

Browse files
committed
[Refactor] Cleanup usage of chainActive.{Tip(),Height()} in a few places
1 parent 026dcc9 commit def064f

File tree

7 files changed

+49
-64
lines changed

7 files changed

+49
-64
lines changed

src/main.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,12 +1623,16 @@ void CheckForkWarningConditions()
16231623
if (IsInitialBlockDownload())
16241624
return;
16251625

1626+
const CBlockIndex* pChainTip = chainActive.Tip();
1627+
if (!pChainTip)
1628+
return;
1629+
16261630
// If our best fork is no longer within 72 blocks (+/- 3 hours if no one mines it)
16271631
// of our head, drop it
1628-
if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
1629-
pindexBestForkTip = NULL;
1632+
if (pindexBestForkTip && pChainTip->nHeight - pindexBestForkTip->nHeight >= 72)
1633+
pindexBestForkTip = nullptr;
16301634

1631-
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6))) {
1635+
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > pChainTip->nChainWork + (GetBlockProof(*pChainTip) * 6))) {
16321636
if (!fLargeWorkForkFound && pindexBestForkBase) {
16331637
if (pindexBestForkBase->phashBlock) {
16341638
std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
@@ -1713,10 +1717,12 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
17131717
log(pindexNew->nChainWork.getdouble()) / log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
17141718
pindexNew->GetBlockTime()));
17151719

1716-
const CBlockIndex* pChainTip = mapBlockIndex[chainActive.Tip()->GetBlockHash()];
1720+
const CBlockIndex* pChainTip = chainActive.Tip();
1721+
assert(pChainTip);
17171722
LogPrintf("InvalidChainFound: current best=%s height=%d log2_work=%.16f date=%s\n",
17181723
pChainTip->GetBlockHash().GetHex(), pChainTip->nHeight, log(pChainTip->nChainWork.getdouble()) / log(2.0),
17191724
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", pChainTip->GetBlockTime()));
1725+
17201726
CheckForkWarningConditions();
17211727
}
17221728

@@ -2682,7 +2688,7 @@ void static UpdateTip(CBlockIndex* pindexNew)
26822688
static bool fWarned = false;
26832689
if (!IsInitialBlockDownload() && !fWarned) {
26842690
int nUpgraded = 0;
2685-
const CBlockIndex* pindex = chainActive.Tip();
2691+
const CBlockIndex* pindex = pChainTip;
26862692
for (int i = 0; i < 100 && pindex != NULL; i++) {
26872693
if (pindex->nVersion > CBlock::CURRENT_VERSION)
26882694
++nUpgraded;

src/masternode.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,22 @@ std::map<int64_t, uint256> mapCacheBlockHashes;
2323
//Get the last hash that matches the modulus given. Processed in reverse order
2424
bool GetBlockHash(uint256& hash, int nBlockHeight)
2525
{
26-
int tipHeight;
27-
const CBlockIndex* tipIndex;
28-
{
29-
LOCK(cs_main);
30-
CBlockIndex *pindex = chainActive.Tip();
31-
if (!pindex) return false;
32-
tipHeight = pindex->nHeight;
33-
tipIndex = mapBlockIndex[pindex->GetBlockHash()];
34-
}
26+
const CBlockIndex* tipIndex = GetChainTip();
27+
if (!tipIndex || !tipIndex->nHeight) return false;
3528

3629
if (nBlockHeight == 0)
37-
nBlockHeight = tipHeight;
30+
nBlockHeight = tipIndex->nHeight;
3831

3932
if (mapCacheBlockHashes.count(nBlockHeight)) {
4033
hash = mapCacheBlockHashes[nBlockHeight];
4134
return true;
4235
}
4336

44-
const CBlockIndex* BlockLastSolved = tipIndex;
45-
const CBlockIndex* BlockReading = tipIndex;
46-
47-
if (BlockLastSolved == nullptr || BlockLastSolved->nHeight == 0 || tipHeight + 1 < nBlockHeight) return false;
48-
4937
int nBlocksAgo = 0;
50-
if (nBlockHeight > 0) nBlocksAgo = (tipHeight + 1) - nBlockHeight;
51-
assert(nBlocksAgo >= 0);
38+
if (nBlockHeight > 0) nBlocksAgo = (tipIndex->nHeight + 1) - nBlockHeight;
39+
if (nBlocksAgo < 0) return false;
5240

41+
const CBlockIndex* BlockReading = tipIndex;
5342
int n = 0;
5443
for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
5544
if (n >= nBlocksAgo) {
@@ -167,7 +156,7 @@ uint256 CMasternode::CalculateScore(int mod, int64_t nBlockHeight)
167156
{
168157
{
169158
LOCK(cs_main);
170-
if (chainActive.Tip() == NULL) return UINT256_ZERO;
159+
if (!chainActive.Tip()) return UINT256_ZERO;
171160
}
172161

173162
uint256 hash;

src/masternode.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,8 @@ class CMasternode : public CSignedMessage
251251

252252
int GetMasternodeInputAge()
253253
{
254-
int tipHeight;
255-
{
256-
LOCK(cs_main);
257-
CBlockIndex *pindex = chainActive.Tip();
258-
if (!pindex) return 0;
259-
tipHeight = pindex->nHeight;
260-
}
254+
int tipHeight = WITH_LOCK(cs_main, return chainActive.Height());
255+
if (tipHeight < 0) return 0;
261256

262257
if (cacheInputAge == 0) {
263258
cacheInputAge = GetInputAge(vin);

src/miner.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "miner.h"
1212

1313
#include "amount.h"
14-
#include "chain.h"
1514
#include "consensus/merkle.h"
1615
#include "consensus/tx_verify.h" // needed in case of no ENABLE_WALLET
1716
#include "hash.h"

src/rpc/blockchain.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ double GetDifficulty(const CBlockIndex* blockindex)
5151
// Floating point number that is a multiple of the minimum difficulty,
5252
// minimum difficulty = 1.0.
5353
if (blockindex == NULL) {
54-
if (chainActive.Tip() == NULL)
54+
const CBlockIndex* pChainTip = GetChainTip();
55+
if (!pChainTip)
5556
return 1.0;
5657
else
57-
blockindex = chainActive.Tip();
58+
blockindex = pChainTip;
5859
}
5960

6061
int nShift = (blockindex->nBits >> 24) & 0xff;
@@ -796,7 +797,7 @@ UniValue verifychain(const JSONRPCRequest& request)
796797
}
797798

798799
/** Implementation of IsSuperMajority with better feedback */
799-
static UniValue SoftForkMajorityDesc(int version, CBlockIndex* pindex, const Consensus::Params& consensusParams)
800+
static UniValue SoftForkMajorityDesc(int version, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
800801
{
801802
UniValue rv(UniValue::VOBJ);
802803
Consensus::UpgradeIndex idx;
@@ -825,7 +826,8 @@ static UniValue SoftForkMajorityDesc(int version, CBlockIndex* pindex, const Con
825826
rv.push_back(Pair("status", consensusParams.NetworkUpgradeActive(pindex->nHeight, idx)));
826827
return rv;
827828
}
828-
static UniValue SoftForkDesc(const std::string &name, int version, CBlockIndex* pindex)
829+
830+
static UniValue SoftForkDesc(const std::string &name, int version, const CBlockIndex* pindex)
829831
{
830832
const Consensus::Params& consensus = Params().GetConsensus();
831833
UniValue rv(UniValue::VOBJ);
@@ -906,22 +908,23 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
906908
LOCK(cs_main);
907909

908910
const Consensus::Params& consensusParams = Params().GetConsensus();
911+
const CBlockIndex* pChainTip = chainActive.Tip();
912+
int nTipHeight = pChainTip ? pChainTip->nHeight : -1;
909913

910914
UniValue obj(UniValue::VOBJ);
911915
obj.push_back(Pair("chain", Params().NetworkIDString()));
912-
obj.push_back(Pair("blocks", (int)chainActive.Height()));
916+
obj.push_back(Pair("blocks", nTipHeight));
913917
obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
914-
obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
918+
obj.push_back(Pair("bestblockhash", pChainTip ? pChainTip->GetBlockHash().GetHex() : ""));
915919
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
916-
obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(chainActive.Tip())));
917-
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
918-
CBlockIndex* tip = chainActive.Tip();
920+
obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(pChainTip)));
921+
obj.push_back(Pair("chainwork", pChainTip ? pChainTip->nChainWork.GetHex() : ""));
919922
UniValue softforks(UniValue::VARR);
920-
softforks.push_back(SoftForkDesc("bip65", 5, tip));
923+
softforks.push_back(SoftForkDesc("bip65", 5, pChainTip));
921924
obj.push_back(Pair("softforks", softforks));
922925
UniValue upgrades(UniValue::VOBJ);
923926
for (int i = Consensus::BASE_NETWORK + 1; i < (int) Consensus::MAX_NETWORK_UPGRADES; i++) {
924-
NetworkUpgradeDescPushBack(upgrades, consensusParams, Consensus::UpgradeIndex(i), tip->nHeight);
927+
NetworkUpgradeDescPushBack(upgrades, consensusParams, Consensus::UpgradeIndex(i), nTipHeight);
925928
}
926929
obj.push_back(Pair("upgrades", upgrades));
927930

src/rpc/budget.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void checkBudgetInputs(const UniValue& params, std::string &strProposalName, std
6565
if (nPaymentCount < 1)
6666
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid payment count, must be more than zero.");
6767

68-
CBlockIndex* pindexPrev = chainActive.Tip();
68+
CBlockIndex* pindexPrev = GetChainTip();
6969
if (!pindexPrev)
7070
throw JSONRPCError(RPC_IN_WARMUP, "Try again after active chain is loaded");
7171

@@ -552,11 +552,11 @@ UniValue getnextsuperblock(const JSONRPCRequest& request)
552552
"\nExamples:\n" +
553553
HelpExampleCli("getnextsuperblock", "") + HelpExampleRpc("getnextsuperblock", ""));
554554

555-
CBlockIndex* pindexPrev = chainActive.Tip();
556-
if (!pindexPrev) return "unknown";
555+
int nChainHeight = WITH_LOCK(cs_main, return chainActive.Height());
556+
if (nChainHeight < 0) return "unknown";
557557

558558
const int nBlocksPerCycle = Params().GetConsensus().nBudgetCycleBlocks;
559-
int nNext = pindexPrev->nHeight - pindexPrev->nHeight % nBlocksPerCycle + nBlocksPerCycle;
559+
int nNext = nChainHeight - nChainHeight % nBlocksPerCycle + nBlocksPerCycle;
560560
return nNext;
561561
}
562562

src/rpc/masternode.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ UniValue listmasternodes(const JSONRPCRequest& request)
5454
HelpExampleCli("listmasternodes", "") + HelpExampleRpc("listmasternodes", ""));
5555

5656
UniValue ret(UniValue::VARR);
57-
int nHeight;
58-
{
59-
LOCK(cs_main);
60-
CBlockIndex* pindex = chainActive.Tip();
61-
if(!pindex) return 0;
62-
nHeight = pindex->nHeight;
63-
}
57+
int nHeight = WITH_LOCK(cs_main, return chainActive.Height());
58+
if (nHeight < 0) return "[]";
59+
6460
std::vector<std::pair<int, CMasternode> > vMasternodeRanks = mnodeman.GetMasternodeRanks(nHeight);
6561
for (PAIRTYPE(int, CMasternode) & s : vMasternodeRanks) {
6662
UniValue obj(UniValue::VOBJ);
@@ -124,9 +120,10 @@ UniValue getmasternodecount (const JSONRPCRequest& request)
124120
int nCount = 0;
125121
int ipv4 = 0, ipv6 = 0, onion = 0;
126122

127-
if (chainActive.Tip())
128-
mnodeman.GetNextMasternodeInQueueForPayment(chainActive.Tip()->nHeight, true, nCount);
123+
int nChainHeight = WITH_LOCK(cs_main, return chainActive.Height());
124+
if (nChainHeight < 0) return "unknown";
129125

126+
mnodeman.GetNextMasternodeInQueueForPayment(nChainHeight, true, nCount);
130127
mnodeman.CountNetworks(ActiveProtocol(), ipv4, ipv6, onion);
131128

132129
obj.push_back(Pair("total", mnodeman.size()));
@@ -562,13 +559,8 @@ UniValue getmasternodewinners (const JSONRPCRequest& request)
562559
"\nExamples:\n" +
563560
HelpExampleCli("getmasternodewinners", "") + HelpExampleRpc("getmasternodewinners", ""));
564561

565-
int nHeight;
566-
{
567-
LOCK(cs_main);
568-
CBlockIndex* pindex = chainActive.Tip();
569-
if(!pindex) return 0;
570-
nHeight = pindex->nHeight;
571-
}
562+
int nHeight = WITH_LOCK(cs_main, return chainActive.Height());
563+
if (nHeight < 0) return "[]";
572564

573565
int nLast = 10;
574566
std::string strFilter = "";
@@ -651,10 +643,11 @@ UniValue getmasternodescores (const JSONRPCRequest& request)
651643
throw std::runtime_error("Exception on param 2");
652644
}
653645
}
646+
int nChainHeight = WITH_LOCK(cs_main, return chainActive.Height());
647+
if (nChainHeight < 0) return "unknown";
654648
UniValue obj(UniValue::VOBJ);
655-
656649
std::vector<CMasternode> vMasternodes = mnodeman.GetFullMasternodeVector();
657-
for (int nHeight = chainActive.Tip()->nHeight - nLast; nHeight < chainActive.Tip()->nHeight + 20; nHeight++) {
650+
for (int nHeight = nChainHeight - nLast; nHeight < nChainHeight + 20; nHeight++) {
658651
uint256 nHigh;
659652
CMasternode* pBestMasternode = NULL;
660653
for (CMasternode& mn : vMasternodes) {

0 commit comments

Comments
 (0)