This is a backport of core#21244 [7/8]
https://github.com/bitcoin/bitcoin/pull/21244/commits/b4190eff72c00e384ad238f9c2f10c8b91be969b
Note: The few minor difference from Core are explained by D4495 (Range-based for loop) and D7579 (blockdb.cpp)
Depends on D10755