@@ -374,10 +374,11 @@ void ProcessBlockAvailability(NodeId nodeid) {
374
374
assert (state != nullptr );
375
375
376
376
if (!state->hashLastUnknownBlock .IsNull ()) {
377
- BlockMap::iterator itOld = mapBlockIndex.find (state->hashLastUnknownBlock );
378
- if (itOld != mapBlockIndex.end () && itOld->second ->nChainWork > 0 ) {
379
- if (state->pindexBestKnownBlock == nullptr || itOld->second ->nChainWork >= state->pindexBestKnownBlock ->nChainWork )
380
- state->pindexBestKnownBlock = itOld->second ;
377
+ const CBlockIndex* pindex = LookupBlockIndex (state->hashLastUnknownBlock );
378
+ if (pindex && pindex->nChainWork > 0 ) {
379
+ if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock ->nChainWork ) {
380
+ state->pindexBestKnownBlock = pindex;
381
+ }
381
382
state->hashLastUnknownBlock .SetNull ();
382
383
}
383
384
}
@@ -390,11 +391,12 @@ void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
390
391
391
392
ProcessBlockAvailability (nodeid);
392
393
393
- BlockMap::iterator it = mapBlockIndex. find (hash);
394
- if (it != mapBlockIndex. end () && it-> second ->nChainWork > 0 ) {
394
+ const CBlockIndex* pindex = LookupBlockIndex (hash);
395
+ if (pindex && pindex ->nChainWork > 0 ) {
395
396
// An actually better block was announced.
396
- if (state->pindexBestKnownBlock == nullptr || it->second ->nChainWork >= state->pindexBestKnownBlock ->nChainWork )
397
- state->pindexBestKnownBlock = it->second ;
397
+ if (state->pindexBestKnownBlock == nullptr || pindex->nChainWork >= state->pindexBestKnownBlock ->nChainWork ) {
398
+ state->pindexBestKnownBlock = pindex;
399
+ }
398
400
} else {
399
401
// An unknown block was announced; just assume that the latest one is the best one.
400
402
state->hashLastUnknownBlock = hash;
@@ -989,7 +991,7 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
989
991
}
990
992
case MSG_BLOCK:
991
993
case MSG_WITNESS_BLOCK:
992
- return mapBlockIndex. count (inv.hash );
994
+ return LookupBlockIndex (inv.hash ) != nullptr ;
993
995
}
994
996
// Don't know what it is, just say we already got one
995
997
return true ;
@@ -1056,11 +1058,10 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1056
1058
bool need_activate_chain = false ;
1057
1059
{
1058
1060
LOCK (cs_main);
1059
- BlockMap::iterator mi = mapBlockIndex.find (inv.hash );
1060
- if (mi != mapBlockIndex.end ())
1061
- {
1062
- if (mi->second ->nChainTx && !mi->second ->IsValid (BLOCK_VALID_SCRIPTS) &&
1063
- mi->second ->IsValid (BLOCK_VALID_TREE)) {
1061
+ const CBlockIndex* pindex = LookupBlockIndex (inv.hash );
1062
+ if (pindex) {
1063
+ if (pindex->nChainTx && !pindex->IsValid (BLOCK_VALID_SCRIPTS) &&
1064
+ pindex->IsValid (BLOCK_VALID_TREE)) {
1064
1065
// If we have the block and all of its parents, but have not yet validated it,
1065
1066
// we might be in the middle of connecting it (ie in the unlock of cs_main
1066
1067
// before ActivateBestChain but after AcceptBlock).
@@ -1076,17 +1077,17 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1076
1077
}
1077
1078
1078
1079
LOCK (cs_main);
1079
- BlockMap::iterator mi = mapBlockIndex. find (inv.hash );
1080
- if (mi != mapBlockIndex. end () ) {
1081
- send = BlockRequestAllowed (mi-> second , consensusParams);
1080
+ const CBlockIndex* pindex = LookupBlockIndex (inv.hash );
1081
+ if (pindex ) {
1082
+ send = BlockRequestAllowed (pindex , consensusParams);
1082
1083
if (!send) {
1083
1084
LogPrint (BCLog::NET, " %s: ignoring request from peer=%i for old block that isn't in the main chain\n " , __func__, pfrom->GetId ());
1084
1085
}
1085
1086
}
1086
1087
const CNetMsgMaker msgMaker (pfrom->GetSendVersion ());
1087
1088
// disconnect node in case we have reached the outbound limit for serving historical blocks
1088
1089
// never disconnect whitelisted nodes
1089
- if (send && connman->OutboundTargetReached (true ) && ( ((pindexBestHeader != nullptr ) && (pindexBestHeader->GetBlockTime () - mi-> second ->GetBlockTime () > HISTORICAL_BLOCK_AGE)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted )
1090
+ if (send && connman->OutboundTargetReached (true ) && ( ((pindexBestHeader != nullptr ) && (pindexBestHeader->GetBlockTime () - pindex ->GetBlockTime () > HISTORICAL_BLOCK_AGE)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted )
1090
1091
{
1091
1092
LogPrint (BCLog::NET, " historical block serving limit reached, disconnect peer=%d\n " , pfrom->GetId ());
1092
1093
@@ -1096,7 +1097,7 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1096
1097
}
1097
1098
// Avoid leaking prune-height by never sending blocks below the NODE_NETWORK_LIMITED threshold
1098
1099
if (send && !pfrom->fWhitelisted && (
1099
- (((pfrom->GetLocalServices () & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED) && ((pfrom->GetLocalServices () & NODE_NETWORK) != NODE_NETWORK) && (chainActive.Tip ()->nHeight - mi-> second ->nHeight > (int )NODE_NETWORK_LIMITED_MIN_BLOCKS + 2 /* add two blocks buffer extension for possible races */ ) )
1100
+ (((pfrom->GetLocalServices () & NODE_NETWORK_LIMITED) == NODE_NETWORK_LIMITED) && ((pfrom->GetLocalServices () & NODE_NETWORK) != NODE_NETWORK) && (chainActive.Tip ()->nHeight - pindex ->nHeight > (int )NODE_NETWORK_LIMITED_MIN_BLOCKS + 2 /* add two blocks buffer extension for possible races */ ) )
1100
1101
)) {
1101
1102
LogPrint (BCLog::NET, " Ignore block request below NODE_NETWORK_LIMITED threshold from peer=%d\n " , pfrom->GetId ());
1102
1103
@@ -1106,15 +1107,15 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1106
1107
}
1107
1108
// Pruned nodes may have deleted the block, so check whether
1108
1109
// it's available before trying to send.
1109
- if (send && (mi-> second ->nStatus & BLOCK_HAVE_DATA))
1110
+ if (send && (pindex ->nStatus & BLOCK_HAVE_DATA))
1110
1111
{
1111
1112
std::shared_ptr<const CBlock> pblock;
1112
- if (a_recent_block && a_recent_block->GetHash () == (*mi). second ->GetBlockHash ()) {
1113
+ if (a_recent_block && a_recent_block->GetHash () == pindex ->GetBlockHash ()) {
1113
1114
pblock = a_recent_block;
1114
1115
} else {
1115
1116
// Send block from disk
1116
1117
std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
1117
- if (!ReadBlockFromDisk (*pblockRead, (*mi). second , consensusParams))
1118
+ if (!ReadBlockFromDisk (*pblockRead, pindex , consensusParams))
1118
1119
assert (!" cannot load block from disk" );
1119
1120
pblock = pblockRead;
1120
1121
}
@@ -1156,8 +1157,8 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1156
1157
// instead we respond with the full, non-compact block.
1157
1158
bool fPeerWantsWitness = State (pfrom->GetId ())->fWantsCmpctWitness ;
1158
1159
int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
1159
- if (CanDirectFetch (consensusParams) && mi-> second ->nHeight >= chainActive.Height () - MAX_CMPCTBLOCK_DEPTH) {
1160
- if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock ) && a_recent_compact_block && a_recent_compact_block->header .GetHash () == mi-> second ->GetBlockHash ()) {
1160
+ if (CanDirectFetch (consensusParams) && pindex ->nHeight >= chainActive.Height () - MAX_CMPCTBLOCK_DEPTH) {
1161
+ if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock ) && a_recent_compact_block && a_recent_compact_block->header .GetHash () == pindex ->GetBlockHash ()) {
1161
1162
connman->PushMessage (pfrom, msgMaker.Make (nSendFlags, NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
1162
1163
} else {
1163
1164
CBlockHeaderAndShortTxIDs cmpctblock (*pblock, fPeerWantsWitness );
@@ -1297,7 +1298,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
1297
1298
// don't connect before giving DoS points
1298
1299
// - Once a headers message is received that is valid and does connect,
1299
1300
// nUnconnectingHeaders gets reset back to 0.
1300
- if (mapBlockIndex. find (headers[0 ].hashPrevBlock ) == mapBlockIndex. end ( ) && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
1301
+ if (! LookupBlockIndex (headers[0 ].hashPrevBlock ) && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
1301
1302
nodestate->nUnconnectingHeaders ++;
1302
1303
connman->PushMessage (pfrom, msgMaker.Make (NetMsgType::GETHEADERS, chainActive.GetLocator (pindexBestHeader), uint256 ()));
1303
1304
LogPrint (BCLog::NET, " received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n " ,
@@ -1327,7 +1328,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
1327
1328
1328
1329
// If we don't have the last header, then they'll have given us
1329
1330
// something new (if these headers are valid).
1330
- if (mapBlockIndex. find (hashLastBlock) == mapBlockIndex. end ( )) {
1331
+ if (! LookupBlockIndex (hashLastBlock)) {
1331
1332
received_new_header = true ;
1332
1333
}
1333
1334
}
@@ -1343,7 +1344,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
1343
1344
} else {
1344
1345
LogPrint (BCLog::NET, " peer=%d: invalid header received\n " , pfrom->GetId ());
1345
1346
}
1346
- if (punish_duplicate_invalid && mapBlockIndex. find (first_invalid_header.GetHash ()) != mapBlockIndex. end ( )) {
1347
+ if (punish_duplicate_invalid && LookupBlockIndex (first_invalid_header.GetHash ())) {
1347
1348
// Goal: don't allow outbound peers to use up our outbound
1348
1349
// connection slots if they are on incompatible chains.
1349
1350
//
@@ -2024,13 +2025,13 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2024
2025
2025
2026
LOCK (cs_main);
2026
2027
2027
- BlockMap::iterator it = mapBlockIndex. find (req.blockhash );
2028
- if (it == mapBlockIndex. end () || !(it-> second ->nStatus & BLOCK_HAVE_DATA)) {
2028
+ const CBlockIndex* pindex = LookupBlockIndex (req.blockhash );
2029
+ if (!pindex || !(pindex ->nStatus & BLOCK_HAVE_DATA)) {
2029
2030
LogPrint (BCLog::NET, " Peer %d sent us a getblocktxn for a block we don't have" , pfrom->GetId ());
2030
2031
return true ;
2031
2032
}
2032
2033
2033
- if (it-> second ->nHeight < chainActive.Height () - MAX_BLOCKTXN_DEPTH) {
2034
+ if (pindex ->nHeight < chainActive.Height () - MAX_BLOCKTXN_DEPTH) {
2034
2035
// If an older block is requested (should never happen in practice,
2035
2036
// but can happen in tests) send a block response instead of a
2036
2037
// blocktxn response. Sending a full block response instead of a
@@ -2048,7 +2049,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2048
2049
}
2049
2050
2050
2051
CBlock block;
2051
- bool ret = ReadBlockFromDisk (block, it-> second , chainparams.GetConsensus ());
2052
+ bool ret = ReadBlockFromDisk (block, pindex , chainparams.GetConsensus ());
2052
2053
assert (ret);
2053
2054
2054
2055
SendBlockTransactions (block, req, pfrom, connman);
@@ -2072,10 +2073,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2072
2073
if (locator.IsNull ())
2073
2074
{
2074
2075
// If locator is null, return the hashStop block
2075
- BlockMap::iterator mi = mapBlockIndex. find (hashStop);
2076
- if (mi == mapBlockIndex. end ())
2076
+ pindex = LookupBlockIndex (hashStop);
2077
+ if (!pindex) {
2077
2078
return true ;
2078
- pindex = (*mi). second ;
2079
+ }
2079
2080
2080
2081
if (!BlockRequestAllowed (pindex, chainparams.GetConsensus ())) {
2081
2082
LogPrint (BCLog::NET, " %s: ignoring request from peer=%i for old block header that isn't in the main chain\n " , __func__, pfrom->GetId ());
@@ -2314,14 +2315,14 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2314
2315
{
2315
2316
LOCK (cs_main);
2316
2317
2317
- if (mapBlockIndex. find (cmpctblock.header .hashPrevBlock ) == mapBlockIndex. end ( )) {
2318
+ if (! LookupBlockIndex (cmpctblock.header .hashPrevBlock )) {
2318
2319
// Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
2319
2320
if (!IsInitialBlockDownload ())
2320
2321
connman->PushMessage (pfrom, msgMaker.Make (NetMsgType::GETHEADERS, chainActive.GetLocator (pindexBestHeader), uint256 ()));
2321
2322
return true ;
2322
2323
}
2323
2324
2324
- if (mapBlockIndex. find (cmpctblock.header .GetHash ()) == mapBlockIndex. end ( )) {
2325
+ if (! LookupBlockIndex (cmpctblock.header .GetHash ())) {
2325
2326
received_new_header = true ;
2326
2327
}
2327
2328
}
@@ -3303,9 +3304,8 @@ bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic<bool>& interruptM
3303
3304
// then send all headers past that one. If we come across any
3304
3305
// headers that aren't on chainActive, give up.
3305
3306
for (const uint256 &hash : pto->vBlockHashesToAnnounce ) {
3306
- BlockMap::iterator mi = mapBlockIndex.find (hash);
3307
- assert (mi != mapBlockIndex.end ());
3308
- const CBlockIndex *pindex = mi->second ;
3307
+ const CBlockIndex* pindex = LookupBlockIndex (hash);
3308
+ assert (pindex);
3309
3309
if (chainActive[pindex->nHeight ] != pindex) {
3310
3310
// Bail out if we reorged away from this block
3311
3311
fRevertToInv = true ;
@@ -3396,9 +3396,8 @@ bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic<bool>& interruptM
3396
3396
// in the past.
3397
3397
if (!pto->vBlockHashesToAnnounce .empty ()) {
3398
3398
const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce .back ();
3399
- BlockMap::iterator mi = mapBlockIndex.find (hashToAnnounce);
3400
- assert (mi != mapBlockIndex.end ());
3401
- const CBlockIndex *pindex = mi->second ;
3399
+ const CBlockIndex* pindex = LookupBlockIndex (hashToAnnounce);
3400
+ assert (pindex);
3402
3401
3403
3402
// Warn if we're announcing a block that is not on the main chain.
3404
3403
// This should be very rare and could be optimized out.
0 commit comments