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