Skip to content

Commit fa413f0

Browse files
author
MarcoFalke
committed
move-only: Move ThreadImport to blockstorage
Can be reviewed with the git options --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
1 parent faf843c commit fa413f0

File tree

4 files changed

+118
-88
lines changed

4 files changed

+118
-88
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ BITCOIN_CORE_H = \
174174
netaddress.h \
175175
netbase.h \
176176
netmessagemaker.h \
177+
node/blockstorage.h \
177178
node/coin.h \
178179
node/coinstats.h \
179180
node/context.h \
@@ -324,6 +325,7 @@ libbitcoin_server_a_SOURCES = \
324325
miner.cpp \
325326
net.cpp \
326327
net_processing.cpp \
328+
node/blockstorage.cpp \
327329
node/coin.cpp \
328330
node/coinstats.cpp \
329331
node/context.cpp \

src/init.cpp

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <chain.h>
1717
#include <chainparams.h>
1818
#include <compat/sanity.h>
19-
#include <consensus/validation.h>
2019
#include <fs.h>
2120
#include <hash.h>
2221
#include <httprpc.h>
@@ -32,6 +31,7 @@
3231
#include <net_permissions.h>
3332
#include <net_processing.h>
3433
#include <netbase.h>
34+
#include <node/blockstorage.h>
3535
#include <node/context.h>
3636
#include <node/ui_interface.h>
3737
#include <policy/feerate.h>
@@ -61,7 +61,6 @@
6161
#include <util/threadnames.h>
6262
#include <util/translation.h>
6363
#include <validation.h>
64-
6564
#include <validationinterface.h>
6665
#include <walletinitinterface.h>
6766

@@ -90,7 +89,6 @@
9089

9190
static const bool DEFAULT_PROXYRANDOMIZE = true;
9291
static const bool DEFAULT_REST_ENABLE = false;
93-
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;
9492

9593
#ifdef WIN32
9694
// Win32 LevelDB doesn't use filedescriptors, and the ones used for
@@ -625,20 +623,6 @@ static void BlockNotifyGenesisWait(const CBlockIndex* pBlockIndex)
625623
}
626624
}
627625

628-
struct CImportingNow
629-
{
630-
CImportingNow() {
631-
assert(fImporting == false);
632-
fImporting = true;
633-
}
634-
635-
~CImportingNow() {
636-
assert(fImporting == true);
637-
fImporting = false;
638-
}
639-
};
640-
641-
642626
// If we're using -prune with -reindex, then delete block files that will be ignored by the
643627
// reindex. Since reindexing works by starting at block file 0 and looping until a blockfile
644628
// is missing, do the same here to delete any later block files after a gap. Also delete all
@@ -691,77 +675,6 @@ static void StartupNotify(const ArgsManager& args)
691675
}
692676
#endif
693677

694-
static void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args)
695-
{
696-
const CChainParams& chainparams = Params();
697-
ScheduleBatchPriority();
698-
699-
{
700-
CImportingNow imp;
701-
702-
// -reindex
703-
if (fReindex) {
704-
int nFile = 0;
705-
while (true) {
706-
FlatFilePos pos(nFile, 0);
707-
if (!fs::exists(GetBlockPosFilename(pos)))
708-
break; // No block files left to reindex
709-
FILE *file = OpenBlockFile(pos, true);
710-
if (!file)
711-
break; // This error is logged in OpenBlockFile
712-
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
713-
::ChainstateActive().LoadExternalBlockFile(chainparams, file, &pos);
714-
if (ShutdownRequested()) {
715-
LogPrintf("Shutdown requested. Exit %s\n", __func__);
716-
return;
717-
}
718-
nFile++;
719-
}
720-
pblocktree->WriteReindexing(false);
721-
fReindex = false;
722-
LogPrintf("Reindexing finished\n");
723-
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
724-
::ChainstateActive().LoadGenesisBlock(chainparams);
725-
}
726-
727-
// -loadblock=
728-
for (const fs::path& path : vImportFiles) {
729-
FILE *file = fsbridge::fopen(path, "rb");
730-
if (file) {
731-
LogPrintf("Importing blocks file %s...\n", path.string());
732-
::ChainstateActive().LoadExternalBlockFile(chainparams, file);
733-
if (ShutdownRequested()) {
734-
LogPrintf("Shutdown requested. Exit %s\n", __func__);
735-
return;
736-
}
737-
} else {
738-
LogPrintf("Warning: Could not open blocks file %s\n", path.string());
739-
}
740-
}
741-
742-
// scan for better chains in the block chain database, that are not yet connected in the active best chain
743-
744-
// We can't hold cs_main during ActivateBestChain even though we're accessing
745-
// the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
746-
// the relevant pointers before the ABC call.
747-
for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
748-
BlockValidationState state;
749-
if (!chainstate->ActivateBestChain(state, chainparams, nullptr)) {
750-
LogPrintf("Failed to connect best block (%s)\n", state.ToString());
751-
StartShutdown();
752-
return;
753-
}
754-
}
755-
756-
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
757-
LogPrintf("Stopping after block import\n");
758-
StartShutdown();
759-
return;
760-
}
761-
} // End scope of CImportingNow
762-
chainman.ActiveChainstate().LoadMempool(args);
763-
}
764-
765678
/** Sanity checks
766679
* Ensure that Bitcoin is running in a usable environment with all
767680
* necessary library support.

src/node/blockstorage.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) 2011-2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <blockstorage.h>
6+
7+
#include <chainparams.h>
8+
#include <fs.h>
9+
#include <shutdown.h>
10+
#include <util/system.h>
11+
#include <validation.h>
12+
13+
struct CImportingNow {
14+
CImportingNow()
15+
{
16+
assert(fImporting == false);
17+
fImporting = true;
18+
}
19+
20+
~CImportingNow()
21+
{
22+
assert(fImporting == true);
23+
fImporting = false;
24+
}
25+
};
26+
27+
void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args)
28+
{
29+
const CChainParams& chainparams = Params();
30+
ScheduleBatchPriority();
31+
32+
{
33+
CImportingNow imp;
34+
35+
// -reindex
36+
if (fReindex) {
37+
int nFile = 0;
38+
while (true) {
39+
FlatFilePos pos(nFile, 0);
40+
if (!fs::exists(GetBlockPosFilename(pos)))
41+
break; // No block files left to reindex
42+
FILE* file = OpenBlockFile(pos, true);
43+
if (!file)
44+
break; // This error is logged in OpenBlockFile
45+
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
46+
::ChainstateActive().LoadExternalBlockFile(chainparams, file, &pos);
47+
if (ShutdownRequested()) {
48+
LogPrintf("Shutdown requested. Exit %s\n", __func__);
49+
return;
50+
}
51+
nFile++;
52+
}
53+
pblocktree->WriteReindexing(false);
54+
fReindex = false;
55+
LogPrintf("Reindexing finished\n");
56+
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
57+
::ChainstateActive().LoadGenesisBlock(chainparams);
58+
}
59+
60+
// -loadblock=
61+
for (const fs::path& path : vImportFiles) {
62+
FILE* file = fsbridge::fopen(path, "rb");
63+
if (file) {
64+
LogPrintf("Importing blocks file %s...\n", path.string());
65+
::ChainstateActive().LoadExternalBlockFile(chainparams, file);
66+
if (ShutdownRequested()) {
67+
LogPrintf("Shutdown requested. Exit %s\n", __func__);
68+
return;
69+
}
70+
} else {
71+
LogPrintf("Warning: Could not open blocks file %s\n", path.string());
72+
}
73+
}
74+
75+
// scan for better chains in the block chain database, that are not yet connected in the active best chain
76+
77+
// We can't hold cs_main during ActivateBestChain even though we're accessing
78+
// the chainman unique_ptrs since ABC requires us not to be holding cs_main, so retrieve
79+
// the relevant pointers before the ABC call.
80+
for (CChainState* chainstate : WITH_LOCK(::cs_main, return chainman.GetAll())) {
81+
BlockValidationState state;
82+
if (!chainstate->ActivateBestChain(state, chainparams, nullptr)) {
83+
LogPrintf("Failed to connect best block (%s)\n", state.ToString());
84+
StartShutdown();
85+
return;
86+
}
87+
}
88+
89+
if (args.GetBoolArg("-stopafterblockimport", DEFAULT_STOPAFTERBLOCKIMPORT)) {
90+
LogPrintf("Stopping after block import\n");
91+
StartShutdown();
92+
return;
93+
}
94+
} // End scope of CImportingNow
95+
chainman.ActiveChainstate().LoadMempool(args);
96+
}

src/node/blockstorage.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2011-2021 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_NODE_BLOCKSTORAGE_H
6+
#define BITCOIN_NODE_BLOCKSTORAGE_H
7+
8+
#include <vector>
9+
10+
#include <fs.h>
11+
12+
class ArgsManager;
13+
class ChainstateManager;
14+
15+
static constexpr bool DEFAULT_STOPAFTERBLOCKIMPORT{false};
16+
17+
void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFiles, const ArgsManager& args);
18+
19+
#endif // BITCOIN_NODE_BLOCKSTORAGE_H

0 commit comments

Comments
 (0)