|
27 | 27 | PortSeed,
|
28 | 28 | assert_equal,
|
29 | 29 | check_json_precision,
|
30 |
| - connect_nodes, |
31 |
| - disconnect_nodes, |
32 | 30 | get_datadir_path,
|
33 | 31 | initialize_datadir,
|
| 32 | + p2p_port, |
34 | 33 | wait_until_helper,
|
35 | 34 | )
|
36 | 35 |
|
@@ -529,10 +528,49 @@ def wait_for_node_exit(self, i, timeout):
|
529 | 528 | self.nodes[i].process.wait(timeout)
|
530 | 529 |
|
531 | 530 | 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) |
533 | 543 |
|
534 | 544 | 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) |
536 | 574 |
|
537 | 575 | def split_network(self):
|
538 | 576 | """
|
|
0 commit comments