Skip to content

Commit 1e8aa02

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24117: index: make indices robust against init aborts
bfcd60f test: activate all index types in feature_init.py (Martin Zumsande) 0243907 index: Don't commit without valid m_best_block_index (Martin Zumsande) Pull request description: When an index thread receives an interrupt during init before it got to index anything (so `m_best_block_index == nullptr` still), it will still try to commit previous "work" before stopping the thread. That means that `BaseIndex::CommitInternal()` calls `GetLocator(nullptr)`, which returns an locator to the tip ([code](https://github.com/bitcoin/bitcoin/blob/06b6369766137756648b3cb62c8f385cca234e69/src/chain.cpp#L31-L32)), and saves it to the index DB. On the next startup, this locator will be read and it will be assumed that we have successfully synced the index to the tip, when in reality we have indexed nothing. In the case of coinstatsindex, this would lead to a shutdown of bitcoind without any indication what went wrong. For the other indexes, there would be no immediate shutdown, but the index would be corrupt. This PR fixes this by not committing when `m_best_block_index==nullptr`, and it also adds an error log message to the silent coinstatsindex shutdown path. This is another small bug found by `feature_init.py` - the second commit enables blockfilterindex and coinstatsindex for this test, enabling coinstatsindex without the first commit would have led to frequent failures. ACKs for top commit: fjahr: reACK bfcd60f shaavan: reACK bfcd60f Tree-SHA512: 8e2bac0fc40cde209518a9e59b597ae0a5a875a2a90898673987c91733718d40e528dada942bf552b58bc021bf46e59da2d0cc5a61045f48f9bae2b1baf6033b
2 parents 7164e00 + bfcd60f commit 1e8aa02

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/index/base.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ bool BaseIndex::Commit()
211211
bool BaseIndex::CommitInternal(CDBBatch& batch)
212212
{
213213
LOCK(cs_main);
214+
// Don't commit anything if we haven't indexed any block yet
215+
// (this could happen if init is interrupted).
216+
if (m_best_block_index == nullptr) {
217+
return false;
218+
}
214219
GetDB().WriteBestBlock(batch, m_chainstate->m_chain.GetLocator(m_best_block_index));
215220
return true;
216221
}

src/index/coinstatsindex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ bool CoinStatsIndex::Init()
360360
if (pindex) {
361361
DBVal entry;
362362
if (!LookUpOne(*m_db, pindex, entry)) {
363-
return false;
363+
return error("%s: Cannot read current %s state; index may be corrupted",
364+
__func__, GetName());
364365
}
365-
366366
m_transaction_output_count = entry.transaction_output_count;
367367
m_bogo_size = entry.bogo_size;
368368
m_total_amount = entry.total_amount;

test/functional/feature_init.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def check_clean_start():
6464
'addcon thread start',
6565
'loadblk thread start',
6666
'txindex thread start',
67+
'block filter index thread start',
68+
'coinstatsindex thread start',
6769
'msghand thread start',
6870
'net thread start',
6971
'addcon thread start',
@@ -74,7 +76,7 @@ def check_clean_start():
7476
for terminate_line in lines_to_terminate_after:
7577
self.log.info(f"Starting node and will exit after line '{terminate_line}'")
7678
with node.wait_for_debug_log([terminate_line], ignore_case=True):
77-
node.start(extra_args=['-txindex=1'])
79+
node.start(extra_args=['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1'])
7880
self.log.debug("Terminating node after terminate line was found")
7981
sigterm_node()
8082

@@ -109,7 +111,7 @@ def check_clean_start():
109111
# investigate doing this later.
110112

111113
node.assert_start_raises_init_error(
112-
extra_args=['-txindex=1'],
114+
extra_args=['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1'],
113115
expected_msg=err_fragment,
114116
match=ErrorMatch.PARTIAL_REGEX,
115117
)

0 commit comments

Comments
 (0)