diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index f948a6cbd79..29ad8285ec0 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -220,17 +220,20 @@ bool BlockManager::LoadBlockIndex( } // Calculate nChainWork - std::vector> vSortedByHeight; + std::vector vSortedByHeight; vSortedByHeight.reserve(m_block_index.size()); for (auto& [_, block_index] : m_block_index) { - vSortedByHeight.push_back(std::make_pair(block_index.nHeight, &block_index)); + vSortedByHeight.push_back(&block_index); } - sort(vSortedByHeight.begin(), vSortedByHeight.end()); + sort(vSortedByHeight.begin(), vSortedByHeight.end(), + [](const CBlockIndex* pa, const CBlockIndex* pb) { + return pa->nHeight < pb->nHeight; + }); // Find start of assumed-valid region. int first_assumed_valid_height = std::numeric_limits::max(); - for (const auto& [height, block] : vSortedByHeight) { + for (const CBlockIndex* block : vSortedByHeight) { if (block->IsAssumedValid()) { auto chainstates = chainman.GetAll(); @@ -242,14 +245,13 @@ bool BlockManager::LoadBlockIndex( assert(any_chain([](auto chainstate) { return chainstate->reliesOnAssumedValid(); })); assert(any_chain([](auto chainstate) { return !chainstate->reliesOnAssumedValid(); })); - first_assumed_valid_height = height; + first_assumed_valid_height = block->nHeight; break; } } - for (const std::pair& item : vSortedByHeight) { + for (CBlockIndex* pindex : vSortedByHeight) { if (ShutdownRequested()) return false; - CBlockIndex* pindex = item.second; pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex); pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);