Skip to content

Commit fa721f1

Browse files
author
MarcoFalke
committed
Move ::nPruneTarget into BlockManager
1 parent 460e394 commit fa721f1

13 files changed

+109
-36
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ BITCOIN_CORE_H = \
174174
interfaces/ipc.h \
175175
interfaces/node.h \
176176
interfaces/wallet.h \
177+
kernel/blockmanager_opts.h \
177178
kernel/chain.h \
178179
kernel/chainstatemanager_opts.h \
179180
kernel/checks.h \
@@ -200,6 +201,7 @@ BITCOIN_CORE_H = \
200201
netbase.h \
201202
netgroup.h \
202203
netmessagemaker.h \
204+
node/blockmanager_args.h \
203205
node/blockstorage.h \
204206
node/caches.h \
205207
node/chainstate.h \
@@ -387,6 +389,7 @@ libbitcoin_node_a_SOURCES = \
387389
net.cpp \
388390
net_processing.cpp \
389391
netgroup.cpp \
392+
node/blockmanager_args.cpp \
390393
node/blockstorage.cpp \
391394
node/caches.cpp \
392395
node/chainstate.cpp \

src/bitcoin-chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int main(int argc, char* argv[])
8585
.datadir = gArgs.GetDataDirNet(),
8686
.adjusted_time_callback = NodeClock::now,
8787
};
88-
ChainstateManager chainman{chainman_opts};
88+
ChainstateManager chainman{chainman_opts, {}};
8989

9090
node::CacheSizes cache_sizes;
9191
cache_sizes.block_tree_db = 2 << 20;

src/init.cpp

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <net_processing.h>
3838
#include <netbase.h>
3939
#include <netgroup.h>
40+
#include <node/blockmanager_args.h>
4041
#include <node/blockstorage.h>
4142
#include <node/caches.h>
4243
#include <node/chainstate.h>
@@ -124,7 +125,6 @@ using node::ThreadImport;
124125
using node::VerifyLoadedChainstate;
125126
using node::fPruneMode;
126127
using node::fReindex;
127-
using node::nPruneTarget;
128128

129129
static constexpr bool DEFAULT_PROXYRANDOMIZE{true};
130130
static constexpr bool DEFAULT_REST_ENABLE{false};
@@ -945,22 +945,6 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
945945
init::SetLoggingCategories(args);
946946
init::SetLoggingLevel(args);
947947

948-
// block pruning; get the amount of disk space (in MiB) to allot for block & undo files
949-
int64_t nPruneArg = args.GetIntArg("-prune", 0);
950-
if (nPruneArg < 0) {
951-
return InitError(_("Prune cannot be configured with a negative value."));
952-
}
953-
nPruneTarget = (uint64_t) nPruneArg * 1024 * 1024;
954-
if (nPruneArg == 1) { // manual pruning: -prune=1
955-
nPruneTarget = std::numeric_limits<uint64_t>::max();
956-
fPruneMode = true;
957-
} else if (nPruneTarget) {
958-
if (nPruneTarget < MIN_DISK_SPACE_FOR_BLOCK_FILES) {
959-
return InitError(strprintf(_("Prune configured below the minimum of %d MiB. Please use a higher number."), MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024));
960-
}
961-
fPruneMode = true;
962-
}
963-
964948
nConnectTimeout = args.GetIntArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
965949
if (nConnectTimeout <= 0) {
966950
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
@@ -1051,6 +1035,10 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
10511035
if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) {
10521036
return InitError(*error);
10531037
}
1038+
node::BlockManager::Options blockman_opts_dummy{};
1039+
if (const auto error{ApplyArgsManOptions(args, blockman_opts_dummy)}) {
1040+
return InitError(*error);
1041+
}
10541042
}
10551043

10561044
return true;
@@ -1450,6 +1438,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14501438
};
14511439
Assert(!ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
14521440

1441+
node::BlockManager::Options blockman_opts{};
1442+
Assert(!ApplyArgsManOptions(args, blockman_opts)); // no error can happen, already checked in AppInitParameterInteraction
1443+
14531444
// cache size calculations
14541445
CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size());
14551446

@@ -1485,7 +1476,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14851476
for (bool fLoaded = false; !fLoaded && !ShutdownRequested();) {
14861477
node.mempool = std::make_unique<CTxMemPool>(mempool_opts);
14871478

1488-
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
1479+
node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
14891480
ChainstateManager& chainman = *node.chainman;
14901481

14911482
node::ChainstateLoadOptions options;

src/kernel/blockmanager_opts.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2022 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_KERNEL_BLOCKMANAGER_OPTS_H
6+
#define BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H
7+
8+
namespace kernel {
9+
10+
/**
11+
* An options struct for `BlockManager`, more ergonomically referred to as
12+
* `BlockManager::Options` due to the using-declaration in `BlockManager`.
13+
*/
14+
struct BlockManagerOpts {
15+
uint64_t prune_target{0};
16+
};
17+
18+
} // namespace kernel
19+
20+
#endif // BITCOIN_KERNEL_BLOCKMANAGER_OPTS_H

src/node/blockmanager_args.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2023 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 <node/blockmanager_args.h>
6+
7+
#include <util/system.h>
8+
#include <validation.h>
9+
10+
namespace node {
11+
std::optional<bilingual_str> ApplyArgsManOptions(const ArgsManager& args, BlockManager::Options& opts)
12+
{
13+
// block pruning; get the amount of disk space (in MiB) to allot for block & undo files
14+
int64_t nPruneArg{args.GetIntArg("-prune", opts.prune_target)};
15+
if (nPruneArg < 0) {
16+
return _("Prune cannot be configured with a negative value.");
17+
}
18+
uint64_t nPruneTarget{uint64_t(nPruneArg) * 1024 * 1024};
19+
if (nPruneArg == 1) { // manual pruning: -prune=1
20+
nPruneTarget = std::numeric_limits<uint64_t>::max();
21+
fPruneMode = true;
22+
} else if (nPruneTarget) {
23+
if (nPruneTarget < MIN_DISK_SPACE_FOR_BLOCK_FILES) {
24+
return strprintf(_("Prune configured below the minimum of %d MiB. Please use a higher number."), MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024);
25+
}
26+
fPruneMode = true;
27+
}
28+
opts.prune_target = nPruneTarget;
29+
30+
return std::nullopt;
31+
}
32+
} // namespace node

src/node/blockmanager_args.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
// Copyright (c) 2023 The Bitcoin Core developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#ifndef BITCOIN_NODE_BLOCKMANAGER_ARGS_H
7+
#define BITCOIN_NODE_BLOCKMANAGER_ARGS_H
8+
9+
#include <node/blockstorage.h>
10+
11+
#include <optional>
12+
13+
class ArgsManager;
14+
struct bilingual_str;
15+
16+
namespace node {
17+
std::optional<bilingual_str> ApplyArgsManOptions(const ArgsManager& args, BlockManager::Options& opts);
18+
} // namespace node
19+
20+
#endif // BITCOIN_NODE_BLOCKMANAGER_ARGS_H

src/node/blockstorage.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ namespace node {
2929
std::atomic_bool fImporting(false);
3030
std::atomic_bool fReindex(false);
3131
bool fPruneMode = false;
32-
uint64_t nPruneTarget = 0;
3332

3433
bool CBlockIndexWorkComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
3534
{
@@ -178,7 +177,7 @@ void BlockManager::FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nM
178177
void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
179178
{
180179
LOCK2(cs_main, cs_LastBlockFile);
181-
if (chain_tip_height < 0 || nPruneTarget == 0) {
180+
if (chain_tip_height < 0 || GetPruneTarget() == 0) {
182181
return;
183182
}
184183
if ((uint64_t)chain_tip_height <= nPruneAfterHeight) {
@@ -194,14 +193,14 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
194193
uint64_t nBytesToPrune;
195194
int count = 0;
196195

197-
if (nCurrentUsage + nBuffer >= nPruneTarget) {
196+
if (nCurrentUsage + nBuffer >= GetPruneTarget()) {
198197
// On a prune event, the chainstate DB is flushed.
199198
// To avoid excessive prune events negating the benefit of high dbcache
200199
// values, we should not prune too rapidly.
201200
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
202201
if (is_ibd) {
203202
// Since this is only relevant during IBD, we use a fixed 10%
204-
nBuffer += nPruneTarget / 10;
203+
nBuffer += GetPruneTarget() / 10;
205204
}
206205

207206
for (int fileNumber = 0; fileNumber < m_last_blockfile; fileNumber++) {
@@ -211,7 +210,7 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
211210
continue;
212211
}
213212

214-
if (nCurrentUsage + nBuffer < nPruneTarget) { // are we below our target?
213+
if (nCurrentUsage + nBuffer < GetPruneTarget()) { // are we below our target?
215214
break;
216215
}
217216

@@ -229,9 +228,9 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
229228
}
230229

231230
LogPrint(BCLog::PRUNE, "target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
232-
nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
233-
((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
234-
nLastBlockWeCanPrune, count);
231+
GetPruneTarget() / 1024 / 1024, nCurrentUsage / 1024 / 1024,
232+
(int64_t(GetPruneTarget()) - int64_t(nCurrentUsage)) / 1024 / 1024,
233+
nLastBlockWeCanPrune, count);
235234
}
236235

237236
void BlockManager::UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) {

src/node/blockstorage.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <attributes.h>
99
#include <chain.h>
1010
#include <fs.h>
11+
#include <kernel/blockmanager_opts.h>
1112
#include <kernel/cs_main.h>
1213
#include <protocol.h>
1314
#include <sync.h>
@@ -49,7 +50,6 @@ static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = CMessageHeader::MESSAG
4950
extern std::atomic_bool fImporting;
5051
extern std::atomic_bool fReindex;
5152
extern bool fPruneMode;
52-
extern uint64_t nPruneTarget;
5353

5454
// Because validation code takes pointers to the map's CBlockIndex objects, if
5555
// we ever switch to another associative container, we need to either use a
@@ -138,7 +138,13 @@ class BlockManager
138138
*/
139139
std::unordered_map<std::string, PruneLockInfo> m_prune_locks GUARDED_BY(::cs_main);
140140

141+
const kernel::BlockManagerOpts m_opts;
142+
141143
public:
144+
using Options = kernel::BlockManagerOpts;
145+
146+
explicit BlockManager(Options opts) : m_opts{std::move(opts)} {};
147+
142148
BlockMap m_block_index GUARDED_BY(cs_main);
143149

144150
std::vector<CBlockIndex*> GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
@@ -184,7 +190,7 @@ class BlockManager
184190
[[nodiscard]] bool IsPruneMode() const { return fPruneMode; }
185191

186192
/** Attempt to stay below this number of bytes of block files. */
187-
[[nodiscard]] uint64_t GetPruneTarget() const { return nPruneTarget; }
193+
[[nodiscard]] uint64_t GetPruneTarget() const { return m_opts.prune_target; }
188194

189195
[[nodiscard]] bool LoadingBlocks() const
190196
{

src/test/blockmanager_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ BOOST_FIXTURE_TEST_SUITE(blockmanager_tests, BasicTestingSetup)
2121
BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
2222
{
2323
const auto params {CreateChainParams(ArgsManager{}, CBaseChainParams::MAIN)};
24-
BlockManager blockman {};
24+
BlockManager blockman{{}};
2525
CChain chain {};
2626
// simulate adding a genesis block normally
2727
BOOST_CHECK_EQUAL(blockman.SaveBlockToDisk(params->GenesisBlock(), 0, chain, *params, nullptr).nPos, BLOCK_SERIALIZATION_HEADER_SIZE);

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
184184
.adjusted_time_callback = GetAdjustedTime,
185185
.check_block_index = true,
186186
};
187-
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
187+
m_node.chainman = std::make_unique<ChainstateManager>(chainman_opts, node::BlockManager::Options{});
188188
m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(DBParams{
189189
.path = m_args.GetDataDirNet() / "blocks" / "index",
190190
.cache_bytes = static_cast<size_t>(m_cache_sizes.block_tree_db),

0 commit comments

Comments
 (0)