Skip to content

Commit 162c009

Browse files
committed
Merge #33251: [29.x] backport #33212
fcac802 test: index with an unclean restart after a reorg (Martin Zumsande) 16b1710 index: don't commit state in BaseIndex::Rewind (Martin Zumsande) Pull request description: Backports #33212 to 29.x ACKs for top commit: achow101: ACK fcac802 stickies-v: ACK fcac802 mzumsande: Code Review ACK fcac802 Tree-SHA512: eeb9213f03bbb1d48c3ccb12121a6e475f436895d314b5171007e7e4ee457c74b312fa7f0d1808d6221dc22b192700a93ea21c4e9e04689da7dde7e1f79e9569
2 parents 027a60d + fcac802 commit 162c009

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/index/base.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,18 +253,13 @@ bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_ti
253253
return false;
254254
}
255255

256-
// In the case of a reorg, ensure persisted block locator is not stale.
256+
// Don't commit here - the committed index state must never be ahead of the
257+
// flushed chainstate, otherwise unclean restarts would lead to index corruption.
257258
// Pruning has a minimum of 288 blocks-to-keep and getting the index
258259
// out of sync may be possible but a users fault.
259260
// In case we reorg beyond the pruned depth, ReadBlock would
260261
// throw and lead to a graceful shutdown
261262
SetBestBlockIndex(new_tip);
262-
if (!Commit()) {
263-
// If commit fails, revert the best block index to avoid corruption.
264-
SetBestBlockIndex(current_tip);
265-
return false;
266-
}
267-
268263
return true;
269264
}
270265

test/functional/feature_coinstatsindex.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,21 @@ def _test_init_index_after_reorg(self):
322322
res1 = index_node.gettxoutsetinfo(hash_type='muhash', hash_or_height=None, use_index=True)
323323
assert_equal(res["muhash"], res1["muhash"])
324324

325+
self.log.info("Test index with an unclean restart after a reorg")
326+
self.restart_node(1, extra_args=self.extra_args[1])
327+
committed_height = index_node.getblockcount()
328+
self.generate(index_node, 2, sync_fun=self.no_op)
329+
self.sync_index_node()
330+
block2 = index_node.getbestblockhash()
331+
index_node.invalidateblock(block2)
332+
self.generatetoaddress(index_node, 1, getnewdestination()[2], sync_fun=self.no_op)
333+
self.sync_index_node()
334+
index_node.kill_process()
335+
self.start_node(1, extra_args=self.extra_args[1])
336+
self.sync_index_node()
337+
# Because of the unclean shutdown above, indexes reset to the point we last committed them to disk.
338+
assert_equal(index_node.getindexinfo()['coinstatsindex']['best_block_height'], committed_height)
339+
325340

326341
if __name__ == '__main__':
327342
CoinStatsIndexTest(__file__).main()

0 commit comments

Comments
 (0)