Ejemplo n.º 1
0
    def rpccall_with_retry(self, method: str, *args):
        """ Do the request and return the result.

        Args:
            method: The JSON-RPC method.
            args: The encoded arguments expected by the method.
                - Object arguments must be supplied as a dictionary.
                - Quantity arguments must be hex encoded starting with '0x' and
                without left zeros.
                - Data arguments must be hex encoded starting with '0x'
        """
        request = self.protocol.create_request(method, args)
        request_serialized = request.serialize().encode()

        for i, timeout in enumerate(timeout_two_stage(10, 3, 10)):
            if self.stop_event and self.stop_event.is_set():
                raise RaidenShuttingDown()

            try:
                reply = self.transport.send_message(request_serialized)
            except (requests.exceptions.ConnectionError, InvalidReplyError):
                log.info(
                    'Timeout in eth client connection to {}. Is the client offline? Trying '
                    'again in {}s.'.format(self.transport.endpoint, timeout))
            else:
                if self.stop_event and self.stop_event.is_set():
                    raise RaidenShuttingDown()

                if i > 0:
                    log.info('Client reconnected')

                jsonrpc_reply = self.protocol.parse_reply(reply)

                if isinstance(jsonrpc_reply, JSONRPCSuccessResponse):
                    return jsonrpc_reply.result
                elif isinstance(jsonrpc_reply, JSONRPCErrorResponse):
                    raise EthNodeCommunicationError(
                        jsonrpc_reply.error,
                        jsonrpc_reply._jsonrpc_error_code,  # pylint: disable=protected-access
                    )
                else:
                    raise EthNodeCommunicationError(
                        'Unknown type of JSONRPC reply')

            gevent.sleep(timeout)
Ejemplo n.º 2
0
        def middleware(method, params):
            # raise exception when shutting down
            if client.stop_event and client.stop_event.is_set():
                raise RaidenShuttingDown()

            try:
                if web3.isConnected():
                    return make_request(method, params)
                else:
                    raise EthNodeCommunicationError('Web3 provider not connected')

            # the isConnected check doesn't currently catch JSON errors
            # see https://github.com/ethereum/web3.py/issues/866
            except JSONDecodeError:
                raise EthNodeCommunicationError('Web3 provider not connected')
Ejemplo n.º 3
0
    def retry_on_disconnect(self, *args, **kwargs):
        for i, timeout in enumerate(timeout_two_stage(10, 3, 10)):

            if self.stop_event and self.stop_event.is_set():
                raise RaidenShuttingDown()

            try:
                result = func(self, *args, **kwargs)
                if i > 0:
                    log.info('Client reconnected')
                return result

            except (requests.exceptions.ConnectionError, InvalidReplyError):
                log.info(
                    'Timeout in eth client connection to {}. Is the client offline? Trying '
                    'again in {}s.'.format(self.transport.endpoint, timeout))
            gevent.sleep(timeout)