This backports the rest of the changes that are not already squashed with other commits in the core#20833 stack. The reason they were not included with other commits is that the relevant code was already landed before I noticed this follow-up (D12101)
This is a partial backport of core#22084
https://github.com/bitcoin/bitcoin/pull/22084/commits/e8ecc621be6afd3252c0f8147e42c3b4918f7f46
Depends on D12133