Skip to content

Commit 7fbb1bc

Browse files
TheCharlatanMarcoFalke
andcommitted
kernel: Move block tree db open to block manager
This commit is done in preparation for the next commit. Here, the block tree options are moved to the blockmanager options and the block tree is instantiated through a helper method of the BlockManager, which is removed again in the next commit. Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^[email protected]>
1 parent 57ba59c commit 7fbb1bc

12 files changed

+46
-27
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ int main(int argc, char* argv[])
106106
};
107107
auto notifications = std::make_unique<KernelNotifications>();
108108

109+
kernel::CacheSizes cache_sizes{DEFAULT_KERNEL_CACHE};
109110

110111
// SETUP: Chainstate
111112
auto chainparams = CChainParams::Main();
@@ -119,11 +120,14 @@ int main(int argc, char* argv[])
119120
.chainparams = chainman_opts.chainparams,
120121
.blocks_dir = abs_datadir / "blocks",
121122
.notifications = chainman_opts.notifications,
123+
.block_tree_db_params = DBParams{
124+
.path = abs_datadir / "blocks" / "index",
125+
.cache_bytes = cache_sizes.block_tree_db,
126+
},
122127
};
123128
util::SignalInterrupt interrupt;
124129
ChainstateManager chainman{interrupt, chainman_opts, blockman_opts};
125130

126-
kernel::CacheSizes cache_sizes{DEFAULT_KERNEL_CACHE};
127131
node::ChainstateLoadOptions options;
128132
auto [status, error] = node::LoadChainstate(chainman, cache_sizes, options);
129133
if (status != node::ChainstateLoadStatus::SUCCESS) {

src/init.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,10 @@ bool AppInitParameterInteraction(const ArgsManager& args)
10571057
.chainparams = chainman_opts_dummy.chainparams,
10581058
.blocks_dir = args.GetBlocksDirPath(),
10591059
.notifications = chainman_opts_dummy.notifications,
1060+
.block_tree_db_params = DBParams{
1061+
.path = args.GetDataDirNet() / "blocks" / "index",
1062+
.cache_bytes = 0,
1063+
},
10601064
};
10611065
auto blockman_result{ApplyArgsManOptions(args, blockman_opts_dummy)};
10621066
if (!blockman_result) {
@@ -1199,10 +1203,16 @@ static ChainstateLoadResult InitAndLoadChainstate(
11991203
.signals = node.validation_signals.get(),
12001204
};
12011205
Assert(ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
1206+
12021207
BlockManager::Options blockman_opts{
12031208
.chainparams = chainman_opts.chainparams,
12041209
.blocks_dir = args.GetBlocksDirPath(),
12051210
.notifications = chainman_opts.notifications,
1211+
.block_tree_db_params = DBParams{
1212+
.path = args.GetDataDirNet() / "blocks" / "index",
1213+
.cache_bytes = cache_sizes.block_tree_db,
1214+
.wipe_data = do_reindex,
1215+
},
12061216
};
12071217
Assert(ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction
12081218
try {
@@ -1233,7 +1243,6 @@ static ChainstateLoadResult InitAndLoadChainstate(
12331243
};
12341244
node::ChainstateLoadOptions options;
12351245
options.mempool = Assert(node.mempool.get());
1236-
options.wipe_block_tree_db = do_reindex;
12371246
options.wipe_chainstate_db = do_reindex || do_reindex_chainstate;
12381247
options.prune = chainman.m_blockman.IsPruneMode();
12391248
options.check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);

src/kernel/blockmanager_opts.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
66
#define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
77

8+
#include <dbwrapper.h>
89
#include <kernel/notifications_interface.h>
910
#include <util/fs.h>
1011

@@ -27,6 +28,7 @@ struct BlockManagerOpts {
2728
bool fast_prune{false};
2829
const fs::path blocks_dir;
2930
Notifications& notifications;
31+
DBParams block_tree_db_params;
3032
};
3133

3234
} // namespace kernel

src/kernel/chainstatemanager_opts.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ struct ChainstateManagerOpts {
4242
std::optional<uint256> assumed_valid_block{};
4343
//! If the tip is older than this, the node is considered to be in initial block download.
4444
std::chrono::seconds max_tip_age{DEFAULT_MAX_TIP_AGE};
45-
DBOptions block_tree_db{};
4645
DBOptions coins_db{};
4746
CoinsViewOptions coins_view{};
4847
Notifications& notifications;

src/node/blockmanager_args.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <common/args.h>
88
#include <node/blockstorage.h>
9+
#include <node/database_args.h>
910
#include <tinyformat.h>
1011
#include <util/result.h>
1112
#include <util/translation.h>
@@ -34,6 +35,8 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, BlockManager::Op
3435

3536
if (auto value{args.GetBoolArg("-fastprune")}) opts.fast_prune = *value;
3637

38+
ReadDatabaseArgs(args, opts.block_tree_db_params.options);
39+
3740
return {};
3841
}
3942
} // namespace node

src/node/blockstorage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ class BlockManager
268268
using Options = kernel::BlockManagerOpts;
269269

270270
explicit BlockManager(const util::SignalInterrupt& interrupt, Options opts);
271+
auto MakeBlockTreeDb() { return std::make_unique<BlockTreeDB>(m_opts.block_tree_db_params); }
272+
auto OptsWipeBlockTreeDb() { return m_opts.block_tree_db_params.wipe_data; }
271273

272274
const util::SignalInterrupt& m_interrupt;
273275
std::atomic<bool> m_importing{false};

src/node/chainstate.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,20 @@ namespace node {
3636
// to ChainstateManager::InitializeChainstate().
3737
static ChainstateLoadResult CompleteChainstateInitialization(
3838
ChainstateManager& chainman,
39-
const CacheSizes& cache_sizes,
4039
const ChainstateLoadOptions& options) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
4140
{
4241
auto& pblocktree{chainman.m_blockman.m_block_tree_db};
4342
// new BlockTreeDB tries to delete the existing file, which
4443
// fails if it's still open from the previous loop. Close it first:
4544
pblocktree.reset();
4645
try {
47-
pblocktree = std::make_unique<BlockTreeDB>(DBParams{
48-
.path = chainman.m_options.datadir / "blocks" / "index",
49-
.cache_bytes = cache_sizes.block_tree_db,
50-
.memory_only = options.block_tree_db_in_memory,
51-
.wipe_data = options.wipe_block_tree_db,
52-
.options = chainman.m_options.block_tree_db});
46+
pblocktree = chainman.m_blockman.MakeBlockTreeDb();
5347
} catch (dbwrapper_error& err) {
5448
LogError("%s\n", err.what());
5549
return {ChainstateLoadStatus::FAILURE, _("Error opening block database")};
5650
}
5751

58-
if (options.wipe_block_tree_db) {
52+
if (chainman.m_blockman.OptsWipeBlockTreeDb()) {
5953
pblocktree->WriteReindexing(true);
6054
chainman.m_blockman.m_blockfiles_indexed = false;
6155
//If we're reindexing in prune mode, wipe away unusable block files and all undo data files
@@ -206,7 +200,7 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
206200
}
207201
}
208202

209-
auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
203+
auto [init_status, init_error] = CompleteChainstateInitialization(chainman, options);
210204
if (init_status != ChainstateLoadStatus::SUCCESS) {
211205
return {init_status, init_error};
212206
}
@@ -242,7 +236,7 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
242236
// for the fully validated chainstate.
243237
chainman.ActiveChainstate().ClearBlockIndexCandidates();
244238

245-
auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
239+
auto [init_status, init_error] = CompleteChainstateInitialization(chainman, options);
246240
if (init_status != ChainstateLoadStatus::SUCCESS) {
247241
return {init_status, init_error};
248242
}

src/node/chainstate.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ namespace node {
2222

2323
struct ChainstateLoadOptions {
2424
CTxMemPool* mempool{nullptr};
25-
bool block_tree_db_in_memory{false};
2625
bool coins_db_in_memory{false};
27-
// Whether to wipe the block tree database when loading it. If set, this
28-
// will also set a reindexing flag so any existing block data files will be
29-
// scanned and added to the database.
30-
bool wipe_block_tree_db{false};
3126
// Whether to wipe the chainstate database when loading it. If set, this
3227
// will cause the chainstate database to be rebuilt starting from genesis.
3328
bool wipe_chainstate_db{false};

src/node/chainstatemanager_args.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ util::Result<void> ApplyArgsManOptions(const ArgsManager& args, ChainstateManage
4949

5050
if (auto value{args.GetIntArg("-maxtipage")}) opts.max_tip_age = std::chrono::seconds{*value};
5151

52-
ReadDatabaseArgs(args, opts.block_tree_db);
5352
ReadDatabaseArgs(args, opts.coins_db);
5453
ReadCoinsViewArgs(args, opts.coins_view);
5554

src/test/blockmanager_tests.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
3333
.chainparams = *params,
3434
.blocks_dir = m_args.GetBlocksDirPath(),
3535
.notifications = notifications,
36+
.block_tree_db_params = DBParams{
37+
.path = m_args.GetDataDirNet() / "blocks" / "index",
38+
.cache_bytes = 0,
39+
},
3640
};
3741
BlockManager blockman{*Assert(m_node.shutdown_signal), blockman_opts};
3842
// simulate adding a genesis block normally
@@ -140,6 +144,10 @@ BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
140144
.chainparams = Params(),
141145
.blocks_dir = m_args.GetBlocksDirPath(),
142146
.notifications = notifications,
147+
.block_tree_db_params = DBParams{
148+
.path = m_args.GetDataDirNet() / "blocks" / "index",
149+
.cache_bytes = 0,
150+
},
143151
};
144152
BlockManager blockman{*Assert(m_node.shutdown_signal), blockman_opts};
145153

0 commit comments

Comments
 (0)