Skip to content

Commit 47fc883

Browse files
author
MarcoFalke
committed
Merge #19967: test: Replace (dis)?connect_nodes globals with TestFramework methods
3c7d9ab test: Move (dis)?connect_nodes globals into TestFramework as helpers (Elliott Jin) 4b16c61 scripted-diff: test: Replace uses of (dis)?connect_nodes global (Prayank) be38684 test: Replace use of (dis)?connect_nodes globals (Elliott Jin) Pull request description: `util.py` defines global helper functions `connect_nodes` and `disconnect_nodes`; however, these functions are confusing because they take a node and an index (instead of two indexes). The `TestFramework` object has enough context to convert from `i` to `self.nodes[i]`, so we can replace all instances of `connect_nodes(self.nodes[a], b)` with `self.connect_nodes(a, b)`. Similarly, we can replace instances of `disconnect_nodes`. The approach taken in this PR builds on #19945 but uses a scripted-diff for the majority of the changes. Fixes: #19821 ACKs for top commit: MarcoFalke: ACK 3c7d9ab guggero: ACK 3c7d9ab Tree-SHA512: e027092748602904abcd986d7299624c8754c3236314b6d8e392e306741c212f266c2207e385adfb194f67ae6559a585ee7b15d639b1d65c4651dbf503e5931a
2 parents 0f86e7f + 3c7d9ab commit 47fc883

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+165
-217
lines changed

test/functional/example_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from test_framework.test_framework import BitcoinTestFramework
2626
from test_framework.util import (
2727
assert_equal,
28-
connect_nodes,
2928
)
3029

3130
# P2PInterface is a class containing callbacks to be executed when a P2P
@@ -115,7 +114,7 @@ def setup_network(self):
115114
# In this test, we're not connecting node2 to node0 or node1. Calls to
116115
# sync_all() should not include node2, since we're not expecting it to
117116
# sync.
118-
connect_nodes(self.nodes[0], 1)
117+
self.connect_nodes(0, 1)
119118
self.sync_all(self.nodes[0:2])
120119

121120
# Use setup_nodes() to customize the node start behaviour (for example if
@@ -183,7 +182,7 @@ def run_test(self):
183182
self.nodes[1].waitforblockheight(11)
184183

185184
self.log.info("Connect node2 and node1")
186-
connect_nodes(self.nodes[1], 2)
185+
self.connect_nodes(1, 2)
187186

188187
self.log.info("Wait for node2 to receive all the blocks from node1")
189188
self.sync_all()

test/functional/feature_abortnode.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313
from test_framework.test_framework import BitcoinTestFramework
14-
from test_framework.util import get_datadir_path, connect_nodes
14+
from test_framework.util import get_datadir_path
1515
import os
1616

1717

@@ -36,7 +36,7 @@ def run_test(self):
3636
# attempt.
3737
self.nodes[1].generate(3)
3838
with self.nodes[0].assert_debug_log(["Failed to disconnect block"]):
39-
connect_nodes(self.nodes[0], 1)
39+
self.connect_nodes(0, 1)
4040
self.nodes[1].generate(1)
4141

4242
# Check that node0 aborted

test/functional/feature_fee_estimation.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
assert_equal,
1414
assert_greater_than,
1515
assert_greater_than_or_equal,
16-
connect_nodes,
1716
satoshi_round,
1817
)
1918

@@ -232,9 +231,9 @@ def run_test(self):
232231
# so the estimates would not be affected by the splitting transactions
233232
self.start_node(1)
234233
self.start_node(2)
235-
connect_nodes(self.nodes[1], 0)
236-
connect_nodes(self.nodes[0], 2)
237-
connect_nodes(self.nodes[2], 1)
234+
self.connect_nodes(1, 0)
235+
self.connect_nodes(0, 2)
236+
self.connect_nodes(2, 1)
238237

239238
self.sync_all()
240239

test/functional/feature_minchainwork.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import time
1919

2020
from test_framework.test_framework import BitcoinTestFramework
21-
from test_framework.util import connect_nodes, assert_equal
21+
from test_framework.util import assert_equal
2222

2323
# 2 hashes required per regtest block (with no difficulty adjustment)
2424
REGTEST_WORK_PER_BLOCK = 2
@@ -39,7 +39,7 @@ def setup_network(self):
3939
# block relay to inbound peers.
4040
self.setup_nodes()
4141
for i in range(self.num_nodes-1):
42-
connect_nodes(self.nodes[i+1], i)
42+
self.connect_nodes(i+1, i)
4343

4444
def run_test(self):
4545
# Start building a chain on node0. node2 shouldn't be able to sync until node1's

test/functional/feature_notifications.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
from test_framework.test_framework import BitcoinTestFramework
1010
from test_framework.util import (
1111
assert_equal,
12-
connect_nodes,
13-
disconnect_nodes,
1412
hex_str_to_bytes,
1513
)
1614

@@ -75,7 +73,7 @@ def run_test(self):
7573
self.log.info("test -walletnotify after rescan")
7674
# restart node to rescan to force wallet notifications
7775
self.start_node(1)
78-
connect_nodes(self.nodes[0], 1)
76+
self.connect_nodes(0, 1)
7977

8078
self.wait_until(lambda: len(os.listdir(self.walletnotify_dir)) == block_count, timeout=10)
8179

@@ -126,12 +124,12 @@ def run_test(self):
126124
# Bump tx2 as bump2 and generate a block on node 0 while
127125
# disconnected, then reconnect and check for notifications on node 1
128126
# about newly confirmed bump2 and newly conflicted tx2.
129-
disconnect_nodes(self.nodes[0], 1)
127+
self.disconnect_nodes(0, 1)
130128
bump2 = self.nodes[0].bumpfee(tx2)["txid"]
131129
self.nodes[0].generatetoaddress(1, ADDRESS_BCRT1_UNSPENDABLE)
132130
assert_equal(self.nodes[0].gettransaction(bump2)["confirmations"], 1)
133131
assert_equal(tx2 in self.nodes[1].getrawmempool(), True)
134-
connect_nodes(self.nodes[0], 1)
132+
self.connect_nodes(0, 1)
135133
self.sync_blocks()
136134
self.expect_wallet_notify([bump2, tx2])
137135
assert_equal(self.nodes[1].gettransaction(bump2)["confirmations"], 1)

test/functional/feature_pruning.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
assert_equal,
1919
assert_greater_than,
2020
assert_raises_rpc_error,
21-
connect_nodes,
22-
disconnect_nodes,
2321
)
2422

2523
# Rescans start at the earliest block up to 2 hours before a key timestamp, so
@@ -102,11 +100,11 @@ def setup_network(self):
102100

103101
self.prunedir = os.path.join(self.nodes[2].datadir, self.chain, 'blocks', '')
104102

105-
connect_nodes(self.nodes[0], 1)
106-
connect_nodes(self.nodes[1], 2)
107-
connect_nodes(self.nodes[0], 2)
108-
connect_nodes(self.nodes[0], 3)
109-
connect_nodes(self.nodes[0], 4)
103+
self.connect_nodes(0, 1)
104+
self.connect_nodes(1, 2)
105+
self.connect_nodes(0, 2)
106+
self.connect_nodes(0, 3)
107+
self.connect_nodes(0, 4)
110108
self.sync_blocks(self.nodes[0:5])
111109

112110
def setup_nodes(self):
@@ -148,17 +146,17 @@ def create_chain_with_staleblocks(self):
148146
for _ in range(12):
149147
# Disconnect node 0 so it can mine a longer reorg chain without knowing about node 1's soon-to-be-stale chain
150148
# Node 2 stays connected, so it hears about the stale blocks and then reorg's when node0 reconnects
151-
disconnect_nodes(self.nodes[0], 1)
152-
disconnect_nodes(self.nodes[0], 2)
149+
self.disconnect_nodes(0, 1)
150+
self.disconnect_nodes(0, 2)
153151
# Mine 24 blocks in node 1
154152
mine_large_blocks(self.nodes[1], 24)
155153

156154
# Reorg back with 25 block chain from node 0
157155
mine_large_blocks(self.nodes[0], 25)
158156

159157
# Create connections in the order so both nodes can see the reorg at the same time
160-
connect_nodes(self.nodes[0], 1)
161-
connect_nodes(self.nodes[0], 2)
158+
self.connect_nodes(0, 1)
159+
self.connect_nodes(0, 2)
162160
self.sync_blocks(self.nodes[0:3])
163161

164162
self.log.info("Usage can be over target because of high stale rate: %d" % calc_usage(self.prunedir))
@@ -187,15 +185,15 @@ def reorg_test(self):
187185
self.log.info("New best height: %d" % self.nodes[1].getblockcount())
188186

189187
# Disconnect node1 and generate the new chain
190-
disconnect_nodes(self.nodes[0], 1)
191-
disconnect_nodes(self.nodes[1], 2)
188+
self.disconnect_nodes(0, 1)
189+
self.disconnect_nodes(1, 2)
192190

193191
self.log.info("Generating new longer chain of 300 more blocks")
194192
self.nodes[1].generate(300)
195193

196194
self.log.info("Reconnect nodes")
197-
connect_nodes(self.nodes[0], 1)
198-
connect_nodes(self.nodes[1], 2)
195+
self.connect_nodes(0, 1)
196+
self.connect_nodes(1, 2)
199197
self.sync_blocks(self.nodes[0:3], timeout=120)
200198

201199
self.log.info("Verify height on node 2: %d" % self.nodes[2].getblockcount())
@@ -336,7 +334,7 @@ def wallet_test(self):
336334
# check that wallet loads successfully when restarting a pruned node after IBD.
337335
# this was reported to fail in #7494.
338336
self.log.info("Syncing node 5 to test wallet")
339-
connect_nodes(self.nodes[0], 5)
337+
self.connect_nodes(0, 5)
340338
nds = [self.nodes[0], self.nodes[5]]
341339
self.sync_blocks(nds, wait=5, timeout=300)
342340
self.restart_node(5, extra_args=["-prune=550"]) # restart to trigger rescan

test/functional/feature_segwit.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
assert_equal,
2323
assert_is_hex_string,
2424
assert_raises_rpc_error,
25-
connect_nodes,
2625
hex_str_to_bytes,
2726
try_rpc,
2827
)
@@ -78,7 +77,7 @@ def skip_test_if_missing_module(self):
7877

7978
def setup_network(self):
8079
super().setup_network()
81-
connect_nodes(self.nodes[0], 2)
80+
self.connect_nodes(0, 2)
8281
self.sync_all()
8382

8483
def success_mine(self, node, txid, sign, redeem_script=""):

test/functional/interface_zmq.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from test_framework.messages import CTransaction, hash256, FromHex
1212
from test_framework.util import (
1313
assert_equal,
14-
connect_nodes,
1514
assert_raises_rpc_error,
1615
)
1716
from io import BytesIO
@@ -102,7 +101,7 @@ def test_basic(self):
102101
rawtx = subs[3]
103102

104103
self.restart_node(0, ["-zmqpub%s=%s" % (sub.topic.decode(), address) for sub in [hashblock, hashtx, rawblock, rawtx]])
105-
connect_nodes(self.nodes[0], 1)
104+
self.connect_nodes(0, 1)
106105
for socket in sockets:
107106
socket.connect(address)
108107

@@ -207,7 +206,7 @@ def test_reorg(self):
207206
connect_blocks = self.nodes[1].generatetoaddress(2, ADDRESS_BCRT1_P2WSH_OP_TRUE)
208207

209208
# nodes[0] will reorg chain after connecting back nodes[1]
210-
connect_nodes(self.nodes[0], 1)
209+
self.connect_nodes(0, 1)
211210
self.sync_blocks() # tx in mempool valid but not advertised
212211

213212
# Should receive nodes[1] tip
@@ -264,7 +263,7 @@ def test_sequence(self):
264263
self.nodes[1].generatetoaddress(2, ADDRESS_BCRT1_P2WSH_OP_TRUE)
265264

266265
# nodes[0] will reorg chain after connecting back nodes[1]
267-
connect_nodes(self.nodes[0], 1)
266+
self.connect_nodes(0, 1)
268267

269268
# Then we receive all block (dis)connect notifications for the 2 block reorg
270269
assert_equal((dc_block, "D", None), seq.receive_sequence())
@@ -406,7 +405,7 @@ def test_mempool_sync(self):
406405
seq = ZMQSubscriber(socket, b'sequence')
407406

408407
self.restart_node(0, ['-zmqpub%s=%s' % (seq.topic.decode(), address)])
409-
connect_nodes(self.nodes[0], 1)
408+
self.connect_nodes(0, 1)
410409
socket.connect(address)
411410
# Relax so that the subscriber is ready before publishing zmq messages
412411
sleep(0.2)

test/functional/mempool_persist.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
assert_equal,
4646
assert_greater_than_or_equal,
4747
assert_raises_rpc_error,
48-
connect_nodes,
49-
disconnect_nodes,
5048
)
5149

5250

@@ -83,11 +81,11 @@ def run_test(self):
8381
assert_greater_than_or_equal(tx_creation_time_higher, tx_creation_time)
8482

8583
# disconnect nodes & make a txn that remains in the unbroadcast set.
86-
disconnect_nodes(self.nodes[0], 1)
84+
self.disconnect_nodes(0, 1)
8785
assert(len(self.nodes[0].getpeerinfo()) == 0)
8886
assert(len(self.nodes[0].p2ps) == 0)
8987
self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), Decimal("12"))
90-
connect_nodes(self.nodes[0], 2)
88+
self.connect_nodes(0, 2)
9189

9290
self.log.debug("Stop-start the nodes. Verify that node0 has the transactions in its mempool and node1 does not. Verify that node2 calculates its balance correctly after loading wallet transactions.")
9391
self.stop_nodes()

test/functional/mempool_unbroadcast.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
from test_framework.test_framework import BitcoinTestFramework
1212
from test_framework.util import (
1313
assert_equal,
14-
connect_nodes,
1514
create_confirmed_utxos,
16-
disconnect_nodes,
1715
)
1816

1917
MAX_INITIAL_BROADCAST_DELAY = 15 * 60 # 15 minutes in seconds
@@ -36,7 +34,7 @@ def test_broadcast(self):
3634
min_relay_fee = node.getnetworkinfo()["relayfee"]
3735
utxos = create_confirmed_utxos(min_relay_fee, node, 10)
3836

39-
disconnect_nodes(node, 1)
37+
self.disconnect_nodes(0, 1)
4038

4139
self.log.info("Generate transactions that only node 0 knows about")
4240

@@ -70,7 +68,7 @@ def test_broadcast(self):
7068
self.restart_node(0)
7169

7270
self.log.info("Reconnect nodes & check if they are sent to node 1")
73-
connect_nodes(node, 1)
71+
self.connect_nodes(0, 1)
7472

7573
# fast forward into the future & ensure that the second node has the txns
7674
node.mockscheduler(MAX_INITIAL_BROADCAST_DELAY)
@@ -91,7 +89,7 @@ def test_broadcast(self):
9189
time.sleep(2) # allow sufficient time for possibility of broadcast
9290
assert_equal(len(conn.get_invs()), 0)
9391

94-
disconnect_nodes(node, 1)
92+
self.disconnect_nodes(0, 1)
9593
node.disconnect_p2ps()
9694

9795
def test_txn_removal(self):

0 commit comments

Comments
 (0)