Test is separated from the rest of the changes to make review easier.
Partial backport of Core PR14021
https://github.com/bitcoin/bitcoin/pull/14021/commits/4c75a69f36a7630342e68b12999115e6b6b1e1e0
https://github.com/bitcoin/bitcoin/pull/14021/commits/cb3511b9d5f3d8a86e090ebed5a4aeaf758430b8
Depends on D6230