Completes backport (8/8) of core PR19326:
https://github.com/bitcoin/bitcoin/pull/19326/commits/77c507358bda9bd6c496f33e0f4418c0603bb08d
Depends on D9153.
There is a missing dependency for the `walletdb.cpp` change for which I
left a comment to make the later backport easier.
This diff also contains a few changes which are specific to our repo.