This concludes backport of core#21767 [4 & 5/5]
https://github.com/bitcoin/bitcoin/pull/21767/commits/db33cde80fff749c6adff9e91fca5f27f4bb6278
https://github.com/bitcoin/bitcoin/pull/21767/commits/7a799c9c2b8652e780d1fd5e1bf7d05b026c1c1a
Note: the second commit is a trivial improvement of the first one.