@@ -26,100 +26,98 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
26
26
return fReset || fReindexChainState || chainstate->CoinsTip ().GetBestBlock ().IsNull ();
27
27
};
28
28
29
- {
30
- LOCK (cs_main);
31
- chainman.InitializeChainstate (mempool);
32
- chainman.m_total_coinstip_cache = nCoinCacheUsage;
33
- chainman.m_total_coinsdb_cache = nCoinDBCache;
34
-
35
- UnloadBlockIndex (mempool, chainman);
36
-
37
- auto & pblocktree{chainman.m_blockman .m_block_tree_db };
38
- // new CBlockTreeDB tries to delete the existing file, which
39
- // fails if it's still open from the previous loop. Close it first:
40
- pblocktree.reset ();
41
- pblocktree.reset (new CBlockTreeDB (nBlockTreeDBCache, block_tree_db_in_memory, fReset ));
42
-
43
- if (fReset ) {
44
- pblocktree->WriteReindexing (true );
45
- // If we're reindexing in prune mode, wipe away unusable block files and all undo data files
46
- if (fPruneMode )
47
- CleanupBlockRevFiles ();
48
- }
29
+ LOCK (cs_main);
30
+ chainman.InitializeChainstate (mempool);
31
+ chainman.m_total_coinstip_cache = nCoinCacheUsage;
32
+ chainman.m_total_coinsdb_cache = nCoinDBCache;
33
+
34
+ UnloadBlockIndex (mempool, chainman);
35
+
36
+ auto & pblocktree{chainman.m_blockman .m_block_tree_db };
37
+ // new CBlockTreeDB tries to delete the existing file, which
38
+ // fails if it's still open from the previous loop. Close it first:
39
+ pblocktree.reset ();
40
+ pblocktree.reset (new CBlockTreeDB (nBlockTreeDBCache, block_tree_db_in_memory, fReset ));
41
+
42
+ if (fReset ) {
43
+ pblocktree->WriteReindexing (true );
44
+ // If we're reindexing in prune mode, wipe away unusable block files and all undo data files
45
+ if (fPruneMode )
46
+ CleanupBlockRevFiles ();
47
+ }
49
48
50
- if (shutdown_requested && shutdown_requested ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
49
+ if (shutdown_requested && shutdown_requested ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
51
50
52
- // LoadBlockIndex will load fHavePruned if we've ever removed a
53
- // block file from disk.
54
- // Note that it also sets fReindex based on the disk flag!
55
- // From here on out fReindex and fReset mean something different!
56
- if (!chainman.LoadBlockIndex ()) {
57
- if (shutdown_requested && shutdown_requested ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
58
- return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
59
- }
51
+ // LoadBlockIndex will load fHavePruned if we've ever removed a
52
+ // block file from disk.
53
+ // Note that it also sets fReindex based on the disk flag!
54
+ // From here on out fReindex and fReset mean something different!
55
+ if (!chainman.LoadBlockIndex ()) {
56
+ if (shutdown_requested && shutdown_requested ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
57
+ return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
58
+ }
60
59
61
- if (!chainman.BlockIndex ().empty () &&
62
- !chainman.m_blockman .LookupBlockIndex (consensus_params.hashGenesisBlock )) {
63
- return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK;
64
- }
60
+ if (!chainman.BlockIndex ().empty () &&
61
+ !chainman.m_blockman .LookupBlockIndex (consensus_params.hashGenesisBlock )) {
62
+ return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK;
63
+ }
65
64
66
- // Check for changed -prune state. What we are concerned about is a user who has pruned blocks
67
- // in the past, but is now trying to run unpruned.
68
- if (fHavePruned && !fPruneMode ) {
69
- return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
70
- }
65
+ // Check for changed -prune state. What we are concerned about is a user who has pruned blocks
66
+ // in the past, but is now trying to run unpruned.
67
+ if (fHavePruned && !fPruneMode ) {
68
+ return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
69
+ }
71
70
72
- // At this point blocktree args are consistent with what's on disk.
73
- // If we're not mid-reindex (based on disk + args), add a genesis block on disk
74
- // (otherwise we use the one already on disk).
75
- // This is called again in ThreadImport after the reindex completes.
76
- if (!fReindex && !chainman.ActiveChainstate ().LoadGenesisBlock ()) {
77
- return ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED;
78
- }
71
+ // At this point blocktree args are consistent with what's on disk.
72
+ // If we're not mid-reindex (based on disk + args), add a genesis block on disk
73
+ // (otherwise we use the one already on disk).
74
+ // This is called again in ThreadImport after the reindex completes.
75
+ if (!fReindex && !chainman.ActiveChainstate ().LoadGenesisBlock ()) {
76
+ return ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED;
77
+ }
79
78
80
- // At this point we're either in reindex or we've loaded a useful
81
- // block tree into BlockIndex()!
79
+ // At this point we're either in reindex or we've loaded a useful
80
+ // block tree into BlockIndex()!
82
81
83
- for (CChainState* chainstate : chainman.GetAll ()) {
84
- chainstate->InitCoinsDB (
85
- /* cache_size_bytes */ nCoinDBCache,
86
- /* in_memory */ coins_db_in_memory,
87
- /* should_wipe */ fReset || fReindexChainState );
82
+ for (CChainState* chainstate : chainman.GetAll ()) {
83
+ chainstate->InitCoinsDB (
84
+ /* cache_size_bytes */ nCoinDBCache,
85
+ /* in_memory */ coins_db_in_memory,
86
+ /* should_wipe */ fReset || fReindexChainState );
88
87
89
- if (coins_error_cb) {
90
- chainstate->CoinsErrorCatcher ().AddReadErrCallback (coins_error_cb);
91
- }
88
+ if (coins_error_cb) {
89
+ chainstate->CoinsErrorCatcher ().AddReadErrCallback (coins_error_cb);
90
+ }
92
91
93
- // If necessary, upgrade from older database format.
94
- // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
95
- if (!chainstate->CoinsDB ().Upgrade ()) {
96
- return ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED;
97
- }
92
+ // If necessary, upgrade from older database format.
93
+ // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
94
+ if (!chainstate->CoinsDB ().Upgrade ()) {
95
+ return ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED;
96
+ }
98
97
99
- // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
100
- if (!chainstate->ReplayBlocks ()) {
101
- return ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED;
102
- }
98
+ // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
99
+ if (!chainstate->ReplayBlocks ()) {
100
+ return ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED;
101
+ }
103
102
104
- // The on-disk coinsdb is now in a good state, create the cache
105
- chainstate->InitCoinsCache (nCoinCacheUsage);
106
- assert (chainstate->CanFlushToDisk ());
103
+ // The on-disk coinsdb is now in a good state, create the cache
104
+ chainstate->InitCoinsCache (nCoinCacheUsage);
105
+ assert (chainstate->CanFlushToDisk ());
107
106
108
- if (!is_coinsview_empty (chainstate)) {
109
- // LoadChainTip initializes the chain based on CoinsTip()'s best block
110
- if (!chainstate->LoadChainTip ()) {
111
- return ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED;
112
- }
113
- assert (chainstate->m_chain .Tip () != nullptr );
107
+ if (!is_coinsview_empty (chainstate)) {
108
+ // LoadChainTip initializes the chain based on CoinsTip()'s best block
109
+ if (!chainstate->LoadChainTip ()) {
110
+ return ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED;
114
111
}
112
+ assert (chainstate->m_chain .Tip () != nullptr );
115
113
}
114
+ }
116
115
117
- if (!fReset ) {
118
- auto chainstates{chainman.GetAll ()};
119
- if (std::any_of (chainstates.begin (), chainstates.end (),
120
- [](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED (cs_main) { return cs->NeedsRedownload (); })) {
121
- return ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED;
122
- }
116
+ if (!fReset ) {
117
+ auto chainstates{chainman.GetAll ()};
118
+ if (std::any_of (chainstates.begin (), chainstates.end (),
119
+ [](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED (cs_main) { return cs->NeedsRedownload (); })) {
120
+ return ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED;
123
121
}
124
122
}
125
123
@@ -138,22 +136,20 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
138
136
return fReset || fReindexChainState || chainstate->CoinsTip ().GetBestBlock ().IsNull ();
139
137
};
140
138
141
- {
142
- LOCK (cs_main);
143
-
144
- for (CChainState* chainstate : chainman.GetAll ()) {
145
- if (!is_coinsview_empty (chainstate)) {
146
- const CBlockIndex* tip = chainstate->m_chain .Tip ();
147
- if (tip && tip->nTime > get_unix_time_seconds () + 2 * 60 * 60 ) {
148
- return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
149
- }
150
-
151
- if (!CVerifyDB ().VerifyDB (
152
- *chainstate, consensus_params, chainstate->CoinsDB (),
153
- check_level,
154
- check_blocks)) {
155
- return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
156
- }
139
+ LOCK (cs_main);
140
+
141
+ for (CChainState* chainstate : chainman.GetAll ()) {
142
+ if (!is_coinsview_empty (chainstate)) {
143
+ const CBlockIndex* tip = chainstate->m_chain .Tip ();
144
+ if (tip && tip->nTime > get_unix_time_seconds () + 2 * 60 * 60 ) {
145
+ return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
146
+ }
147
+
148
+ if (!CVerifyDB ().VerifyDB (
149
+ *chainstate, consensus_params, chainstate->CoinsDB (),
150
+ check_level,
151
+ check_blocks)) {
152
+ return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
157
153
}
158
154
}
159
155
}
0 commit comments