diff options
author | MarcoFalke <falke.marco@gmail.com> | 2021-04-02 19:17:00 +0200 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2021-04-04 18:07:24 +0200 |
commit | fa413f07a14744e7d7f7746e861aabd9cf938f61 (patch) | |
tree | 9dcc034c59e07dbf0ccfec1699583f57d1dcd223 /src/node | |
parent | faf843c07f99f91603e08ea858f972516f1d669a (diff) |
move-only: Move ThreadImport to blockstorage
Can be reviewed with the git options
--color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
Diffstat (limited to 'src/node')
-rw-r--r-- | src/node/blockstorage.cpp | 96 | ||||
-rw-r--r-- | src/node/blockstorage.h | 19 |
2 files changed, 115 insertions, 0 deletions
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp new file mode 100644 index 0000000000..235dbb1693 --- /dev/null +++ b/src/node/blockstorage.cpp @@ -0,0 +1,96 @@ +// Copyright (c) 2011-2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include <blockstorage.h> + +#include <chainparams.h> +#include <fs.h> +#include <shutdown.h> +#include <util/system.h> +#include <validation.h> + +struct CImportingNow { + CImportingNow() + { + assert(fImporting == false); + fImporting = true; + } + + ~CImportingNow() + { + assert(fImporting == true); + fImporting = false; + } +}; + +void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args) +{ + const CChainParams& chainparams = Params(); + ScheduleBatchPriority(); + + { + CImportingNow imp; + + // -reindex + if (fReindex) { + int nFile = 0; + while (true) { + FlatFilePos pos(nFile, 0); + if (!fs::exists(GetBlockPosFilename(pos))) + break; // No block files left to reindex + FILE* file = OpenBlockFile(pos, true); + if (!file) + break; // This error is logged in OpenBlockFile + LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile); + ::ChainstateActive().LoadExternalBlockFile(chainparams, file, &pos); + if (ShutdownRequested()) { + LogPrintf("Shutdown requested. Exit %s\n", __func__); + return; + } + nFile++; + } + pblocktree->WriteReindexing(false); + fReindex = false; + LogPrintf("Reindexing finished\n"); + // To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked): + ::ChainstateActive().LoadGenesisBlock(chainparams); + } + + // -loadblock= + for (const fs::path& path : vImportFiles) { + FILE* file = fsbridge::fopen(path, "rb"); + if (file) { + LogPrintf("Importing blocks file %s...\n", path.string()); + ::ChainstateActive().LoadExternalBlockFile(chainparams, file); + if (ShutdownRequested()) { + LogPrintf("Shutdown requested. Exit %s\n", __func__); + return; + } + } else { + LogPrintf("Warning: Could not open blocks file %s\n", path.string()); + } + } + + // scan for better chains in the block chain database, that are not yet connected in the active best chain + + // We can't hold cs_main during ActivateBestChain even though we're accessing + // the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve + // the relevant pointers before the ABC call. + for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) { + BlockValidationState state; + if (!chainstate->ActivateBestChain(state, chainparams, nullptr)) { + LogPrintf("Failed to connect best block (%s)\n", state.ToString()); + StartShutdown(); + return; + } + } + + if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) { + LogPrintf("Stopping after block import\n"); + StartShutdown(); + return; + } + } // End scope of CImportingNow + chainman.ActiveChainstate().LoadMempool(args); +} diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h new file mode 100644 index 0000000000..d81b39f9f9 --- /dev/null +++ b/src/node/blockstorage.h @@ -0,0 +1,19 @@ +// Copyright (c) 2011-2021 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_NODE_BLOCKSTORAGE_H +#define BITCOIN_NODE_BLOCKSTORAGE_H + +#include <vector> + +#include <fs.h> + +class ArgsManager; +class ChainstateManager; + +static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false}; + +void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args); + +#endif // BITCOIN_NODE_BLOCKSTORAGE_H |