Skip to content

Commit cc84460

Browse files
committed
test: move sync_blocks and sync_mempool functions to test_framework.py
1 parent 9ad6f14 commit cc84460

File tree

5 files changed

+56
-66
lines changed

5 files changed

+56
-66
lines changed

test/functional/feature_backwards_compatibility.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
from test_framework.util import (
2929
adjust_bitcoin_conf_for_pre_17,
3030
assert_equal,
31-
sync_blocks,
32-
sync_mempools,
3331
)
3432

3533

@@ -68,7 +66,7 @@ def setup_nodes(self):
6866
def run_test(self):
6967
self.nodes[0].generatetoaddress(101, self.nodes[0].getnewaddress())
7068

71-
sync_blocks(self.nodes)
69+
self.sync_blocks()
7270

7371
# Sanity check the test framework:
7472
res = self.nodes[self.num_nodes - 1].getblockchaininfo()
@@ -93,17 +91,17 @@ def run_test(self):
9391
# Create a confirmed transaction, receiving coins
9492
address = wallet.getnewaddress()
9593
self.nodes[0].sendtoaddress(address, 10)
96-
sync_mempools(self.nodes)
94+
self.sync_mempools()
9795
self.nodes[0].generate(1)
98-
sync_blocks(self.nodes)
96+
self.sync_blocks()
9997
# Create a conflicting transaction using RBF
10098
return_address = self.nodes[0].getnewaddress()
10199
tx1_id = self.nodes[1].sendtoaddress(return_address, 1)
102100
tx2_id = self.nodes[1].bumpfee(tx1_id)["txid"]
103101
# Confirm the transaction
104-
sync_mempools(self.nodes)
102+
self.sync_mempools()
105103
self.nodes[0].generate(1)
106-
sync_blocks(self.nodes)
104+
self.sync_blocks()
107105
# Create another conflicting transaction using RBF
108106
tx3_id = self.nodes[1].sendtoaddress(return_address, 1)
109107
tx4_id = self.nodes[1].bumpfee(tx3_id)["txid"]

test/functional/rpc_getblockfilter.py

Lines changed: 2 additions & 2 deletions
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

Lines changed: 48 additions & 11 deletions
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

@@ -541,15 +539,54 @@ def join_network(self):
541539
connect_nodes(self.nodes[1], 2)
542540
self.sync_all()
543541

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

554591
# Private helper methods. These should not be accessed by the subclass test scripts.
555592

test/functional/test_framework/util.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -420,50 +420,6 @@ def connect_nodes(from_connection, node_num):
420420
wait_until(lambda: all(peer['bytesrecv_per_msg'].pop('verack', 0) == 24 for peer in from_connection.getpeerinfo()))
421421

422422

423-
def sync_blocks(rpc_connections, *, wait=1, timeout=60):
424-
"""
425-
Wait until everybody has the same tip.
426-
427-
sync_blocks needs to be called with an rpc_connections set that has least
428-
one node already synced to the latest, stable tip, otherwise there's a
429-
chance it might return before all nodes are stably synced.
430-
"""
431-
stop_time = time.time() + timeout
432-
while time.time() <= stop_time:
433-
best_hash = [x.getbestblockhash() for x in rpc_connections]
434-
if best_hash.count(best_hash[0]) == len(rpc_connections):
435-
return
436-
# Check that each peer has at least one connection
437-
assert (all([len(x.getpeerinfo()) for x in rpc_connections]))
438-
time.sleep(wait)
439-
raise AssertionError("Block sync timed out after {}s:{}".format(
440-
timeout,
441-
"".join("\n {!r}".format(b) for b in best_hash),
442-
))
443-
444-
445-
def sync_mempools(rpc_connections, *, wait=1, timeout=60, flush_scheduler=True):
446-
"""
447-
Wait until everybody has the same transactions in their memory
448-
pools
449-
"""
450-
stop_time = time.time() + timeout
451-
while time.time() <= stop_time:
452-
pool = [set(r.getrawmempool()) for r in rpc_connections]
453-
if pool.count(pool[0]) == len(rpc_connections):
454-
if flush_scheduler:
455-
for r in rpc_connections:
456-
r.syncwithvalidationinterfacequeue()
457-
return
458-
# Check that each peer has at least one connection
459-
assert (all([len(x.getpeerinfo()) for x in rpc_connections]))
460-
time.sleep(wait)
461-
raise AssertionError("Mempool sync timed out after {}s:{}".format(
462-
timeout,
463-
"".join("\n {!r}".format(m) for m in pool),
464-
))
465-
466-
467423
# Transaction/Block functions
468424
#############################
469425

test/functional/wallet_balance.py

Lines changed: 1 addition & 2 deletions
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)