Partial backport of Core PR14119
https://github.com/bitcoin/bitcoin/pull/14119/files#diff-a22114218da7fa06c1bcd16d75c29bc7
Depends on D3862
Reviewer note: The endSome parts of the test differs due to changes done in D521. I left out the forced single-threaded validation since it's no longer relevant.