Reject headers building on invalid chains by tracking invalidity
Reject headers building on invalid chains by tracking invalidity

This tracks the set of all known invalid-themselves blocks (ie
blocks which we attempted to connect but which were found to be
invalid). This is used to cheaply check if new headers build on an
invalid chain.

While we're at it we also resolve an edge-case in invalidateblock
on pruned nodes which results in them needing a reindex if they
fail to reorg.

Github-Pull: #11531
Rebased-From: 015a5258adffb0cf394f387a95ac9c8afc34cfc3

GroestlCoin/GroestlCoin,romanornr/viacoin,Anfauglith/iop-hd,wangxinxi/litecoin,gjhiggins/vcoincore,GroestlCoin/GroestlCoin,GroestlCoin/bitcoin,Anfauglith/iop-hd,romanornr/viacoin,GroestlCoin/GroestlCoin,qtumproject/qtum,tecnovert/particl-core,qtumproject/qtum,UFOCoins/ufo,romanornr/viacoin,wangxinxi/litecoin,digibyte/digibyte,GroestlCoin/GroestlCoin,UFOCoins/ufo,BitzenyCoreDevelopers/bitzeny,Jcing95/iop-hd,BitzenyCoreDevelopers/bitzeny,BitzenyCoreDevelopers/bitzeny,particl/particl-core,tecnovert/particl-core,tecnovert/particl-core,UFOCoins/ufo,gjhiggins/vcoincore,wangxinxi/litecoin,GroestlCoin/bitcoin,Anfauglith/iop-hd,gjhiggins/vcoincore,particl/particl-core,GroestlCoin/GroestlCoin,qtumproject/qtum,Jcing95/iop-hd,Jcing95/iop-hd,tecnovert/particl-core,qtumproject/qtum,tecnovert/particl-core,wangxinxi/litecoin,digibyte/digibyte,romanornr/viacoin,gjhiggins/vcoincore,Anfauglith/iop-hd,BitzenyCoreDevelopers/bitzeny,digibyte/digibyte,GroestlCoin/bitcoin,wangxinxi/litecoin,BitzenyCoreDevelopers/bitzeny,Jcing95/iop-hd,particl/particl-core,Anfauglith/iop-hd,qtumproject/qtum,Anfauglith/iop-hd,GroestlCoin/bitcoin,BitzenyCoreDevelopers/bitzeny,particl/particl-core,romanornr/viacoin,gjhiggins/vcoincore,GroestlCoin/bitcoin,qtumproject/qtum,GroestlCoin/bitcoin,qtumproject/qtum,particl/particl-core,tecnovert/particl-core,wangxinxi/litecoin,digibyte/digibyte,GroestlCoin/GroestlCoin,UFOCoins/ufo,Jcing95/iop-hd,digibyte/digibyte,UFOCoins/ufo,particl/particl-core,Jcing95/iop-hd,romanornr/viacoin,digibyte/digibyte,gjhiggins/vcoincore,UFOCoins/ufo