8
8
#include < coins.h>
9
9
#include < consensus/params.h>
10
10
#include < node/blockstorage.h>
11
+ #include < node/caches.h>
11
12
#include < sync.h>
12
13
#include < threadsafety.h>
13
14
#include < txdb.h>
22
23
#include < vector>
23
24
24
25
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)
37
28
{
38
29
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 ();
40
31
};
41
32
42
33
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 ;
46
37
47
38
auto & pblocktree{chainman.m_blockman .m_block_tree_db };
48
39
// new CBlockTreeDB tries to delete the existing file, which
49
40
// fails if it's still open from the previous loop. Close it first:
50
41
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 ));
52
43
53
- if (fReset ) {
44
+ if (options. reindex ) {
54
45
pblocktree->WriteReindexing (true );
55
46
// If we're reindexing in prune mode, wipe away unusable block files and all undo data files
56
- if (fPruneMode )
47
+ if (options. prune ) {
57
48
CleanupBlockRevFiles ();
49
+ }
58
50
}
59
51
60
- if (shutdown_requested && shutdown_requested ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
52
+ if (options. check_interrupt && options. check_interrupt ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
61
53
62
54
// LoadBlockIndex will load m_have_pruned if we've ever removed a
63
55
// 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!
66
58
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;
68
60
return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
69
61
}
70
62
@@ -75,7 +67,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
75
67
76
68
// Check for changed -prune state. What we are concerned about is a user who has pruned blocks
77
69
// 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 ) {
79
71
return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
80
72
}
81
73
@@ -92,12 +84,12 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
92
84
93
85
for (CChainState* chainstate : chainman.GetAll ()) {
94
86
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 );
98
90
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 );
101
93
}
102
94
103
95
// Refuse to load unsupported database format.
@@ -112,7 +104,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
112
104
}
113
105
114
106
// The on-disk coinsdb is now in a good state, create the cache
115
- chainstate->InitCoinsCache (nCoinCacheUsage );
107
+ chainstate->InitCoinsCache (cache_sizes. coins );
116
108
assert (chainstate->CanFlushToDisk ());
117
109
118
110
if (!is_coinsview_empty (chainstate)) {
@@ -124,7 +116,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
124
116
}
125
117
}
126
118
127
- if (!fReset ) {
119
+ if (!options. reindex ) {
128
120
auto chainstates{chainman.GetAll ()};
129
121
if (std::any_of (chainstates.begin (), chainstates.end (),
130
122
[](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED (cs_main) { return cs->NeedsRedownload (); })) {
@@ -136,13 +128,10 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
136
128
}
137
129
138
130
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate (ChainstateManager& chainman,
139
- bool fReset ,
140
- bool fReindexChainState ,
141
- int check_blocks,
142
- int check_level)
131
+ const ChainstateLoadOptions& options)
143
132
{
144
133
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 ();
146
135
};
147
136
148
137
LOCK (cs_main);
@@ -156,8 +145,8 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
156
145
157
146
if (!CVerifyDB ().VerifyDB (
158
147
*chainstate, chainman.GetConsensus (), chainstate->CoinsDB (),
159
- check_level,
160
- check_blocks)) {
148
+ options. check_level ,
149
+ options. check_blocks )) {
161
150
return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
162
151
}
163
152
}
0 commit comments