def __init__(self, connection, is_handshake_initiator, private_key,
                 public_key):
        super(EthBaseConnectionProtocol, self).__init__(
            connection,
            block_cleanup_poll_interval_s=eth_common_constants.
            BLOCK_CLEANUP_NODE_BLOCK_LIST_POLL_INTERVAL_S)

        self.node = cast("EthGatewayNode", connection.node)

        self.rlpx_cipher = RLPxCipher(is_handshake_initiator, private_key,
                                      public_key)

        self.connection_status = EthConnectionProtocolStatus()

        self._last_ping_pong_time = None
        self._handshake_complete = False

        connection.hello_messages = [
            EthProtocolMessageType.AUTH,
            EthProtocolMessageType.AUTH_ACK,
            EthProtocolMessageType.HELLO,
            EthProtocolMessageType.STATUS,
            # Ethereum Parity sends PING message before handshake is completed
            EthProtocolMessageType.PING,
            EthProtocolMessageType.DISCONNECT
        ]

        connection.message_factory = EthProtocolMessageFactory(
            self.rlpx_cipher)
        expected_first_msg_type = EthProtocolMessageType.AUTH_ACK if is_handshake_initiator \
            else EthProtocolMessageType.AUTH
        connection.message_factory.set_expected_msg_type(
            expected_first_msg_type)
        connection.message_handlers = {
            EthProtocolMessageType.AUTH: self.msg_auth,
            EthProtocolMessageType.AUTH_ACK: self.msg_auth_ack,
            EthProtocolMessageType.HELLO: self.msg_hello,
            EthProtocolMessageType.STATUS: self.msg_status,
            EthProtocolMessageType.DISCONNECT: self.msg_disconnect,
            EthProtocolMessageType.PING: self.msg_ping,
            EthProtocolMessageType.PONG: self.msg_pong,
            EthProtocolMessageType.GET_BLOCK_HEADERS:
            self.msg_get_block_headers
        }
        connection.ping_message = PingEthProtocolMessage(None)
        connection.pong_message = PongEthProtocolMessage(None)

        self._waiting_checkpoint_headers_request = True

        if is_handshake_initiator:
            self.connection.log_trace(
                "Public key is known. Starting handshake.")
            self._enqueue_auth_message()
        else:
            self.connection.log_trace(
                "Public key is unknown. Waiting for handshake request.")
            self.node.alarm_queue.register_alarm(
                eth_common_constants.HANDSHAKE_TIMEOUT_SEC,
                self._handshake_timeout)
Пример #2
0
    def setUp(self):
        self.local_node_fileno = 1
        self.local_node_fileno_2 = 2
        self.remote_node_fileno = 3
        self.local_blockchain_ip = "127.0.0.1"
        self.local_blockchain_port = 30303
        self.local_blockchain_port_2 = 30302

        eth_node_private_key = crypto_utils.make_private_key(helpers.generate_bytearray(111))
        self.gateway_node = spies.make_spy_node(
            EthGatewayNode, 8000, include_default_eth_args=True, blockchain_protocol="Ethereum",
            blockchain_address=(self.local_blockchain_ip, self.local_blockchain_port),
            blockchain_peers="enode://d76d7d11a822fab02836f8b0ea462205916253eb630935d15191fb6f9d218cd94a768fc5b3d5516b9ed5010a4765f95aea7124a39d0ab8aaf6fa3d57e21ef396@127.0.0.1:30302",
            pub_key="d76d7d11a822fab02836f8b0ea462205916253eb630935d15191fb6f9d218cd94a768fc5b3d5516b9ed5010a4765f95aea7124a39d0ab8aaf6fa3d57e21ef396")
        gateway_node_private_key = convert.hex_to_bytes(self.gateway_node.opts.private_key)
        eth_node_public_key = crypto_utils.private_to_public_key(eth_node_private_key)

        self.eth_node_cipher, gateway_cipher = self.setup_ciphers(eth_node_private_key, gateway_node_private_key)

        self.gateway_node._node_public_key = eth_node_public_key
        self.gateway_node._remote_public_key = eth_node_public_key

        self.eth_node_connection = spies.make_spy_connection(EthNodeConnection, self.local_node_fileno,
                                                             self.local_blockchain_port, self.gateway_node)

        self.eth_node_connection_2 = spies.make_spy_connection(EthNodeConnection, self.local_node_fileno_2,
                                                               self.local_blockchain_port_2, self.gateway_node)
        self.eth_remote_node_connection = spies.make_spy_connection(EthRemoteConnection, self.remote_node_fileno, 8003,
                                                                    self.gateway_node)

        self.eth_node_connection._rlpx_cipher = gateway_cipher
        self.eth_node_connection_2._rlpx_cipher = gateway_cipher
        self.eth_node_connection.message_factory = EthProtocolMessageFactory(gateway_cipher)
        self.eth_node_connection_2.message_factory = EthProtocolMessageFactory(gateway_cipher)
        self.eth_remote_node_connection._rlpx_cipher = gateway_cipher
        self.eth_remote_node_connection.message_factory = EthProtocolMessageFactory(gateway_cipher)
        self.gateway_node.remote_node_conn = self.eth_remote_node_connection

        self.gateway_node.connection_pool.add(
            self.local_node_fileno, LOCALHOST, self.local_blockchain_port, self.eth_node_connection
        )
        self.gateway_node.connection_pool.add(
            self.local_node_fileno_2, LOCALHOST, self.local_blockchain_port_2, self.eth_node_connection_2
        )
        self.gateway_node.connection_pool.add(self.remote_node_fileno, LOCALHOST, 8003, self.eth_remote_node_connection)
Пример #3
0
 def connection_message_factory(self) -> AbstractMessageFactory:
     factory = EthProtocolMessageFactory(self.rlpx_cipher)
     if self.is_handshake_initiator:
         factory.set_expected_msg_type(EthProtocolMessageType.AUTH_ACK)
     else:
         factory.set_expected_msg_type(EthProtocolMessageType.AUTH)
     return factory