Skip to content

Commit 3b91d4b

Browse files
committed
refactor: Reduce number of LoadChainstate parameters
1 parent 5560682 commit 3b91d4b

File tree

5 files changed

+86
-113
lines changed

5 files changed

+86
-113
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <consensus/validation.h>
1919
#include <core_io.h>
2020
#include <node/blockstorage.h>
21+
#include <node/caches.h>
2122
#include <node/chainstate.h>
2223
#include <scheduler.h>
2324
#include <script/sigcache.h>
@@ -83,26 +84,18 @@ int main(int argc, char* argv[])
8384
};
8485
ChainstateManager chainman{chainman_opts};
8586

86-
auto rv = node::LoadChainstate(false,
87-
std::ref(chainman),
88-
nullptr,
89-
false,
90-
false,
91-
2 << 20,
92-
2 << 22,
93-
(450 << 20) - (2 << 20) - (2 << 22),
94-
false,
95-
false,
96-
[]() { return false; });
87+
node::CacheSizes cache_sizes;
88+
cache_sizes.block_tree_db = 2 << 20;
89+
cache_sizes.coins_db = 2 << 22;
90+
cache_sizes.coins = (450 << 20) - (2 << 20) - (2 << 22);
91+
node::ChainstateLoadOptions options;
92+
options.check_interrupt = [] { return false; };
93+
auto rv = node::LoadChainstate(chainman, cache_sizes, options);
9794
if (rv.has_value()) {
9895
std::cerr << "Failed to load Chain state from your datadir." << std::endl;
9996
goto epilogue;
10097
} else {
101-
auto maybe_verify_error = node::VerifyLoadedChainstate(std::ref(chainman),
102-
false,
103-
false,
104-
DEFAULT_CHECKBLOCKS,
105-
DEFAULT_CHECKLEVEL);
98+
auto maybe_verify_error = node::VerifyLoadedChainstate(chainman, options);
10699
if (maybe_verify_error.has_value()) {
107100
std::cerr << "Failed to verify loaded Chain state from your datadir." << std::endl;
108101
goto epilogue;

src/init.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,29 +1452,27 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14521452
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
14531453
ChainstateManager& chainman = *node.chainman;
14541454

1455-
const bool fReset = fReindex;
14561455
bilingual_str strLoadError;
14571456

1457+
node::ChainstateLoadOptions options;
1458+
options.mempool = Assert(node.mempool.get());
1459+
options.reindex = node::fReindex;
1460+
options.reindex_chainstate = fReindexChainState;
1461+
options.prune = node::fPruneMode;
1462+
options.check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
1463+
options.check_level = args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
1464+
options.check_interrupt = ShutdownRequested;
1465+
options.coins_error_cb = [] {
1466+
uiInterface.ThreadSafeMessageBox(
1467+
_("Error reading from database, shutting down."),
1468+
"", CClientUIInterface::MSG_ERROR);
1469+
};
1470+
14581471
uiInterface.InitMessage(_("Loading block index…").translated);
14591472
const int64_t load_block_index_start_time = GetTimeMillis();
14601473
std::optional<ChainstateLoadingError> maybe_load_error;
14611474
try {
1462-
maybe_load_error = LoadChainstate(fReset,
1463-
chainman,
1464-
Assert(node.mempool.get()),
1465-
fPruneMode,
1466-
fReindexChainState,
1467-
cache_sizes.block_tree_db,
1468-
cache_sizes.coins_db,
1469-
cache_sizes.coins,
1470-
/*block_tree_db_in_memory=*/false,
1471-
/*coins_db_in_memory=*/false,
1472-
/*shutdown_requested=*/ShutdownRequested,
1473-
/*coins_error_cb=*/[]() {
1474-
uiInterface.ThreadSafeMessageBox(
1475-
_("Error reading from database, shutting down."),
1476-
"", CClientUIInterface::MSG_ERROR);
1477-
});
1475+
maybe_load_error = LoadChainstate(chainman, cache_sizes, options);
14781476
} catch (const std::exception& e) {
14791477
LogPrintf("%s\n", e.what());
14801478
maybe_load_error = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
@@ -1518,16 +1516,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15181516
std::optional<ChainstateLoadVerifyError> maybe_verify_error;
15191517
try {
15201518
uiInterface.InitMessage(_("Verifying blocks…").translated);
1521-
auto check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
1522-
if (chainman.m_blockman.m_have_pruned && check_blocks > MIN_BLOCKS_TO_KEEP) {
1519+
if (chainman.m_blockman.m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
15231520
LogPrintfCategory(BCLog::PRUNE, "pruned datadir may not have more than %d blocks; only checking available blocks\n",
15241521
MIN_BLOCKS_TO_KEEP);
15251522
}
1526-
maybe_verify_error = VerifyLoadedChainstate(chainman,
1527-
fReset,
1528-
fReindexChainState,
1529-
check_blocks,
1530-
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
1523+
maybe_verify_error = VerifyLoadedChainstate(chainman, options);
15311524
} catch (const std::exception& e) {
15321525
LogPrintf("%s\n", e.what());
15331526
maybe_verify_error = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
@@ -1554,7 +1547,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15541547

15551548
if (!fLoaded && !ShutdownRequested()) {
15561549
// first suggest a reindex
1557-
if (!fReset) {
1550+
if (!options.reindex) {
15581551
bool fRet = uiInterface.ThreadSafeQuestion(
15591552
strLoadError + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
15601553
strLoadError.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",

src/node/chainstate.cpp

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <coins.h>
99
#include <consensus/params.h>
1010
#include <node/blockstorage.h>
11+
#include <node/caches.h>
1112
#include <sync.h>
1213
#include <threadsafety.h>
1314
#include <txdb.h>
@@ -22,49 +23,40 @@
2223
#include <vector>
2324

2425
namespace node {
25-
std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
26-
ChainstateManager& chainman,
27-
CTxMemPool* mempool,
28-
bool fPruneMode,
29-
bool fReindexChainState,
30-
int64_t nBlockTreeDBCache,
31-
int64_t nCoinDBCache,
32-
int64_t nCoinCacheUsage,
33-
bool block_tree_db_in_memory,
34-
bool coins_db_in_memory,
35-
std::function<bool()> shutdown_requested,
36-
std::function<void()> coins_error_cb)
26+
std::optional<ChainstateLoadingError> LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
27+
const ChainstateLoadOptions& options)
3728
{
3829
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
39-
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
30+
return options.reindex || options.reindex_chainstate || chainstate->CoinsTip().GetBestBlock().IsNull();
4031
};
4132

4233
LOCK(cs_main);
43-
chainman.InitializeChainstate(mempool);
44-
chainman.m_total_coinstip_cache = nCoinCacheUsage;
45-
chainman.m_total_coinsdb_cache = nCoinDBCache;
34+
chainman.InitializeChainstate(options.mempool);
35+
chainman.m_total_coinstip_cache = cache_sizes.coins;
36+
chainman.m_total_coinsdb_cache = cache_sizes.coins_db;
4637

4738
auto& pblocktree{chainman.m_blockman.m_block_tree_db};
4839
// new CBlockTreeDB tries to delete the existing file, which
4940
// fails if it's still open from the previous loop. Close it first:
5041
pblocktree.reset();
51-
pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, block_tree_db_in_memory, fReset));
42+
pblocktree.reset(new CBlockTreeDB(cache_sizes.block_tree_db, options.block_tree_db_in_memory, options.reindex));
5243

53-
if (fReset) {
44+
if (options.reindex) {
5445
pblocktree->WriteReindexing(true);
5546
//If we're reindexing in prune mode, wipe away unusable block files and all undo data files
56-
if (fPruneMode)
47+
if (options.prune) {
5748
CleanupBlockRevFiles();
49+
}
5850
}
5951

60-
if (shutdown_requested && shutdown_requested()) return ChainstateLoadingError::SHUTDOWN_PROBED;
52+
if (options.check_interrupt && options.check_interrupt()) return ChainstateLoadingError::SHUTDOWN_PROBED;
6153

6254
// LoadBlockIndex will load m_have_pruned if we've ever removed a
6355
// block file from disk.
64-
// Note that it also sets fReindex based on the disk flag!
65-
// From here on out fReindex and fReset mean something different!
56+
// Note that it also sets fReindex global based on the disk flag!
57+
// From here on, fReindex and options.reindex values may be different!
6658
if (!chainman.LoadBlockIndex()) {
67-
if (shutdown_requested && shutdown_requested()) return ChainstateLoadingError::SHUTDOWN_PROBED;
59+
if (options.check_interrupt && options.check_interrupt()) return ChainstateLoadingError::SHUTDOWN_PROBED;
6860
return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
6961
}
7062

@@ -75,7 +67,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
7567

7668
// Check for changed -prune state. What we are concerned about is a user who has pruned blocks
7769
// in the past, but is now trying to run unpruned.
78-
if (chainman.m_blockman.m_have_pruned && !fPruneMode) {
70+
if (chainman.m_blockman.m_have_pruned && !options.prune) {
7971
return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
8072
}
8173

@@ -92,12 +84,12 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
9284

9385
for (CChainState* chainstate : chainman.GetAll()) {
9486
chainstate->InitCoinsDB(
95-
/*cache_size_bytes=*/nCoinDBCache,
96-
/*in_memory=*/coins_db_in_memory,
97-
/*should_wipe=*/fReset || fReindexChainState);
87+
/*cache_size_bytes=*/cache_sizes.coins_db,
88+
/*in_memory=*/options.coins_db_in_memory,
89+
/*should_wipe=*/options.reindex || options.reindex_chainstate);
9890

99-
if (coins_error_cb) {
100-
chainstate->CoinsErrorCatcher().AddReadErrCallback(coins_error_cb);
91+
if (options.coins_error_cb) {
92+
chainstate->CoinsErrorCatcher().AddReadErrCallback(options.coins_error_cb);
10193
}
10294

10395
// Refuse to load unsupported database format.
@@ -112,7 +104,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
112104
}
113105

114106
// The on-disk coinsdb is now in a good state, create the cache
115-
chainstate->InitCoinsCache(nCoinCacheUsage);
107+
chainstate->InitCoinsCache(cache_sizes.coins);
116108
assert(chainstate->CanFlushToDisk());
117109

118110
if (!is_coinsview_empty(chainstate)) {
@@ -124,7 +116,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
124116
}
125117
}
126118

127-
if (!fReset) {
119+
if (!options.reindex) {
128120
auto chainstates{chainman.GetAll()};
129121
if (std::any_of(chainstates.begin(), chainstates.end(),
130122
[](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
@@ -136,13 +128,10 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
136128
}
137129

138130
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
139-
bool fReset,
140-
bool fReindexChainState,
141-
int check_blocks,
142-
int check_level)
131+
const ChainstateLoadOptions& options)
143132
{
144133
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
145-
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
134+
return options.reindex || options.reindex_chainstate || chainstate->CoinsTip().GetBestBlock().IsNull();
146135
};
147136

148137
LOCK(cs_main);
@@ -156,8 +145,8 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
156145

157146
if (!CVerifyDB().VerifyDB(
158147
*chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
159-
check_level,
160-
check_blocks)) {
148+
options.check_level,
149+
options.check_blocks)) {
161150
return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
162151
}
163152
}

src/node/chainstate.h

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef BITCOIN_NODE_CHAINSTATE_H
66
#define BITCOIN_NODE_CHAINSTATE_H
77

8+
#include <validation.h>
9+
810
#include <cstdint>
911
#include <functional>
1012
#include <optional>
@@ -13,6 +15,22 @@ class ChainstateManager;
1315
class CTxMemPool;
1416

1517
namespace node {
18+
19+
struct CacheSizes;
20+
21+
struct ChainstateLoadOptions {
22+
CTxMemPool* mempool{nullptr};
23+
bool block_tree_db_in_memory{false};
24+
bool coins_db_in_memory{false};
25+
bool reindex{false};
26+
bool reindex_chainstate{false};
27+
bool prune{false};
28+
int64_t check_blocks{DEFAULT_CHECKBLOCKS};
29+
int64_t check_level{DEFAULT_CHECKLEVEL};
30+
std::function<bool()> check_interrupt;
31+
std::function<void()> coins_error_cb;
32+
};
33+
1634
enum class ChainstateLoadingError {
1735
ERROR_LOADING_BLOCK_DB,
1836
ERROR_BAD_GENESIS_BLOCK,
@@ -52,18 +70,8 @@ enum class ChainstateLoadingError {
5270
* - else
5371
* - Success!
5472
*/
55-
std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
56-
ChainstateManager& chainman,
57-
CTxMemPool* mempool,
58-
bool fPruneMode,
59-
bool fReindexChainState,
60-
int64_t nBlockTreeDBCache,
61-
int64_t nCoinDBCache,
62-
int64_t nCoinCacheUsage,
63-
bool block_tree_db_in_memory,
64-
bool coins_db_in_memory,
65-
std::function<bool()> shutdown_requested = nullptr,
66-
std::function<void()> coins_error_cb = nullptr);
73+
std::optional<ChainstateLoadingError> LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
74+
const ChainstateLoadOptions& options);
6775

6876
enum class ChainstateLoadVerifyError {
6977
ERROR_BLOCK_FROM_FUTURE,
@@ -72,10 +80,7 @@ enum class ChainstateLoadVerifyError {
7280
};
7381

7482
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
75-
bool fReset,
76-
bool fReindexChainState,
77-
int check_blocks,
78-
int check_level);
83+
const ChainstateLoadOptions& options);
7984
} // namespace node
8085

8186
#endif // BITCOIN_NODE_CHAINSTATE_H

src/test/util/setup_common.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454

5555
using node::BlockAssembler;
5656
using node::CalculateCacheSizes;
57-
using node::fPruneMode;
58-
using node::fReindex;
5957
using node::LoadChainstate;
6058
using node::NodeContext;
6159
using node::RegenerateCommitments;
@@ -218,24 +216,19 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
218216
// instead of unit tests, but for now we need these here.
219217
RegisterAllCoreRPCCommands(tableRPC);
220218

221-
auto maybe_load_error = LoadChainstate(fReindex.load(),
222-
*Assert(m_node.chainman.get()),
223-
Assert(m_node.mempool.get()),
224-
fPruneMode,
225-
m_args.GetBoolArg("-reindex-chainstate", false),
226-
m_cache_sizes.block_tree_db,
227-
m_cache_sizes.coins_db,
228-
m_cache_sizes.coins,
229-
/*block_tree_db_in_memory=*/true,
230-
/*coins_db_in_memory=*/true);
219+
node::ChainstateLoadOptions options;
220+
options.mempool = Assert(m_node.mempool.get());
221+
options.block_tree_db_in_memory = true;
222+
options.coins_db_in_memory = true;
223+
options.reindex = node::fReindex;
224+
options.reindex_chainstate = m_args.GetBoolArg("-reindex-chainstate", false);
225+
options.prune = node::fPruneMode;
226+
options.check_blocks = m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
227+
options.check_level = m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
228+
auto maybe_load_error = LoadChainstate(*Assert(m_node.chainman), m_cache_sizes, options);
231229
assert(!maybe_load_error.has_value());
232230

233-
auto maybe_verify_error = VerifyLoadedChainstate(
234-
*Assert(m_node.chainman),
235-
fReindex.load(),
236-
m_args.GetBoolArg("-reindex-chainstate", false),
237-
m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS),
238-
m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL));
231+
auto maybe_verify_error = VerifyLoadedChainstate(*Assert(m_node.chainman), options);
239232
assert(!maybe_verify_error.has_value());
240233

241234
BlockValidationState state;

0 commit comments

Comments
 (0)