This is a backport of core#21270 [8/12] and core#21525 [9/10]
https://github.com/bitcoin/bitcoin/pull/21270/commits/8a1d580b2156268e3ab30f902b3fc9aa87bd2819
https://github.com/bitcoin/bitcoin/pull/21525/commits/98c4e252f0d09bebb2e4ad3289407459c2cda5d5
Depends on D11423