Skip to content

Commit ca05b28

Browse files
committed
Merge bitcoin#31859: test: Rename send_message to send_without_ping
fa9cf38 scripted-diff: test: Rename send_message to send_without_ping (MarcoFalke) fa43567 test: Prefer send_and_ping over send_message+sync_with_ping (MarcoFalke) Pull request description: `send_message` is problematic, because it is easy to forget a `sync_with_ping` (or other `wait_until`), leading to intermittent test failures. (Example: bitcoin#31837 (comment)) There are more uses of `send_and_ping` in the codebase than `send_message`, so in most cases `send_and_ping` is needed anyway. For the remaining cases, clearly document that no sync happens by renaming `send_message` to `send_without_ping`. ACKs for top commit: instagibbs: ACK bitcoin@fa9cf38 Tree-SHA512: 31caa6568d292ae3d3dda931a94aaa30cc1205ec2ef537a484393eb55687f86c212f1e751ac4a7636610bdf591502a50995dc63bf02f97be9fdc482072160b07
2 parents ab2df17 + fa9cf38 commit ca05b28

35 files changed

+159
-161
lines changed

test/functional/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ way is the use the `profile_with_perf` context manager, e.g.
183183
with node.profile_with_perf("send-big-msgs"):
184184
# Perform activity on the node you're interested in profiling, e.g.:
185185
for _ in range(10000):
186-
node.p2ps[0].send_message(some_large_message)
186+
node.p2ps[0].send_without_ping(some_large_message)
187187
```
188188

189189
To see useful textual output, run

test/functional/example_test.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def run_test(self):
184184
block.solve()
185185
block_message = msg_block(block)
186186
# Send message is used to send a P2P message to the node over our P2PInterface
187-
peer_messaging.send_message(block_message)
187+
peer_messaging.send_without_ping(block_message)
188188
self.tip = block.sha256
189189
blocks.append(self.tip)
190190
self.block_time += 1
@@ -209,7 +209,7 @@ def run_test(self):
209209
getdata_request = msg_getdata()
210210
for block in blocks:
211211
getdata_request.inv.append(CInv(MSG_BLOCK, block))
212-
peer_receiving.send_message(getdata_request)
212+
peer_receiving.send_without_ping(getdata_request)
213213

214214
# wait_until() will loop until a predicate condition is met. Use it to test properties of the
215215
# P2PInterface objects.

test/functional/feature_assumeutxo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def test_sync_from_assumeutxo_node(self, snapshot):
309309
msg = msg_headers()
310310
for block_num in range(1, miner.getblockcount()+1):
311311
msg.headers.append(from_hex(CBlockHeader(), miner.getblockheader(miner.getblockhash(block_num), verbose=False)))
312-
headers_provider_conn.send_message(msg)
312+
headers_provider_conn.send_without_ping(msg)
313313

314314
# Ensure headers arrived
315315
default_value = {'status': ''} # No status

test/functional/feature_assumevalid.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class BaseNode(P2PInterface):
5858
def send_header_for_blocks(self, new_blocks):
5959
headers_message = msg_headers()
6060
headers_message.headers = [CBlockHeader(b) for b in new_blocks]
61-
self.send_message(headers_message)
61+
self.send_without_ping(headers_message)
6262

6363

6464
class AssumeValidTest(BitcoinTestFramework):
@@ -80,7 +80,7 @@ def send_blocks_until_disconnected(self, p2p_conn):
8080
if not p2p_conn.is_connected:
8181
break
8282
try:
83-
p2p_conn.send_message(msg_block(self.blocks[i]))
83+
p2p_conn.send_without_ping(msg_block(self.blocks[i]))
8484
except IOError:
8585
assert not p2p_conn.is_connected
8686
break
@@ -157,7 +157,7 @@ def run_test(self):
157157

158158
# Send all blocks to node1. All blocks will be accepted.
159159
for i in range(2202):
160-
p2p1.send_message(msg_block(self.blocks[i]))
160+
p2p1.send_without_ping(msg_block(self.blocks[i]))
161161
# Syncing 2200 blocks can take a while on slow systems. Give it plenty of time to sync.
162162
p2p1.sync_with_ping(timeout=960)
163163
assert_equal(self.nodes[1].getblock(self.nodes[1].getbestblockhash())['height'], 2202)

test/functional/feature_maxuploadtarget.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def run_test(self):
124124
# the test has been running so far).
125125
with self.nodes[0].assert_debug_log(expected_msgs=["historical block serving limit reached, disconnecting peer=0"]):
126126
for _ in range(3):
127-
p2p_conns[0].send_message(getdata_request)
127+
p2p_conns[0].send_without_ping(getdata_request)
128128
p2p_conns[0].wait_for_disconnect()
129129
assert_equal(len(self.nodes[0].getpeerinfo()), 2)
130130
self.log.info("Peer 0 disconnected after downloading old block too many times")
@@ -148,7 +148,7 @@ def run_test(self):
148148
# But if p2p_conns[1] tries for an old block, it gets disconnected too.
149149
getdata_request.inv = [CInv(MSG_BLOCK, big_old_block)]
150150
with self.nodes[0].assert_debug_log(expected_msgs=["historical block serving limit reached, disconnecting peer=1"]):
151-
p2p_conns[1].send_message(getdata_request)
151+
p2p_conns[1].send_without_ping(getdata_request)
152152
p2p_conns[1].wait_for_disconnect()
153153
assert_equal(len(self.nodes[0].getpeerinfo()), 1)
154154

@@ -198,7 +198,7 @@ def run_test(self):
198198

199199
self.log.info("Peer gets disconnected for a mempool request after limit is reached")
200200
with self.nodes[0].assert_debug_log(expected_msgs=["mempool request with bandwidth limit reached, disconnecting peer=0"]):
201-
peer.send_message(msg_mempool())
201+
peer.send_without_ping(msg_mempool())
202202
peer.wait_for_disconnect()
203203

204204
self.log.info("Test passing an unparsable value to -maxuploadtarget throws an error")

test/functional/feature_versionbits_warning.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def send_blocks_with_version(self, peer, numblocks, version):
4747
for _ in range(numblocks):
4848
block = create_block(tip, create_coinbase(height + 1), block_time, version=version)
4949
block.solve()
50-
peer.send_message(msg_block(block))
50+
peer.send_without_ping(msg_block(block))
5151
block_time += 1
5252
height += 1
5353
tip = block.sha256

test/functional/interface_usdt_net.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ def handle_misbehaving_connection(_, data, __):
473473
for _ in range(EXPECTED_MISBEHAVING_CONNECTIONS):
474474
testnode = P2PInterface()
475475
self.nodes[0].add_p2p_connection(testnode)
476-
testnode.send_message(msg)
476+
testnode.send_without_ping(msg)
477477
bpf.perf_buffer_poll(timeout=500)
478478
testnode.peer_disconnect()
479479

test/functional/p2p_addr_relay.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def addr_received(self):
7676

7777
def on_version(self, message):
7878
self.send_version()
79-
self.send_message(msg_verack())
79+
self.send_without_ping(msg_verack())
8080
if (self.send_getaddr):
81-
self.send_message(msg_getaddr())
81+
self.send_without_ping(msg_getaddr())
8282

8383
def getaddr_received(self):
8484
return self.message_count['getaddr'] > 0
@@ -142,7 +142,7 @@ def oversized_addr_test(self):
142142

143143
msg = self.setup_addr_msg(1010)
144144
with self.nodes[0].assert_debug_log(['addr message size = 1010']):
145-
addr_source.send_message(msg)
145+
addr_source.send_without_ping(msg)
146146
addr_source.wait_for_disconnect()
147147

148148
self.nodes[0].disconnect_p2ps()

test/functional/p2p_addrfetch.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def run_test(self):
6262

6363
self.log.info("Check that answering with larger addr messages leads to disconnect")
6464
msg.addrs = [ADDR] * 2
65-
peer.send_message(msg)
65+
peer.send_without_ping(msg)
6666
peer.wait_for_disconnect(timeout=5)
6767

6868
self.log.info("Check timeout for addr-fetch peer that does not send addrs")

test/functional/p2p_addrv2_relay.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def run_test(self):
7979
self.log.info('Check disconnection when sending sendaddrv2 after verack')
8080
conn = self.nodes[0].add_p2p_connection(P2PInterface())
8181
with self.nodes[0].assert_debug_log(['sendaddrv2 received after verack, disconnecting peer=0']):
82-
conn.send_message(msg_sendaddrv2())
82+
conn.send_without_ping(msg_sendaddrv2())
8383
conn.wait_for_disconnect()
8484

8585
self.log.info('Create connection that sends addrv2 messages')
@@ -104,7 +104,7 @@ def run_test(self):
104104
self.log.info('Send too-large addrv2 message')
105105
msg.addrs = ADDRS * 101
106106
with self.nodes[0].assert_debug_log(['addrv2 message size = 1010']):
107-
addr_source.send_message(msg)
107+
addr_source.send_without_ping(msg)
108108
addr_source.wait_for_disconnect()
109109

110110

test/functional/p2p_blockfilters.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def run_test(self):
212212
for request in requests:
213213
peer_1 = self.nodes[1].add_p2p_connection(P2PInterface())
214214
with self.nodes[1].assert_debug_log(expected_msgs=["requested unsupported block filter type"]):
215-
peer_1.send_message(request)
215+
peer_1.send_without_ping(request)
216216
peer_1.wait_for_disconnect()
217217

218218
self.log.info("Check that invalid requests result in disconnection.")
@@ -259,7 +259,7 @@ def run_test(self):
259259
for request, expected_log_msg in requests:
260260
peer_0 = self.nodes[0].add_p2p_connection(P2PInterface())
261261
with self.nodes[0].assert_debug_log(expected_msgs=[expected_log_msg]):
262-
peer_0.send_message(request)
262+
peer_0.send_without_ping(request)
263263
peer_0.wait_for_disconnect()
264264

265265
self.log.info("Test -peerblockfilters without -blockfilterindex raises an error")

test/functional/p2p_blocksonly.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def blocksonly_mode_tests(self):
3434
self.log.info('Check that tx invs also violate the protocol')
3535
self.nodes[0].add_p2p_connection(P2PInterface())
3636
with self.nodes[0].assert_debug_log(['transaction (0000000000000000000000000000000000000000000000000000000000001234) inv sent in violation of protocol, disconnecting peer']):
37-
self.nodes[0].p2ps[0].send_message(msg_inv([CInv(t=MSG_WTX, h=0x1234)]))
37+
self.nodes[0].p2ps[0].send_without_ping(msg_inv([CInv(t=MSG_WTX, h=0x1234)]))
3838
self.nodes[0].p2ps[0].wait_for_disconnect()
3939
del self.nodes[0].p2ps[0]
4040

@@ -68,7 +68,7 @@ def blocksonly_mode_tests(self):
6868
# But if, for some reason, first_peer decides to relay transactions to us anyway, we should relay them to
6969
# second_peer since we gave relay permission to first_peer.
7070
# See https://github.com/bitcoin/bitcoin/issues/19943 for details.
71-
first_peer.send_message(msg_tx(tx))
71+
first_peer.send_without_ping(msg_tx(tx))
7272
self.log.info('Check that the peer with relay-permission is still connected after sending the transaction')
7373
assert_equal(first_peer.is_connected, True)
7474
second_peer.wait_for_tx(txid)
@@ -107,7 +107,7 @@ def blocks_relay_conn_tests(self):
107107
def check_p2p_inv_violation(self, peer):
108108
self.log.info("Check that tx-invs from P2P are rejected and result in disconnect")
109109
with self.nodes[0].assert_debug_log(["inv sent in violation of protocol, disconnecting peer"]):
110-
peer.send_message(msg_inv([CInv(t=MSG_WTX, h=0x12345)]))
110+
peer.send_without_ping(msg_inv([CInv(t=MSG_WTX, h=0x12345)]))
111111
peer.wait_for_disconnect()
112112
self.nodes[0].disconnect_p2ps()
113113

@@ -116,7 +116,7 @@ def check_p2p_tx_violation(self):
116116
spendtx = self.miniwallet.create_self_transfer()
117117

118118
with self.nodes[0].assert_debug_log(['transaction sent in violation of protocol, disconnecting peer=0']):
119-
self.nodes[0].p2ps[0].send_message(msg_tx(spendtx['tx']))
119+
self.nodes[0].p2ps[0].send_without_ping(msg_tx(spendtx['tx']))
120120
self.nodes[0].p2ps[0].wait_for_disconnect()
121121
assert_equal(self.nodes[0].getmempoolinfo()['size'], 0)
122122
self.nodes[0].disconnect_p2ps()

test/functional/p2p_compactblocks.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ def get_headers(self, locator, hashstop):
113113
msg = msg_getheaders()
114114
msg.locator.vHave = locator
115115
msg.hashstop = hashstop
116-
self.send_message(msg)
116+
self.send_without_ping(msg)
117117

118118
def send_header_for_blocks(self, new_blocks):
119119
headers_message = msg_headers()
120120
headers_message.headers = [CBlockHeader(b) for b in new_blocks]
121-
self.send_message(headers_message)
121+
self.send_without_ping(headers_message)
122122

123123
def request_headers_and_sync(self, locator, hashstop=0):
124124
self.clear_block_announcement()
@@ -138,7 +138,7 @@ def send_await_disconnect(self, message, timeout=30):
138138
139139
This is used when we want to send a message into the node that we expect
140140
will get us disconnected, eg an invalid block."""
141-
self.send_message(message)
141+
self.send_without_ping(message)
142142
self.wait_for_disconnect(timeout=timeout)
143143

144144
class CompactBlocksTest(BitcoinTestFramework):
@@ -325,7 +325,7 @@ def test_compactblock_construction(self, test_node):
325325
# Now fetch the compact block using a normal non-announce getdata
326326
test_node.clear_block_announcement()
327327
inv = CInv(MSG_CMPCT_BLOCK, block_hash)
328-
test_node.send_message(msg_getdata([inv]))
328+
test_node.send_without_ping(msg_getdata([inv]))
329329

330330
test_node.wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30)
331331

@@ -386,7 +386,7 @@ def test_compactblock_requests(self, test_node):
386386
block = self.build_block_on_tip(node)
387387

388388
if announce == "inv":
389-
test_node.send_message(msg_inv([CInv(MSG_BLOCK, block.sha256)]))
389+
test_node.send_without_ping(msg_inv([CInv(MSG_BLOCK, block.sha256)]))
390390
test_node.wait_for_getheaders(timeout=30)
391391
test_node.send_header_for_blocks([block])
392392
else:
@@ -497,7 +497,7 @@ def test_tip_after_message(node, peer, msg, tip):
497497
block = self.build_block_with_transactions(node, utxo, 10)
498498
self.utxos.append([block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue])
499499
for tx in block.vtx[1:]:
500-
test_node.send_message(msg_tx(tx))
500+
test_node.send_without_ping(msg_tx(tx))
501501
test_node.sync_with_ping()
502502
# Make sure all transactions were accepted.
503503
mempool = node.getrawmempool()
@@ -525,7 +525,7 @@ def test_incorrect_blocktxn_response(self, test_node):
525525
self.utxos.append([block.vtx[-1].sha256, 0, block.vtx[-1].vout[0].nValue])
526526
# Relay the first 5 transactions from the block in advance
527527
for tx in block.vtx[1:6]:
528-
test_node.send_message(msg_tx(tx))
528+
test_node.send_without_ping(msg_tx(tx))
529529
test_node.sync_with_ping()
530530
# Make sure all transactions were accepted.
531531
mempool = node.getrawmempool()
@@ -581,7 +581,7 @@ def test_getblocktxn_handler(self, test_node):
581581
msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [])
582582
num_to_request = random.randint(1, len(block.vtx))
583583
msg.block_txn_request.from_absolute(sorted(random.sample(range(len(block.vtx)), num_to_request)))
584-
test_node.send_message(msg)
584+
test_node.send_without_ping(msg)
585585
test_node.wait_until(lambda: "blocktxn" in test_node.last_message, timeout=10)
586586

587587
[tx.calc_sha256() for tx in block.vtx]
@@ -616,7 +616,7 @@ def test_getblocktxn_handler(self, test_node):
616616
block = from_hex(CBlock(), node.getblock(block_hash, False))
617617
msg.block_txn_request = BlockTransactionsRequest(int(block_hash, 16), [len(block.vtx)])
618618
with node.assert_debug_log(['getblocktxn with out-of-bounds tx indices']):
619-
bad_peer.send_message(msg)
619+
bad_peer.send_without_ping(msg)
620620
bad_peer.wait_for_disconnect()
621621

622622
def test_low_work_compactblocks(self, test_node):
@@ -651,7 +651,7 @@ def test_compactblocks_not_at_tip(self, test_node):
651651
test_node.wait_until(test_node.received_block_announcement, timeout=30)
652652

653653
test_node.clear_block_announcement()
654-
test_node.send_message(msg_getdata([CInv(MSG_CMPCT_BLOCK, int(new_blocks[0], 16))]))
654+
test_node.send_without_ping(msg_getdata([CInv(MSG_CMPCT_BLOCK, int(new_blocks[0], 16))]))
655655
test_node.wait_until(lambda: "cmpctblock" in test_node.last_message, timeout=30)
656656

657657
test_node.clear_block_announcement()
@@ -660,7 +660,7 @@ def test_compactblocks_not_at_tip(self, test_node):
660660
test_node.clear_block_announcement()
661661
with p2p_lock:
662662
test_node.last_message.pop("block", None)
663-
test_node.send_message(msg_getdata([CInv(MSG_CMPCT_BLOCK, int(new_blocks[0], 16))]))
663+
test_node.send_without_ping(msg_getdata([CInv(MSG_CMPCT_BLOCK, int(new_blocks[0], 16))]))
664664
test_node.wait_until(lambda: "block" in test_node.last_message, timeout=30)
665665
with p2p_lock:
666666
test_node.last_message["block"].block.calc_sha256()
@@ -768,7 +768,7 @@ def announce_cmpct_block(node, peer):
768768
block, cmpct_block = announce_cmpct_block(node, stalling_peer)
769769

770770
for tx in block.vtx[1:]:
771-
delivery_peer.send_message(msg_tx(tx))
771+
delivery_peer.send_without_ping(msg_tx(tx))
772772
delivery_peer.sync_with_ping()
773773
mempool = node.getrawmempool()
774774
for tx in block.vtx[1:]:
@@ -783,7 +783,7 @@ def announce_cmpct_block(node, peer):
783783

784784
block, cmpct_block = announce_cmpct_block(node, stalling_peer)
785785
for tx in block.vtx[1:]:
786-
delivery_peer.send_message(msg_tx(tx))
786+
delivery_peer.send_without_ping(msg_tx(tx))
787787
delivery_peer.sync_with_ping()
788788

789789
cmpct_block.prefilled_txn[0].tx.wit.vtxinwit = [CTxInWitness()]

test/functional/p2p_compactblocks_blocksonly.py

+3-6
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")
@@ -115,7 +112,7 @@ def test_for_cmpctblock(block):
115112
return False
116113
return p2p_conn_blocksonly.last_message['cmpctblock'].header_and_shortids.header.rehash() == block.sha256
117114

118-
p2p_conn_blocksonly.send_message(msg_getdata([CInv(MSG_CMPCT_BLOCK, block0.sha256)]))
115+
p2p_conn_blocksonly.send_without_ping(msg_getdata([CInv(MSG_CMPCT_BLOCK, block0.sha256)]))
119116
p2p_conn_blocksonly.wait_until(lambda: test_for_cmpctblock(block0))
120117

121118
# Request BIP152 high bandwidth mode from the -blocksonly node.

test/functional/p2p_eviction.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434
class SlowP2PDataStore(P2PDataStore):
3535
def on_ping(self, message):
3636
time.sleep(0.1)
37-
self.send_message(msg_pong(message.nonce))
37+
self.send_without_ping(msg_pong(message.nonce))
3838

3939

4040
class SlowP2PInterface(P2PInterface):
4141
def on_ping(self, message):
4242
time.sleep(0.1)
43-
self.send_message(msg_pong(message.nonce))
43+
self.send_without_ping(msg_pong(message.nonce))
4444

4545

4646
class P2PEvict(BitcoinTestFramework):
@@ -82,7 +82,7 @@ def run_test(self):
8282
txpeer.sync_with_ping()
8383

8484
tx = self.wallet.create_self_transfer()['tx']
85-
txpeer.send_message(msg_tx(tx))
85+
txpeer.send_without_ping(msg_tx(tx))
8686
protected_peers.add(current_peer)
8787

8888
self.log.info("Create 8 peers and protect them from eviction by having faster pings")

0 commit comments

Comments
 (0)