Partial backport of Core PR14119
https://github.com/bitcoin/bitcoin/pull/14119/files
Rationale for partially completing this out-of-order backport:
* We get the benefits of this change today with minimal negative impact on future backporting efforts.
* The original PR is a bit large and could do with splitting up the review.
Only two additional lines of code are necessary to make this backport digestible in chunks (and then all TODOs can be removed once completed).
Depends on D3860, D3861