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