Skip to content

Commit e0dc305

Browse files
committed
validation: Move LoadExternalBlockFile to CChainState
[META] This commit should be followed up by removing the comments and assertions meant only to show that the change is correct. LoadExternalBlockFile mainly acts on CChainState.
1 parent 5f8cd7b commit e0dc305

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImp
705705
if (!file)
706706
break; // This error is logged in OpenBlockFile
707707
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
708-
LoadExternalBlockFile(chainparams, file, &pos);
708+
::ChainstateActive().LoadExternalBlockFile(chainparams, file, &pos);
709709
if (ShutdownRequested()) {
710710
LogPrintf("Shutdown requested. Exit %s\n", __func__);
711711
return;
@@ -724,7 +724,7 @@ static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImp
724724
FILE *file = fsbridge::fopen(path, "rb");
725725
if (file) {
726726
LogPrintf("Importing blocks file %s...\n", path.string());
727-
LoadExternalBlockFile(chainparams, file);
727+
::ChainstateActive().LoadExternalBlockFile(chainparams, file);
728728
if (ShutdownRequested()) {
729729
LogPrintf("Shutdown requested. Exit %s\n", __func__);
730730
return;

src/test/fuzz/load_external_block_file.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ FUZZ_TARGET_INIT(load_external_block_file, initialize_load_external_block_file)
2727
return;
2828
}
2929
FlatFilePos flat_file_pos;
30-
LoadExternalBlockFile(Params(), fuzzed_block_file, fuzzed_data_provider.ConsumeBool() ? &flat_file_pos : nullptr);
30+
::ChainstateActive().LoadExternalBlockFile(Params(), fuzzed_block_file, fuzzed_data_provider.ConsumeBool() ? &flat_file_pos : nullptr);
3131
}

src/validation.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4602,7 +4602,7 @@ bool LoadGenesisBlock(const CChainParams& chainparams)
46024602
return ::ChainstateActive().LoadGenesisBlock(chainparams);
46034603
}
46044604

4605-
void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFilePos* dbp)
4605+
void CChainState::LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFilePos* dbp)
46064606
{
46074607
// Map of disk positions for blocks with unknown parent (only used for reindex)
46084608
static std::multimap<uint256, FlatFilePos> mapBlocksUnknownParent;
@@ -4651,7 +4651,8 @@ void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFi
46514651
{
46524652
LOCK(cs_main);
46534653
// detect out of order blocks, and store them for later
4654-
if (hash != chainparams.GetConsensus().hashGenesisBlock && !g_chainman.m_blockman.LookupBlockIndex(block.hashPrevBlock)) {
4654+
assert(std::addressof(g_chainman.m_blockman) == std::addressof(m_blockman));
4655+
if (hash != chainparams.GetConsensus().hashGenesisBlock && !m_blockman.LookupBlockIndex(block.hashPrevBlock)) {
46554656
LogPrint(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
46564657
block.hashPrevBlock.ToString());
46574658
if (dbp)
@@ -4660,10 +4661,12 @@ void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFi
46604661
}
46614662

46624663
// process in case the block isn't known yet
4663-
CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
4664+
assert(std::addressof(g_chainman.m_blockman) == std::addressof(m_blockman));
4665+
CBlockIndex* pindex = m_blockman.LookupBlockIndex(hash);
46644666
if (!pindex || (pindex->nStatus & BLOCK_HAVE_DATA) == 0) {
46654667
BlockValidationState state;
4666-
if (::ChainstateActive().AcceptBlock(pblock, state, chainparams, nullptr, true, dbp, nullptr)) {
4668+
assert(std::addressof(::ChainstateActive()) == std::addressof(*this));
4669+
if (AcceptBlock(pblock, state, chainparams, nullptr, true, dbp, nullptr)) {
46674670
nLoaded++;
46684671
}
46694672
if (state.IsError()) {
@@ -4677,12 +4680,14 @@ void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFi
46774680
// Activate the genesis block so normal node progress can continue
46784681
if (hash == chainparams.GetConsensus().hashGenesisBlock) {
46794682
BlockValidationState state;
4680-
if (!::ChainstateActive().ActivateBestChain(state, chainparams, nullptr)) {
4683+
assert(std::addressof(::ChainstateActive()) == std::addressof(*this));
4684+
if (!ActivateBestChain(state, chainparams, nullptr)) {
46814685
break;
46824686
}
46834687
}
46844688

4685-
NotifyHeaderTip(::ChainstateActive());
4689+
assert(std::addressof(::ChainstateActive()) == std::addressof(*this));
4690+
NotifyHeaderTip(*this);
46864691

46874692
// Recursively process earlier encountered successors of this block
46884693
std::deque<uint256> queue;
@@ -4700,15 +4705,17 @@ void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFi
47004705
head.ToString());
47014706
LOCK(cs_main);
47024707
BlockValidationState dummy;
4703-
if (::ChainstateActive().AcceptBlock(pblockrecursive, dummy, chainparams, nullptr, true, &it->second, nullptr))
4708+
assert(std::addressof(::ChainstateActive()) == std::addressof(*this));
4709+
if (AcceptBlock(pblockrecursive, dummy, chainparams, nullptr, true, &it->second, nullptr))
47044710
{
47054711
nLoaded++;
47064712
queue.push_back(pblockrecursive->GetHash());
47074713
}
47084714
}
47094715
range.first++;
47104716
mapBlocksUnknownParent.erase(it);
4711-
NotifyHeaderTip(::ChainstateActive());
4717+
assert(std::addressof(::ChainstateActive()) == std::addressof(*this));
4718+
NotifyHeaderTip(*this);
47124719
}
47134720
}
47144721
} catch (const std::exception& e) {

src/validation.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ extern const std::vector<std::string> CHECKLEVEL_DOC;
144144
FILE* OpenBlockFile(const FlatFilePos &pos, bool fReadOnly = false);
145145
/** Translation to a filesystem path */
146146
fs::path GetBlockPosFilename(const FlatFilePos &pos);
147-
/** Import blocks from an external file */
148-
void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFilePos* dbp = nullptr);
149147
/** Ensures we have a genesis block in the block tree, possibly writing one to disk. */
150148
bool LoadGenesisBlock(const CChainParams& chainparams);
151149
/** Unload database information */
@@ -616,6 +614,9 @@ class CChainState
616614
bool ResizeCoinsCaches(size_t coinstip_size, size_t coinsdb_size)
617615
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
618616

617+
/** Import blocks from an external file */
618+
void LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, FlatFilePos* dbp = nullptr);
619+
619620
/**
620621
* Update the on-disk chain state.
621622
* The caches and indexes are flushed depending on the mode we're called with

0 commit comments

Comments
 (0)