MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / add_p2p_connection

Method add_p2p_connection

test/functional/test_framework/test_node.py:713–770  ·  view source on GitHub ↗

Add an inbound p2p connection to the node. This method adds the p2p connection to the self.p2ps list and also returns the connection to the caller. When self.use_v2transport is True, TestNode advertises NODE_P2P_V2 service flag An inbound connection is made from Te

(self, p2p_conn, *, wait_for_verack=True, send_version=True, supports_v2_p2p=None, wait_for_v2_handshake=True, expect_success=True, **kwargs)

Source from the content-addressed store, hash-verified

711 self._raise_assertion_error(assert_msg)
712
713 def add_p2p_connection(self, p2p_conn, *, wait_for_verack=True, send_version=True, supports_v2_p2p=None, wait_for_v2_handshake=True, expect_success=True, **kwargs):
714 """Add an inbound p2p connection to the node.
715
716 This method adds the p2p connection to the self.p2ps list and also
717 returns the connection to the caller.
718
719 When self.use_v2transport is True, TestNode advertises NODE_P2P_V2 service flag
720
721 An inbound connection is made from TestNode <------ P2PConnection
722 - if TestNode doesn&#x27;t advertise NODE_P2P_V2 service, P2PConnection sends version message and v1 P2P is followed
723 - if TestNode advertises NODE_P2P_V2 service, (and if P2PConnections supports v2 P2P)
724 P2PConnection sends ellswift bytes and v2 P2P is followed
725 """
726 if 'dstport' not in kwargs:
727 kwargs['dstport'] = p2p_port(self.index)
728 if 'dstaddr' not in kwargs:
729 kwargs['dstaddr'] = '127.0.0.1'
730 if supports_v2_p2p is None:
731 supports_v2_p2p = self.use_v2transport
732
733 if self.use_v2transport:
734 kwargs['services'] = kwargs.get('services', P2P_SERVICES) | NODE_P2P_V2
735 supports_v2_p2p = self.use_v2transport and supports_v2_p2p
736 p2p_conn.peer_connect(**kwargs, send_version=send_version, net=self.chain, timeout_factor=self.timeout_factor, supports_v2_p2p=supports_v2_p2p)()
737
738 self.p2ps.append(p2p_conn)
739 if not expect_success:
740 return p2p_conn
741 p2p_conn.wait_until(lambda: p2p_conn.is_connected, check_connected=False)
742 if supports_v2_p2p and wait_for_v2_handshake:
743 p2p_conn.wait_until(lambda: p2p_conn.v2_state.tried_v2_handshake)
744 if send_version:
745 p2p_conn.wait_until(lambda: not p2p_conn.on_connection_send_msg)
746 if wait_for_verack:
747 # Wait for the node to send us the version and verack
748 p2p_conn.wait_for_verack()
749 # At this point we have sent our version message and received the version and verack, however the full node
750 # has not yet received the verack from us (in reply to their version). So, the connection is not yet fully
751 # established (fSuccessfullyConnected).
752 #
753 # This shouldn't lead to any issues when sending messages, since the verack will be in-flight before the
754 # message we send. However, it might lead to races where we are expecting to receive a message. E.g. a
755 # transaction that will be added to the mempool as soon as we return here.
756 #
757 # So syncing here is redundant when we only want to send a message, but the cost is low (a few milliseconds)
758 # in comparison to the upside of making tests less fragile and unexpected intermittent errors less likely.
759 p2p_conn.sync_with_ping()
760
761 # Consistency check that the node received our user agent string.
762 # Find our connection in getpeerinfo by our address:port and theirs, as this combination is unique.
763 sockname = p2p_conn._transport.get_extra_info("socket").getsockname()
764 our_addr_and_port = f"{sockname[0]}:{sockname[1]}"
765 dst_addr_and_port = f"{p2p_conn.dstaddr}:{p2p_conn.dstport}"
766 info = [peer for peer in self.getpeerinfo() if peer["addr"] == our_addr_and_port and peer["addrbind"] == dst_addr_and_port]
767 assert_equal(len(info), 1)
768 assert_equal(info[0]["subver"], P2P_SUBVERSION)
769
770 return p2p_conn

Callers 15

run_testMethod · 0.80
test_getpeerinfoMethod · 0.80
test_service_flagsMethod · 0.80
test_getnodeaddressesMethod · 0.80
run_testMethod · 0.80
test_reorg_relayMethod · 0.80
run_testMethod · 0.80
run_testMethod · 0.80
run_testMethod · 0.80
run_testMethod · 0.80
test_tx_in_blockMethod · 0.80

Calls 7

p2p_portFunction · 0.85
assert_equalFunction · 0.85
wait_for_verackMethod · 0.80
sync_with_pingMethod · 0.80
getMethod · 0.45
peer_connectMethod · 0.45
wait_untilMethod · 0.45

Tested by

no test coverage detected