refactor: Make mapBlocksUnknownParent local, and rename it

Co-authored-by: Larry Ruane <larryruane@gmail.com>
This commit is contained in:
Hennadii Stepanov
2020-07-26 23:43:01 +03:00
committed by Larry Ruane
parent f002f8a0e7
commit dd065dae9f
4 changed files with 60 additions and 12 deletions

View File

@@ -57,6 +57,7 @@
#include <warnings.h>
#include <algorithm>
#include <cassert>
#include <chrono>
#include <deque>
#include <numeric>
@@ -4261,11 +4262,16 @@ bool CChainState::LoadGenesisBlock()
return true;
}
void CChainState::LoadExternalBlockFile(FILE* fileIn, FlatFilePos* dbp)
void CChainState::LoadExternalBlockFile(
FILE* fileIn,
FlatFilePos* dbp,
std::multimap<uint256, FlatFilePos>* blocks_with_unknown_parent)
{
AssertLockNotHeld(m_chainstate_mutex);
// Map of disk positions for blocks with unknown parent (only used for reindex)
static std::multimap<uint256, FlatFilePos> mapBlocksUnknownParent;
// Either both should be specified (-reindex), or neither (-loadblock).
assert(!dbp == !blocks_with_unknown_parent);
int64_t nStart = GetTimeMillis();
int nLoaded = 0;
@@ -4315,8 +4321,9 @@ void CChainState::LoadExternalBlockFile(FILE* fileIn, FlatFilePos* dbp)
if (hash != m_params.GetConsensus().hashGenesisBlock && !m_blockman.LookupBlockIndex(block.hashPrevBlock)) {
LogPrint(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
block.hashPrevBlock.ToString());
if (dbp)
mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
if (dbp && blocks_with_unknown_parent) {
blocks_with_unknown_parent->emplace(block.hashPrevBlock, *dbp);
}
continue;
}
@@ -4345,13 +4352,15 @@ void CChainState::LoadExternalBlockFile(FILE* fileIn, FlatFilePos* dbp)
NotifyHeaderTip(*this);
if (!blocks_with_unknown_parent) continue;
// Recursively process earlier encountered successors of this block
std::deque<uint256> queue;
queue.push_back(hash);
while (!queue.empty()) {
uint256 head = queue.front();
queue.pop_front();
std::pair<std::multimap<uint256, FlatFilePos>::iterator, std::multimap<uint256, FlatFilePos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
auto range = blocks_with_unknown_parent->equal_range(head);
while (range.first != range.second) {
std::multimap<uint256, FlatFilePos>::iterator it = range.first;
std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
@@ -4366,7 +4375,7 @@ void CChainState::LoadExternalBlockFile(FILE* fileIn, FlatFilePos* dbp)
}
}
range.first++;
mapBlocksUnknownParent.erase(it);
blocks_with_unknown_parent->erase(it);
NotifyHeaderTip(*this);
}
}