def test_func_param_address2(): value = create_address() params = {"value": str(value)} annotations = TypeConverter.make_annotations_from_method( TEST_SCORE.func_param_address2) TypeConverter.convert_data_params(annotations, params) assert value == TEST_SCORE.func_param_address2(**params)
def test_func_param_int_none(): value = None params = {"value": value} annotations = TypeConverter.make_annotations_from_method( TEST_SCORE.func_param_int) TypeConverter.convert_data_params(annotations, params) assert value == TEST_SCORE.func_param_int(**params)
def test_func_param_bool(): value = True params = {"value": hex(int(value))} annotations = TypeConverter.make_annotations_from_method( TEST_SCORE.func_param_bool) TypeConverter.convert_data_params(annotations, params) assert value == TEST_SCORE.func_param_bool(**params)
def test_func_param_bytes(self): value = b'bytes' params = {"value": bytes.hex(value)} annotations = TypeConverter.make_annotations_from_method( self.test_score.func_param_bytes) TypeConverter.convert_data_params(annotations, params) self.assertEqual(value, self.test_score.func_param_bytes(**params))
def test_func_param_str(): value = 'a' params = {"value": value} annotations = TypeConverter.make_annotations_from_method( TEST_SCORE.func_param_str) TypeConverter.convert_data_params(annotations, params) assert value == TEST_SCORE.func_param_str(**params)
def test_func_param_bytes(): value = b'bytes' params = {"value": bytes.hex(value)} annotations = TypeConverter.make_annotations_from_method( TEST_SCORE.func_param_bytes) TypeConverter.convert_data_params(annotations, params) assert value == TEST_SCORE.func_param_bytes(**params)
def test_func_param_bool(self): value = True params = {"value": hex(int(value))} annotations = TypeConverter.make_annotations_from_method( self.test_score.func_param_bool) TypeConverter.convert_data_params(annotations, params) self.assertEqual(value, self.test_score.func_param_bool(**params)) params = {"value": hex(int(value))} TypeConverter.adjust_params_to_method(self.test_score.func_param_bool, params) self.assertEqual(value, self.test_score.func_param_bool(**params))
def test_func_param_str(self): value = 'a' params = {"value": value} annotations = TypeConverter.make_annotations_from_method( self.test_score.func_param_str) TypeConverter.convert_data_params(annotations, params) self.assertEqual(value, self.test_score.func_param_str(**params)) params = {"value": value} TypeConverter.adjust_params_to_method(self.test_score.func_param_str, params) self.assertEqual(value, self.test_score.func_param_str(**params))
def test_func_param_address_none(self): value = None params = {"value": value} annotations = TypeConverter.make_annotations_from_method( self.test_score.func_param_address1) TypeConverter.convert_data_params(annotations, params) self.assertEqual(value, self.test_score.func_param_address1(**params)) params = {"value": value} TypeConverter.adjust_params_to_method( self.test_score.func_param_address1, params) self.assertEqual(value, self.test_score.func_param_address1(**params))
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
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
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
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)
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]
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]
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]
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
def test_sample_result(self): from_ = Address.from_data(AddressPrefix.EOA, b'from') to_ = Address.from_data(AddressPrefix.CONTRACT, b'to') self._mock_context.tx = Transaction(os.urandom(32), 1234, from_, to_, 0) self._mock_context.msg = Message(from_) IconScoreContext.engine.deploy.invoke = Mock() self._icon_service_engine._process_transaction = Mock(return_value=to_) tx_result = self._icon_service_engine._handle_icx_send_transaction( self._mock_context, { 'from': from_, 'to': to_ }) tx_result.score_address = to_ tx_result.event_logs = [ EventLog( Address.from_data(AddressPrefix.CONTRACT, b'addr_to'), [b'indexed', Address.from_data(AddressPrefix.EOA, b'index')], [True, 1234, 'str', None, b'test']) ] tx_result.logs_bloom = BloomFilter() tx_result.logs_bloom.add(b'1') tx_result.logs_bloom.add(b'2') tx_result.logs_bloom.add(b'3') tx_result.block = Block(123, hashlib.sha3_256(b'block').digest(), 1, None, 0) camel_dict = tx_result.to_dict(to_camel_case) self.assertIn('txHash', camel_dict) self.assertIn('blockHeight', camel_dict) self.assertIn('txIndex', camel_dict) self.assertIn('to', camel_dict) self.assertIn('scoreAddress', camel_dict) self.assertIn('stepUsed', camel_dict) self.assertIn('stepPrice', camel_dict) self.assertIn('eventLogs', camel_dict) self.assertIn('logsBloom', camel_dict) self.assertIn('status', camel_dict) self.assertEqual(1, len(camel_dict['eventLogs'])) self.assertIn('scoreAddress', camel_dict['eventLogs'][0]) self.assertIn('indexed', camel_dict['eventLogs'][0]) self.assertIn('data', camel_dict['eventLogs'][0]) self.assertEqual(256, len(camel_dict['logsBloom'])) converted_result = TypeConverter.convert_type_reverse(camel_dict) self.assertFalse(converted_result['txHash'].startswith('0x')) self.assertTrue(converted_result['blockHeight'].startswith('0x')) self.assertTrue(converted_result['txIndex'].startswith('0x')) self.assertTrue(converted_result['to'].startswith('cx')) self.assertTrue(converted_result['scoreAddress'].startswith('cx')) self.assertTrue(converted_result['stepUsed'].startswith('0x')) self.assertTrue(converted_result['logsBloom'].startswith('0x')) self.assertTrue(converted_result['status'].startswith('0x'))
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]
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]
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]
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]
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]
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]