async def handshake(self, remote: NodeAPI) -> BasePeer: p2p_handshake_params = DevP2PHandshakeParams( self.context.client_version_string, self.context.listen_port, self.context.p2p_version, ) handshakers = await self.get_handshakers() connection = await dial_out(remote=remote, private_key=self.privkey, p2p_handshake_params=p2p_handshake_params, protocol_handshakers=handshakers, token=self.cancel_token) return self.create_peer(connection)
def __init__( self, privkey: datatypes.PrivateKey, port: int, chain: AsyncChainAPI, chaindb: BaseAsyncChainDB, headerdb: BaseAsyncHeaderDB, base_db: AtomicDatabaseAPI, network_id: int, max_peers: int = DEFAULT_MAX_PEERS, bootstrap_nodes: Sequence[NodeAPI] = None, preferred_nodes: Sequence[NodeAPI] = None, event_bus: EndpointAPI = None, token: CancelToken = None, ) -> None: super().__init__(token) # cross process event bus self.event_bus = event_bus # setup parameters for the base devp2p handshake. self.p2p_handshake_params = DevP2PHandshakeParams( client_version_string=construct_trinity_client_identifier(), listen_port=port, version=DEVP2P_V5, ) # chain information self.chain = chain self.chaindb = chaindb self.headerdb = headerdb self.base_db = base_db # node information self.privkey = privkey self.port = port self.network_id = network_id self.max_peers = max_peers self.bootstrap_nodes = bootstrap_nodes self.preferred_nodes = preferred_nodes if self.preferred_nodes is None and network_id in DEFAULT_PREFERRED_NODES: self.preferred_nodes = DEFAULT_PREFERRED_NODES[self.network_id] # child services self.peer_pool = self._make_peer_pool() if not bootstrap_nodes: self.logger.warning("Running with no bootstrap nodes")
async def test_handshake_with_v4_and_v5_disables_snappy(): token = CancelTokenFactory() alice_transport, bob_transport = MemoryTransportPairFactory() alice_p2p_params = DevP2PHandshakeParamsFactory( client_version_string='alice-client', listen_port=alice_transport.remote.address.tcp_port, ) bob_p2p_params = DevP2PHandshakeParams( client_version_string='bob-client', listen_port=bob_transport.remote.address.tcp_port, version=4, ) protocol_class = ProtocolFactory() alice_coro = negotiate_protocol_handshakes( alice_transport, alice_p2p_params, (NoopHandshaker(protocol_class), ), token=token, ) bob_coro = negotiate_protocol_handshakes( bob_transport, bob_p2p_params, (NoopHandshaker(protocol_class), ), token=token, ) alice_result, bob_result = await asyncio.gather(alice_coro, bob_coro) alice_multiplexer, alice_p2p_receipt, alice_receipts = alice_result bob_multiplexer, bob_p2p_receipt, bob_receipts = bob_result alice_p2p_protocol = alice_p2p_receipt.protocol bob_p2p_protocol = bob_p2p_receipt.protocol assert isinstance(alice_p2p_protocol, P2PProtocolV5) assert alice_p2p_protocol.snappy_support is False assert isinstance(bob_p2p_protocol, P2PProtocolV4) assert bob_p2p_protocol.snappy_support is False
def __init__( self, privkey: datatypes.PrivateKey, port: int, chain: AsyncChainAPI, chaindb: BaseAsyncChainDB, headerdb: BaseAsyncHeaderDB, base_db: AtomicDatabaseAPI, network_id: int, max_peers: int = DEFAULT_MAX_PEERS, event_bus: EndpointAPI = None, metrics_registry: MetricsRegistry = None, ) -> None: self.logger = get_logger(self.__module__ + '.' + self.__class__.__name__) # cross process event bus self.event_bus = event_bus self.metrics_registry = metrics_registry # setup parameters for the base devp2p handshake. self.p2p_handshake_params = DevP2PHandshakeParams( client_version_string=construct_trinity_client_identifier(), listen_port=port, version=DEVP2P_V5, ) # chain information self.chain = chain self.chaindb = chaindb self.headerdb = headerdb self.base_db = base_db # node information self.privkey = privkey self.port = port self.network_id = network_id self.max_peers = max_peers # child services self.peer_pool = self._make_peer_pool()
async def _do_handshake(alice_handshakers, alice_version, bob_handshakers, bob_version): alice_transport, bob_transport = MemoryTransportPairFactory() alice_p2p_params = DevP2PHandshakeParamsFactory( client_version_string='alice-client', listen_port=alice_transport.remote.address.tcp_port, version=alice_version, ) bob_p2p_params = DevP2PHandshakeParams( client_version_string='bob-client', listen_port=bob_transport.remote.address.tcp_port, version=bob_version, ) alice_coro = negotiate_protocol_handshakes( alice_transport, alice_p2p_params, alice_handshakers, ) bob_coro = negotiate_protocol_handshakes( bob_transport, bob_p2p_params, bob_handshakers, ) alice_result, bob_result = await asyncio.gather(alice_coro, bob_coro) alice_multiplexer, alice_p2p_receipt, alice_receipts = alice_result bob_multiplexer, bob_p2p_receipt, bob_receipts = bob_result yield (alice_multiplexer, alice_p2p_receipt, alice_receipts, bob_multiplexer, bob_p2p_receipt, bob_receipts) # Need to manually stop the background streaming task started by # negotiate_protocol_handshakes() because we never use the multiplexers in a Connection, which # would do that for us. await alice_multiplexer.stop_streaming() await bob_multiplexer.stop_streaming()