Skip to content

Commit c05cf7a

Browse files
committed
style: Modernize range-based loops over m_block_index
1 parent c2a1655 commit c05cf7a

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

src/node/blockstorage.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ bool BlockManager::LoadBlockIndex(
228228
// Calculate nChainWork
229229
std::vector<std::pair<int, CBlockIndex*>> vSortedByHeight;
230230
vSortedByHeight.reserve(m_block_index.size());
231-
for (std::pair<const uint256, CBlockIndex>& item : m_block_index) {
232-
CBlockIndex* pindex = &item.second;
231+
for (auto& [_, block_index] : m_block_index) {
232+
CBlockIndex* pindex = &block_index;
233233
vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
234234
}
235235
sort(vSortedByHeight.begin(), vSortedByHeight.end());
@@ -386,8 +386,8 @@ bool BlockManager::LoadBlockIndexDB(ChainstateManager& chainman)
386386
// Check presence of blk files
387387
LogPrintf("Checking all blk files are present...\n");
388388
std::set<int> setBlkDataFiles;
389-
for (const std::pair<const uint256, CBlockIndex>& item : m_block_index) {
390-
const CBlockIndex* pindex = &item.second;
389+
for (const auto& [_, block_index] : m_block_index) {
390+
const CBlockIndex* pindex = &block_index;
391391
if (pindex->nStatus & BLOCK_HAVE_DATA) {
392392
setBlkDataFiles.insert(pindex->nFile);
393393
}

src/rpc/blockchain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,10 +1753,10 @@ static RPCHelpMan getchaintips()
17531753
std::set<const CBlockIndex*> setOrphans;
17541754
std::set<const CBlockIndex*> setPrevs;
17551755

1756-
for (const std::pair<const uint256, CBlockIndex>& item : chainman.BlockIndex()) {
1757-
if (!active_chain.Contains(&item.second)) {
1758-
setOrphans.insert(&item.second);
1759-
setPrevs.insert(item.second.pprev);
1756+
for (const auto& [_, block_index] : chainman.BlockIndex()) {
1757+
if (!active_chain.Contains(&block_index)) {
1758+
setOrphans.insert(&block_index);
1759+
setPrevs.insert(block_index.pprev);
17601760
}
17611761
}
17621762

src/validation.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3133,12 +3133,10 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
31333133
// it up here, this should be an essentially unobservable error.
31343134
// Loop back over all block index entries and add any missing entries
31353135
// to setBlockIndexCandidates.
3136-
BlockMap::iterator it = m_blockman.m_block_index.begin();
3137-
while (it != m_blockman.m_block_index.end()) {
3138-
if (it->second.IsValid(BLOCK_VALID_TRANSACTIONS) && it->second.HaveTxsDownloaded() && !setBlockIndexCandidates.value_comp()(&it->second, m_chain.Tip())) {
3139-
setBlockIndexCandidates.insert(&it->second);
3136+
for (auto& [_, block_index] : m_blockman.m_block_index) {
3137+
if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveTxsDownloaded() && !setBlockIndexCandidates.value_comp()(&block_index, m_chain.Tip())) {
3138+
setBlockIndexCandidates.insert(&block_index);
31403139
}
3141-
it++;
31423140
}
31433141

31443142
InvalidChainFound(to_mark_failed);
@@ -3157,21 +3155,19 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
31573155
int nHeight = pindex->nHeight;
31583156

31593157
// Remove the invalidity flag from this block and all its descendants.
3160-
BlockMap::iterator it = m_blockman.m_block_index.begin();
3161-
while (it != m_blockman.m_block_index.end()) {
3162-
if (!it->second.IsValid() && it->second.GetAncestor(nHeight) == pindex) {
3163-
it->second.nStatus &= ~BLOCK_FAILED_MASK;
3164-
m_blockman.m_dirty_blockindex.insert(&it->second);
3165-
if (it->second.IsValid(BLOCK_VALID_TRANSACTIONS) && it->second.HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), &it->second)) {
3166-
setBlockIndexCandidates.insert(&it->second);
3158+
for (auto& [_, block_index] : m_blockman.m_block_index) {
3159+
if (!block_index.IsValid() && block_index.GetAncestor(nHeight) == pindex) {
3160+
block_index.nStatus &= ~BLOCK_FAILED_MASK;
3161+
m_blockman.m_dirty_blockindex.insert(&block_index);
3162+
if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), &block_index)) {
3163+
setBlockIndexCandidates.insert(&block_index);
31673164
}
3168-
if (&it->second == m_chainman.m_best_invalid) {
3165+
if (&block_index == m_chainman.m_best_invalid) {
31693166
// Reset invalid block marker if it was pointing to one of those.
31703167
m_chainman.m_best_invalid = nullptr;
31713168
}
3172-
m_chainman.m_failed_blocks.erase(&it->second);
3169+
m_chainman.m_failed_blocks.erase(&block_index);
31733170
}
3174-
it++;
31753171
}
31763172

31773173
// Remove the invalidity flag from all ancestors too.
@@ -4261,8 +4257,8 @@ void CChainState::CheckBlockIndex()
42614257

42624258
// Build forward-pointing map of the entire block tree.
42634259
std::multimap<CBlockIndex*,CBlockIndex*> forward;
4264-
for (std::pair<const uint256, CBlockIndex>& entry : m_blockman.m_block_index) {
4265-
forward.insert(std::make_pair(entry.second.pprev, &entry.second));
4260+
for (auto& [_, block_index] : m_blockman.m_block_index) {
4261+
forward.emplace(block_index.pprev, &block_index);
42664262
}
42674263

42684264
assert(forward.size() == m_blockman.m_block_index.size());

0 commit comments

Comments
 (0)