| 869 | }; |
| 870 | |
| 871 | void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args) |
| 872 | { |
| 873 | SetSyscallSandboxPolicy(SyscallSandboxPolicy::INITIALIZATION_LOAD_BLOCKS); |
| 874 | ScheduleBatchPriority(); |
| 875 | |
| 876 | { |
| 877 | CImportingNow imp; |
| 878 | |
| 879 | // -reindex |
| 880 | if (fReindex) { |
| 881 | int nFile = 0; |
| 882 | while (true) { |
| 883 | FlatFilePos pos(nFile, 0); |
| 884 | if (!fs::exists(GetBlockPosFilename(pos))) { |
| 885 | break; // No block files left to reindex |
| 886 | } |
| 887 | FILE* file = OpenBlockFile(pos, true); |
| 888 | if (!file) { |
| 889 | break; // This error is logged in OpenBlockFile |
| 890 | } |
| 891 | LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile); |
| 892 | chainman.ActiveChainstate().LoadExternalBlockFile(file, &pos); |
| 893 | if (ShutdownRequested()) { |
| 894 | LogPrintf("Shutdown requested. Exit %s\n", __func__); |
| 895 | return; |
| 896 | } |
| 897 | nFile++; |
| 898 | } |
| 899 | WITH_LOCK(::cs_main, chainman.m_blockman.m_block_tree_db->WriteReindexing(false)); |
| 900 | fReindex = false; |
| 901 | LogPrintf("Reindexing finished\n"); |
| 902 | // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked): |
| 903 | chainman.ActiveChainstate().LoadGenesisBlock(); |
| 904 | } |
| 905 | |
| 906 | // -loadblock= |
| 907 | for (const fs::path& path : vImportFiles) { |
| 908 | FILE* file = fsbridge::fopen(path, "rb"); |
| 909 | if (file) { |
| 910 | LogPrintf("Importing blocks file %s...\n", fs::PathToString(path)); |
| 911 | chainman.ActiveChainstate().LoadExternalBlockFile(file); |
| 912 | if (ShutdownRequested()) { |
| 913 | LogPrintf("Shutdown requested. Exit %s\n", __func__); |
| 914 | return; |
| 915 | } |
| 916 | } else { |
| 917 | LogPrintf("Warning: Could not open blocks file %s\n", fs::PathToString(path)); |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | // scan for better chains in the block chain database, that are not yet connected in the active best chain |
| 922 | |
| 923 | // We can't hold cs_main during ActivateBestChain even though we're accessing |
| 924 | // the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve |
| 925 | // the relevant pointers before the ABC call. |
| 926 | for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) { |
| 927 | BlockValidationState state; |
| 928 | if (!chainstate->ActivateBestChain(state, nullptr)) { |
no test coverage detected