merge bitcoin#21796: Avoid async shutdown on init error

This commit is contained in:
Kittywhiskers Van Gogh 2023-07-30 11:55:00 +00:00 committed by PastaPastaPasta
parent d97dcb22e1
commit d577d8bc57
8 changed files with 24 additions and 18 deletions

View File

@ -97,9 +97,7 @@ bool BaseIndex::Init()
}
}
if (prune_violation) {
// throw error and graceful shutdown if we can't build the index
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());
return false;
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()));
}
}
return true;
@ -338,18 +336,18 @@ void BaseIndex::Interrupt()
m_interrupt();
}
void BaseIndex::Start()
bool BaseIndex::Start()
{
// Need to register this ValidationInterface before running Init(), so that
// callbacks are not missed if Init sets m_synced to true.
RegisterValidationInterface(this);
if (!Init()) {
FatalError("%s: %s failed to initialize", __func__, GetName());
return;
return false;
}
m_thread_sync = std::thread(&TraceThread<std::function<void()>>, GetName(),
std::bind(&BaseIndex::ThreadSync, this));
return true;
}
void BaseIndex::Stop()

View File

@ -84,7 +84,7 @@ protected:
const CBlockIndex* CurrentIndex() { return m_best_block_index.load(); };
/// Initialize internal state from the database and block index.
virtual bool Init();
[[nodiscard]] virtual bool Init();
/// Write update index entries for a newly connected block.
virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
@ -117,7 +117,7 @@ public:
/// Start initializes the sync state and registers the instance as a
/// ValidationInterface so that it stays in sync with blockchain updates.
void Start();
[[nodiscard]] bool Start();
/// Stops the instance from staying in sync with blockchain updates.
void Stop();

View File

@ -2186,17 +2186,23 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
// ********************************************************* Step 8: start indexers
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex);
g_txindex->Start();
if (!g_txindex->Start()) {
return false;
}
}
for (const auto& filter_type : g_enabled_filter_types) {
InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex);
GetBlockFilterIndex(filter_type)->Start();
if (!GetBlockFilterIndex(filter_type)->Start()) {
return false;
}
}
if (args.GetBoolArg("-coinstatsindex", DEFAULT_COINSTATSINDEX)) {
g_coin_stats_index = std::make_unique<CoinStatsIndex>(/* cache size */ 0, false, fReindex);
g_coin_stats_index->Start();
if (!g_coin_stats_index->Start()) {
return false;
}
}
// ********************************************************* Step 9: load wallet

View File

@ -138,7 +138,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
// BlockUntilSyncedToCurrentChain should return false before index is started.
BOOST_CHECK(!filter_index.BlockUntilSyncedToCurrentChain());
filter_index.Start();
BOOST_REQUIRE(filter_index.Start());
// Allow filter index to catch up with the block index.
constexpr int64_t timeout_ms = 10 * 1000;

View File

@ -32,7 +32,7 @@ BOOST_FIXTURE_TEST_CASE(coinstatsindex_initial_sync, TestChain100Setup)
// is started.
BOOST_CHECK(!coin_stats_index.BlockUntilSyncedToCurrentChain());
coin_stats_index.Start();
BOOST_REQUIRE(coin_stats_index.Start());
// Allow the CoinStatsIndex to catch up with the block index that is syncing
// in a background thread.

View File

@ -27,7 +27,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
// BlockUntilSyncedToCurrentChain should return false before txindex is started.
BOOST_CHECK(!txindex.BlockUntilSyncedToCurrentChain());
txindex.Start();
BOOST_REQUIRE(txindex.Start());
// Allow tx index to catch up with the block index.
constexpr int64_t timeout_ms = 10 * 1000;

View File

@ -313,7 +313,7 @@ void TestChainSetup::mineBlocks(int num_blocks)
}
g_txindex = std::make_unique<TxIndex>(1 << 20, true);
g_txindex->Start();
assert(g_txindex->Start());
// Allow tx index to catch up with the block index.
constexpr int64_t timeout_ms = 10 * 1000;

View File

@ -55,11 +55,13 @@ class FeatureBlockfilterindexPruneTest(BitcoinTestFramework):
self.stop_node(0, expected_stderr='Warning: You are starting with governance validation disabled. This is expected because you are running a pruned node.')
self.log.info("make sure we get an init error when starting the node again with block filters")
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)"]):
self.nodes[0].assert_start_raises_init_error(extra_args=["-fastprune", "-prune=1", "-blockfilterindex=1"])
self.nodes[0].assert_start_raises_init_error(
extra_args=["-fastprune", "-prune=1", "-blockfilterindex=1"],
expected_msg="Warning: You are starting with governance validation disabled. This is expected because you are running a pruned node.\nError: 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)",
)
self.log.info("make sure the node starts again with the -reindex arg")
self.start_node(0, extra_args = ["-fastprune", "-prune=1", "-blockfilterindex", "-reindex"])
self.start_node(0, extra_args=["-fastprune", "-prune=1", "-blockfilterindex", "-reindex"])
self.stop_nodes(expected_stderr='Warning: You are starting with governance validation disabled. This is expected because you are running a pruned node.')