Changeset View
Changeset View
Standalone View
Standalone View
src/node/blockstorage.cpp
Show First 20 Lines • Show All 863 Lines • ▼ Show 20 Lines | struct CImportingNow { | ||||
} | } | ||||
~CImportingNow() { | ~CImportingNow() { | ||||
assert(fImporting == true); | assert(fImporting == true); | ||||
fImporting = false; | fImporting = false; | ||||
} | } | ||||
}; | }; | ||||
void ThreadImport(const Config &config, ChainstateManager &chainman, | void ThreadImport(ChainstateManager &chainman, | ||||
std::vector<fs::path> vImportFiles, const ArgsManager &args, | std::vector<fs::path> vImportFiles, const ArgsManager &args, | ||||
const fs::path &mempool_path) { | const fs::path &mempool_path) { | ||||
ScheduleBatchPriority(); | ScheduleBatchPriority(); | ||||
{ | { | ||||
const CChainParams &chainParams = config.GetChainParams(); | |||||
CImportingNow imp; | CImportingNow imp; | ||||
// -reindex | // -reindex | ||||
if (fReindex) { | if (fReindex) { | ||||
int nFile = 0; | int nFile = 0; | ||||
// Map of disk positions for blocks with unknown parent (only used | // Map of disk positions for blocks with unknown parent (only used | ||||
// for reindex); parent hash -> child disk position, multiple | // for reindex); parent hash -> child disk position, multiple | ||||
// children can have the same parent. | // children can have the same parent. | ||||
std::multimap<BlockHash, FlatFilePos> blocks_with_unknown_parent; | std::multimap<BlockHash, FlatFilePos> blocks_with_unknown_parent; | ||||
while (true) { | while (true) { | ||||
FlatFilePos pos(nFile, 0); | FlatFilePos pos(nFile, 0); | ||||
if (!fs::exists(GetBlockPosFilename(pos))) { | if (!fs::exists(GetBlockPosFilename(pos))) { | ||||
// No block files left to reindex | // No block files left to reindex | ||||
break; | break; | ||||
} | } | ||||
FILE *file = OpenBlockFile(pos, true); | FILE *file = OpenBlockFile(pos, true); | ||||
if (!file) { | if (!file) { | ||||
// This error is logged in OpenBlockFile | // This error is logged in OpenBlockFile | ||||
break; | break; | ||||
} | } | ||||
LogPrintf("Reindexing block file blk%05u.dat...\n", | LogPrintf("Reindexing block file blk%05u.dat...\n", | ||||
(unsigned int)nFile); | (unsigned int)nFile); | ||||
chainman.ActiveChainstate().LoadExternalBlockFile( | chainman.ActiveChainstate().LoadExternalBlockFile( | ||||
config, file, &pos, &blocks_with_unknown_parent); | file, &pos, &blocks_with_unknown_parent); | ||||
if (ShutdownRequested()) { | if (ShutdownRequested()) { | ||||
LogPrintf("Shutdown requested. Exit %s\n", __func__); | LogPrintf("Shutdown requested. Exit %s\n", __func__); | ||||
return; | return; | ||||
} | } | ||||
nFile++; | nFile++; | ||||
} | } | ||||
WITH_LOCK( | WITH_LOCK( | ||||
::cs_main, | ::cs_main, | ||||
chainman.m_blockman.m_block_tree_db->WriteReindexing(false)); | chainman.m_blockman.m_block_tree_db->WriteReindexing(false)); | ||||
fReindex = false; | fReindex = false; | ||||
LogPrintf("Reindexing finished\n"); | LogPrintf("Reindexing finished\n"); | ||||
// To avoid ending up in a situation without genesis block, re-try | // To avoid ending up in a situation without genesis block, re-try | ||||
// initializing (no-op if reindexing worked): | // initializing (no-op if reindexing worked): | ||||
chainman.ActiveChainstate().LoadGenesisBlock(); | chainman.ActiveChainstate().LoadGenesisBlock(); | ||||
} | } | ||||
// -loadblock= | // -loadblock= | ||||
for (const fs::path &path : vImportFiles) { | for (const fs::path &path : vImportFiles) { | ||||
FILE *file = fsbridge::fopen(path, "rb"); | FILE *file = fsbridge::fopen(path, "rb"); | ||||
if (file) { | if (file) { | ||||
LogPrintf("Importing blocks file %s...\n", | LogPrintf("Importing blocks file %s...\n", | ||||
fs::PathToString(path)); | fs::PathToString(path)); | ||||
chainman.ActiveChainstate().LoadExternalBlockFile(config, file); | chainman.ActiveChainstate().LoadExternalBlockFile(file); | ||||
if (ShutdownRequested()) { | if (ShutdownRequested()) { | ||||
LogPrintf("Shutdown requested. Exit %s\n", __func__); | LogPrintf("Shutdown requested. Exit %s\n", __func__); | ||||
return; | return; | ||||
} | } | ||||
} else { | } else { | ||||
LogPrintf("Warning: Could not open blocks file %s\n", | LogPrintf("Warning: Could not open blocks file %s\n", | ||||
fs::PathToString(path)); | fs::PathToString(path)); | ||||
} | } | ||||
} | } | ||||
// Reconsider blocks we know are valid. They may have been marked | // Reconsider blocks we know are valid. They may have been marked | ||||
// invalid by, for instance, running an outdated version of the node | // invalid by, for instance, running an outdated version of the node | ||||
// software. | // software. | ||||
const MapCheckpoints &checkpoints = | const MapCheckpoints &checkpoints = | ||||
chainParams.Checkpoints().mapCheckpoints; | chainman.GetParams().Checkpoints().mapCheckpoints; | ||||
for (const MapCheckpoints::value_type &i : checkpoints) { | for (const MapCheckpoints::value_type &i : checkpoints) { | ||||
const BlockHash &hash = i.second; | const BlockHash &hash = i.second; | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
CBlockIndex *pblockindex = | CBlockIndex *pblockindex = | ||||
chainman.m_blockman.LookupBlockIndex(hash); | chainman.m_blockman.LookupBlockIndex(hash); | ||||
if (pblockindex && !pblockindex->nStatus.isValid()) { | if (pblockindex && !pblockindex->nStatus.isValid()) { | ||||
LogPrintf("Reconsidering checkpointed block %s ...\n", | LogPrintf("Reconsidering checkpointed block %s ...\n", | ||||
Show All 39 Lines |