Skip to content

Commit 1f14130

Browse files
committed
Merge bitcoin#21575: refactor: Create blockstorage module
fadcd3f doc: Remove irrelevant link to GitHub (MarcoFalke) fa121b6 blockstorage: [refactor] Use chainman reference where possible (MarcoFalke) fa0c7d9 move-only: Move *Disk functions to blockstorage (MarcoFalke) fa91b2b move-only: Move AbortNode to shutdown (MarcoFalke) fa413f0 move-only: Move ThreadImport to blockstorage (MarcoFalke) faf843c refactor: Move load block thread into ChainstateManager (MarcoFalke) Pull request description: This picks up the closed pull request bitcoin#21030 and is the first step toward fixing bitcoin#21220. The basic idea is to move all disk access into a separate module with benefits: * Breaking down the massive files init.cpp and validation.cpp into logical units * Creating a standalone-module to reduce the mental complexity * Pave the way to fix validation related circular dependencies * Pave the way to mock disk access for testing, especially where it is performance critical (like fuzzing) ACKs for top commit: promag: Code review ACK fadcd3f, checked (almost) moved only changes. This is a nice tidy up change and doesn't change behavior. Easily reviewed commit by commit. jamesob: ACK fadcd3f ([`jamesob/ackr/21575.1.MarcoFalke.refactor_create_blocksto`](https://github.com/jamesob/bitcoin/tree/ackr/21575.1.MarcoFalke.refactor_create_blocksto)) ryanofsky: Code review ACK fadcd3f. New organization makes sense, moves extraneous things outside of validation.cpp. PR is also easy to review with helpfully split up moveonly commits. Tree-SHA512: 917996592b6d8f9998289d8cb2b1b78b23d1fdb3b07216c9caec1380df33baa09dc2c1e706da669d440b497e79c9c62a01ca20dc202df5ad974a75f3ef7a143b
2 parents 88331aa + fadcd3f commit 1f14130

19 files changed

+330
-258
lines changed

src/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ BITCOIN_CORE_H = \
175175
netaddress.h \
176176
netbase.h \
177177
netmessagemaker.h \
178+
node/blockstorage.h \
178179
node/coin.h \
179180
node/coinstats.h \
180181
node/context.h \
@@ -323,6 +324,7 @@ libbitcoin_server_a_SOURCES = \
323324
miner.cpp \
324325
net.cpp \
325326
net_processing.cpp \
327+
node/blockstorage.cpp \
326328
node/coin.cpp \
327329
node/coinstats.cpp \
328330
node/context.cpp \

src/index/base.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
#include <chainparams.h>
66
#include <index/base.h>
7+
#include <node/blockstorage.h>
78
#include <node/ui_interface.h>
89
#include <shutdown.h>
910
#include <tinyformat.h>
1011
#include <util/system.h>
1112
#include <util/translation.h>
12-
#include <validation.h>
13+
#include <validation.h> // For g_chainman
1314
#include <warnings.h>
1415

1516
constexpr char DB_BEST_BLOCK = 'B';

src/index/blockfilterindex.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#include <dbwrapper.h>
88
#include <index/blockfilterindex.h>
9+
#include <node/blockstorage.h>
910
#include <util/system.h>
10-
#include <validation.h>
1111

1212
/* The index database stores three items for each block: the disk location of the encoded filter,
1313
* its dSHA256 hash, and the header. Those belonging to blocks on the active chain are indexed by

src/init.cpp

+3-92
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
@@ -155,8 +153,6 @@ static fs::path GetPidFile(const ArgsManager& args)
155153

156154
static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
157155

158-
static std::thread g_load_block;
159-
160156
void Interrupt(NodeContext& node)
161157
{
162158
InterruptHTTPServer();
@@ -220,7 +216,7 @@ void Shutdown(NodeContext& node)
220216
// After everything has been shut down, but before things get flushed, stop the
221217
// CScheduler/checkqueue, scheduler and load block thread.
222218
if (node.scheduler) node.scheduler->stop();
223-
if (g_load_block.joinable()) g_load_block.join();
219+
if (node.chainman && node.chainman->m_load_block.joinable()) node.chainman->m_load_block.join();
224220
StopScriptCheckWorkerThreads();
225221

226222
// After the threads that potentially access these pointers have been stopped,
@@ -627,20 +623,6 @@ static void BlockNotifyGenesisWait(const CBlockIndex* pBlockIndex)
627623
}
628624
}
629625

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

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

1883-
g_load_block = std::thread(&TraceThread<std::function<void()>>, "loadblk", [=, &chainman, &args] {
1794+
chainman.m_load_block = std::thread(&TraceThread<std::function<void()>>, "loadblk", [=, &chainman, &args] {
18841795
ThreadImport(chainman, vImportFiles, args);
18851796
});
18861797

src/net_processing.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <merkleblock.h>
1717
#include <netbase.h>
1818
#include <netmessagemaker.h>
19+
#include <node/blockstorage.h>
1920
#include <policy/fees.h>
2021
#include <policy/policy.h>
2122
#include <primitives/block.h>

src/node/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ As a rule of thumb, code in one of the [`src/node/`](./),
1515
calling code in the other directories directly, and only invoke it indirectly
1616
through the more limited [`src/interfaces/`](../interfaces/) classes.
1717

18-
The [`src/node/`](./) directory is a new directory introduced in
19-
[#14978](https://github.com/bitcoin/bitcoin/pull/14978) and at the moment is
18+
This directory is at the moment
2019
sparsely populated. Eventually more substantial files like
2120
[`src/validation.cpp`](../validation.cpp) and
2221
[`src/txmempool.cpp`](../txmempool.cpp) might be moved there.

0 commit comments

Comments
 (0)