bugfix: the end of a reorged chain is invalid when connect fails
Summary:
Introduced in 4e0eed88acdd41826868c151373068bfad18b84d
When an invalid block is found during a reorg, we know the last of the blocks in
the was-to-be-connected chain is invalid, but not necessarily the first. As
vpIndexToConnect is ordered in decreasing height, the end of the reorg is the
front of the vector, and not the back.This only affected the warning system.
I noticed this bug when working on D18002. Previously InvalidChainFound didn't set a failure flag on the block. After D18022, it will flag the block has having failed parents. So with this bug in p2p_unrequested_blocks.py we would flag a valid block (height 289) having a invalid child (height 291) as having failed parents. This causes a CheckBlockIndex failure down the line, as the check finds that mistakenly "failed" block 289 which is not invalid and has no invalid parents.
This is a backport of core#13185
Test Plan:
rebase D18022 onto this diff, and check that now p2p_unrequested_blocks.py passes
Check that everything else still passes.
ninja all check-all
Reviewers: #bitcoin_abc, Fabien
Reviewed By: #bitcoin_abc, Fabien
Differential Revision: https://reviews.bitcoinabc.org/D18042