This is a backport of core#19328 [2/4]
https://github.com/bitcoin/bitcoin/pull/19328/commits/a712cf6f6801157667fcf36d1c498b6fff6d328a
Backport note: D507 kept the name hash_serialized instead of using hash_serialized_2 which was introduced in core#10426
Depends on D9978