Note: I used this opportunity to reorder the function parameters and match the ones used by Core.
This is a backport of [[https://github.com/bitcoin/bitcoin/pull/21055 | core#21055]] [4/16]
https://github.com/bitcoin/bitcoin/pull/21055/commits/4bada76237d734c1de38d3bd58689caeefd5e8cb