This PR fixes a bug in the headers sync logic that enables submitting headers to a nodes block index that don’t lead to a chain that surpasses our DoS limit.
The issue is that we ignore the return value on the first IsContinuationOfLowWorkHeadersSync call after a new headers sync is started, which leads to us passing headers to ProcessNewBlockHeaders when that initial IsContinuationOfLowWorkHeadersSync call returns false. One easy way (maybe the only?) to trigger this is by sending 2000 headers where the last header has a different nBits value than the prior headers (which fails the pre-sync logic here). Those 2000 headers will be passed to ProcessNewBlockHeaders.
I haven’t included a test here so far because we can’t test this without changing the default value for CRegTestParams::consensus.fPowAllowMinDifficultyBlocks or doing some more involved refactoring.