Skip to content

Commit 3c7d9ab

Browse files
committed
test: Move (dis)?connect_nodes globals into TestFramework as helpers
1 parent 4b16c61 commit 3c7d9ab

File tree

2 files changed

+42
-45
lines changed

2 files changed

+42
-45
lines changed

test/functional/test_framework/test_framework.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
PortSeed,
2828
assert_equal,
2929
check_json_precision,
30-
connect_nodes,
31-
disconnect_nodes,
3230
get_datadir_path,
3331
initialize_datadir,
32+
p2p_port,
3433
wait_until_helper,
3534
)
3635

@@ -529,10 +528,49 @@ def wait_for_node_exit(self, i, timeout):
529528
self.nodes[i].process.wait(timeout)
530529

531530
def connect_nodes(self, a, b):
532-
connect_nodes(self.nodes[a], b)
531+
def connect_nodes_helper(from_connection, node_num):
532+
ip_port = "127.0.0.1:" + str(p2p_port(node_num))
533+
from_connection.addnode(ip_port, "onetry")
534+
# poll until version handshake complete to avoid race conditions
535+
# with transaction relaying
536+
# See comments in net_processing:
537+
# * Must have a version message before anything else
538+
# * Must have a verack message before anything else
539+
wait_until_helper(lambda: all(peer['version'] != 0 for peer in from_connection.getpeerinfo()))
540+
wait_until_helper(lambda: all(peer['bytesrecv_per_msg'].pop('verack', 0) == 24 for peer in from_connection.getpeerinfo()))
541+
542+
connect_nodes_helper(self.nodes[a], b)
533543

534544
def disconnect_nodes(self, a, b):
535-
disconnect_nodes(self.nodes[a], b)
545+
def disconnect_nodes_helper(from_connection, node_num):
546+
def get_peer_ids():
547+
result = []
548+
for peer in from_connection.getpeerinfo():
549+
if "testnode{}".format(node_num) in peer['subver']:
550+
result.append(peer['id'])
551+
return result
552+
553+
peer_ids = get_peer_ids()
554+
if not peer_ids:
555+
self.log.warning("disconnect_nodes: {} and {} were not connected".format(
556+
from_connection.index,
557+
node_num,
558+
))
559+
return
560+
for peer_id in peer_ids:
561+
try:
562+
from_connection.disconnectnode(nodeid=peer_id)
563+
except JSONRPCException as e:
564+
# If this node is disconnected between calculating the peer id
565+
# and issuing the disconnect, don't worry about it.
566+
# This avoids a race condition if we're mass-disconnecting peers.
567+
if e.error['code'] != -29: # RPC_CLIENT_NODE_NOT_CONNECTED
568+
raise
569+
570+
# wait to disconnect
571+
wait_until_helper(lambda: not get_peer_ids(), timeout=5)
572+
573+
disconnect_nodes_helper(self.nodes[a], b)
536574

537575
def split_network(self):
538576
"""

test/functional/test_framework/util.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -411,47 +411,6 @@ def set_node_times(nodes, t):
411411
node.setmocktime(t)
412412

413413

414-
def disconnect_nodes(from_connection, node_num):
415-
def get_peer_ids():
416-
result = []
417-
for peer in from_connection.getpeerinfo():
418-
if "testnode{}".format(node_num) in peer['subver']:
419-
result.append(peer['id'])
420-
return result
421-
422-
peer_ids = get_peer_ids()
423-
if not peer_ids:
424-
logger.warning("disconnect_nodes: {} and {} were not connected".format(
425-
from_connection.index,
426-
node_num,
427-
))
428-
return
429-
for peer_id in peer_ids:
430-
try:
431-
from_connection.disconnectnode(nodeid=peer_id)
432-
except JSONRPCException as e:
433-
# If this node is disconnected between calculating the peer id
434-
# and issuing the disconnect, don't worry about it.
435-
# This avoids a race condition if we're mass-disconnecting peers.
436-
if e.error['code'] != -29: # RPC_CLIENT_NODE_NOT_CONNECTED
437-
raise
438-
439-
# wait to disconnect
440-
wait_until_helper(lambda: not get_peer_ids(), timeout=5)
441-
442-
443-
def connect_nodes(from_connection, node_num):
444-
ip_port = "127.0.0.1:" + str(p2p_port(node_num))
445-
from_connection.addnode(ip_port, "onetry")
446-
# poll until version handshake complete to avoid race conditions
447-
# with transaction relaying
448-
# See comments in net_processing:
449-
# * Must have a version message before anything else
450-
# * Must have a verack message before anything else
451-
wait_until_helper(lambda: all(peer['version'] != 0 for peer in from_connection.getpeerinfo()))
452-
wait_until_helper(lambda: all(peer['bytesrecv_per_msg'].pop('verack', 0) == 24 for peer in from_connection.getpeerinfo()))
453-
454-
455414
# Transaction/Block functions
456415
#############################
457416

0 commit comments

Comments
 (0)