There is now only one callsite for these functions, so we can just move the code where it is needed.
I deviated from the original commit by not creating the ..._helper functions. It doesn't make much sense to define local functions that are used only once and immediately after being defined.
This is a backport of core#19967 [3/3]
https://github.com/bitcoin/bitcoin/pull/19967/commits/3c7d9ab8c8ec5284cdad1a53ee310b79b931f12f