示例#1
0
 async def get(self, request):
     logging.debug('transaction result')
     args = request.raw_args
     tx_hash = args['hash']
     verify_result = dict()
     if utils.is_hex(tx_hash):
         logging.debug('tx_hash : ' + tx_hash)
         channel_name = get_channel_name_from_args(args)
         grpc_response = PeerServiceStub().get_invoke_result(
             channel=channel_name, tx_hash=tx_hash)
         verify_result['response_code'] = str(grpc_response.response_code)
         if len(grpc_response.result) is not 0:
             try:
                 result = json.loads(grpc_response.result)
                 result['jsonrpc'] = '2.0'
                 verify_result['response'] = result
             except json.JSONDecodeError as e:
                 logging.warning("your data is not json, your data(" +
                                 str(grpc_response.data) + ")")
                 verify_result['response_code'] = str(
                     message_code.Response.fail.value)
         else:
             verify_result['response_code'] = str(
                 message_code.Response.fail.value)
     else:
         verify_result['response_code'] = str(
             message_code.Response.fail_validate_params.value)
         verify_result['message'] = "Invalid transaction hash."
     return response.json(verify_result)
示例#2
0
    async def get(self, request):
        args = request.raw_args
        tx_hash = args['hash']
        tx_data = dict()

        if utils.is_hex(tx_hash):
            grpc_response = ServerComponents().get_transaction(
                tx_hash, get_channel_name_from_args(args))
            tx_data['response_code'] = str(grpc_response.response_code)
            tx_data['data'] = ""
            if len(grpc_response.data) is not 0:
                try:
                    tx_data['data'] = json.loads(grpc_response.data)
                except json.JSONDecodeError as e:
                    logging.warning("your data is not json, your data(" +
                                    str(grpc_response.data) + ")")
                    tx_data['data'] = grpc_response.data

            tx_data['meta'] = ""
            if len(grpc_response.meta) is not 0:
                tx_data['meta'] = json.loads(grpc_response.meta)

            tx_data['more_info'] = grpc_response.more_info
            b64_sign = base64.b64encode(grpc_response.signature)
            tx_data['signature'] = b64_sign.decode()
            b64_public_key = base64.b64encode(grpc_response.public_key)
            tx_data['public_key'] = b64_public_key.decode()
        else:
            tx_data['response_code'] = str(
                message_code.Response.fail_validate_params.value)
            tx_data['message'] = "Invalid transaction hash."

        return response.json(tx_data)
示例#3
0
    async def icx_getTransactionResult(**kwargs):
        channel_name = conf.LOOPCHAIN_DEFAULT_CHANNEL
        channel_stub = StubCollection().channel_stubs[channel_name]
        verify_result = {}

        message = None

        tx_hash = kwargs["tx_hash"]
        if util.is_hex(tx_hash):
            response_code, result = await channel_stub.async_task(
            ).get_invoke_result(tx_hash)
            if response_code == message_code.Response.success:
                # loopchain success
                if result:
                    try:
                        # apply tx_result_convert
                        result_dict = json.loads(result)
                        fail_status = bool(result_dict.get('failure'))
                        if fail_status:
                            error_code = message_code.Response.fail_validate_params
                            message = "Invalid transaction hash."
                        else:
                            error_code = message_code.Response.success
                    except Exception as e:
                        error_message = f"your result is not json, result({result}), {e}"
                        logging.warning(error_message)
                        error_code = message_code.Response.fail_validate_params
                        message = error_message
                else:
                    error_code = message_code.Response.fail_validate_params
                    message = 'tx_result is empty'
            else:
                error_code = message_code.Response.fail_validate_params
                message = "Invalid transaction hash."
        else:
            # fail
            error_code = message_code.Response.fail_validate_params
            message = "response_code is fail"

        # parsing response
        verify_result['response_code'] = str(error_code)
        if error_code == message_code.Response.success:
            verify_result['response'] = {'code': error_code}
        if message:
            verify_result['message'] = message

        return verify_result
示例#4
0
    async def get(self, request):
        args = request.raw_args
        channel = get_channel_name_from_args(args)
        block_data = dict()

        if 'hash' in args:
            block_hash = args['hash']

            if utils.is_hex(block_hash):
                grpc_response = PeerServiceStub().get_block(
                    channel=channel, block_hash=block_hash)
                logging.debug(f"response : {grpc_response}")
                block_data['block_hash'] = grpc_response.block_hash
                block_data['block_data_json'] = json.loads(
                    grpc_response.block_data_json)

                if len(grpc_response.tx_data_json) < 1:
                    block_data['tx_data_json'] = ''
                else:
                    tx_data = list()
                    tx_json_data = grpc_response.tx_data_json

                    for i in range(0, len(tx_json_data)):
                        tx_data.append(json.loads(tx_json_data[i]))

                    block_data['tx_data_json'] = json.loads(
                        json.dumps(tx_data))
            else:
                block_data['response_code'] = str(
                    message_code.Response.fail_validate_params.value)
                block_data['message'] = "Invalid transaction hash."
        else:
            block_hash = PeerServiceStub().get_last_block_hash(channel=channel)
            grpc_response = PeerServiceStub().get_block(channel=channel,
                                                        block_hash=block_hash)
            logging.debug(f"response : {grpc_response}")
            block_data['response_code'] = grpc_response.response_code
            block_data['block_hash'] = grpc_response.block_hash
            block_data['block_data_json'] = json.loads(
                grpc_response.block_data_json)

        return response.json(block_data)
    def __validate_icx_params(self, icx_origin_data, tx_hash):
        if not util.is_hex(tx_hash):
            raise TransactionInvalidHashForamtError(tx_hash)
        if not self.__is_address(icx_origin_data['from']):
            raise TransactionInvalidAddressError(tx_hash, icx_origin_data['from'], "from addrees is invalid.")

        try:
            expect_tx_hash = self.hash_generator.generate_hash(icx_origin_data)
        except BaseException:
            raise TransactionInvalidHashGenerationError(tx_hash, icx_origin_data)

        if tx_hash != expect_tx_hash:
            logging.info(f"tx tx_hash validate fail expect : {expect_tx_hash} input : {tx_hash}")
            raise TransactionInvalidHashNotMatchError(tx_hash, expect_tx_hash)

        version = icx_origin_data.get("version", None)
        if version is not None and version == hex(conf.ApiVersion.v3):
            nid = icx_origin_data.get("nid", None)
            if nid is None or nid != ChannelProperty().nid:
                raise TransactionInvalidNoNidError(tx_hash, nid, ChannelProperty().nid)

        return True