mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
test: Prefer send_and_ping over send_message+sync_with_ping
Also, add an explanation for the preference in the docs.
This commit is contained in:
parent
698f86964c
commit
fa4356717d
3 changed files with 14 additions and 16 deletions
|
@ -93,19 +93,16 @@ class P2PCompactBlocksBlocksOnly(BitcoinTestFramework):
|
||||||
|
|
||||||
block1 = self.build_block_on_tip()
|
block1 = self.build_block_on_tip()
|
||||||
|
|
||||||
p2p_conn_blocksonly.send_message(msg_headers(headers=[CBlockHeader(block1)]))
|
p2p_conn_blocksonly.send_and_ping(msg_headers(headers=[CBlockHeader(block1)]))
|
||||||
p2p_conn_blocksonly.sync_with_ping()
|
|
||||||
assert_equal(p2p_conn_blocksonly.last_message['getdata'].inv, [CInv(MSG_BLOCK | MSG_WITNESS_FLAG, block1.sha256)])
|
assert_equal(p2p_conn_blocksonly.last_message['getdata'].inv, [CInv(MSG_BLOCK | MSG_WITNESS_FLAG, block1.sha256)])
|
||||||
|
|
||||||
p2p_conn_high_bw.send_message(msg_headers(headers=[CBlockHeader(block1)]))
|
p2p_conn_high_bw.send_and_ping(msg_headers(headers=[CBlockHeader(block1)]))
|
||||||
p2p_conn_high_bw.sync_with_ping()
|
|
||||||
assert_equal(p2p_conn_high_bw.last_message['getdata'].inv, [CInv(MSG_CMPCT_BLOCK, block1.sha256)])
|
assert_equal(p2p_conn_high_bw.last_message['getdata'].inv, [CInv(MSG_CMPCT_BLOCK, block1.sha256)])
|
||||||
|
|
||||||
self.log.info("Test that getdata(CMPCT) is still sent on BIP152 low bandwidth connections"
|
self.log.info("Test that getdata(CMPCT) is still sent on BIP152 low bandwidth connections"
|
||||||
" when no -blocksonly nodes are involved")
|
" when no -blocksonly nodes are involved")
|
||||||
|
|
||||||
p2p_conn_low_bw.send_and_ping(msg_headers(headers=[CBlockHeader(block1)]))
|
p2p_conn_low_bw.send_and_ping(msg_headers(headers=[CBlockHeader(block1)]))
|
||||||
p2p_conn_low_bw.sync_with_ping()
|
|
||||||
assert_equal(p2p_conn_low_bw.last_message['getdata'].inv, [CInv(MSG_CMPCT_BLOCK, block1.sha256)])
|
assert_equal(p2p_conn_low_bw.last_message['getdata'].inv, [CInv(MSG_CMPCT_BLOCK, block1.sha256)])
|
||||||
|
|
||||||
self.log.info("Test that -blocksonly nodes still serve compact blocks")
|
self.log.info("Test that -blocksonly nodes still serve compact blocks")
|
||||||
|
|
|
@ -229,8 +229,7 @@ class TxDownloadTest(BitcoinTestFramework):
|
||||||
else:
|
else:
|
||||||
peer = self.nodes[0].add_p2p_connection(TestP2PConn())
|
peer = self.nodes[0].add_p2p_connection(TestP2PConn())
|
||||||
|
|
||||||
peer.send_message(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
|
peer.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
|
||||||
peer.sync_with_ping()
|
|
||||||
if connection_type != ConnectionType.INBOUND:
|
if connection_type != ConnectionType.INBOUND:
|
||||||
peer.wait_until(lambda: peer.tx_getdata_count >= 1, timeout=1)
|
peer.wait_until(lambda: peer.tx_getdata_count >= 1, timeout=1)
|
||||||
else:
|
else:
|
||||||
|
@ -250,8 +249,7 @@ class TxDownloadTest(BitcoinTestFramework):
|
||||||
# of which is preferred and one which is not
|
# of which is preferred and one which is not
|
||||||
unresponsive_peer = self.nodes[0].add_outbound_p2p_connection(
|
unresponsive_peer = self.nodes[0].add_outbound_p2p_connection(
|
||||||
TestP2PConn(), wait_for_verack=True, p2p_idx=0, connection_type="outbound-full-relay")
|
TestP2PConn(), wait_for_verack=True, p2p_idx=0, connection_type="outbound-full-relay")
|
||||||
unresponsive_peer.send_message(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
|
unresponsive_peer.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
|
||||||
unresponsive_peer.sync_with_ping()
|
|
||||||
unresponsive_peer.wait_until(lambda: unresponsive_peer.tx_getdata_count >= 1, timeout=1)
|
unresponsive_peer.wait_until(lambda: unresponsive_peer.tx_getdata_count >= 1, timeout=1)
|
||||||
|
|
||||||
# A bunch of incoming (non-preferred) connections that advertise the same tx
|
# A bunch of incoming (non-preferred) connections that advertise the same tx
|
||||||
|
@ -259,8 +257,7 @@ class TxDownloadTest(BitcoinTestFramework):
|
||||||
NUM_INBOUND = 10
|
NUM_INBOUND = 10
|
||||||
for _ in range(NUM_INBOUND):
|
for _ in range(NUM_INBOUND):
|
||||||
non_pref_peers.append(self.nodes[0].add_p2p_connection(TestP2PConn()))
|
non_pref_peers.append(self.nodes[0].add_p2p_connection(TestP2PConn()))
|
||||||
non_pref_peers[-1].send_message(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
|
non_pref_peers[-1].send_and_ping(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
|
||||||
non_pref_peers[-1].sync_with_ping()
|
|
||||||
|
|
||||||
# Check that no request made due to in-flight
|
# Check that no request made due to in-flight
|
||||||
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)
|
self.nodes[0].bumpmocktime(NONPREF_PEER_TX_DELAY)
|
||||||
|
@ -272,8 +269,7 @@ class TxDownloadTest(BitcoinTestFramework):
|
||||||
# upon advertisement
|
# upon advertisement
|
||||||
pref_peer = self.nodes[0].add_outbound_p2p_connection(
|
pref_peer = self.nodes[0].add_outbound_p2p_connection(
|
||||||
TestP2PConn(), wait_for_verack=True, p2p_idx=1, connection_type="outbound-full-relay")
|
TestP2PConn(), wait_for_verack=True, p2p_idx=1, connection_type="outbound-full-relay")
|
||||||
pref_peer.send_message(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
|
pref_peer.send_and_ping(msg_inv([CInv(t=MSG_WTX, h=0xff00ff00)]))
|
||||||
pref_peer.sync_with_ping()
|
|
||||||
|
|
||||||
assert_equal(len(self.nodes[0].getpeerinfo()), NUM_INBOUND + 2)
|
assert_equal(len(self.nodes[0].getpeerinfo()), NUM_INBOUND + 2)
|
||||||
|
|
||||||
|
@ -302,8 +298,7 @@ class TxDownloadTest(BitcoinTestFramework):
|
||||||
# Add a second wtxid-relay connection otherwise TXID_RELAY_DELAY is waived in
|
# Add a second wtxid-relay connection otherwise TXID_RELAY_DELAY is waived in
|
||||||
# lack of wtxid-relay peers
|
# lack of wtxid-relay peers
|
||||||
self.nodes[0].add_p2p_connection(TestP2PConn(wtxidrelay=True))
|
self.nodes[0].add_p2p_connection(TestP2PConn(wtxidrelay=True))
|
||||||
peer.send_message(msg_inv([CInv(t=MSG_TX, h=0xff11ff11)]))
|
peer.send_and_ping(msg_inv([CInv(t=MSG_TX, h=0xff11ff11)]))
|
||||||
peer.sync_with_ping()
|
|
||||||
with p2p_lock:
|
with p2p_lock:
|
||||||
assert_equal(peer.tx_getdata_count, 0 if glob_wtxid else 1)
|
assert_equal(peer.tx_getdata_count, 0 if glob_wtxid else 1)
|
||||||
self.nodes[0].setmocktime(mock_time + TXID_RELAY_DELAY)
|
self.nodes[0].setmocktime(mock_time + TXID_RELAY_DELAY)
|
||||||
|
|
|
@ -382,7 +382,13 @@ class P2PConnection(asyncio.Protocol):
|
||||||
"""Send a P2P message over the socket.
|
"""Send a P2P message over the socket.
|
||||||
|
|
||||||
This method takes a P2P payload, builds the P2P header and adds
|
This method takes a P2P payload, builds the P2P header and adds
|
||||||
the message to the send buffer to be sent over the socket."""
|
the message to the send buffer to be sent over the socket.
|
||||||
|
|
||||||
|
When a message does not lead to a disconnect, send_and_ping is usually
|
||||||
|
preferred to send a message. This can help to reduce intermittent test
|
||||||
|
failures due to a missing sync. Also, it includes a call to
|
||||||
|
sync_with_ping, allowing for concise test code.
|
||||||
|
"""
|
||||||
with self._send_lock:
|
with self._send_lock:
|
||||||
tmsg = self.build_message(message, is_decoy)
|
tmsg = self.build_message(message, is_decoy)
|
||||||
self._log_message("send", message)
|
self._log_message("send", message)
|
||||||
|
|
Loading…
Add table
Reference in a new issue