Skip to content

Commit fa43567

Browse files
author
MarcoFalke
committed
test: Prefer send_and_ping over send_message+sync_with_ping
Also, add an explanation for the preference in the docs.
1 parent 698f869 commit fa43567

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

test/functional/p2p_compactblocks_blocksonly.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,16 @@ def run_test(self):
9393

9494
block1 = self.build_block_on_tip()
9595

96-
p2p_conn_blocksonly.send_message(msg_headers(headers=[CBlockHeader(block1)]))
97-
p2p_conn_blocksonly.sync_with_ping()
96+
p2p_conn_blocksonly.send_and_ping(msg_headers(headers=[CBlockHeader(block1)]))
9897
assert_equal(p2p_conn_blocksonly.last_message['getdata'].inv, [CInv(MSG_BLOCK | MSG_WITNESS_FLAG, block1.sha256)])
9998

100-
p2p_conn_high_bw.send_message(msg_headers(headers=[CBlockHeader(block1)]))
101-
p2p_conn_high_bw.sync_with_ping()
99+
p2p_conn_high_bw.send_and_ping(msg_headers(headers=[CBlockHeader(block1)]))
102100
assert_equal(p2p_conn_high_bw.last_message['getdata'].inv, [CInv(MSG_CMPCT_BLOCK, block1.sha256)])
103101

104102
self.log.info("Test that getdata(CMPCT) is still sent on BIP152 low bandwidth connections"
105103
" when no -blocksonly nodes are involved")
106104

107105
p2p_conn_low_bw.send_and_ping(msg_headers(headers=[CBlockHeader(block1)]))
108-
p2p_conn_low_bw.sync_with_ping()
109106
assert_equal(p2p_conn_low_bw.last_message['getdata'].inv, [CInv(MSG_CMPCT_BLOCK, block1.sha256)])
110107

111108
self.log.info("Test that -blocksonly nodes still serve compact blocks")

test/functional/p2p_tx_download.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,7 @@ def test_preferred_inv(self, connection_type: ConnectionType):
229229
else:
230230
peer = self.nodes[0].add_p2p_connection(TestP2PConn())
231231

232-
peer.send_message(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
233-
peer.sync_with_ping()
232+
peer.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
234233
if connection_type != ConnectionType.INBOUND:
235234
peer.wait_until(lambda: peer.tx_getdata_count >= 1, timeout=1)
236235
else:
@@ -250,17 +249,15 @@ def test_preferred_tiebreaker_inv(self):
250249
# of which is preferred and one which is not
251250
unresponsive_peer = self.nodes[0].add_outbound_p2p_connection(
252251
TestP2PConn(), wait_for_verack=True, p2p_idx=0, connection_type="outbound-full-relay")
253-
unresponsive_peer.send_message(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
254-
unresponsive_peer.sync_with_ping()
252+
unresponsive_peer.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
255253
unresponsive_peer.wait_until(lambda: unresponsive_peer.tx_getdata_count >= 1, timeout=1)
256254

257255
# A bunch of incoming (non-preferred) connections that advertise the same tx
258256
non_pref_peers = []
259257
NUM_INBOUND = 10
260258
for _ in range(NUM_INBOUND):
261259
non_pref_peers.append(self.nodes[0].add_p2p_connection(TestP2PConn()))
262-
non_pref_peers[-1].send_message(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
263-
non_pref_peers[-1].sync_with_ping()
260+
non_pref_peers[-1].send_and_ping(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
264261

265262
# Check that no request made due to in-flight
266263
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)
@@ -272,8 +269,7 @@ def test_preferred_tiebreaker_inv(self):
272269
# upon advertisement
273270
pref_peer = self.nodes[0].add_outbound_p2p_connection(
274271
TestP2PConn(), wait_for_verack=True, p2p_idx=1, connection_type="outbound-full-relay")
275-
pref_peer.send_message(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
276-
pref_peer.sync_with_ping()
272+
pref_peer.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
277273

278274
assert_equal(len(self.nodes[0].getpeerinfo()), NUM_INBOUND + 2)
279275

@@ -302,8 +298,7 @@ def test_txid_inv_delay(self, glob_wtxid=False):
302298
# Add a second wtxid-relay connection otherwise TXID_RELAY_DELAY is waived in
303299
# lack of wtxid-relay peers
304300
self.nodes[0].add_p2p_connection(TestP2PConn(wtxidrelay=True))
305-
peer.send_message(msg_inv([CInv(t=MSG_TX, h=0xff11ff11)]))
306-
peer.sync_with_ping()
301+
peer.send_and_ping(msg_inv([CInv(t=MSG_TX, h=0xff11ff11)]))
307302
with p2p_lock:
308303
assert_equal(peer.tx_getdata_count, 0 if glob_wtxid else 1)
309304
self.nodes[0].setmocktime(mock_time + TXID_RELAY_DELAY)

test/functional/test_framework/p2p.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,13 @@ def send_message(self, message, is_decoy=False):
382382
"""Send a P2P message over the socket.
383383
384384
This method takes a P2P payload, builds the P2P header and adds
385-
the message to the send buffer to be sent over the socket."""
385+
the message to the send buffer to be sent over the socket.
386+
387+
When a message does not lead to a disconnect, send_and_ping is usually
388+
preferred to send a message. This can help to reduce intermittent test
389+
failures due to a missing sync. Also, it includes a call to
390+
sync_with_ping, allowing for concise test code.
391+
"""
386392
with self._send_lock:
387393
tmsg = self.build_message(message, is_decoy)
388394
self._log_message("send", message)

0 commit comments

Comments
 (0)