This is a backport of core#19077 [3/26]
https://github.com/bitcoin/bitcoin/pull/19077/commits/7577b6e1c88a1a7b45ecf5c7f1735bae6f5a82bf
Depends on D10472
Note: I had to comment out m_mock to avoid triggering compiler warnings about unused variables.