Skip to content

Commit d7e402c

Browse files
committed
node: sort m_dirty_blockindex by block hash
1 parent 94a9e8c commit d7e402c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/node/blockstorage.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ bool CBlockIndexHeightOnlyComparator::operator()(const CBlockIndex* pa, const CB
179179
return pa->nHeight < pb->nHeight;
180180
}
181181

182+
bool CBlockIndexBlockHashComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
183+
{
184+
return UintToArith256(pa->GetBlockHash()) < UintToArith256(pb->GetBlockHash());
185+
}
186+
182187
std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
183188
{
184189
AssertLockHeld(cs_main);
@@ -487,7 +492,7 @@ bool BlockManager::WriteBlockIndexDB()
487492
}
488493
std::vector<const CBlockIndex*> vBlocks;
489494
vBlocks.reserve(m_dirty_blockindex.size());
490-
for (std::set<CBlockIndex*>::iterator it = m_dirty_blockindex.begin(); it != m_dirty_blockindex.end();) {
495+
for (std::set<CBlockIndex*, CBlockIndexBlockHashComparator>::iterator it = m_dirty_blockindex.begin(); it != m_dirty_blockindex.end();) {
491496
vBlocks.push_back(*it);
492497
m_dirty_blockindex.erase(it++);
493498
}

src/node/blockstorage.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ struct CBlockIndexHeightOnlyComparator {
9595
bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
9696
};
9797

98+
struct CBlockIndexBlockHashComparator {
99+
bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
100+
};
101+
98102
struct PruneLockInfo {
99103
int height_first{std::numeric_limits<int>::max()}; //! Height of earliest block that should be kept and not pruned
100104
};
@@ -238,7 +242,7 @@ class BlockManager
238242
const Obfuscation m_obfuscation;
239243

240244
/** Dirty block index entries. */
241-
std::set<CBlockIndex*> m_dirty_blockindex;
245+
std::set<CBlockIndex*, CBlockIndexBlockHashComparator> m_dirty_blockindex;
242246

243247
/** Dirty block file entries. */
244248
std::set<int> m_dirty_fileinfo;

0 commit comments

Comments
 (0)