Skip to content

Commit 6a18bb9

Browse files
committed
[tests] sync_with_ping should assert that ping hasn't timed out
sync_with_ping currently returns false if the timeout expires, and it is the caller's responsibility to fail the test. However, none of the tests currently assert on sync_with_ping()'s return code. This commit adds an assert to sync_with_ping so the test will fail if the timeout expires. This commit also removes all the duplicate implementations of sync_with_ping() from the individual tests.
1 parent 6426716 commit 6a18bb9

File tree

7 files changed

+13
-64
lines changed

7 files changed

+13
-64
lines changed

test/functional/assumevalid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ def run_test(self):
190190
# Send all blocks to node1. All blocks will be accepted.
191191
for i in range(2202):
192192
node1.send_message(msg_block(self.blocks[i]))
193-
node1.sync_with_ping() # make sure the most recent block is synced
193+
# Syncing 2200 blocks can take a while on slow systems. Give it plenty of time to sync.
194+
node1.sync_with_ping(120)
194195
assert_equal(self.nodes[1].getblock(self.nodes[1].getbestblockhash())['height'], 2202)
195196

196197
# Send blocks to node2. Block 102 will be rejected.

test/functional/maxuploadtarget.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,6 @@ def on_pong(self, conn, message):
6868
def on_close(self, conn):
6969
self.peer_disconnected = True
7070

71-
# Sync up with the node after delivery of a block
72-
def sync_with_ping(self, timeout=30):
73-
def received_pong():
74-
return (self.last_pong.nonce == self.ping_counter)
75-
self.connection.send_message(msg_ping(nonce=self.ping_counter))
76-
success = wait_until(received_pong, timeout=timeout)
77-
self.ping_counter += 1
78-
return success
79-
8071
class MaxUploadTest(BitcoinTestFramework):
8172

8273
def __init__(self):

test/functional/p2p-acceptblock.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,6 @@ def send_message(self, message):
8888
def on_pong(self, conn, message):
8989
self.last_pong = message
9090

91-
# Sync up with the node after delivery of a block
92-
def sync_with_ping(self, timeout=30):
93-
self.connection.send_message(msg_ping(nonce=self.ping_counter))
94-
received_pong = False
95-
sleep_time = 0.05
96-
while not received_pong and timeout > 0:
97-
time.sleep(sleep_time)
98-
timeout -= sleep_time
99-
with mininode_lock:
100-
if self.last_pong.nonce == self.ping_counter:
101-
received_pong = True
102-
self.ping_counter += 1
103-
return received_pong
104-
105-
10691
class AcceptBlockTest(BitcoinTestFramework):
10792
def add_options(self, parser):
10893
parser.add_option("--testbinary", dest="testbinary",

test/functional/p2p-mempool.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ def on_pong(self, conn, message):
6262
def on_close(self, conn):
6363
self.peer_disconnected = True
6464

65-
# Sync up with the node after delivery of a block
66-
def sync_with_ping(self, timeout=30):
67-
def received_pong():
68-
return (self.last_pong.nonce == self.ping_counter)
69-
self.connection.send_message(msg_ping(nonce=self.ping_counter))
70-
success = wait_until(received_pong, timeout=timeout)
71-
self.ping_counter += 1
72-
return success
73-
7465
def send_mempool(self):
7566
self.lastInv = []
7667
self.send_message(msg_mempool())

test/functional/p2p-segwit.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ def sync(self, test_function, timeout=60):
8080
timeout -= self.sleep_time
8181
raise AssertionError("Sync failed to complete")
8282

83-
def sync_with_ping(self, timeout=60):
84-
self.send_message(msg_ping(nonce=self.ping_counter))
85-
test_function = lambda: self.last_pong.nonce == self.ping_counter
86-
self.sync(test_function, timeout)
87-
self.ping_counter += 1
88-
return
89-
9083
def wait_for_block(self, blockhash, timeout=60):
9184
test_function = lambda: self.last_block != None and self.last_block.sha256 == blockhash
9285
self.sync(test_function, timeout)
@@ -148,7 +141,7 @@ def test_transaction_acceptance(self, tx, with_witness, accepted, reason=None):
148141
if with_witness:
149142
tx_message = msg_witness_tx(tx)
150143
self.send_message(tx_message)
151-
self.sync_with_ping()
144+
self.sync_with_ping(60)
152145
assert_equal(tx.hash in self.connection.rpc.getrawmempool(), accepted)
153146
if (reason != None and not accepted):
154147
# Check the rejection reason as well.
@@ -161,7 +154,7 @@ def test_witness_block(self, block, accepted, with_witness=True):
161154
self.send_message(msg_witness_block(block))
162155
else:
163156
self.send_message(msg_block(block))
164-
self.sync_with_ping()
157+
self.sync_with_ping(60)
165158
assert_equal(self.connection.rpc.getbestblockhash() == block.hash, accepted)
166159

167160

@@ -235,7 +228,7 @@ def test_non_witness_transaction(self):
235228
block = self.build_next_block(nVersion=1)
236229
block.solve()
237230
self.test_node.send_message(msg_block(block))
238-
self.test_node.sync_with_ping() # make sure the block was processed
231+
self.test_node.sync_with_ping(60) # make sure the block was processed
239232
txid = block.vtx[0].sha256
240233

241234
self.nodes[0].generate(99) # let the block mature
@@ -251,7 +244,7 @@ def test_non_witness_transaction(self):
251244
assert_equal(msg_tx(tx).serialize(), msg_witness_tx(tx).serialize())
252245

253246
self.test_node.send_message(msg_witness_tx(tx))
254-
self.test_node.sync_with_ping() # make sure the tx was processed
247+
self.test_node.sync_with_ping(60) # make sure the tx was processed
255248
assert(tx.hash in self.nodes[0].getrawmempool())
256249
# Save this transaction for later
257250
self.utxo.append(UTXO(tx.sha256, 0, 49*100000000))
@@ -291,7 +284,7 @@ def test_unnecessary_witness_before_segwit_activation(self):
291284
# But it should not be permanently marked bad...
292285
# Resend without witness information.
293286
self.test_node.send_message(msg_block(block))
294-
self.test_node.sync_with_ping()
287+
self.test_node.sync_with_ping(60)
295288
assert_equal(self.nodes[0].getbestblockhash(), block.hash)
296289

297290
sync_blocks(self.nodes)
@@ -1257,7 +1250,7 @@ def test_segwit_versions(self):
12571250
# Spending a higher version witness output is not allowed by policy,
12581251
# even with fRequireStandard=false.
12591252
self.test_node.test_transaction_acceptance(tx3, with_witness=True, accepted=False)
1260-
self.test_node.sync_with_ping()
1253+
self.test_node.sync_with_ping(60)
12611254
with mininode_lock:
12621255
assert(b"reserved for soft-fork upgrades" in self.test_node.last_reject.reason)
12631256

@@ -1387,7 +1380,7 @@ def test_signature_version_1(self):
13871380
for i in range(NUM_TESTS):
13881381
# Ping regularly to keep the connection alive
13891382
if (not i % 100):
1390-
self.test_node.sync_with_ping()
1383+
self.test_node.sync_with_ping(60)
13911384
# Choose random number of inputs to use.
13921385
num_inputs = random.randint(1, 10)
13931386
# Create a slight bias for producing more utxos

test/functional/p2p-versionbits-warning.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,6 @@ def send_message(self, message):
4646
def on_pong(self, conn, message):
4747
self.last_pong = message
4848

49-
# Sync up with the node after delivery of a block
50-
def sync_with_ping(self, timeout=30):
51-
self.connection.send_message(msg_ping(nonce=self.ping_counter))
52-
received_pong = False
53-
sleep_time = 0.05
54-
while not received_pong and timeout > 0:
55-
time.sleep(sleep_time)
56-
timeout -= sleep_time
57-
with mininode_lock:
58-
if self.last_pong.nonce == self.ping_counter:
59-
received_pong = True
60-
self.ping_counter += 1
61-
return received_pong
62-
63-
6449
class VersionBitsWarningTest(BitcoinTestFramework):
6550
def __init__(self):
6651
super().__init__()

test/functional/test_framework/mininode.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,11 +1563,14 @@ def send_and_ping(self, message):
15631563
self.sync_with_ping()
15641564

15651565
# Sync up with the node
1566-
def sync_with_ping(self, timeout=30):
1566+
def sync_with_ping(self, timeout=60):
15671567
def received_pong():
15681568
return (self.last_pong.nonce == self.ping_counter)
15691569
self.send_message(msg_ping(nonce=self.ping_counter))
15701570
success = wait_until(received_pong, timeout=timeout)
1571+
if not success:
1572+
logger.error("sync_with_ping failed!")
1573+
raise AssertionError("sync_with_ping failed!")
15711574
self.ping_counter += 1
15721575

15731576
return success

0 commit comments

Comments
 (0)