Skip to content

Commit 28ba031

Browse files
committed
Add and use CBlockIndexHeightOnlyComparator
...also use std::sort for clarity
1 parent 12eb05d commit 28ba031

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/node/blockstorage.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ bool CBlockIndexWorkComparator::operator()(const CBlockIndex* pa, const CBlockIn
4747
return false;
4848
}
4949

50+
bool CBlockIndexHeightOnlyComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
51+
{
52+
return pa->nHeight < pb->nHeight;
53+
}
54+
5055
static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
5156
static FlatFileSeq BlockFileSeq();
5257
static FlatFileSeq UndoFileSeq();
@@ -242,10 +247,8 @@ bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params)
242247
for (auto& [_, block_index] : m_block_index) {
243248
vSortedByHeight.push_back(&block_index);
244249
}
245-
sort(vSortedByHeight.begin(), vSortedByHeight.end(),
246-
[](const CBlockIndex* pa, const CBlockIndex* pb) {
247-
return pa->nHeight < pb->nHeight;
248-
});
250+
std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
251+
CBlockIndexHeightOnlyComparator());
249252

250253
for (CBlockIndex* pindex : vSortedByHeight) {
251254
if (ShutdownRequested()) return false;

src/node/blockstorage.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ struct CBlockIndexWorkComparator {
6262
bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
6363
};
6464

65+
struct CBlockIndexHeightOnlyComparator {
66+
/* Only compares the height of two block indices, doesn't try to tie-break */
67+
bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
68+
};
69+
6570
/**
6671
* Maintains a tree of blocks (stored in `m_block_index`) which is consulted
6772
* to determine where the most-work tip is.

src/validation.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,22 @@
6565
using node::BLOCKFILE_CHUNK_SIZE;
6666
using node::BlockManager;
6767
using node::BlockMap;
68+
using node::CBlockIndexHeightOnlyComparator;
6869
using node::CBlockIndexWorkComparator;
6970
using node::CCoinsStats;
7071
using node::CoinStatsHashType;
72+
using node::fHavePruned;
73+
using node::fImporting;
74+
using node::fPruneMode;
75+
using node::fReindex;
7176
using node::GetUTXOStats;
77+
using node::nPruneTarget;
7278
using node::OpenBlockFile;
7379
using node::ReadBlockFromDisk;
7480
using node::SnapshotMetadata;
7581
using node::UNDOFILE_CHUNK_SIZE;
7682
using node::UndoReadFromDisk;
7783
using node::UnlinkPrunedFiles;
78-
using node::fHavePruned;
79-
using node::fImporting;
80-
using node::fPruneMode;
81-
using node::fReindex;
82-
using node::nPruneTarget;
8384

8485
#define MICRO 0.000001
8586
#define MILLI 0.001
@@ -4073,10 +4074,8 @@ bool ChainstateManager::LoadBlockIndex()
40734074
for (auto& [_, block_index] : m_blockman.m_block_index) {
40744075
vSortedByHeight.push_back(&block_index);
40754076
}
4076-
sort(vSortedByHeight.begin(), vSortedByHeight.end(),
4077-
[](const CBlockIndex* pa, const CBlockIndex* pb) {
4078-
return pa->nHeight < pb->nHeight;
4079-
});
4077+
std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
4078+
CBlockIndexHeightOnlyComparator());
40804079

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

0 commit comments

Comments
 (0)