This is a partial backport of core#22677, core#23649 and core#23683
https://github.com/bitcoin/bitcoin/pull/22677/commits/bedf246f1e2497a3641093c6e8fa11fb34dddac4
https://github.com/bitcoin/bitcoin/pull/23649/commits/b01784f0270dc20f8076ea4e46203c97b40b93ef
https://github.com/bitcoin/bitcoin/pull/23683/commits/b4adc5ad6769e4a5a6179dfff271cd4c9dc47a5b
Note that core#23683 mostly reverts the commit from core#22677. The remaining diff after applying both commits consists of just an additional assertion and comment.