Skip to content

Commit 6112a20

Browse files
committed
test: replace (send_message + sync_with_ping) with send_and_ping
1 parent b97e3a7 commit 6112a20

8 files changed

+27
-56
lines changed

test/functional/feature_maxuploadtarget.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ def run_test(self):
9494
# 576MB will be reserved for relaying new blocks, so expect this to
9595
# succeed for ~235 tries.
9696
for i in range(success_count):
97-
p2p_conns[0].send_message(getdata_request)
98-
p2p_conns[0].sync_with_ping()
97+
p2p_conns[0].send_and_ping(getdata_request)
9998
assert_equal(p2p_conns[0].block_receive_map[big_old_block], i+1)
10099

101100
assert_equal(len(self.nodes[0].getpeerinfo()), 3)
@@ -112,8 +111,7 @@ def run_test(self):
112111
# We'll try 800 times
113112
getdata_request.inv = [CInv(2, big_new_block)]
114113
for i in range(800):
115-
p2p_conns[1].send_message(getdata_request)
116-
p2p_conns[1].sync_with_ping()
114+
p2p_conns[1].send_and_ping(getdata_request)
117115
assert_equal(p2p_conns[1].block_receive_map[big_new_block], i+1)
118116

119117
self.log.info("Peer 1 able to repeatedly download new block")
@@ -132,8 +130,7 @@ def run_test(self):
132130
# and p2p_conns[2] should be able to retrieve the old block.
133131
self.nodes[0].setmocktime(int(time.time()))
134132
p2p_conns[2].sync_with_ping()
135-
p2p_conns[2].send_message(getdata_request)
136-
p2p_conns[2].sync_with_ping()
133+
p2p_conns[2].send_and_ping(getdata_request)
137134
assert_equal(p2p_conns[2].block_receive_map[big_old_block], 1)
138135

139136
self.log.info("Peer 2 able to download old block")
@@ -150,8 +147,7 @@ def run_test(self):
150147
#retrieve 20 blocks which should be enough to break the 1MB limit
151148
getdata_request.inv = [CInv(2, big_new_block)]
152149
for i in range(20):
153-
self.nodes[0].p2p.send_message(getdata_request)
154-
self.nodes[0].p2p.sync_with_ping()
150+
self.nodes[0].p2p.send_and_ping(getdata_request)
155151
assert_equal(self.nodes[0].p2p.block_receive_map[big_new_block], i+1)
156152

157153
getdata_request.inv = [CInv(2, big_old_block)]

test/functional/p2p_filter.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ def skip_test_if_missing_module(self):
6464
def run_test(self):
6565
self.log.info('Add filtered P2P connection to the node')
6666
filter_node = self.nodes[0].add_p2p_connection(FilterNode())
67-
filter_node.send_message(filter_node.watch_filter_init)
68-
filter_node.sync_with_ping()
67+
filter_node.send_and_ping(filter_node.watch_filter_init)
6968
filter_address = self.nodes[0].decodescript(filter_node.watch_script_pubkey)['addresses'][0]
7069

7170
self.log.info('Check that we receive merkleblock and tx if the filter matches a tx in a block')
@@ -99,8 +98,7 @@ def run_test(self):
9998
assert not filter_node.merkleblock_received
10099

101100
self.log.info('Check that after deleting filter all txs get relayed again')
102-
filter_node.send_message(msg_filterclear())
103-
filter_node.sync_with_ping()
101+
filter_node.send_and_ping(msg_filterclear())
104102
for _ in range(5):
105103
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 7)
106104
filter_node.wait_for_tx(txid)

test/functional/p2p_leak_tx.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ def run_test(self):
3939
want_tx = msg_getdata()
4040
want_tx.inv.append(CInv(t=1, h=int(txid, 16)))
4141
inbound_peer.last_message.pop('notfound', None)
42-
inbound_peer.send_message(want_tx)
43-
inbound_peer.sync_with_ping()
42+
inbound_peer.send_and_ping(want_tx)
4443

4544
if inbound_peer.last_message.get('notfound'):
4645
self.log.debug('tx {} was not yet announced to us.'.format(txid))

test/functional/p2p_segwit.py

+5-10
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,7 @@ def test_non_witness_transaction(self):
303303

304304
block = self.build_next_block(version=1)
305305
block.solve()
306-
self.test_node.send_message(msg_no_witness_block(block))
307-
self.test_node.sync_with_ping() # make sure the block was processed
306+
self.test_node.send_and_ping(msg_no_witness_block(block)) # make sure the block was processed
308307
txid = block.vtx[0].sha256
309308

310309
self.nodes[0].generate(99) # let the block mature
@@ -319,8 +318,7 @@ def test_non_witness_transaction(self):
319318
# This is a sanity check of our testing framework.
320319
assert_equal(msg_no_witness_tx(tx).serialize(), msg_tx(tx).serialize())
321320

322-
self.test_node.send_message(msg_tx(tx))
323-
self.test_node.sync_with_ping() # make sure the tx was processed
321+
self.test_node.send_and_ping(msg_tx(tx)) # make sure the block was processed
324322
assert tx.hash in self.nodes[0].getrawmempool()
325323
# Save this transaction for later
326324
self.utxo.append(UTXO(tx.sha256, 0, 49 * 100000000))
@@ -350,8 +348,7 @@ def test_unnecessary_witness_before_segwit_activation(self):
350348

351349
# But it should not be permanently marked bad...
352350
# Resend without witness information.
353-
self.test_node.send_message(msg_no_witness_block(block))
354-
self.test_node.sync_with_ping()
351+
self.test_node.send_and_ping(msg_no_witness_block(block)) # make sure the block was processed
355352
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
356353

357354
# Update our utxo list; we spent the first entry.
@@ -2054,16 +2051,14 @@ def serialize(self):
20542051
tx = FromHex(CTransaction(), raw)
20552052
assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, serialize_with_bogus_witness(tx).hex())
20562053
with self.nodes[0].assert_debug_log(['Superfluous witness record']):
2057-
self.nodes[0].p2p.send_message(msg_bogus_tx(tx))
2058-
self.nodes[0].p2p.sync_with_ping()
2054+
self.nodes[0].p2p.send_and_ping(msg_bogus_tx(tx))
20592055
raw = self.nodes[0].signrawtransactionwithwallet(raw)
20602056
assert raw['complete']
20612057
raw = raw['hex']
20622058
tx = FromHex(CTransaction(), raw)
20632059
assert_raises_rpc_error(-22, "TX decode failed", self.nodes[0].decoderawtransaction, serialize_with_bogus_witness(tx).hex())
20642060
with self.nodes[0].assert_debug_log(['Unknown transaction optional data']):
2065-
self.nodes[0].p2p.send_message(msg_bogus_tx(tx))
2066-
self.nodes[0].p2p.sync_with_ping()
2061+
self.nodes[0].p2p.send_and_ping(msg_bogus_tx(tx))
20672062

20682063

20692064
if __name__ == '__main__':

test/functional/p2p_sendheaders.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ def test_nonnull_locators(self, test_node, inv_node):
307307
new_block.solve()
308308
test_node.send_header_for_blocks([new_block])
309309
test_node.wait_for_getdata([new_block.sha256])
310-
test_node.send_message(msg_block(new_block))
311-
test_node.sync_with_ping() # make sure this block is processed
310+
test_node.send_and_ping(msg_block(new_block)) # make sure this block is processed
312311
wait_until(lambda: inv_node.block_announced, timeout=60, lock=mininode_lock)
313312
inv_node.clear_block_announcements()
314313
test_node.clear_block_announcements()

test/functional/p2p_tx_download.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ def test_tx_requests(self):
6565
self.log.info("Announce the txid from each incoming peer to node 0")
6666
msg = msg_inv([CInv(t=1, h=txid)])
6767
for p in self.nodes[0].p2ps:
68-
p.send_message(msg)
69-
p.sync_with_ping()
68+
p.send_and_ping(msg)
7069

7170
outstanding_peer_index = [i for i in range(len(self.nodes[0].p2ps))]
7271

@@ -107,8 +106,7 @@ def test_inv_block(self):
107106
"Announce the transaction to all nodes from all {} incoming peers, but never send it".format(NUM_INBOUND))
108107
msg = msg_inv([CInv(t=1, h=txid)])
109108
for p in self.peers:
110-
p.send_message(msg)
111-
p.sync_with_ping()
109+
p.send_and_ping(msg)
112110

113111
self.log.info("Put the tx in node 0's mempool")
114112
self.nodes[0].sendrawtransaction(tx)

test/functional/p2p_unrequested_blocks.py

+11-24
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,9 @@ def run_test(self):
9797
blocks_h2.append(create_block(tips[i], create_coinbase(2), block_time))
9898
blocks_h2[i].solve()
9999
block_time += 1
100-
test_node.send_message(msg_block(blocks_h2[0]))
101-
min_work_node.send_message(msg_block(blocks_h2[1]))
100+
test_node.send_and_ping(msg_block(blocks_h2[0]))
101+
min_work_node.send_and_ping(msg_block(blocks_h2[1]))
102102

103-
for x in [test_node, min_work_node]:
104-
x.sync_with_ping()
105103
assert_equal(self.nodes[0].getblockcount(), 2)
106104
assert_equal(self.nodes[1].getblockcount(), 1)
107105
self.log.info("First height 2 block accepted by node0; correctly rejected by node1")
@@ -110,9 +108,8 @@ def run_test(self):
110108
block_h1f = create_block(int("0x" + self.nodes[0].getblockhash(0), 0), create_coinbase(1), block_time)
111109
block_time += 1
112110
block_h1f.solve()
113-
test_node.send_message(msg_block(block_h1f))
111+
test_node.send_and_ping(msg_block(block_h1f))
114112

115-
test_node.sync_with_ping()
116113
tip_entry_found = False
117114
for x in self.nodes[0].getchaintips():
118115
if x['hash'] == block_h1f.hash:
@@ -125,9 +122,8 @@ def run_test(self):
125122
block_h2f = create_block(block_h1f.sha256, create_coinbase(2), block_time)
126123
block_time += 1
127124
block_h2f.solve()
128-
test_node.send_message(msg_block(block_h2f))
125+
test_node.send_and_ping(msg_block(block_h2f))
129126

130-
test_node.sync_with_ping()
131127
# Since the earlier block was not processed by node, the new block
132128
# can't be fully validated.
133129
tip_entry_found = False
@@ -144,9 +140,8 @@ def run_test(self):
144140
# 4b. Now send another block that builds on the forking chain.
145141
block_h3 = create_block(block_h2f.sha256, create_coinbase(3), block_h2f.nTime+1)
146142
block_h3.solve()
147-
test_node.send_message(msg_block(block_h3))
143+
test_node.send_and_ping(msg_block(block_h3))
148144

149-
test_node.sync_with_ping()
150145
# Since the earlier block was not processed by node, the new block
151146
# can't be fully validated.
152147
tip_entry_found = False
@@ -172,17 +167,15 @@ def run_test(self):
172167
tip = next_block
173168

174169
# Now send the block at height 5 and check that it wasn't accepted (missing header)
175-
test_node.send_message(msg_block(all_blocks[1]))
176-
test_node.sync_with_ping()
170+
test_node.send_and_ping(msg_block(all_blocks[1]))
177171
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getblock, all_blocks[1].hash)
178172
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getblockheader, all_blocks[1].hash)
179173

180174
# The block at height 5 should be accepted if we provide the missing header, though
181175
headers_message = msg_headers()
182176
headers_message.headers.append(CBlockHeader(all_blocks[0]))
183177
test_node.send_message(headers_message)
184-
test_node.send_message(msg_block(all_blocks[1]))
185-
test_node.sync_with_ping()
178+
test_node.send_and_ping(msg_block(all_blocks[1]))
186179
self.nodes[0].getblock(all_blocks[1].hash)
187180

188181
# Now send the blocks in all_blocks
@@ -207,9 +200,7 @@ def run_test(self):
207200

208201
test_node = self.nodes[0].add_p2p_connection(P2PInterface())
209202

210-
test_node.send_message(msg_block(block_h1f))
211-
212-
test_node.sync_with_ping()
203+
test_node.send_and_ping(msg_block(block_h1f))
213204
assert_equal(self.nodes[0].getblockcount(), 2)
214205
self.log.info("Unrequested block that would complete more-work chain was ignored")
215206

@@ -230,9 +221,7 @@ def run_test(self):
230221
self.log.info("Inv at tip triggered getdata for unprocessed block")
231222

232223
# 7. Send the missing block for the third time (now it is requested)
233-
test_node.send_message(msg_block(block_h1f))
234-
235-
test_node.sync_with_ping()
224+
test_node.send_and_ping(msg_block(block_h1f))
236225
assert_equal(self.nodes[0].getblockcount(), 290)
237226
self.nodes[0].getblock(all_blocks[286].hash)
238227
assert_equal(self.nodes[0].getbestblockhash(), all_blocks[286].hash)
@@ -259,9 +248,8 @@ def run_test(self):
259248
headers_message.headers.append(CBlockHeader(block_290f))
260249
headers_message.headers.append(CBlockHeader(block_291))
261250
headers_message.headers.append(CBlockHeader(block_292))
262-
test_node.send_message(headers_message)
251+
test_node.send_and_ping(headers_message)
263252

264-
test_node.sync_with_ping()
265253
tip_entry_found = False
266254
for x in self.nodes[0].getchaintips():
267255
if x['hash'] == block_292.hash:
@@ -271,9 +259,8 @@ def run_test(self):
271259
assert_raises_rpc_error(-1, "Block not found on disk", self.nodes[0].getblock, block_292.hash)
272260

273261
test_node.send_message(msg_block(block_289f))
274-
test_node.send_message(msg_block(block_290f))
262+
test_node.send_and_ping(msg_block(block_290f))
275263

276-
test_node.sync_with_ping()
277264
self.nodes[0].getblock(block_289f.hash)
278265
self.nodes[0].getblock(block_290f.hash)
279266

test/functional/rpc_blockchain.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,7 @@ def _test_waitforblockheight(self):
315315
def solve_and_send_block(prevhash, height, time):
316316
b = create_block(prevhash, create_coinbase(height), time)
317317
b.solve()
318-
node.p2p.send_message(msg_block(b))
319-
node.p2p.sync_with_ping()
318+
node.p2p.send_and_ping(msg_block(b))
320319
return b
321320

322321
b21f = solve_and_send_block(int(b20hash, 16), 21, b20['time'] + 1)

0 commit comments

Comments
 (0)