@@ -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;
@@ -1015,7 +1017,7 @@ bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1015
1017
}
1016
1018
case MSG_BLOCK:
1017
1019
case MSG_WITNESS_BLOCK:
1018
- return mapBlockIndex. count (inv.hash );
1020
+ return LookupBlockIndex (inv.hash ) != nullptr ;
1019
1021
}
1020
1022
// Don't know what it is, just say we already got one
1021
1023
return true ;
@@ -1082,11 +1084,10 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1082
1084
bool need_activate_chain = false ;
1083
1085
{
1084
1086
LOCK (cs_main);
1085
- BlockMap::iterator mi = mapBlockIndex.find (inv.hash );
1086
- if (mi != mapBlockIndex.end ())
1087
- {
1088
- if (mi->second ->nChainTx && !mi->second ->IsValid (BLOCK_VALID_SCRIPTS) &&
1089
- mi->second ->IsValid (BLOCK_VALID_TREE)) {
1087
+ const CBlockIndex* pindex = LookupBlockIndex (inv.hash );
1088
+ if (pindex) {
1089
+ if (pindex->nChainTx && !pindex->IsValid (BLOCK_VALID_SCRIPTS) &&
1090
+ pindex->IsValid (BLOCK_VALID_TREE)) {
1090
1091
// If we have the block and all of its parents, but have not yet validated it,
1091
1092
// we might be in the middle of connecting it (ie in the unlock of cs_main
1092
1093
// before ActivateBestChain but after AcceptBlock).
@@ -1102,17 +1103,17 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1102
1103
}
1103
1104
1104
1105
LOCK (cs_main);
1105
- BlockMap::iterator mi = mapBlockIndex. find (inv.hash );
1106
- if (mi != mapBlockIndex. end () ) {
1107
- send = BlockRequestAllowed (mi-> second , consensusParams);
1106
+ const CBlockIndex* pindex = LookupBlockIndex (inv.hash );
1107
+ if (pindex ) {
1108
+ send = BlockRequestAllowed (pindex , consensusParams);
1108
1109
if (!send) {
1109
1110
LogPrint (BCLog::NET, " %s: ignoring request from peer=%i for old block that isn't in the main chain\n " , __func__, pfrom->GetId ());
1110
1111
}
1111
1112
}
1112
1113
const CNetMsgMaker msgMaker (pfrom->GetSendVersion ());
1113
1114
// disconnect node in case we have reached the outbound limit for serving historical blocks
1114
1115
// never disconnect whitelisted nodes
1115
- if (send && connman->OutboundTargetReached (true ) && ( ((pindexBestHeader != nullptr ) && (pindexBestHeader->GetBlockTime () - mi-> second ->GetBlockTime () > HISTORICAL_BLOCK_AGE)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted )
1116
+ if (send && connman->OutboundTargetReached (true ) && ( ((pindexBestHeader != nullptr ) && (pindexBestHeader->GetBlockTime () - pindex ->GetBlockTime () > HISTORICAL_BLOCK_AGE)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted )
1116
1117
{
1117
1118
LogPrint (BCLog::NET, " historical block serving limit reached, disconnect peer=%d\n " , pfrom->GetId ());
1118
1119
@@ -1122,7 +1123,7 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1122
1123
}
1123
1124
// Avoid leaking prune-height by never sending blocks below the NODE_NETWORK_LIMITED threshold
1124
1125
if (send && !pfrom->fWhitelisted && (
1125
- (((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 */ ) )
1126
+ (((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 */ ) )
1126
1127
)) {
1127
1128
LogPrint (BCLog::NET, " Ignore block request below NODE_NETWORK_LIMITED threshold from peer=%d\n " , pfrom->GetId ());
1128
1129
@@ -1132,15 +1133,15 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1132
1133
}
1133
1134
// Pruned nodes may have deleted the block, so check whether
1134
1135
// it's available before trying to send.
1135
- if (send && (mi-> second ->nStatus & BLOCK_HAVE_DATA))
1136
+ if (send && (pindex ->nStatus & BLOCK_HAVE_DATA))
1136
1137
{
1137
1138
std::shared_ptr<const CBlock> pblock;
1138
- if (a_recent_block && a_recent_block->GetHash () == (*mi). second ->GetBlockHash ()) {
1139
+ if (a_recent_block && a_recent_block->GetHash () == pindex ->GetBlockHash ()) {
1139
1140
pblock = a_recent_block;
1140
1141
} else {
1141
1142
// Send block from disk
1142
1143
std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
1143
- if (!ReadBlockFromDisk (*pblockRead, (*mi). second , consensusParams))
1144
+ if (!ReadBlockFromDisk (*pblockRead, pindex , consensusParams))
1144
1145
assert (!" cannot load block from disk" );
1145
1146
pblock = pblockRead;
1146
1147
}
@@ -1182,8 +1183,8 @@ void static ProcessGetBlockData(CNode* pfrom, const Consensus::Params& consensus
1182
1183
// instead we respond with the full, non-compact block.
1183
1184
bool fPeerWantsWitness = State (pfrom->GetId ())->fWantsCmpctWitness ;
1184
1185
int nSendFlags = fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS;
1185
- if (CanDirectFetch (consensusParams) && mi-> second ->nHeight >= chainActive.Height () - MAX_CMPCTBLOCK_DEPTH) {
1186
- if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock ) && a_recent_compact_block && a_recent_compact_block->header .GetHash () == mi-> second ->GetBlockHash ()) {
1186
+ if (CanDirectFetch (consensusParams) && pindex ->nHeight >= chainActive.Height () - MAX_CMPCTBLOCK_DEPTH) {
1187
+ if ((fPeerWantsWitness || !fWitnessesPresentInARecentCompactBlock ) && a_recent_compact_block && a_recent_compact_block->header .GetHash () == pindex ->GetBlockHash ()) {
1187
1188
connman->PushMessage (pfrom, msgMaker.Make (nSendFlags, NetMsgType::CMPCTBLOCK, *a_recent_compact_block));
1188
1189
} else {
1189
1190
CBlockHeaderAndShortTxIDs cmpctblock (*pblock, fPeerWantsWitness );
@@ -1323,7 +1324,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
1323
1324
// don't connect before giving DoS points
1324
1325
// - Once a headers message is received that is valid and does connect,
1325
1326
// nUnconnectingHeaders gets reset back to 0.
1326
- if (mapBlockIndex. find (headers[0 ].hashPrevBlock ) == mapBlockIndex. end ( ) && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
1327
+ if (! LookupBlockIndex (headers[0 ].hashPrevBlock ) && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
1327
1328
nodestate->nUnconnectingHeaders ++;
1328
1329
connman->PushMessage (pfrom, msgMaker.Make (NetMsgType::GETHEADERS, chainActive.GetLocator (pindexBestHeader), uint256 ()));
1329
1330
LogPrint (BCLog::NET, " received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n " ,
@@ -1353,7 +1354,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
1353
1354
1354
1355
// If we don't have the last header, then they'll have given us
1355
1356
// something new (if these headers are valid).
1356
- if (mapBlockIndex. find (hashLastBlock) == mapBlockIndex. end ( )) {
1357
+ if (! LookupBlockIndex (hashLastBlock)) {
1357
1358
received_new_header = true ;
1358
1359
}
1359
1360
}
@@ -1369,7 +1370,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
1369
1370
} else {
1370
1371
LogPrint (BCLog::NET, " peer=%d: invalid header received\n " , pfrom->GetId ());
1371
1372
}
1372
- if (punish_duplicate_invalid && mapBlockIndex. find (first_invalid_header.GetHash ()) != mapBlockIndex. end ( )) {
1373
+ if (punish_duplicate_invalid && LookupBlockIndex (first_invalid_header.GetHash ())) {
1373
1374
// Goal: don't allow outbound peers to use up our outbound
1374
1375
// connection slots if they are on incompatible chains.
1375
1376
//
@@ -2050,13 +2051,13 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2050
2051
2051
2052
LOCK (cs_main);
2052
2053
2053
- BlockMap::iterator it = mapBlockIndex. find (req.blockhash );
2054
- if (it == mapBlockIndex. end () || !(it-> second ->nStatus & BLOCK_HAVE_DATA)) {
2054
+ const CBlockIndex* pindex = LookupBlockIndex (req.blockhash );
2055
+ if (!pindex || !(pindex ->nStatus & BLOCK_HAVE_DATA)) {
2055
2056
LogPrint (BCLog::NET, " Peer %d sent us a getblocktxn for a block we don't have" , pfrom->GetId ());
2056
2057
return true ;
2057
2058
}
2058
2059
2059
- if (it-> second ->nHeight < chainActive.Height () - MAX_BLOCKTXN_DEPTH) {
2060
+ if (pindex ->nHeight < chainActive.Height () - MAX_BLOCKTXN_DEPTH) {
2060
2061
// If an older block is requested (should never happen in practice,
2061
2062
// but can happen in tests) send a block response instead of a
2062
2063
// blocktxn response. Sending a full block response instead of a
@@ -2074,7 +2075,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2074
2075
}
2075
2076
2076
2077
CBlock block;
2077
- bool ret = ReadBlockFromDisk (block, it-> second , chainparams.GetConsensus ());
2078
+ bool ret = ReadBlockFromDisk (block, pindex , chainparams.GetConsensus ());
2078
2079
assert (ret);
2079
2080
2080
2081
SendBlockTransactions (block, req, pfrom, connman);
@@ -2098,10 +2099,10 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2098
2099
if (locator.IsNull ())
2099
2100
{
2100
2101
// If locator is null, return the hashStop block
2101
- BlockMap::iterator mi = mapBlockIndex. find (hashStop);
2102
- if (mi == mapBlockIndex. end ())
2102
+ pindex = LookupBlockIndex (hashStop);
2103
+ if (!pindex) {
2103
2104
return true ;
2104
- pindex = (*mi). second ;
2105
+ }
2105
2106
2106
2107
if (!BlockRequestAllowed (pindex, chainparams.GetConsensus ())) {
2107
2108
LogPrint (BCLog::NET, " %s: ignoring request from peer=%i for old block header that isn't in the main chain\n " , __func__, pfrom->GetId ());
@@ -2340,14 +2341,14 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
2340
2341
{
2341
2342
LOCK (cs_main);
2342
2343
2343
- if (mapBlockIndex. find (cmpctblock.header .hashPrevBlock ) == mapBlockIndex. end ( )) {
2344
+ if (! LookupBlockIndex (cmpctblock.header .hashPrevBlock )) {
2344
2345
// Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
2345
2346
if (!IsInitialBlockDownload ())
2346
2347
connman->PushMessage (pfrom, msgMaker.Make (NetMsgType::GETHEADERS, chainActive.GetLocator (pindexBestHeader), uint256 ()));
2347
2348
return true ;
2348
2349
}
2349
2350
2350
- if (mapBlockIndex. find (cmpctblock.header .GetHash ()) == mapBlockIndex. end ( )) {
2351
+ if (! LookupBlockIndex (cmpctblock.header .GetHash ())) {
2351
2352
received_new_header = true ;
2352
2353
}
2353
2354
}
@@ -3329,9 +3330,8 @@ bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic<bool>& interruptM
3329
3330
// then send all headers past that one. If we come across any
3330
3331
// headers that aren't on chainActive, give up.
3331
3332
for (const uint256 &hash : pto->vBlockHashesToAnnounce ) {
3332
- BlockMap::iterator mi = mapBlockIndex.find (hash);
3333
- assert (mi != mapBlockIndex.end ());
3334
- const CBlockIndex *pindex = mi->second ;
3333
+ const CBlockIndex* pindex = LookupBlockIndex (hash);
3334
+ assert (pindex);
3335
3335
if (chainActive[pindex->nHeight ] != pindex) {
3336
3336
// Bail out if we reorged away from this block
3337
3337
fRevertToInv = true ;
@@ -3422,9 +3422,8 @@ bool PeerLogicValidation::SendMessages(CNode* pto, std::atomic<bool>& interruptM
3422
3422
// in the past.
3423
3423
if (!pto->vBlockHashesToAnnounce .empty ()) {
3424
3424
const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce .back ();
3425
- BlockMap::iterator mi = mapBlockIndex.find (hashToAnnounce);
3426
- assert (mi != mapBlockIndex.end ());
3427
- const CBlockIndex *pindex = mi->second ;
3425
+ const CBlockIndex* pindex = LookupBlockIndex (hashToAnnounce);
3426
+ assert (pindex);
3428
3427
3429
3428
// Warn if we're announcing a block that is not on the main chain.
3430
3429
// This should be very rare and could be optimized out.
0 commit comments