Skip to content

Commit f865cf8

Browse files
committed
Add and use BlockManager::GetAllBlockIndices
1 parent 28ba031 commit f865cf8

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/node/blockstorage.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ static FILE* OpenUndoFile(const FlatFilePos& pos, bool fReadOnly = false);
5656
static FlatFileSeq BlockFileSeq();
5757
static FlatFileSeq UndoFileSeq();
5858

59+
std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
60+
{
61+
AssertLockHeld(cs_main);
62+
std::vector<CBlockIndex*> rv;
63+
rv.reserve(m_block_index.size());
64+
for (auto& [_, block_index] : m_block_index) {
65+
rv.push_back(&block_index);
66+
}
67+
return rv;
68+
}
69+
5970
CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash)
6071
{
6172
AssertLockHeld(cs_main);
@@ -242,11 +253,7 @@ bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params)
242253
}
243254

244255
// Calculate nChainWork
245-
std::vector<CBlockIndex*> vSortedByHeight;
246-
vSortedByHeight.reserve(m_block_index.size());
247-
for (auto& [_, block_index] : m_block_index) {
248-
vSortedByHeight.push_back(&block_index);
249-
}
256+
std::vector<CBlockIndex*> vSortedByHeight{GetAllBlockIndices()};
250257
std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
251258
CBlockIndexHeightOnlyComparator());
252259

src/node/blockstorage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ class BlockManager
123123
public:
124124
BlockMap m_block_index GUARDED_BY(cs_main);
125125

126+
std::vector<CBlockIndex*> GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
127+
126128
/**
127129
* All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
128130
* Pruned nodes may have entries where B is missing data.

src/validation.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4069,11 +4069,7 @@ bool ChainstateManager::LoadBlockIndex()
40694069
bool ret = m_blockman.LoadBlockIndexDB();
40704070
if (!ret) return false;
40714071

4072-
std::vector<CBlockIndex*> vSortedByHeight;
4073-
vSortedByHeight.reserve(m_blockman.m_block_index.size());
4074-
for (auto& [_, block_index] : m_blockman.m_block_index) {
4075-
vSortedByHeight.push_back(&block_index);
4076-
}
4072+
std::vector<CBlockIndex*> vSortedByHeight{m_blockman.GetAllBlockIndices()};
40774073
std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
40784074
CBlockIndexHeightOnlyComparator());
40794075

0 commit comments

Comments
 (0)