Skip to content

Commit 42e56d9

Browse files
committed
style-only: No need for std::pair for vSortedByHeight
...since the height information in already in CBlockIndex* and we can use an easy custom sorter.
1 parent 3bbb6fe commit 42e56d9

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/node/blockstorage.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,20 @@ bool BlockManager::LoadBlockIndex(
220220
}
221221

222222
// Calculate nChainWork
223-
std::vector<std::pair<int, CBlockIndex*>> vSortedByHeight;
223+
std::vector<CBlockIndex*> vSortedByHeight;
224224
vSortedByHeight.reserve(m_block_index.size());
225225
for (auto& [_, block_index] : m_block_index) {
226-
vSortedByHeight.push_back(std::make_pair(block_index.nHeight, &block_index));
226+
vSortedByHeight.push_back(&block_index);
227227
}
228-
sort(vSortedByHeight.begin(), vSortedByHeight.end());
228+
sort(vSortedByHeight.begin(), vSortedByHeight.end(),
229+
[](const CBlockIndex* pa, const CBlockIndex* pb) {
230+
return pa->nHeight < pb->nHeight;
231+
});
229232

230233
// Find start of assumed-valid region.
231234
int first_assumed_valid_height = std::numeric_limits<int>::max();
232235

233-
for (const auto& [height, block] : vSortedByHeight) {
236+
for (const CBlockIndex* block : vSortedByHeight) {
234237
if (block->IsAssumedValid()) {
235238
auto chainstates = chainman.GetAll();
236239

@@ -242,14 +245,13 @@ bool BlockManager::LoadBlockIndex(
242245
assert(any_chain([](auto chainstate) { return chainstate->reliesOnAssumedValid(); }));
243246
assert(any_chain([](auto chainstate) { return !chainstate->reliesOnAssumedValid(); }));
244247

245-
first_assumed_valid_height = height;
248+
first_assumed_valid_height = block->nHeight;
246249
break;
247250
}
248251
}
249252

250-
for (const std::pair<int, CBlockIndex*>& item : vSortedByHeight) {
253+
for (CBlockIndex* pindex : vSortedByHeight) {
251254
if (ShutdownRequested()) return false;
252-
CBlockIndex* pindex = item.second;
253255
pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
254256
pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
255257

0 commit comments

Comments
 (0)