Skip to content

Commit 4b5c919

Browse files
author
MarcoFalke
committed
Merge bitcoin#19208: test: move sync_blocks and sync_mempool functions to test_framework.py
cc84460 test: move sync_blocks and sync_mempool functions to test_framework.py (Roy Shao) Pull request description: This PR moves `sync_blocks` and `sync_mempool` out from `test_framework/util.py` to `test_framework/test_framework.py` so they can take contextual information of test framework into account. * Change all reference callers to call functions from `test_framework.py` * Remove `**kwargs` which is not used * Take into account of `timeout_factor` when respecting timeout in function implementations. * Pass all tests by running `./test/functional/test_runner.py` fixes bitcoin#18930 ACKs for top commit: MarcoFalke: ACK cc84460 , reviewed with --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space 💫 Tree-SHA512: a79b2a3fa842fc26a7aacb834bb2aea88b3049916c0b754e60002a77ce94bb5954e0ea3b436bf268e9295efb62d721dfef263a09339a55c684ac3fda388c275e
2 parents c273308 + cc84460 commit 4b5c919

File tree

5 files changed

+56
-66
lines changed

5 files changed

+56
-66
lines changed

test/functional/feature_backwards_compatibility.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
from test_framework.util import (
2929
assert_equal,
30-
sync_blocks,
31-
sync_mempools,
3230
)
3331

3432

@@ -65,7 +63,7 @@ def setup_nodes(self):
6563
def run_test(self):
6664
self.nodes[0].generatetoaddress(101, self.nodes[0].getnewaddress())
6765

68-
sync_blocks(self.nodes)
66+
self.sync_blocks()
6967

7068
# Sanity check the test framework:
7169
res = self.nodes[self.num_nodes - 1].getblockchaininfo()
@@ -90,17 +88,17 @@ def run_test(self):
9088
# Create a confirmed transaction, receiving coins
9189
address = wallet.getnewaddress()
9290
self.nodes[0].sendtoaddress(address, 10)
93-
sync_mempools(self.nodes)
91+
self.sync_mempools()
9492
self.nodes[0].generate(1)
95-
sync_blocks(self.nodes)
93+
self.sync_blocks()
9694
# Create a conflicting transaction using RBF
9795
return_address = self.nodes[0].getnewaddress()
9896
tx1_id = self.nodes[1].sendtoaddress(return_address, 1)
9997
tx2_id = self.nodes[1].bumpfee(tx1_id)["txid"]
10098
# Confirm the transaction
101-
sync_mempools(self.nodes)
99+
self.sync_mempools()
102100
self.nodes[0].generate(1)
103-
sync_blocks(self.nodes)
101+
self.sync_blocks()
104102
# Create another conflicting transaction using RBF
105103
tx3_id = self.nodes[1].sendtoaddress(return_address, 1)
106104
tx4_id = self.nodes[1].bumpfee(tx3_id)["txid"]

test/functional/rpc_getblockfilter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from test_framework.test_framework import BitcoinTestFramework
88
from test_framework.util import (
99
assert_equal, assert_is_hex_string, assert_raises_rpc_error,
10-
connect_nodes, disconnect_nodes, sync_blocks
10+
connect_nodes, disconnect_nodes
1111
)
1212

1313
FILTER_TYPES = ["basic"]
@@ -30,7 +30,7 @@ def run_test(self):
3030

3131
# Reorg node 0 to a new chain
3232
connect_nodes(self.nodes[0], 1)
33-
sync_blocks(self.nodes)
33+
self.sync_blocks()
3434

3535
assert_equal(self.nodes[0].getblockcount(), 4)
3636
chain1_hashes = [self.nodes[0].getblockhash(block_height) for block_height in range(4)]

test/functional/test_framework/test_framework.py

+48-11
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
disconnect_nodes,
3232
get_datadir_path,
3333
initialize_datadir,
34-
sync_blocks,
35-
sync_mempools,
3634
)
3735

3836

@@ -549,15 +547,54 @@ def join_network(self):
549547
connect_nodes(self.nodes[1], 2)
550548
self.sync_all()
551549

552-
def sync_blocks(self, nodes=None, **kwargs):
553-
sync_blocks(nodes or self.nodes, **kwargs)
554-
555-
def sync_mempools(self, nodes=None, **kwargs):
556-
sync_mempools(nodes or self.nodes, **kwargs)
557-
558-
def sync_all(self, nodes=None, **kwargs):
559-
self.sync_blocks(nodes, **kwargs)
560-
self.sync_mempools(nodes, **kwargs)
550+
def sync_blocks(self, nodes=None, wait=1, timeout=60):
551+
"""
552+
Wait until everybody has the same tip.
553+
sync_blocks needs to be called with an rpc_connections set that has least
554+
one node already synced to the latest, stable tip, otherwise there's a
555+
chance it might return before all nodes are stably synced.
556+
"""
557+
rpc_connections = nodes or self.nodes
558+
timeout = int(timeout * self.options.timeout_factor)
559+
stop_time = time.time() + timeout
560+
while time.time() <= stop_time:
561+
best_hash = [x.getbestblockhash() for x in rpc_connections]
562+
if best_hash.count(best_hash[0]) == len(rpc_connections):
563+
return
564+
# Check that each peer has at least one connection
565+
assert (all([len(x.getpeerinfo()) for x in rpc_connections]))
566+
time.sleep(wait)
567+
raise AssertionError("Block sync timed out after {}s:{}".format(
568+
timeout,
569+
"".join("\n {!r}".format(b) for b in best_hash),
570+
))
571+
572+
def sync_mempools(self, nodes=None, wait=1, timeout=60, flush_scheduler=True):
573+
"""
574+
Wait until everybody has the same transactions in their memory
575+
pools
576+
"""
577+
rpc_connections = nodes or self.nodes
578+
timeout = int(timeout * self.options.timeout_factor)
579+
stop_time = time.time() + timeout
580+
while time.time() <= stop_time:
581+
pool = [set(r.getrawmempool()) for r in rpc_connections]
582+
if pool.count(pool[0]) == len(rpc_connections):
583+
if flush_scheduler:
584+
for r in rpc_connections:
585+
r.syncwithvalidationinterfacequeue()
586+
return
587+
# Check that each peer has at least one connection
588+
assert (all([len(x.getpeerinfo()) for x in rpc_connections]))
589+
time.sleep(wait)
590+
raise AssertionError("Mempool sync timed out after {}s:{}".format(
591+
timeout,
592+
"".join("\n {!r}".format(m) for m in pool),
593+
))
594+
595+
def sync_all(self, nodes=None):
596+
self.sync_blocks(nodes)
597+
self.sync_mempools(nodes)
561598

562599
# Private helper methods. These should not be accessed by the subclass test scripts.
563600

test/functional/test_framework/util.py

-44
Original file line numberDiff line numberDiff line change
@@ -444,50 +444,6 @@ def connect_nodes(from_connection, node_num):
444444
wait_until(lambda: all(peer['bytesrecv_per_msg'].pop('verack', 0) == 24 for peer in from_connection.getpeerinfo()))
445445

446446

447-
def sync_blocks(rpc_connections, *, wait=1, timeout=60):
448-
"""
449-
Wait until everybody has the same tip.
450-
451-
sync_blocks needs to be called with an rpc_connections set that has least
452-
one node already synced to the latest, stable tip, otherwise there's a
453-
chance it might return before all nodes are stably synced.
454-
"""
455-
stop_time = time.time() + timeout
456-
while time.time() <= stop_time:
457-
best_hash = [x.getbestblockhash() for x in rpc_connections]
458-
if best_hash.count(best_hash[0]) == len(rpc_connections):
459-
return
460-
# Check that each peer has at least one connection
461-
assert (all([len(x.getpeerinfo()) for x in rpc_connections]))
462-
time.sleep(wait)
463-
raise AssertionError("Block sync timed out after {}s:{}".format(
464-
timeout,
465-
"".join("\n {!r}".format(b) for b in best_hash),
466-
))
467-
468-
469-
def sync_mempools(rpc_connections, *, wait=1, timeout=60, flush_scheduler=True):
470-
"""
471-
Wait until everybody has the same transactions in their memory
472-
pools
473-
"""
474-
stop_time = time.time() + timeout
475-
while time.time() <= stop_time:
476-
pool = [set(r.getrawmempool()) for r in rpc_connections]
477-
if pool.count(pool[0]) == len(rpc_connections):
478-
if flush_scheduler:
479-
for r in rpc_connections:
480-
r.syncwithvalidationinterfacequeue()
481-
return
482-
# Check that each peer has at least one connection
483-
assert (all([len(x.getpeerinfo()) for x in rpc_connections]))
484-
time.sleep(wait)
485-
raise AssertionError("Mempool sync timed out after {}s:{}".format(
486-
timeout,
487-
"".join("\n {!r}".format(m) for m in pool),
488-
))
489-
490-
491447
# Transaction/Block functions
492448
#############################
493449

test/functional/wallet_balance.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
assert_equal,
1313
assert_raises_rpc_error,
1414
connect_nodes,
15-
sync_blocks,
1615
)
1716

1817

@@ -264,7 +263,7 @@ def test_balances(*, fee_node_1=0):
264263
# Now confirm tx_orig
265264
self.restart_node(1, ['-persistmempool=0'])
266265
connect_nodes(self.nodes[0], 1)
267-
sync_blocks(self.nodes)
266+
self.sync_blocks()
268267
self.nodes[1].sendrawtransaction(tx_orig)
269268
self.nodes[1].generatetoaddress(1, ADDRESS_WATCHONLY)
270269
self.sync_all()

0 commit comments

Comments
 (0)