Skip to content

Commit 3917dff

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23855: refactor: Post-"Chainstate loading sequence coalescence" fixups
e3544c8 init: Use clang-tidy named args syntax (Carl Dong) 3401630 style-only: Rename *Chainstate return values (Carl Dong) 1dd5827 docs: Make LoadChainstate comment more accurate (Carl Dong) 6b83576 node/chainstate: Use MAX_FUTURE_BLOCK_TIME (Carl Dong) Pull request description: There are 2 proposed fixups in discussions in #23280 which I have not implemented: 1. An overhaul to return types and an option type for the two `*Chainstate` functions: bitcoin/bitcoin#23280 (comment) - The change reintroduces stringy return types and is quite involved. It could be discussed in a separate PR. 2. Passing in the unix time to `VerifyChainstate` instead of a callback to get the time: bitcoin/bitcoin#23280 (comment) - I'm not sure it matters much whether it's a callback or just the actual unix time. Also, I think `VerifyDB` can take quite a while, and I don't want to impose that the function have to "run quickly" in order to have it be correct. If reviewers feel strongly about either of the two fixups listed above, please feel free to open a PR based on mine and I'll close this one! ACKs for top commit: ryanofsky: Code review ACK e3544c8 MarcoFalke: ACK e3544c8 🐸 Tree-SHA512: dd1de0265b6785eef306e724b678ce03d7c54ea9f4b5ea0ccd7af59cce2ea3aba73fd4af0c15e2dca9265807dc4075f9afa2ec103672677b6638b1a4fc090904
2 parents 0620957 + e3544c8 commit 3917dff

File tree

4 files changed

+50
-50
lines changed

4 files changed

+50
-50
lines changed

src/init.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,31 +1404,31 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14041404

14051405
uiInterface.InitMessage(_("Loading block index…").translated);
14061406
const int64_t load_block_index_start_time = GetTimeMillis();
1407-
std::optional<ChainstateLoadingError> rv;
1407+
std::optional<ChainstateLoadingError> maybe_load_error;
14081408
try {
1409-
rv = LoadChainstate(fReset,
1410-
chainman,
1411-
Assert(node.mempool.get()),
1412-
fPruneMode,
1413-
chainparams.GetConsensus(),
1414-
fReindexChainState,
1415-
cache_sizes.block_tree_db,
1416-
cache_sizes.coins_db,
1417-
cache_sizes.coins,
1418-
false,
1419-
false,
1420-
ShutdownRequested,
1421-
[]() {
1422-
uiInterface.ThreadSafeMessageBox(
1423-
_("Error reading from database, shutting down."),
1424-
"", CClientUIInterface::MSG_ERROR);
1425-
});
1409+
maybe_load_error = LoadChainstate(fReset,
1410+
chainman,
1411+
Assert(node.mempool.get()),
1412+
fPruneMode,
1413+
chainparams.GetConsensus(),
1414+
fReindexChainState,
1415+
cache_sizes.block_tree_db,
1416+
cache_sizes.coins_db,
1417+
cache_sizes.coins,
1418+
/*block_tree_db_in_memory=*/false,
1419+
/*coins_db_in_memory=*/false,
1420+
/*shutdown_requested=*/ShutdownRequested,
1421+
/*coins_error_cb=*/[]() {
1422+
uiInterface.ThreadSafeMessageBox(
1423+
_("Error reading from database, shutting down."),
1424+
"", CClientUIInterface::MSG_ERROR);
1425+
});
14261426
} catch (const std::exception& e) {
14271427
LogPrintf("%s\n", e.what());
1428-
rv = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
1428+
maybe_load_error = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
14291429
}
1430-
if (rv.has_value()) {
1431-
switch (rv.value()) {
1430+
if (maybe_load_error.has_value()) {
1431+
switch (maybe_load_error.value()) {
14321432
case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB:
14331433
strLoadError = _("Error loading block database");
14341434
break;
@@ -1462,27 +1462,27 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14621462
break;
14631463
}
14641464
} else {
1465-
std::optional<ChainstateLoadVerifyError> rv2;
1465+
std::optional<ChainstateLoadVerifyError> maybe_verify_error;
14661466
try {
14671467
uiInterface.InitMessage(_("Verifying blocks…").translated);
14681468
auto check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
14691469
if (fHavePruned && check_blocks > MIN_BLOCKS_TO_KEEP) {
14701470
LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks\n",
14711471
MIN_BLOCKS_TO_KEEP);
14721472
}
1473-
rv2 = VerifyLoadedChainstate(chainman,
1474-
fReset,
1475-
fReindexChainState,
1476-
chainparams.GetConsensus(),
1477-
check_blocks,
1478-
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
1479-
static_cast<int64_t(*)()>(GetTime));
1473+
maybe_verify_error = VerifyLoadedChainstate(chainman,
1474+
fReset,
1475+
fReindexChainState,
1476+
chainparams.GetConsensus(),
1477+
check_blocks,
1478+
args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
1479+
/*get_unix_time_seconds=*/static_cast<int64_t(*)()>(GetTime));
14801480
} catch (const std::exception& e) {
14811481
LogPrintf("%s\n", e.what());
1482-
rv2 = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
1482+
maybe_verify_error = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
14831483
}
1484-
if (rv2.has_value()) {
1485-
switch (rv2.value()) {
1484+
if (maybe_verify_error.has_value()) {
1485+
switch (maybe_verify_error.value()) {
14861486
case ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE:
14871487
strLoadError = _("The block database contains a block which appears to be from the future. "
14881488
"This may be due to your computer's date and time being set incorrectly. "

src/node/chainstate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
141141
for (CChainState* chainstate : chainman.GetAll()) {
142142
if (!is_coinsview_empty(chainstate)) {
143143
const CBlockIndex* tip = chainstate->m_chain.Tip();
144-
if (tip && tip->nTime > get_unix_time_seconds() + 2 * 60 * 60) {
144+
if (tip && tip->nTime > get_unix_time_seconds() + MAX_FUTURE_BLOCK_TIME) {
145145
return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
146146
}
147147

src/node/chainstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ enum class ChainstateLoadingError {
5050
* this sequence, when we explicitly checked shutdown_requested() at
5151
* arbitrary points, one of those calls returned true". Therefore, a
5252
* return value other than SHUTDOWN_PROBED does not guarantee that
53-
* shutdown_requested() hasn't been called indirectly.
53+
* shutdown hasn't been called indirectly.
5454
* - else
5555
* - Success!
5656
*/

src/test/util/setup_common.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -182,28 +182,28 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
182182
// instead of unit tests, but for now we need these here.
183183
RegisterAllCoreRPCCommands(tableRPC);
184184

185-
auto rv = LoadChainstate(fReindex.load(),
186-
*Assert(m_node.chainman.get()),
187-
Assert(m_node.mempool.get()),
188-
fPruneMode,
189-
chainparams.GetConsensus(),
190-
m_args.GetBoolArg("-reindex-chainstate", false),
191-
m_cache_sizes.block_tree_db,
192-
m_cache_sizes.coins_db,
193-
m_cache_sizes.coins,
194-
true,
195-
true);
196-
assert(!rv.has_value());
197-
198-
auto maybe_verify_failure = VerifyLoadedChainstate(
185+
auto maybe_load_error = LoadChainstate(fReindex.load(),
186+
*Assert(m_node.chainman.get()),
187+
Assert(m_node.mempool.get()),
188+
fPruneMode,
189+
chainparams.GetConsensus(),
190+
m_args.GetBoolArg("-reindex-chainstate", false),
191+
m_cache_sizes.block_tree_db,
192+
m_cache_sizes.coins_db,
193+
m_cache_sizes.coins,
194+
/*block_tree_db_in_memory=*/true,
195+
/*coins_db_in_memory=*/true);
196+
assert(!maybe_load_error.has_value());
197+
198+
auto maybe_verify_error = VerifyLoadedChainstate(
199199
*Assert(m_node.chainman),
200200
fReindex.load(),
201201
m_args.GetBoolArg("-reindex-chainstate", false),
202202
chainparams.GetConsensus(),
203203
m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS),
204204
m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
205-
static_cast<int64_t(*)()>(GetTime));
206-
assert(!maybe_verify_failure.has_value());
205+
/*get_unix_time_seconds=*/static_cast<int64_t(*)()>(GetTime));
206+
assert(!maybe_verify_error.has_value());
207207

208208
BlockValidationState state;
209209
if (!m_node.chainman->ActiveChainstate().ActivateBestChain(state)) {

0 commit comments

Comments
 (0)