From 50a1f63b1b7954e25c810913a3efd37cbd731cbb Mon Sep 17 00:00:00 2001 From: Larry Ruane Date: Wed, 26 May 2021 16:17:55 -0600 Subject: [PATCH] when rewinding, set pindexBestHeader to the highest-work block index --- src/main.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index ad6a969b1..2b1e12373 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5135,6 +5135,14 @@ bool RewindBlockIndex(const CChainParams& chainparams, bool& clearWitnessCaches) PruneBlockIndexCandidates(); + // Ensure that pindexBestHeader points to the block index entry with the most work; + // setBlockIndexCandidates entries are sorted by work, highest at the end. + { + std::set::reverse_iterator it = setBlockIndexCandidates.rbegin(); + assert(it != setBlockIndexCandidates.rend()); + pindexBestHeader = *it; + } + CheckBlockIndex(chainparams.GetConsensus()); if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_ALWAYS)) {