Skip to content

Commit 58779a0

Browse files
committed
Add LookupBlockIndex function
Adapted from btc@92fabcd443322dcfdf2b3477515fae79e8647d86
1 parent 94f96a5 commit 58779a0

File tree

13 files changed

+89
-102
lines changed

13 files changed

+89
-102
lines changed

src/budget/budgetmanager.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,13 +1677,10 @@ bool CheckCollateral(const uint256& nTxCollateralHash, const uint256& nExpectedH
16771677
int nProposalHeight = 0;
16781678
{
16791679
LOCK(cs_main);
1680-
BlockMap::iterator mi = mapBlockIndex.find(nBlockHash);
1681-
if (mi != mapBlockIndex.end() && (*mi).second) {
1682-
CBlockIndex* pindex = (*mi).second;
1683-
if (chainActive.Contains(pindex)) {
1684-
nProposalHeight = pindex->nHeight;
1685-
nTime = pindex->nTime;
1686-
}
1680+
CBlockIndex* pindex = LookupBlockIndex(nBlockHash);
1681+
if (pindex && chainActive.Contains(pindex)) {
1682+
nProposalHeight = pindex->nHeight;
1683+
nTime = pindex->nTime;
16871684
}
16881685
}
16891686

src/budget/finalizedbudget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ bool CFinalizedBudget::IsPaidAlready(const uint256& nProposalHash, const uint256
311311
// -> reject transaction so it gets paid to a masternode instead
312312
if (nBlockHash != nPaidBlockHash) {
313313
LOCK(cs_main);
314-
auto it = mapBlockIndex.find(nPaidBlockHash);
315-
return it != mapBlockIndex.end() && chainActive.Contains(it->second);
314+
CBlockIndex* pindex = LookupBlockIndex(nPaidBlockHash);
315+
return pindex && chainActive.Contains(pindex);
316316
}
317317

318318
// Re-checking same block. Not a double payment.

src/evo/specialtx_validation.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,10 @@ bool VerifyLLMQCommitment(const llmq::CFinalCommitment& qfc, const CBlockIndex*
442442

443443
if (pindexPrev) {
444444
// Get quorum index
445-
auto it = mapBlockIndex.find(qfc.quorumHash);
446-
if (it == mapBlockIndex.end()) {
445+
CBlockIndex* pindexQuorum = LookupBlockIndex(qfc.quorumHash);
446+
if (!pindexQuorum) {
447447
return state.DoS(100, false, REJECT_INVALID, "bad-qc-quorum-hash-not-found");
448448
}
449-
const CBlockIndex* pindexQuorum = it->second;
450449

451450
// Check height
452451
if (pindexQuorum->nHeight % params->dkgInterval != 0) {

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,8 +1503,9 @@ bool AppInitMain()
15031503

15041504
// If the loaded chain has a wrong genesis, bail out immediately
15051505
// (we're likely using a testnet datadir, or the other way around).
1506-
if (!mapBlockIndex.empty() && mapBlockIndex.count(consensus.hashGenesisBlock) == 0)
1506+
if (!mapBlockIndex.empty() && !LookupBlockIndex(consensus.hashGenesisBlock)) {
15071507
return UIError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
1508+
}
15081509

15091510
// Check for changed -txindex state
15101511
if (fTxIndex != gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {

src/net_processing.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,10 @@ void ProcessBlockAvailability(NodeId nodeid)
312312
assert(state != nullptr);
313313

314314
if (!state->hashLastUnknownBlock.IsNull()) {
315-
BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
316-
if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
317-
if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
318-
state->pindexBestKnownBlock = itOld->second;
315+
CBlockIndex* pindex = LookupBlockIndex(state->hashLastUnknownBlock);
316+
if (pindex && pindex->nChainWork > 0) {
317+
if (state->pindexBestKnownBlock == NULL || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork)
318+
state->pindexBestKnownBlock = pindex;
319319
state->hashLastUnknownBlock.SetNull();
320320
}
321321
}
@@ -329,11 +329,11 @@ void UpdateBlockAvailability(NodeId nodeid, const uint256& hash)
329329

330330
ProcessBlockAvailability(nodeid);
331331

332-
BlockMap::iterator it = mapBlockIndex.find(hash);
333-
if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
332+
CBlockIndex* pindex = LookupBlockIndex(hash);
333+
if (pindex && pindex->nChainWork > 0) {
334334
// An actually better block was announced.
335-
if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
336-
state->pindexBestKnownBlock = it->second;
335+
if (state->pindexBestKnownBlock == NULL || pindex->nChainWork >= state->pindexBestKnownBlock->nChainWork)
336+
state->pindexBestKnownBlock = pindex;
337337
} else {
338338
// An unknown block was announced; just assume that the latest one is the best one.
339339
state->hashLastUnknownBlock = hash;
@@ -625,9 +625,9 @@ static void CheckBlockSpam(NodeId nodeId, const uint256& hashBlock)
625625
nodestate = State(nodeId);
626626
if (!nodestate) { return; }
627627

628-
const auto it = mapBlockIndex.find(hashBlock);
629-
if (it == mapBlockIndex.end()) { return; }
630-
blockReceivedHeight = it->second->nHeight;
628+
CBlockIndex* pindex = LookupBlockIndex(hashBlock);
629+
if (!pindex) { return; }
630+
blockReceivedHeight = pindex->nHeight;
631631
}
632632

633633
nodestate->nodeBlocks.onBlockReceived(blockReceivedHeight);
@@ -766,7 +766,7 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
766766
}
767767

768768
case MSG_BLOCK:
769-
return mapBlockIndex.count(inv.hash);
769+
return LookupBlockIndex(inv.hash) != nullptr;
770770
case MSG_TXLOCK_REQUEST:
771771
// deprecated
772772
return true;
@@ -956,26 +956,26 @@ void static ProcessGetBlockData(CNode* pfrom, const CInv& inv, CConnman* connman
956956
CNetMsgMaker msgMaker(pfrom->GetSendVersion());
957957

958958
bool send = false;
959-
BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
960-
if (mi != mapBlockIndex.end()) {
961-
if (chainActive.Contains(mi->second)) {
959+
CBlockIndex* pindex = LookupBlockIndex(inv.hash);
960+
if (pindex) {
961+
if (chainActive.Contains(pindex)) {
962962
send = true;
963963
} else {
964964
// To prevent fingerprinting attacks, only send blocks outside of the active
965965
// chain if they are valid, and no more than a max reorg depth than the best header
966966
// chain we know about.
967-
send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
968-
(chainActive.Height() - mi->second->nHeight < gArgs.GetArg("-maxreorg", DEFAULT_MAX_REORG_DEPTH));
967+
send = pindex->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
968+
(chainActive.Height() - pindex->nHeight < gArgs.GetArg("-maxreorg", DEFAULT_MAX_REORG_DEPTH));
969969
if (!send) {
970970
LogPrint(BCLog::NET, "ProcessGetData(): ignoring request from peer=%i for old block that isn't in the main chain\n", pfrom->GetId());
971971
}
972972
}
973973
}
974974
// Don't send not-validated blocks
975-
if (send && (mi->second->nStatus & BLOCK_HAVE_DATA)) {
975+
if (send && (pindex->nStatus & BLOCK_HAVE_DATA)) {
976976
// Send block from disk
977977
CBlock block;
978-
if (!ReadBlockFromDisk(block, (*mi).second))
978+
if (!ReadBlockFromDisk(block, pindex))
979979
assert(!"cannot load block from disk");
980980
if (inv.type == MSG_BLOCK)
981981
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::BLOCK, block));
@@ -1512,10 +1512,9 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
15121512
CBlockIndex* pindex = NULL;
15131513
if (locator.IsNull()) {
15141514
// If locator is null, return the hashStop block
1515-
BlockMap::iterator mi = mapBlockIndex.find(hashStop);
1516-
if (mi == mapBlockIndex.end())
1515+
CBlockIndex* pindex = LookupBlockIndex(hashStop);
1516+
if (!pindex)
15171517
return true;
1518-
pindex = (*mi).second;
15191518
} else {
15201519
// Find the last block the caller has in the main chain
15211520
pindex = FindForkInGlobalIndex(chainActive, locator);

src/rest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ static bool rest_headers(HTTPRequest* req,
131131
headers.reserve(count);
132132
{
133133
LOCK(cs_main);
134-
BlockMap::const_iterator it = mapBlockIndex.find(hash);
135-
const CBlockIndex *pindex = (it != mapBlockIndex.end()) ? it->second : NULL;
134+
CBlockIndex* pindex = LookupBlockIndex(hash);
136135
while (pindex != NULL && chainActive.Contains(pindex)) {
137136
headers.push_back(pindex);
138137
if (headers.size() == (unsigned long)count)

src/rpc/blockchain.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ static bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats)
733733
stats.hashBlock = pcursor->GetBestBlock();
734734
{
735735
LOCK(cs_main);
736-
stats.nHeight = mapBlockIndex.find(stats.hashBlock)->second->nHeight;
736+
stats.nHeight = LookupBlockIndex(stats.hashBlock)->nHeight;
737737
}
738738
ss << stats.hashBlock;
739739
uint256 prevkey;
@@ -863,8 +863,7 @@ UniValue gettxout(const JSONRPCRequest& request)
863863
}
864864
}
865865

866-
BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
867-
CBlockIndex* pindex = it->second;
866+
CBlockIndex* pindex = LookupBlockIndex(pcoinsTip->GetBestBlock());
868867
assert(pindex != nullptr);
869868
ret.pushKV("bestblock", pindex->GetBlockHash().GetHex());
870869
if (coin.nHeight == MEMPOOL_HEIGHT) {

src/rpc/mining.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,8 @@ UniValue getblocktemplate(const JSONRPCRequest& request)
548548
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Block decode failed");
549549

550550
uint256 hash = block.GetHash();
551-
BlockMap::iterator mi = mapBlockIndex.find(hash);
552-
if (mi != mapBlockIndex.end()) {
553-
CBlockIndex* pindex = mi->second;
551+
CBlockIndex* pindex = LookupBlockIndex(hash);
552+
if (pindex) {
554553
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
555554
return "duplicate";
556555
if (pindex->nStatus & BLOCK_FAILED_MASK)
@@ -761,9 +760,8 @@ UniValue submitblock(const JSONRPCRequest& request)
761760
bool fBlockPresent = false;
762761
{
763762
LOCK(cs_main);
764-
BlockMap::iterator mi = mapBlockIndex.find(hash);
765-
if (mi != mapBlockIndex.end()) {
766-
CBlockIndex* pindex = mi->second;
763+
CBlockIndex* pindex = LookupBlockIndex(hash);
764+
if (pindex) {
767765
if (pindex->IsValid(BLOCK_VALID_SCRIPTS))
768766
return "duplicate";
769767
if (pindex->nStatus & BLOCK_FAILED_MASK)

src/rpc/rawtransaction.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ void TxToJSON(CWallet* const pwallet, const CTransaction& tx, const uint256 hash
9999

100100
if (!hashBlock.IsNull()) {
101101
entry.pushKV("blockhash", hashBlock.GetHex());
102-
BlockMap::iterator mi = mapBlockIndex.find(hashBlock);
103-
if (mi != mapBlockIndex.end() && (*mi).second) {
104-
CBlockIndex* pindex = (*mi).second;
102+
CBlockIndex* pindex = LookupBlockIndex(hashBlock);
103+
if (pindex) {
105104
if (chainActive.Contains(pindex)) {
106105
entry.pushKV("confirmations", 1 + chainActive.Height() - pindex->nHeight);
107106
entry.pushKV("time", pindex->GetBlockTime());
@@ -236,11 +235,10 @@ UniValue getrawtransaction(const JSONRPCRequest& request)
236235

237236
if (!request.params[2].isNull()) {
238237
uint256 blockhash = ParseHashV(request.params[2], "parameter 3");
239-
BlockMap::iterator it = mapBlockIndex.find(blockhash);
240-
if (it == mapBlockIndex.end()) {
238+
blockindex = LookupBlockIndex(blockhash);
239+
if (!blockindex) {
241240
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block hash not found");
242241
}
243-
blockindex = it->second;
244242
in_active_chain = chainActive.Contains(blockindex);
245243
}
246244

0 commit comments

Comments
 (0)