def setUp(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( 'hxcf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf' ) self.addresses = [ empty_address, short_address_without_hx, short_address, long_address_without_hx, long_address ] self.db_name = 'icx.db' db = ContextDatabase.from_path(self.db_name) self.assertIsNotNone(db) self.storage = IcxStorage(db) context = IconScoreContext(IconScoreContextType.DIRECT) mock_block: 'Mock' = Mock(spec=Block) mock_block.attach_mock(Mock(return_value=0), 'height') context.block = mock_block self.context = context
def setUp(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' ) self.malformed_addresses = [ empty_address, short_address_without_hx, short_address, long_address_without_hx, long_address ] self.db_name = 'engine.db' db = ContextDatabase.from_path(self.db_name) self.engine = IcxEngine() self._from = Address.from_string('hx' + 'a' * 40) self.to = Address.from_string('hx' + 'b' * 40) self.genesis_address = Address.from_string('hx' + '0' * 40) self.fee_treasury_address = Address.from_string('hx' + '1' * 40) self.total_supply = 10**20 # 100 icx self.context = IconScoreContext(IconScoreContextType.DIRECT) icx_storage = IcxStorage(db) self.engine.open(icx_storage) self.engine.init_account(self.context, AccountType.GENESIS, 'genesis', self.genesis_address, self.total_supply) self.engine.init_account(self.context, AccountType.TREASURY, 'treasury', self.fee_treasury_address, 0)
def test_from_string(self): address = MalformedAddress.from_string('') self.assertEqual(address.prefix, AddressPrefix.EOA) self.assertEqual(address.body, b'') self.assertEqual(str(address), 'hx') short_address_without_hx = MalformedAddress.from_string('123124124125') self.assertEqual(short_address_without_hx.prefix, AddressPrefix.EOA) self.assertEqual(str(short_address_without_hx), 'hx123124124125') self.assertEqual(short_address_without_hx.body, bytes.fromhex('123124124125')) text = 'bf85fac2d1b507a2db9ce9526e6d91476f16a2d269f51636f9c4b2d512017faf' long_address_without_hx = MalformedAddress.from_string(text) self.assertEqual(long_address_without_hx.prefix, AddressPrefix.EOA) self.assertEqual(str(long_address_without_hx), f'hx{text}') self.assertEqual( long_address_without_hx.body, bytes.fromhex(text)) long_address = MalformedAddress.from_string(f'hx{text}') self.assertEqual(long_address, long_address_without_hx) text = 'hxa23651905d221dd36b' short_address = MalformedAddress.from_string(text) self.assertEqual(short_address.prefix, AddressPrefix.EOA) self.assertEqual(str(short_address), text) self.assertEqual(short_address.body, bytes.fromhex('a23651905d221dd36b'))
def convert_to_address(to: str) -> Union['Address', 'MalformedAddress']: try: address = Address.from_string(to) except InvalidParamsException: address = MalformedAddress.from_string(to) return address
def setUp(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' ) self.malformed_addresses = [ empty_address, short_address_without_hx, short_address, long_address_without_hx, long_address ] self.db_name = 'engine.db' db = ContextDatabase.from_path(self.db_name) self.engine = IcxEngine() self._from = Address.from_string('hx' + 'a' * 40) self.to = Address.from_string('hx' + 'b' * 40) self.genesis_address = Address.from_string('hx' + '0' * 40) self.fee_treasury_address = Address.from_string('hx' + '1' * 40) self.total_supply = 10**20 # 100 icx self.context = IconScoreContext(IconScoreContextType.DIRECT) block = Mock(spec=Block) block.attach_mock(Mock(return_value=0), 'height') self.context.block = block self.storage = IcxStorage(db) self.engine.open() accounts: list = [{ 'address': self.genesis_address, 'balance': self.total_supply }, { 'address': self.fee_treasury_address, 'balance': 0 }] self.context.storage = ContextStorage(deploy=None, fee=None, icx=self.storage, iiss=None, prep=None, issue=None, rc=None, meta=None) self.storage.put_genesis_accounts(self.context, accounts)
def test_invoke_v2_with_zero_fee_and_malformed_to_address(self): block_height = 1 block_hash = create_block_hash() block_timestamp = 0 tx_hash = create_tx_hash() value = 1 * 10 ** 18 to = MalformedAddress.from_string('') fixed_fee: int = 10 ** 16 tx_v2 = { 'method': 'icx_sendTransaction', 'params': { 'from': self.from_, 'to': to, 'value': value, 'fee': fixed_fee, 'timestamp': 1234567890, 'txHash': tx_hash } } block = Block(block_height, block_hash, block_timestamp, self.genesis_block.hash, 0) tx_results, state_root_hash, _, _ = self._engine.invoke(block, [tx_v2]) self.assertIsInstance(state_root_hash, bytes) self.assertEqual(len(state_root_hash), 32) self.assertEqual(len(tx_results), 1) tx_result: 'TransactionResult' = tx_results[0] self.assertIsNone(tx_result.failure) self.assertIsNone(tx_result.score_address) self.assertEqual(tx_result.status, 1) self.assertEqual(tx_result.block_height, block_height) self.assertEqual(tx_result.block_hash, block_hash) self.assertEqual(tx_result.tx_index, 0) self.assertEqual(tx_result.tx_hash, tx_hash) # step_used MUST BE 10**6 on protocol v2 self.assertEqual(tx_result.step_used, 10**6) step_price = self._engine._step_counter_factory.get_step_price() self.assertEqual(tx_result.step_price, step_price) # Write updated states to levelDB self._engine.commit(block.height, block.hash, None) # Check whether fee charging works well context = _create_context(IconScoreContextType.DIRECT) from_balance: int = IconScoreContext.engine.icx.get_balance( context, self.from_) to_balance: int = IconScoreContext.engine.icx.get_balance(context, to) fee = tx_result.step_price * tx_result.step_used self.assertEqual(0, fee) self.assertEqual(value, to_balance) self.assertEqual(from_balance, self._total_supply - value - fee)
def test_invoke_v2_with_zero_fee_and_malformed_to_address(self): block_height = 1 block_hash = create_block_hash() block_timestamp = 0 tx_hash = create_tx_hash() value = 1 * 10**18 to = MalformedAddress.from_string('') fixed_fee: int = 10**16 tx_v2 = { 'method': 'icx_sendTransaction', 'params': { 'from': self._admin.address, 'to': to, 'value': value, 'fee': fixed_fee, 'timestamp': 1234567890, 'txHash': tx_hash } } block = Block(block_height, block_hash, block_timestamp, self.genesis_block.hash, 0) tx_results, state_root_hash, _, _, is_shutdown = self.icon_service_engine.invoke( block, [tx_v2]) self.assertIsInstance(state_root_hash, bytes) self.assertEqual(len(state_root_hash), 32) self.assertEqual(len(tx_results), 1) self.assertFalse(is_shutdown) tx_result: 'TransactionResult' = tx_results[0] self.assertIsNone(tx_result.failure) self.assertIsNone(tx_result.score_address) self.assertEqual(tx_result.status, 1) self.assertEqual(tx_result.block_height, block_height) self.assertEqual(tx_result.block_hash, block_hash) self.assertEqual(tx_result.tx_index, 0) self.assertEqual(tx_result.tx_hash, tx_hash) # step_used MUST BE 10**6 on protocol v2 self.assertEqual(tx_result.step_used, 10**6) self.assertEqual(tx_result.step_price, 0) # Write updated states to levelDB self.icon_service_engine.commit(block.height, block.hash, block.hash) # Check whether fee charging works well from_balance: int = self.get_balance(self._admin.address) to_balance: int = self.get_balance(to) fee = tx_result.step_price * tx_result.step_used self.assertEqual(0, fee) self.assertEqual(value, to_balance) self.assertEqual(from_balance, icx_to_loop(TOTAL_SUPPLY) - value - fee)
def test_send_icx_using_malformed_address2(self): value1 = 1 * ICX_IN_LOOP malformed_address = MalformedAddress.from_string("11") tx = self.create_transfer_icx_tx(from_=self._admin, to_=malformed_address, value=value1, disable_pre_validate=True, support_v2=True) self.process_confirm_block_tx([tx]) self.assertEqual(value1, self.get_balance(malformed_address))
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 setUp(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( 'hxcf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf' ) self.addresses = [ empty_address, short_address_without_hx, short_address, long_address_without_hx, long_address ] self.db_name = 'icx.db' db = ContextDatabase.from_path(self.db_name) self.assertIsNotNone(db) self.storage = IcxStorage(db) context = IconScoreContext(IconScoreContextType.DIRECT) self.context = context
def test_send_icx_using_malformed_address2(self): value1 = 1 * self._icx_factor malformed_address = MalformedAddress.from_string("11") tx = self._make_icx_send_tx(self._genesis, malformed_address, value1, disable_pre_validate=True, support_v2=True) prev_block, tx_results = self._make_and_req_block([tx]) self._write_precommit_state(prev_block) self.assertEqual(tx_results[0].status, int(True)) query_request = { "address": malformed_address } response = self._query(query_request, 'icx_getBalance') self.assertEqual(response, value1)
class TestIcxEngineForMalformedAddress: MALFORMED_STRING_LIST = [ '', # empty '12341234', # short without hx 'hx1234512345', # short 'cf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf', # long without hx 'hxdf85fac2d0b507a2db9ce9526e6d01476f16a2d269f51636f9c4b2d512017faf' # long ] @pytest.mark.parametrize("malformed_address", [ MalformedAddress.from_string(string) for string in MALFORMED_STRING_LIST ]) def test_get_balance(self, context_with_icx_storage, icx_engine, malformed_address): balance = icx_engine.get_balance(context_with_icx_storage, malformed_address) assert balance == 0 def test_transfer(self, context_with_icx_storage, icx_engine, genesis_address, fee_treasury_address): amount = 10**18 # 1 icx for i, to in enumerate([ MalformedAddress.from_string(string) for string in self.MALFORMED_STRING_LIST ]): icx_engine.transfer(context=context_with_icx_storage, from_=genesis_address, to=to, amount=amount) from_balance = icx_engine.get_balance(context_with_icx_storage, genesis_address) fee_treasury_balance = icx_engine.get_balance( context_with_icx_storage, fee_treasury_address) to_balance = icx_engine.get_balance(context_with_icx_storage, to) assert to_balance == amount assert fee_treasury_balance == 0 assert TOTAL_SUPPLY == from_balance + fee_treasury_balance + amount * ( i + 1)
def test_transfer(self, context_with_icx_storage, icx_engine, genesis_address, fee_treasury_address): amount = 10**18 # 1 icx for i, to in enumerate([ MalformedAddress.from_string(string) for string in self.MALFORMED_STRING_LIST ]): icx_engine.transfer(context=context_with_icx_storage, from_=genesis_address, to=to, amount=amount) from_balance = icx_engine.get_balance(context_with_icx_storage, genesis_address) fee_treasury_balance = icx_engine.get_balance( context_with_icx_storage, fee_treasury_address) to_balance = icx_engine.get_balance(context_with_icx_storage, to) assert to_balance == amount assert fee_treasury_balance == 0 assert TOTAL_SUPPLY == from_balance + fee_treasury_balance + amount * ( i + 1)
def test_invoke_v2_with_malformed_to_address_and_type_converter(self): to = '' to_address = MalformedAddress.from_string(to) fixed_fee: int = 10**16 value = 1 * 10**18 block_height = 1 block_hash: bytes = create_block_hash(b'block') prev_block_hash: bytes = self.genesis_block.hash tx_hash: bytes = create_tx_hash(b'tx') timestamp: int = int(time.time() * 1000) request = { 'block': { 'blockHeight': hex(block_height), 'blockHash': block_hash.hex(), 'prevBlockHash': prev_block_hash.hex(), 'timestamp': str(timestamp) }, 'transactions': [{ 'method': 'icx_sendTransaction', 'params': { 'from': str(self._admin.address), 'to': to, 'fee': hex(fixed_fee), 'value': hex(value), 'timestamp': '0x574024617ae39', 'nonce': '0x1', 'signature': 'yKMiB12Os0ZK9+XYiBSwydvMXA0y/LS9HzmZwtczQ1VAK98/mGUOmpwTjByFArjdkx72GOWIOzu6eqyZnKeHBAE=', 'txHash': tx_hash.hex() } }] } params = TypeConverter.convert(request, ParamType.INVOKE) converted_block_params = params['block'] block = Block.from_dict(converted_block_params) self.assertEqual(block_height, block.height) self.assertEqual(block_hash, block.hash) self.assertEqual(prev_block_hash, block.prev_hash) self.assertEqual(timestamp, block.timestamp) transactions: list = params['transactions'] self.assertIsInstance(transactions[0]['params']['to'], MalformedAddress) tx_results, state_root_hash, _, _, is_shutdown = self.icon_service_engine.invoke( block, transactions) self.assertIsInstance(state_root_hash, bytes) self.assertEqual(len(state_root_hash), 32) self.assertEqual(len(tx_results), 1) self.assertFalse(is_shutdown) tx_result: 'TransactionResult' = tx_results[0] self.assertIsNone(tx_result.failure) self.assertIsNone(tx_result.score_address) self.assertEqual(tx_result.status, 1) self.assertEqual(tx_result.block_height, block_height) self.assertEqual(tx_result.block_hash, block_hash) self.assertEqual(tx_result.tx_index, 0) self.assertEqual(tx_result.tx_hash, tx_hash) # step_used MUST BE 10**6 on protocol v2 self.assertEqual(tx_result.step_used, 10**6) self.assertEqual(tx_result.step_price, 0) # Write updated states to levelDB self.icon_service_engine.commit(block.height, block.hash, block.hash) # Check whether fee charging works well from_balance: int = self.get_balance(self._admin.address) to_balance: int = self.get_balance(to_address) fee = tx_result.step_price * tx_result.step_used self.assertEqual(0, fee) self.assertEqual(value, to_balance) self.assertEqual(from_balance, icx_to_loop(TOTAL_SUPPLY) - value - fee)
def test_from_string_with_prefix(self): text = 'hxa23651905d221dd36b' short_address = MalformedAddress.from_string(text) assert short_address.prefix == AddressPrefix.EOA assert str(short_address) == text assert short_address.body == bytes.fromhex('a23651905d221dd36b')
def test_malformed_address(self): address: str = "hx123456" addr = MalformedAddress.from_string(address) self.assertEqual(str(addr), address)
def test_from_string_without_prefix(self, address_string): address = MalformedAddress.from_string(address_string) assert address.prefix == AddressPrefix.EOA assert address.body == bytes.fromhex(address_string) assert str(address) == f'hx{address_string}'