The no-immediate-response check in p2p_orphan_handling.py currently can't fail and thus doesn't test anything: assert_no_immediate_response saves self.last_message before and after send_and_ping, but both names refer to the same dict, which on_message updates in place. Fix it by copying the dict before sending. The second copy isn't strictly needed, since nothing changes the dict after the ping returns, but it's added so both snapshots are taken the same way.
This affects both call sites, including the check that the node doesn't serve a not-yet-announced parent to a spy peer.
To check the helper itself, apply the subtest below (e.g. with git apply). An outbound peer gets an immediate getdata for an announced tx, which the helper should detect. On master it fails with "No exception raised"; with this PR it passes:
<details> <summary>Patch</summary>
diff --git a/test/functional/p2p_orphan_handling.py b/test/functional/p2p_orphan_handling.py
--- a/test/functional/p2p_orphan_handling.py
+++ b/test/functional/p2p_orphan_handling.py
@@ -33,6 +33,7 @@ from test_framework.p2p import (
TXID_RELAY_DELAY,
)
from test_framework.util import (
+ assert_raises,
assert_not_equal,
assert_equal,
)
@@ -139,6 +140,14 @@ class OrphanHandlingTest(BitcoinTestFramework):
peer.wait_for_getdata([wtxid])
peer.send_and_ping(msg_tx(tx))
+ [@cleanup](/bitcoin-bitcoin/contributor/cleanup/)
+ def test_no_immediate_response_helper(self):
+ self.log.info("Check that assert_no_immediate_response detects an immediate response")
+ # Outbound peers are preferred, so an announced wtxid is requested without delay
+ peer = self.nodes[0].add_outbound_p2p_connection(PeerTxRelayer(), p2p_idx=0)
+ assert_raises(AssertionError, peer.assert_no_immediate_response, msg_inv([CInv(t=MSG_WTX, h=1)]))
+ assert_equal(peer.message_count["getdata"], 1)
+ [@cleanup](/bitcoin-bitcoin/contributor/cleanup/)
def test_arrival_timing_orphan(self):
self.log.info("Test missing parents that arrive during delay are not requested")
@@ -883,6 +892,7 @@ class OrphanHandlingTest(BitcoinTestFramework):
self.wallet = MiniWallet(self.nodes[0])
self.generate(self.wallet, 160)
+ self.test_no_immediate_response_helper()
self.test_arrival_timing_orphan()
self.test_orphan_rejected_parents_exceptions()
self.test_orphan_multiple_parents()
</details>
I'm not sure it makes sense to add a test for a test helper, so this isn't part of the PR for now. Happy to add it as a separate commit if reviewers think it's worth it.