Backports #35878 to 31.x.
Cherry-pick of 1278a5970d5ada0979052a5bad899e896b8ab40b. No conflicts, the diff is identical to the original commit.
Tested locally (Debian bookworm, gcc, RelWithDebInfo): p2p_tx_download.py (contains test_duplicate_tx_inv from that commit), p2p_orphan_handling.py and p2p_opportunistic_1p1c.py pass. Unit tests and the rest of the functional suite were not run.
Contributed by turingpoint.