Skip to content

Commit d2ec372

Browse files
committed
Merge bitcoin#21796: index: Avoid async shutdown on init error
faad68f index: Avoid async shutdown on init error (MarcoFalke) Pull request description: An async shutdown during init is confusing when a simple boolean return value can be used for a synchronous shutdown. This also changes the error message on stderr from: ``` Error: A fatal internal error occurred, see debug.log for details Error: A fatal internal error occurred, see debug.log for details ``` To: ``` Error: basic block filter index best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again) ACKs for top commit: laanwj: Code review ACK faad68f Tree-SHA512: 92dd895266d6d15a6b1a5c081c9b83f83d5c82e9bfceb3ea0664f48540812239e274c829ff0271c4a0afb6d6a8f67d89c5af20d719982ad62999a41ca0623274
2 parents 91db985 + faad68f commit d2ec372

File tree

7 files changed

+23
-17
lines changed

7 files changed

+23
-17
lines changed

src/index/base.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ bool BaseIndex::Init()
9898
}
9999
}
100100
if (prune_violation) {
101-
// throw error and graceful shutdown if we can't build the index
102-
FatalError("%s: %s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)", __func__, GetName());
103-
return false;
101+
return InitError(strprintf(Untranslated("%s best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)"), GetName()));
104102
}
105103
}
106104
return true;
@@ -339,17 +337,17 @@ void BaseIndex::Interrupt()
339337
m_interrupt();
340338
}
341339

342-
void BaseIndex::Start()
340+
bool BaseIndex::Start()
343341
{
344342
// Need to register this ValidationInterface before running Init(), so that
345343
// callbacks are not missed if Init sets m_synced to true.
346344
RegisterValidationInterface(this);
347345
if (!Init()) {
348-
FatalError("%s: %s failed to initialize", __func__, GetName());
349-
return;
346+
return false;
350347
}
351348

352349
m_thread_sync = std::thread(&util::TraceThread, GetName(), [this] { ThreadSync(); });
350+
return true;
353351
}
354352

355353
void BaseIndex::Stop()

src/index/base.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class BaseIndex : public CValidationInterface
8484
const CBlockIndex* CurrentIndex() { return m_best_block_index.load(); };
8585

8686
/// Initialize internal state from the database and block index.
87-
virtual bool Init();
87+
[[nodiscard]] virtual bool Init();
8888

8989
/// Write update index entries for a newly connected block.
9090
virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
@@ -117,7 +117,7 @@ class BaseIndex : public CValidationInterface
117117

118118
/// Start initializes the sync state and registers the instance as a
119119
/// ValidationInterface so that it stays in sync with blockchain updates.
120-
void Start();
120+
[[nodiscard]] bool Start();
121121

122122
/// Stops the instance from staying in sync with blockchain updates.
123123
void Stop();

src/init.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,17 +1550,23 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15501550
// ********************************************************* Step 8: start indexers
15511551
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
15521552
g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex);
1553-
g_txindex->Start();
1553+
if (!g_txindex->Start()) {
1554+
return false;
1555+
}
15541556
}
15551557

15561558
for (const auto& filter_type : g_enabled_filter_types) {
15571559
InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex);
1558-
GetBlockFilterIndex(filter_type)->Start();
1560+
if (!GetBlockFilterIndex(filter_type)->Start()) {
1561+
return false;
1562+
}
15591563
}
15601564

15611565
if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {
15621566
g_coin_stats_index = std::make_unique<CoinStatsIndex>(/* cache size */ 0, false, fReindex);
1563-
g_coin_stats_index->Start();
1567+
if (!g_coin_stats_index->Start()) {
1568+
return false;
1569+
}
15641570
}
15651571

15661572
// ********************************************************* Step 9: load wallet

src/test/blockfilter_index_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
131131
// BlockUntilSyncedToCurrentChain should return false before index is started.
132132
BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
133133

134-
filter_index.Start();
134+
BOOST_REQUIRE(filter_index.Start());
135135

136136
// Allow filter index to catch up with the block index.
137137
constexpr int64_t timeout_ms = 10 * 1000;

src/test/coinstatsindex_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
3232
// is started.
3333
BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
3434

35-
coin_stats_index.Start();
35+
BOOST_REQUIRE(coin_stats_index.Start());
3636

3737
// Allow the CoinStatsIndex to catch up with the block index that is syncing
3838
// in a background thread.

src/test/txindex_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
2727
// BlockUntilSyncedToCurrentChain should return false before txindex is started.
2828
BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
2929

30-
txindex.Start();
30+
BOOST_REQUIRE(txindex.Start());
3131

3232
// Allow tx index to catch up with the block index.
3333
constexpr int64_t timeout_ms = 10 * 1000;

test/functional/feature_blockfilterindex_prune.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ def run_test(self):
5454
self.stop_node(0)
5555

5656
self.log.info("make sure we get an init error when starting the node again with block filters")
57-
with self.nodes[0].assert_debug_log(["basic block filter index best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)"]):
58-
self.nodes[0].assert_start_raises_init_error(extra_args=["-fastprune", "-prune=1", "-blockfilterindex=1"])
57+
self.nodes[0].assert_start_raises_init_error(
58+
extra_args=["-fastprune", "-prune=1", "-blockfilterindex=1"],
59+
expected_msg="Error: basic block filter index best block of the index goes beyond pruned data. Please disable the index or reindex (which will download the whole blockchain again)",
60+
)
5961

6062
self.log.info("make sure the node starts again with the -reindex arg")
61-
self.start_node(0, extra_args = ["-fastprune", "-prune=1", "-blockfilterindex", "-reindex"])
63+
self.start_node(0, extra_args=["-fastprune", "-prune=1", "-blockfilterindex", "-reindex"])
6264

6365

6466
if __name__ == '__main__':

0 commit comments

Comments
 (0)