This is a partial backport of [[https://github.com/bitcoin/bitcoin/pull/22677 | core#22677]] and [[https://github.com/bitcoin/bitcoin/pull/23649 | core#23649]]
https://github.com/bitcoin/bitcoin/pull/22677/commits/1b3a11e126b258fba975ed7c452221608f2c5472
https://github.com/bitcoin/bitcoin/pull/23649/commits/ddd74ff65c471c5c25815ac0643d16ea0d51f58f