Skip to content

Commit cbfbf46

Browse files
committed
Merge bitcoin#25781: Remove almost all blockstorage globals
fadf8b8 refactor: Add and use PRUNE_TARGET_MANUAL constexpr (MarcoFalke) fa9bd7b Move ::fImporting to BlockManager (MarcoFalke) fa442b1 Pass fImporting to ImportingNow helper class (MarcoFalke) fa177d7 Move ::fPruneMode into BlockManager (MarcoFalke) fa721f1 Move ::nPruneTarget into BlockManager (MarcoFalke) Pull request description: It seems preferable to assign globals to a class (in this case `BlockManager`), than to leave them dangling. This should clarify scope for code-readers, as well as clarifying unit test behaviour. ACKs for top commit: TheCharlatan: Code review ACK fadf8b8 achow101: ACK fadf8b8 dergoegge: Code review ACK fadf8b8 Tree-SHA512: d261b69257560c9f460bbe85944ca478d0390b498a5af514bafcb4f6444841e5ea58c2e8982f38c48685d6f649039234aec853a934e24ebf23e20d975991a5dc
2 parents 8c4958b + fadf8b8 commit cbfbf46

16 files changed

+135
-64
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 & 19 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>
@@ -122,9 +123,7 @@ using node::ShouldPersistMempool;
122123
using node::NodeContext;
123124
using node::ThreadImport;
124125
using node::VerifyLoadedChainstate;
125-
using node::fPruneMode;
126126
using node::fReindex;
127-
using node::nPruneTarget;
128127

129128
static constexpr bool DEFAULT_PROXYRANDOMIZE{true};
130129
static constexpr bool DEFAULT_REST_ENABLE{false};
@@ -945,22 +944,6 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
945944
init::SetLoggingCategories(args);
946945
init::SetLoggingLevel(args);
947946

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-
964947
nConnectTimeout = args.GetIntArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
965948
if (nConnectTimeout <= 0) {
966949
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
@@ -1051,6 +1034,10 @@ bool AppInitParameterInteraction(const ArgsManager& args, bool use_syscall_sandb
10511034
if (const auto error{ApplyArgsManOptions(args, chainman_opts_dummy)}) {
10521035
return InitError(*error);
10531036
}
1037+
node::BlockManager::Options blockman_opts_dummy{};
1038+
if (const auto error{ApplyArgsManOptions(args, blockman_opts_dummy)}) {
1039+
return InitError(*error);
1040+
}
10541041
}
10551042

10561043
return true;
@@ -1450,6 +1437,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14501437
};
14511438
Assert(!ApplyArgsManOptions(args, chainman_opts)); // no error can happen, already checked in AppInitParameterInteraction
14521439

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

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

1488-
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
1478+
node.chainman = std::make_unique<ChainstateManager>(chainman_opts, blockman_opts);
14891479
ChainstateManager& chainman = *node.chainman;
14901480

14911481
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 = BlockManager::PRUNE_TARGET_MANUAL;
21+
} else if (nPruneTarget) {
22+
if (nPruneTarget < MIN_DISK_SPACE_FOR_BLOCK_FILES) {
23+
return strprintf(_("Prune configured below the minimum of %d MiB. Please use a higher number."), MIN_DISK_SPACE_FOR_BLOCK_FILES / 1024 / 1024);
24+
}
25+
}
26+
opts.prune_target = nPruneTarget;
27+
28+
return std::nullopt;
29+
}
30+
} // 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: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
#include <unordered_map>
2727

2828
namespace node {
29-
std::atomic_bool fImporting(false);
3029
std::atomic_bool fReindex(false);
31-
bool fPruneMode = false;
32-
uint64_t nPruneTarget = 0;
3330

3431
bool CBlockIndexWorkComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const
3532
{
@@ -154,7 +151,7 @@ void BlockManager::PruneOneBlockFile(const int fileNumber)
154151

155152
void BlockManager::FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
156153
{
157-
assert(fPruneMode && nManualPruneHeight > 0);
154+
assert(IsPruneMode() && nManualPruneHeight > 0);
158155

159156
LOCK2(cs_main, cs_LastBlockFile);
160157
if (chain_tip_height < 0) {
@@ -178,7 +175,7 @@ void BlockManager::FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nM
178175
void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
179176
{
180177
LOCK2(cs_main, cs_LastBlockFile);
181-
if (chain_tip_height < 0 || nPruneTarget == 0) {
178+
if (chain_tip_height < 0 || GetPruneTarget() == 0) {
182179
return;
183180
}
184181
if ((uint64_t)chain_tip_height <= nPruneAfterHeight) {
@@ -194,14 +191,14 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
194191
uint64_t nBytesToPrune;
195192
int count = 0;
196193

197-
if (nCurrentUsage + nBuffer >= nPruneTarget) {
194+
if (nCurrentUsage + nBuffer >= GetPruneTarget()) {
198195
// On a prune event, the chainstate DB is flushed.
199196
// To avoid excessive prune events negating the benefit of high dbcache
200197
// values, we should not prune too rapidly.
201198
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
202199
if (is_ibd) {
203200
// Since this is only relevant during IBD, we use a fixed 10%
204-
nBuffer += nPruneTarget / 10;
201+
nBuffer += GetPruneTarget() / 10;
205202
}
206203

207204
for (int fileNumber = 0; fileNumber < m_last_blockfile; fileNumber++) {
@@ -211,7 +208,7 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
211208
continue;
212209
}
213210

214-
if (nCurrentUsage + nBuffer < nPruneTarget) { // are we below our target?
211+
if (nCurrentUsage + nBuffer < GetPruneTarget()) { // are we below our target?
215212
break;
216213
}
217214

@@ -229,9 +226,9 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
229226
}
230227

231228
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);
229+
GetPruneTarget() / 1024 / 1024, nCurrentUsage / 1024 / 1024,
230+
(int64_t(GetPruneTarget()) - int64_t(nCurrentUsage)) / 1024 / 1024,
231+
nLastBlockWeCanPrune, count);
235232
}
236233

237234
void BlockManager::UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) {
@@ -657,7 +654,7 @@ bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigne
657654
if (out_of_space) {
658655
return AbortNode("Disk space is too low!", _("Disk space is too low!"));
659656
}
660-
if (bytes_allocated != 0 && fPruneMode) {
657+
if (bytes_allocated != 0 && IsPruneMode()) {
661658
m_check_for_pruning = true;
662659
}
663660
}
@@ -681,7 +678,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
681678
if (out_of_space) {
682679
return AbortNode(state, "Disk space is too low!", _("Disk space is too low!"));
683680
}
684-
if (bytes_allocated != 0 && fPruneMode) {
681+
if (bytes_allocated != 0 && IsPruneMode()) {
685682
m_check_for_pruning = true;
686683
}
687684

@@ -846,17 +843,20 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CCha
846843
return blockPos;
847844
}
848845

849-
struct CImportingNow {
850-
CImportingNow()
846+
class ImportingNow
847+
{
848+
std::atomic<bool>& m_importing;
849+
850+
public:
851+
ImportingNow(std::atomic<bool>& importing) : m_importing{importing}
851852
{
852-
assert(fImporting == false);
853-
fImporting = true;
853+
assert(m_importing == false);
854+
m_importing = true;
854855
}
855-
856-
~CImportingNow()
856+
~ImportingNow()
857857
{
858-
assert(fImporting == true);
859-
fImporting = false;
858+
assert(m_importing == true);
859+
m_importing = false;
860860
}
861861
};
862862

@@ -866,7 +866,7 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
866866
ScheduleBatchPriority();
867867

868868
{
869-
CImportingNow imp;
869+
ImportingNow imp{chainman.m_blockman.m_importing};
870870

871871
// -reindex
872872
if (fReindex) {
@@ -932,7 +932,7 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
932932
StartShutdown();
933933
return;
934934
}
935-
} // End scope of CImportingNow
935+
} // End scope of ImportingNow
936936
chainman.ActiveChainstate().LoadMempool(mempool_path);
937937
}
938938
} // namespace node

src/node/blockstorage.h

Lines changed: 17 additions & 9 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>
@@ -46,10 +47,7 @@ static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
4647
/** Size of header written by WriteBlockToDisk before a serialized CBlock */
4748
static constexpr size_t BLOCK_SERIALIZATION_HEADER_SIZE = CMessageHeader::MESSAGE_START_SIZE + sizeof(unsigned int);
4849

49-
extern std::atomic_bool fImporting;
5050
extern std::atomic_bool fReindex;
51-
extern bool fPruneMode;
52-
extern uint64_t nPruneTarget;
5351

5452
// Because validation code takes pointers to the map's CBlockIndex objects, if
5553
// we ever switch to another associative container, we need to either use a
@@ -124,6 +122,8 @@ class BlockManager
124122
*/
125123
bool m_check_for_pruning = false;
126124

125+
const bool m_prune_mode;
126+
127127
/** Dirty block index entries. */
128128
std::set<CBlockIndex*> m_dirty_blockindex;
129129

@@ -138,7 +138,17 @@ 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)
147+
: m_prune_mode{opts.prune_target > 0},
148+
m_opts{std::move(opts)} {};
149+
150+
std::atomic<bool> m_importing{false};
151+
142152
BlockMap m_block_index GUARDED_BY(cs_main);
143153

144154
std::vector<CBlockIndex*> GetAllBlockIndices() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
@@ -181,15 +191,13 @@ class BlockManager
181191
FlatFilePos SaveBlockToDisk(const CBlock& block, int nHeight, CChain& active_chain, const CChainParams& chainparams, const FlatFilePos* dbp);
182192

183193
/** Whether running in -prune mode. */
184-
[[nodiscard]] bool IsPruneMode() const { return fPruneMode; }
194+
[[nodiscard]] bool IsPruneMode() const { return m_prune_mode; }
185195

186196
/** Attempt to stay below this number of bytes of block files. */
187-
[[nodiscard]] uint64_t GetPruneTarget() const { return nPruneTarget; }
197+
[[nodiscard]] uint64_t GetPruneTarget() const { return m_opts.prune_target; }
198+
static constexpr auto PRUNE_TARGET_MANUAL{std::numeric_limits<uint64_t>::max()};
188199

189-
[[nodiscard]] bool LoadingBlocks() const
190-
{
191-
return fImporting || fReindex;
192-
}
200+
[[nodiscard]] bool LoadingBlocks() const { return m_importing || fReindex; }
193201

194202
/** Calculate the amount of disk space the block & undo files currently use */
195203
uint64_t CalculateCurrentUsage();

src/node/chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
169169
if (chainman.MinimumChainWork() < UintToArith256(chainman.GetConsensus().nMinimumChainWork)) {
170170
LogPrintf("Warning: nMinimumChainWork set below default value of %s\n", chainman.GetConsensus().nMinimumChainWork.GetHex());
171171
}
172-
if (chainman.m_blockman.GetPruneTarget() == std::numeric_limits<uint64_t>::max()) {
172+
if (chainman.m_blockman.GetPruneTarget() == BlockManager::PRUNE_TARGET_MANUAL) {
173173
LogPrintf("Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.\n");
174174
} else if (chainman.m_blockman.GetPruneTarget()) {
175175
LogPrintf("Prune configured to target %u MiB on disk for block and undo files.\n", chainman.m_blockman.GetPruneTarget() / 1024 / 1024);

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class NodeImpl : public Node
295295
bool isInitialBlockDownload() override {
296296
return chainman().ActiveChainstate().IsInitialBlockDownload();
297297
}
298-
bool isLoadingBlocks() override { return node::fReindex || node::fImporting; }
298+
bool isLoadingBlocks() override { return chainman().m_blockman.LoadingBlocks(); }
299299
void setNetworkActive(bool active) override
300300
{
301301
if (m_context->connman) {

0 commit comments

Comments
 (0)