This is a partial backport of [[https://github.com/bitcoin/bitcoin/pull/22677 | core#22677]], [[https://github.com/bitcoin/bitcoin/pull/23649 | core#23649]] and [[https://github.com/bitcoin/bitcoin/pull/23683 | 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 [[https://github.com/bitcoin/bitcoin/pull/23683 | core#23683]] mostly reverts the commit from [[https://github.com/bitcoin/bitcoin/pull/22677 | core#22677]]. The remaining diff after applying both commits consists of just an additional assertion and comment.