Пример #1
0
    def _query(self, request: dict):
        response = None

        try:
            method = request['method']

            if method == 'debug_estimateStep':
                converted_request = TypeConverter.convert(
                    request, ParamType.INVOKE_TRANSACTION)
                value = self._icon_service_engine.estimate_step(
                    converted_request)
            else:
                converted_request = TypeConverter.convert(
                    request, ParamType.QUERY)
                value = self._icon_service_engine.query(
                    method, converted_request['params'])

            if isinstance(value, Address):
                value = str(value)
            response = MakeResponse.make_response(value)
        except IconServiceBaseException as icon_e:
            self._log_exception(icon_e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                icon_e.code, icon_e.message)
        except Exception as e:
            self._log_exception(e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SERVER_ERROR, str(e))
        finally:
            Logger.info(f'query response with {response}', ICON_INNER_LOG_TAG)
            return response
    def test_get_balance_with_malformed_address_and_type_converter(self):
        empty_address = MalformedAddress.from_string('')
        short_address_without_hx = MalformedAddress.from_string('12341234')
        short_address = MalformedAddress.from_string('hx1234512345')
        long_address_without_hx = MalformedAddress.from_string(
            'cf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf')
        long_address = MalformedAddress.from_string(
            'hxdf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf'
        )
        malformed_addresses = [
            '', '12341234', 'hx1234123456',
            'cf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf',
            'hxdf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf'
        ]

        method: str = 'icx_getBalance'

        for address in malformed_addresses:
            request = {'method': method, 'params': {'address': address}}

            converted_request = TypeConverter.convert(request, ParamType.QUERY)
            self.assertEqual(method, converted_request['method'])

            params: dict = converted_request['params']
            self.assertEqual(MalformedAddress.from_string(address),
                             params['address'])

            balance: int = self._engine.query(converted_request['method'],
                                              converted_request['params'])
            self.assertEqual(0, balance)
Пример #3
0
    def _rollback(self, request: dict) -> dict:
        Logger.info(tag=ICON_INNER_LOG_TAG,
                    msg=f"_rollback() start: {request}")

        response = {}
        try:
            converted_params = TypeConverter.convert(request,
                                                     ParamType.ROLLBACK)
            block_height: int = converted_params[ConstantKeys.BLOCK_HEIGHT]
            block_hash: bytes = converted_params[ConstantKeys.BLOCK_HASH]

            response: dict = self._icon_service_engine.rollback(
                block_height, block_hash)
            response = MakeResponse.make_response(response)
        except FatalException as e:
            self._log_exception(e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SYSTEM_ERROR, str(e))
            self._close()
        except IconServiceBaseException as icon_e:
            self._log_exception(icon_e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                icon_e.code, icon_e.message)
        except BaseException as e:
            self._log_exception(e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SYSTEM_ERROR, str(e))
        finally:
            Logger.info(tag=ICON_INNER_LOG_TAG,
                        msg=f"_rollback() end: {response}")
            return response
Пример #4
0
    def _invoke(self, request: dict):
        """Process transactions in a block

        :param request:
        :return:
        """

        response = None
        try:
            params = TypeConverter.convert(request, ParamType.INVOKE)
            converted_block_params = params['block']
            block = Block.from_dict(converted_block_params)

            converted_tx_requests = params['transactions']
            tx_results, state_root_hash = self._icon_service_engine.invoke(
                block=block, tx_requests=converted_tx_requests)

            convert_tx_results = \
                {bytes.hex(tx_result.tx_hash): tx_result.to_dict(to_camel_case) for tx_result in tx_results}
            results = {
                'txResults': convert_tx_results,
                'stateRootHash': bytes.hex(state_root_hash)
            }
            response = MakeResponse.make_response(results)
        except IconServiceBaseException as icon_e:
            self._log_exception(icon_e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                icon_e.code, icon_e.message)
        except Exception as e:
            self._log_exception(e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SERVER_ERROR, str(e))
        finally:
            Logger.info(f'invoke response with {response}', ICON_INNER_LOG_TAG)
            return response
Пример #5
0
def test_deploy_data_convert():
    content_type = 'application/zip'
    content = CONTENT
    data_from = create_address()
    data_to = create_address()
    data_value = 1 * ICX_FACTOR

    request = {
        ConstantKeys.CONTENT_TYPE: content_type,
        ConstantKeys.CONTENT: content,
        ConstantKeys.PARAMS: {
            ConstantKeys.FROM: str(data_from),
            ConstantKeys.TO: str(data_to),
            ConstantKeys.VALUE: hex(data_value)
        }
    }

    ret_params = TypeConverter.convert(request, ParamType.DEPLOY_DATA)

    assert content_type == ret_params[ConstantKeys.CONTENT_TYPE]
    assert content == ret_params[ConstantKeys.CONTENT]
    params = ret_params[ConstantKeys.PARAMS]
    assert data_from != params[ConstantKeys.FROM]
    assert data_to != params[ConstantKeys.TO]
    assert data_value != params[ConstantKeys.VALUE]
Пример #6
0
def _test_base_transaction_convert(data: dict,
                                   method: str = "icx_sendTransaction"):
    version = 3
    timestamp = 12345
    data_type = 'base'
    nonce = 123

    request = {
        ConstantKeys.METHOD: method,
        ConstantKeys.PARAMS: {
            ConstantKeys.VERSION: hex(version),
            ConstantKeys.TIMESTAMP: hex(timestamp),
            ConstantKeys.NONCE: hex(nonce),
            ConstantKeys.DATA_TYPE: data_type,
            ConstantKeys.DATA: {}
        }
    }
    data_params = request[ConstantKeys.PARAMS][ConstantKeys.DATA]
    for group in data.keys():
        data_params[group] = {
            key: hex(value)
            for key, value in data[group].items()
        }

    ret_params = TypeConverter.convert(request, ParamType.INVOKE_TRANSACTION)
    assert method == ret_params[ConstantKeys.METHOD]
    assert version == ret_params[ConstantKeys.PARAMS][ConstantKeys.VERSION]
    assert timestamp == ret_params[ConstantKeys.PARAMS][ConstantKeys.TIMESTAMP]
    assert nonce == ret_params[ConstantKeys.PARAMS][ConstantKeys.NONCE]
    assert data_type == ret_params[ConstantKeys.PARAMS][ConstantKeys.DATA_TYPE]
    ret_data_params = ret_params[ConstantKeys.PARAMS][ConstantKeys.DATA]
    for group in data.keys():
        for key in data[group].keys():
            assert data[group][key] == ret_data_params[group][key]
Пример #7
0
    def test_icx_call_convert(self):
        version = 3
        from_addr = create_address()
        to_addr = create_address(1)
        data_type = "call"
        data_method = "get_balance"
        data_addr = create_address()

        request = {
            ConstantKeys.VERSION: hex(version),
            ConstantKeys.FROM: str(from_addr),
            ConstantKeys.TO: str(to_addr),
            ConstantKeys.DATA_TYPE: data_type,
            ConstantKeys.DATA: {
                ConstantKeys.METHOD: data_method,
                ConstantKeys.PARAMS: {
                    ConstantKeys.ADDRESS: str(data_addr)
                }
            }
        }

        ret_params = TypeConverter.convert(request, ParamType.ICX_CALL)

        self.assertEqual(version, ret_params[ConstantKeys.VERSION])
        self.assertEqual(from_addr, ret_params[ConstantKeys.FROM])
        self.assertEqual(to_addr, ret_params[ConstantKeys.TO])
        self.assertEqual(data_type, ret_params[ConstantKeys.DATA_TYPE])

        data_params = ret_params[ConstantKeys.DATA]
        self.assertEqual(data_method, data_params[ConstantKeys.METHOD])
        data_params_params = data_params[ConstantKeys.PARAMS]
        self.assertNotEqual(data_addr,
                            data_params_params[ConstantKeys.ADDRESS])
Пример #8
0
    def _remove_precommit_state(self, request: dict):
        response = None
        try:
            converted_block_params = TypeConverter.convert(
                request, ParamType.WRITE_PRECOMMIT)
            block_height, instant_block_hash, _ = \
                self._get_block_info_for_precommit_state(converted_block_params)

            self._icon_service_engine.remove_precommit_state(
                block_height, instant_block_hash)
            response = MakeResponse.make_response(ExceptionCode.OK)
        except FatalException as e:
            self._log_exception(e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SYSTEM_ERROR, str(e))
            self._close()
        except IconServiceBaseException as icon_e:
            self._log_exception(icon_e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                icon_e.code, icon_e.message)
        except Exception as e:
            self._log_exception(e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SYSTEM_ERROR, str(e))
        finally:
            Logger.info(f'remove_precommit_state response with {response}',
                        ICON_INNER_LOG_TAG)
            return response
Пример #9
0
    def test_deploy_data_convert(self):
        content_type = 'application/zip'
        content = self.content
        data_from = create_address()
        data_to = create_address()
        data_value = 1 * self.icx_factor

        request = {
            ConstantKeys.CONTENT_TYPE: content_type,
            ConstantKeys.CONTENT: content,
            ConstantKeys.PARAMS: {
                ConstantKeys.FROM: str(data_from),
                ConstantKeys.TO: str(data_to),
                ConstantKeys.VALUE: hex(data_value)
            }
        }

        ret_params = TypeConverter.convert(request, ParamType.DEPLOY_DATA)

        self.assertEqual(content_type, ret_params[ConstantKeys.CONTENT_TYPE])
        self.assertEqual(content, ret_params[ConstantKeys.CONTENT])
        params = ret_params[ConstantKeys.PARAMS]
        self.assertNotEqual(data_from, params[ConstantKeys.FROM])
        self.assertNotEqual(data_to, params[ConstantKeys.TO])
        self.assertNotEqual(data_value, params[ConstantKeys.VALUE])
Пример #10
0
    def _write_precommit_state(self, request: dict) -> dict:
        Logger.info(tag=_TAG, msg=f'WRITE_PRECOMMIT_STATE Request: {request}')

        try:
            converted_params = TypeConverter.convert(request, ParamType.WRITE_PRECOMMIT)
            block_height: int = converted_params[ConstantKeys.BLOCK_HEIGHT]
            instant_block_hash: bytes = converted_params[ConstantKeys.OLD_BLOCK_HASH]
            block_hash = converted_params[ConstantKeys.NEW_BLOCK_HASH]

            Logger.info(tag=_TAG, msg=f'WRITE_PRECOMMIT_STATE: '
                                      f'BH={block_height} '
                                      f'instant_block_hash={bytes_to_hex(instant_block_hash)} '
                                      f'block_hash={bytes_to_hex(block_hash)}')

            self._icon_service_engine.commit(block_height, instant_block_hash, block_hash)
            response = MakeResponse.make_response(ExceptionCode.OK)
        except FatalException as e:
            self._log_exception(e, _TAG)
            response = MakeResponse.make_error_response(ExceptionCode.SYSTEM_ERROR, str(e))
            self._close()
        except IconServiceBaseException as icon_e:
            self._log_exception(icon_e, _TAG)
            response = MakeResponse.make_error_response(icon_e.code, icon_e.message)
        except Exception as e:
            self._log_exception(e, _TAG)
            response = MakeResponse.make_error_response(ExceptionCode.SYSTEM_ERROR, str(e))

        Logger.info(tag=_TAG, msg=f'WRITE_PRECOMMIT_STATE Response: {response}')
        return response
Пример #11
0
def test_icx_call_convert():
    version = 3
    from_addr = create_address()
    to_addr = create_address(1)
    data_type = "call"
    data_method = "get_balance"
    data_addr = create_address()

    request = {
        ConstantKeys.VERSION: hex(version),
        ConstantKeys.FROM: str(from_addr),
        ConstantKeys.TO: str(to_addr),
        ConstantKeys.DATA_TYPE: data_type,
        ConstantKeys.DATA: {
            ConstantKeys.METHOD: data_method,
            ConstantKeys.PARAMS: {
                ConstantKeys.ADDRESS: str(data_addr)
            }
        }
    }

    ret_params = TypeConverter.convert(request, ParamType.ICX_CALL)

    assert version == ret_params[ConstantKeys.VERSION]
    assert from_addr == ret_params[ConstantKeys.FROM]
    assert to_addr == ret_params[ConstantKeys.TO]
    assert data_type == ret_params[ConstantKeys.DATA_TYPE]

    data_params = ret_params[ConstantKeys.DATA]
    assert data_method == data_params[ConstantKeys.METHOD]
    data_params_params = data_params[ConstantKeys.PARAMS]
    assert data_addr != data_params_params[ConstantKeys.ADDRESS]
Пример #12
0
    def _rollback(self, request: dict) -> dict:
        Logger.info(tag=_TAG, msg=f"ROLLBACK Request: {request}")

        try:
            converted_params = TypeConverter.convert(request,
                                                     ParamType.ROLLBACK)
            block_height: int = converted_params[ConstantKeys.BLOCK_HEIGHT]
            block_hash: bytes = converted_params[ConstantKeys.BLOCK_HASH]
            Logger.info(
                tag=_TAG,
                msg=
                f"ROLLBACK: BH={block_height} block_hash={bytes_to_hex(block_hash)}"
            )

            response: dict = self._icon_service_engine.rollback(
                block_height, block_hash)
            response = MakeResponse.make_response(response)
        except FatalException as e:
            self._log_exception(e, _TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SYSTEM_ERROR, str(e))
            self._close()
        except IconServiceBaseException as icon_e:
            self._log_exception(icon_e, _TAG)
            response = MakeResponse.make_error_response(
                icon_e.code, icon_e.message)
        except BaseException as e:
            self._log_exception(e, _TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SYSTEM_ERROR, str(e))

        Logger.info(tag=_TAG, msg=f"ROLLBACK Response: {response}")
        return response
Пример #13
0
def test_icx_total_supply_convert():
    version = 3

    request = {ConstantKeys.VERSION: hex(version)}

    ret_params = TypeConverter.convert(request, ParamType.ICX_GET_TOTAL_SUPPLY)

    assert version == ret_params[ConstantKeys.VERSION]
Пример #14
0
def test_get_stake():
    address = create_address()

    request = {
        ConstantKeys.ADDRESS: str(address),
    }

    ret_params = TypeConverter.convert(request, ParamType.IISS_GET_STAKE)
    assert address == ret_params[ConstantKeys.ADDRESS]
Пример #15
0
def test_query_i_score():
    address = create_address()

    request = {
        ConstantKeys.ADDRESS: str(address),
    }

    ret_params = TypeConverter.convert(request, ParamType.IISS_QUERY_ISCORE)
    assert address == ret_params[ConstantKeys.ADDRESS]
Пример #16
0
def test_get_delegation():
    address = create_address()

    request = {
        ConstantKeys.ADDRESS: str(address),
    }

    ret_params = TypeConverter.convert(request, ParamType.IISS_GET_DELEGATION)
    assert address == ret_params[ConstantKeys.ADDRESS]
Пример #17
0
def test_set_governance_variable():
    irep = 12345

    request = {
        ConstantKeys.IREP: hex(irep),
    }

    ret_params = TypeConverter.convert(request, ParamType.IISS_SET_GOVERNANCE_VARIABLES)
    assert irep == ret_params[ConstantKeys.IREP]
Пример #18
0
def test_set_stake():
    stake = 1 * 10 ** 18

    request = {
        ConstantKeys.VALUE: hex(stake),
    }

    ret_params = TypeConverter.convert(request, ParamType.IISS_SET_STAKE)
    assert stake == ret_params[ConstantKeys.VALUE]
Пример #19
0
def test_get_prep_list():
    start_rank = 10
    end_rank = 20

    request = {
        ConstantKeys.START_RANKING: hex(start_rank),
        ConstantKeys.END_RANKING: hex(end_rank)
    }

    ret_params = TypeConverter.convert(request, ParamType.IISS_GET_PREP_LIST)
    assert start_rank == ret_params[ConstantKeys.START_RANKING]
    assert end_rank == ret_params[ConstantKeys.END_RANKING]
Пример #20
0
    def test_remove_precommit_convert(self):
        block_height = 1001
        block_hash = create_block_hash()

        request = {
            ConstantKeys.BLOCK_HEIGHT: hex(block_height),
            ConstantKeys.BLOCK_HASH: bytes.hex(block_hash)
        }

        ret_params = TypeConverter.convert(request, ParamType.REMOVE_PRECOMMIT)

        self.assertEqual(block_height, ret_params[ConstantKeys.BLOCK_HEIGHT])
        self.assertEqual(block_hash, ret_params[ConstantKeys.BLOCK_HASH])
Пример #21
0
def test_write_precommit_convert():
    block_height = 1001
    block_hash = create_block_hash()

    request = {
        ConstantKeys.BLOCK_HEIGHT: hex(block_height),
        ConstantKeys.BLOCK_HASH: bytes.hex(block_hash)
    }

    ret_params = TypeConverter.convert(request, ParamType.WRITE_PRECOMMIT)

    assert block_height == ret_params[ConstantKeys.BLOCK_HEIGHT]
    assert block_hash == ret_params[ConstantKeys.BLOCK_HASH]
Пример #22
0
    def test_icx_get_balance_convert(self):
        version = 3
        addr1 = create_address()

        request = {
            ConstantKeys.VERSION: hex(version),
            ConstantKeys.ADDRESS: str(addr1)
        }

        ret_params = TypeConverter.convert(request, ParamType.ICX_GET_BALANCE)

        self.assertEqual(version, ret_params[ConstantKeys.VERSION])
        self.assertEqual(addr1, ret_params[ConstantKeys.ADDRESS])
Пример #23
0
def test_icx_get_score_api_convert():
    version = 3

    score_addr = create_address(1)

    request = {
        ConstantKeys.VERSION: hex(version),
        ConstantKeys.ADDRESS: str(score_addr)
    }

    ret_params = TypeConverter.convert(request, ParamType.ICX_GET_SCORE_API)

    assert version == ret_params[ConstantKeys.VERSION]
    assert score_addr == ret_params[ConstantKeys.ADDRESS]
Пример #24
0
def _test_account_convert(balance: int):
    name = 'genesis'
    address = create_address()

    request = {
        ConstantKeys.NAME: name,
        ConstantKeys.ADDRESS: str(address),
        ConstantKeys.BALANCE: hex(balance)
    }

    ret_params = TypeConverter.convert(request, ParamType.ACCOUNT_DATA)

    assert name == ret_params[ConstantKeys.NAME]
    assert address == ret_params[ConstantKeys.ADDRESS]
    assert balance == ret_params[ConstantKeys.BALANCE]
Пример #25
0
    def _test_account_convert(self, balance: int):
        name = 'genesis'
        address = create_address()

        request = {
            ConstantKeys.NAME: name,
            ConstantKeys.ADDRESS: str(address),
            ConstantKeys.BALANCE: hex(balance)
        }

        ret_params = TypeConverter.convert(request, ParamType.ACCOUNT_DATA)

        self.assertEqual(name, ret_params[ConstantKeys.NAME])
        self.assertEqual(address, ret_params[ConstantKeys.ADDRESS])
        self.assertEqual(balance, ret_params[ConstantKeys.BALANCE])
Пример #26
0
    def _process_transaction_in_local(self, request: dict) -> dict:
        params = TypeConverter.convert(request, ParamType.TRANSACTION_PARAMS_DATA)
        params['txHash'] = create_tx_hash()
        tx = {
            'method': 'icx_sendTransaction',
            'params': params
        }

        prev_block, tx_results = self._make_and_req_block([tx])
        self._write_precommit_state(prev_block)

        # convert TX result as sdk style
        convert_transaction_result(tx_results[0])

        return tx_results[0]
Пример #27
0
 def _validate_transaction(self, request: dict):
     response = None
     try:
         converted_request = TypeConverter.convert(
             request, ParamType.VALIDATE_TRANSACTION)
         self._icon_service_engine.validate_transaction(converted_request)
         response = MakeResponse.make_response(ExceptionCode.OK)
     except IconServiceBaseException as icon_e:
         self._log_exception(icon_e, ICON_SERVICE_LOG_TAG)
         response = MakeResponse.make_error_response(icon_e.code, icon_e.message)
     except Exception as e:
         self._log_exception(e, ICON_SERVICE_LOG_TAG)
         response = MakeResponse.make_error_response(ExceptionCode.SERVER_ERROR, str(e))
     finally:
         Logger.info(f'pre_validate_check response with {response}', ICON_INNER_LOG_TAG)
         return response
Пример #28
0
def test_query_convert_icx_get_total_supply():
    method = "icx_getTotalSupply"
    version = 3

    request = {
        ConstantKeys.METHOD: method,
        ConstantKeys.PARAMS: {
            ConstantKeys.VERSION: hex(version)
        }
    }

    ret_params = TypeConverter.convert(request, ParamType.QUERY)

    assert method == ret_params[ConstantKeys.METHOD]

    params_params = ret_params[ConstantKeys.PARAMS]
    assert version == params_params[ConstantKeys.VERSION]
Пример #29
0
    def test_query_convert_icx_get_total_supply(self):
        method = "icx_getTotalSupply"
        version = 3

        request = {
            ConstantKeys.METHOD: method,
            ConstantKeys.PARAMS: {
                ConstantKeys.VERSION: hex(version)
            }
        }

        ret_params = TypeConverter.convert(request, ParamType.QUERY)

        self.assertEqual(method, ret_params[ConstantKeys.METHOD])

        params_params = ret_params[ConstantKeys.PARAMS]
        self.assertEqual(version, params_params[ConstantKeys.VERSION])
Пример #30
0
def test_write_precommit_convert_new_format():
    # newly defined interface (jira issue LC-306)
    block_height = 1001
    old_block_hash = create_block_hash()
    new_block_hash = create_block_hash()

    request = {
        ConstantKeys.BLOCK_HEIGHT: hex(block_height),
        ConstantKeys.OLD_BLOCK_HASH: bytes.hex(old_block_hash),
        ConstantKeys.NEW_BLOCK_HASH: bytes.hex(new_block_hash)
    }

    ret_params = TypeConverter.convert(request, ParamType.WRITE_PRECOMMIT)

    assert block_height == ret_params[ConstantKeys.BLOCK_HEIGHT]
    assert old_block_hash == ret_params[ConstantKeys.OLD_BLOCK_HASH]
    assert new_block_hash == ret_params[ConstantKeys.NEW_BLOCK_HASH]