예제 #1
0
    def test_put_deploy_tx_params(self):
        context = Mock(spec=IconScoreContext)
        tx_hash = create_tx_hash()
        tx_params = IconScoreDeployTXParams(tx_hash, DeployType.INSTALL,
                                            create_address(1), {})
        self.storage._create_db_key = Mock(return_value=tx_hash)
        self.storage._db.put = Mock()

        self.storage.put_deploy_tx_params(context, tx_params)
        self.storage._db.put.assert_called_once_with(context,
                                                     tx_params.tx_hash,
                                                     tx_params.to_bytes())
예제 #2
0
    def test_governance_call_about_add_blacklist_already_blacklist(self):
        score_addr = create_address(1)

        self.score_call(from_=self._admin,
                        to_=GOVERNANCE_SCORE_ADDRESS,
                        func_name="addToScoreBlackList",
                        params={"address": str(score_addr)})

        self.score_call(from_=self._admin,
                        to_=GOVERNANCE_SCORE_ADDRESS,
                        func_name="addToScoreBlackList",
                        params={"address": str(score_addr)})
    def load_proj(self, proj: str) -> callable:
        addr_score = create_address(1, data=proj.encode())
        target_path = path.join(self._score_path, addr_score.to_bytes().hex())
        makedirs(target_path, exist_ok=True)
        tx_hash = create_tx_hash()
        converted_tx_hash = f'0x{bytes.hex(tx_hash)}'
        target_path = path.join(target_path, converted_tx_hash)

        ref_path = path.join(TEST_ROOT_PATH, 'tests/sample/{}'.format(proj))
        symlink(ref_path, target_path, target_is_directory=True)
        score_path = self._loader.make_score_path(addr_score, tx_hash)
        return self._loader.load_score(score_path)
예제 #4
0
    def test_get_db(self):
        tx_hash = bytes.hex(create_tx_hash())
        from_ = create_address(AddressPrefix.EOA)
        to_ = create_address(AddressPrefix.CONTRACT)

        request = create_request([ReqData(tx_hash, from_, to_, 'call', {})])

        self._inner_task._icon_service_engine.\
            _icx_context_db.get = Mock(return_value=b'1' * 100)

        # noinspection PyUnusedLocal
        def intercept_invoke(*args, **kwargs):
            ContextContainer._push_context(args[0])

            context_db = self._inner_task._icon_service_engine._icx_context_db
            score = SampleScore(IconScoreDatabase(to_, context_db))
            score.get_db()

            ContextContainer._pop_context()

        IconScoreEngine.invoke = Mock(side_effect=intercept_invoke)

        result = self._inner_task_invoke(request)
        IconScoreEngine.invoke.assert_called()

        self.assertEqual(result['txResults'][tx_hash]['status'], '0x1')

        call_args_for_apply_step = self.step_counter.apply_step.call_args_list

        self.assertEqual((StepType.DEFAULT, 1), call_args_for_apply_step[0][0])
        self.assertEqual((StepType.INPUT, 0), call_args_for_apply_step[1][0])
        self.assertEqual((StepType.CONTRACT_CALL, 1),
                         call_args_for_apply_step[2][0])
        self.assertEqual((StepType.GET, 100), call_args_for_apply_step[3][0])
        self.assertEqual(4, len(call_args_for_apply_step))

        step_used = self._calc_step_used(0, len(call_args_for_apply_step))

        # check stepUsed value
        self._assert_step_used(step_used, request, tx_hash)
예제 #5
0
def test_query_convert_icx_call():
    method = "icx_call"
    version = 3
    from_addr = create_address()
    to_addr = create_address(1)
    data_type = "call"
    data_method = "get_balance"
    data_addr = create_address()

    request = {
        ConstantKeys.METHOD: method,
        ConstantKeys.PARAMS: {
            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.QUERY)

    assert method == ret_params[ConstantKeys.METHOD]

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

    data_params = params_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_query_convert_icx_call(self):
        method = "icx_call"
        version = 3
        from_addr = create_address()
        to_addr = create_address(1)
        data_type = "call"
        data_method = "get_balance"
        data_addr = create_address()

        request = {
            ConstantKeys.METHOD: method,
            ConstantKeys.PARAMS: {
                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.QUERY)

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

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

        data_params = params_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])
    def test_is_inactive_score(self):
        address = create_address()
        self.validator._score_manager.is_score_active = Mock(return_value=True)
        self.assertFalse(self.validator._is_inactive_score(address))
        self.validator._score_manager.is_score_active.assert_called_once_with(
            None, address)

        address = create_address()
        self.validator._score_manager.is_score_active = Mock(
            return_value=False)
        self.assertFalse(self.validator._is_inactive_score(address))
        self.validator._score_manager.is_score_active.assert_called_once_with(
            None, address)

        address = ZERO_SCORE_ADDRESS
        self.validator._score_manager.is_score_active = Mock(return_value=True)
        self.assertFalse(self.validator._is_inactive_score(address))
        self.validator._score_manager.is_score_active.assert_called_once_with(
            None, address)

        address = ZERO_SCORE_ADDRESS
        self.validator._score_manager.is_score_active = Mock(
            return_value=False)
        self.assertFalse(self.validator._is_inactive_score(address))
        self.validator._score_manager.is_score_active.assert_called_once_with(
            None, address)

        address = create_address(1)
        self.validator._score_manager.is_score_active = Mock(return_value=True)
        self.assertFalse(self.validator._is_inactive_score(address))
        self.validator._score_manager.is_score_active.assert_called_once_with(
            None, address)

        address = create_address(1)
        self.validator._score_manager.is_score_active = Mock(
            return_value=False)
        self.assertTrue(self.validator._is_inactive_score(address))
        self.validator._score_manager.is_score_active.assert_called_once_with(
            None, address)
예제 #8
0
    def test_delete_account(self):
        context = self.context
        account = Account()
        account.address = create_address(AddressPrefix.EOA)
        self.storage.put_account(context, account.address, account)

        ret = self.storage.is_address_present(context, account.address)
        self.assertTrue(ret)

        self.storage.delete_account(context, account.address)

        ret = self.storage.is_address_present(context, self.address)
        self.assertFalse(ret)
예제 #9
0
    def test_query_db(self, score_query):
        from_ = create_address(AddressPrefix.EOA)
        to_ = create_address(AddressPrefix.CONTRACT)

        request = {
            'method': 'icx_call',
            'params': {
                'to': to_.__str__(),
                'from': from_.__str__(),
            },
        }

        self._inner_task._icon_service_engine. \
            _icx_context_db.get = Mock(return_value=b'1' * 100)

        # noinspection PyUnusedLocal
        def intercept_query(*args, **kwargs):
            ContextContainer._push_context(args[0])
            context_db = self._inner_task._icon_service_engine._icx_context_db
            ori_func = IconScoreContextUtil.get_owner
            IconScoreContextUtil.get_owner = Mock()
            score = SampleScore(IconScoreDatabase(to_, context_db))
            IconScoreContextUtil.get_owner = ori_func
            ret = score.query_db()
            ContextContainer._pop_context()
            return ret

        score_query.side_effect = intercept_query

        result = self._inner_task._query(request)
        score_query.assert_called()

        self.assertIsNotNone(result)

        call_args_for_apply_step = self.step_counter.apply_step.call_args_list

        self.assertEqual((StepType.CONTRACT_CALL, 1),
                         call_args_for_apply_step[0][0])
        self.assertEqual((StepType.GET, 100), call_args_for_apply_step[1][0])
    def test_get_score_owner(self):
        context = Mock(spec=IconScoreContext)
        score_address = create_address(1)

        self.storage.get_deploy_info = Mock(return_value=None)
        self.assertEqual(False, self.storage.is_score_active(context, score_address))
        self.storage.get_deploy_info.assert_called_once_with(context, score_address)

        deploy_info = Mock(spec=IconScoreDeployInfo)
        deploy_info.attach_mock(Mock(), 'owner')
        self.storage.get_deploy_info = Mock(return_value=deploy_info)
        self.assertEqual(deploy_info.owner, self.storage.get_score_owner(context, score_address))
        self.storage.get_deploy_info.assert_called_once_with(context, score_address)
    def test_success_variable(self):
        test_var = VarDB('test_var', self.db, value_type=int)
        self.assertNotEqual(test_var._db, self.db)
        self.assertEqual(test_var._db._prefix, b'\x02')

        test_var.set(10**19+1)

        self.assertEqual(test_var.get(), 10**19+1)

        test_var2 = VarDB(2,
                          self.db, value_type=Address)
        address = create_address(AddressPrefix.CONTRACT)
        test_var2.set(address)
        data = test_var2.get()
        self.assertEqual(data, address)

        test_var4 = VarDB(4,
                          self.db, value_type=Address)

        address3 = create_address(AddressPrefix.CONTRACT)
        test_var4.set(address3)
        self.assertEqual(test_var4.get(), address3)
예제 #12
0
def test_v2_invoke_convert():
    method = "icx_sendTransaction"
    tx_hash = create_block_hash()
    from_addr = create_address()
    to_addr = create_address(1)
    value = 10 * ICX_FACTOR
    fee = 10 * ICX_FEE
    timestamp = 12345
    nonce = 123
    signature = SIGNATURE

    request_params = {
        ConstantKeys.METHOD: method,
        ConstantKeys.PARAMS: {
            ConstantKeys.OLD_TX_HASH: bytes.hex(tx_hash),
            ConstantKeys.FROM: str(from_addr),
            ConstantKeys.TO: str(to_addr),
            ConstantKeys.VALUE: hex(value),
            ConstantKeys.FEE: hex(fee),
            ConstantKeys.TIMESTAMP: hex(timestamp),
            ConstantKeys.NONCE: hex(nonce),
            ConstantKeys.SIGNATURE: signature
        }
    }

    ret_params = TypeConverter.convert(request_params,
                                       ParamType.VALIDATE_TRANSACTION)

    assert method, ret_params[ConstantKeys.METHOD]

    params_params = ret_params[ConstantKeys.PARAMS]
    assert tx_hash == params_params[ConstantKeys.TX_HASH]
    assert from_addr == params_params[ConstantKeys.FROM]
    assert to_addr == params_params[ConstantKeys.TO]
    assert value == params_params[ConstantKeys.VALUE]
    assert fee == params_params[ConstantKeys.FEE]
    assert timestamp == params_params[ConstantKeys.TIMESTAMP]
    assert nonce == params_params[ConstantKeys.NONCE]
    assert signature == params_params[ConstantKeys.SIGNATURE]
예제 #13
0
def test_transaction_convert_malformed_address_success():
    method = "icx_sendTransaction"
    tx_hash = create_block_hash()
    from_addr = create_address()
    to_addr = ""
    value = 10 * ICX_FACTOR
    data_type = "deploy"
    content_type = "application/zip"
    content = CONTENT

    with pytest.raises(BaseException) as e:
        _test_transaction_convert(method, tx_hash, from_addr, to_addr, value, data_type,
                                  content_type=content_type, content=content)
예제 #14
0
def test_call_data_convert():
    method = 'icx_sendTransaction'
    data_from = create_address()
    data_to = create_address()
    data_value = 1 * ICX_FACTOR

    request = {
        ConstantKeys.METHOD: method,
        ConstantKeys.PARAMS: {
            ConstantKeys.FROM: str(data_from),
            ConstantKeys.TO: str(data_to),
            ConstantKeys.VALUE: hex(data_value)
        }
    }

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

    assert method == ret_params[ConstantKeys.METHOD]
    params = ret_params[ConstantKeys.PARAMS]
    assert data_from != params[ConstantKeys.FROM]
    assert data_to != params[ConstantKeys.TO]
    assert data_value != params[ConstantKeys.VALUE]
    def test_get_db(self):
        tx_hash = bytes.hex(create_tx_hash())
        from_ = create_address(AddressPrefix.EOA)
        to_ = create_address(AddressPrefix.CONTRACT)

        request = create_request([ReqData(tx_hash, from_, to_, 'call', {})])

        self._inner_task._icon_service_engine.\
            _icx_context_db.get = Mock(return_value=b'1' * 100)

        # noinspection PyUnusedLocal
        def intercept_invoke(*args, **kwargs):
            ContextContainer._push_context(args[0])
            context_db = self._inner_task._icon_service_engine._icx_context_db
            score = SampleScore(IconScoreDatabase(to_, context_db))
            score.get_db()

        score_engine_invoke = Mock(side_effect=intercept_invoke)
        self._inner_task._icon_service_engine._validate_score_blacklist = Mock(
        )
        self._inner_task._icon_service_engine. \
            _icon_score_engine.invoke = score_engine_invoke

        self._inner_task._icon_service_engine._icon_score_mapper.get_icon_score = Mock(
            return_value=None)
        result = self._inner_task._invoke(request)
        score_engine_invoke.assert_called()

        self.assertEqual(result['txResults'][tx_hash]['status'], '0x1')

        self.assertEqual(self.step_counter.apply_step.call_args_list[0][0],
                         (StepType.DEFAULT, 1))
        self.assertEqual(self.step_counter.apply_step.call_args_list[1][0],
                         (StepType.INPUT, 0))
        self.assertEqual(self.step_counter.apply_step.call_args_list[2][0],
                         (StepType.CONTRACT_CALL, 1))
        self.assertEqual(self.step_counter.apply_step.call_args_list[3][0],
                         (StepType.GET, 100))
        self.assertEqual(len(self.step_counter.apply_step.call_args_list), 4)
예제 #16
0
    def test_get_put_account(self):
        context = self.context

        address = create_address(AddressPrefix.EOA)
        coin_part: 'CoinPart' = CoinPart()
        account: 'Account' = Account(address, 0, coin_part=coin_part)
        account.deposit(10**19)

        self.storage.put_account(context, account)

        address: 'Address' = create_address(AddressPrefix.EOA)
        coin_part: 'CoinPart' = CoinPart()
        account: 'Account' = Account(address,
                                     self.context.block.height,
                                     coin_part=coin_part)

        account.deposit(10**19)

        self.storage.put_account(self.context, account)

        account2 = self.storage.get_account(self.context, account.address)
        self.assertEqual(account, account2)
    def test_set_delegation(self):
        address1 = create_address()
        value1 = 1 * 10**18

        address2 = create_address()
        value2 = 2 * 10**18

        request = [{
            ConstantKeys.ADDRESS: str(address1),
            ConstantKeys.VALUE: hex(value1)
        }, {
            ConstantKeys.ADDRESS: str(address2),
            ConstantKeys.VALUE: hex(value2)
        }]

        ret_delegations = TypeConverter.convert(request,
                                                ParamType.IISS_SET_DELEGATION)
        self.assertEqual(address1, ret_delegations[0][ConstantKeys.ADDRESS],
                         f'{type(ret_delegations[0][ConstantKeys.ADDRESS])}')
        self.assertEqual(value1, ret_delegations[0][ConstantKeys.VALUE])
        self.assertEqual(address2, ret_delegations[1][ConstantKeys.ADDRESS])
        self.assertEqual(value2, ret_delegations[1][ConstantKeys.VALUE])
예제 #18
0
    def load_proj(self, proj: str) -> callable:
        score_address: 'Address' = create_address(1, data=proj.encode())
        score_path = os.path.join(self._score_root_path,
                                  score_address.to_bytes().hex())
        os.makedirs(score_path, exist_ok=True)

        tx_hash: bytes = create_tx_hash()
        score_deploy_path: str = os.path.join(score_path, f'0x{tx_hash.hex()}')

        ref_path = os.path.join(TEST_ROOT_PATH, 'tests/sample/{}'.format(proj))
        os.symlink(ref_path, score_deploy_path, target_is_directory=True)
        return IconScoreClassLoader.run(score_address, tx_hash,
                                        self._SCORE_ROOT_PATH)
예제 #19
0
    def test_transaction_convert_malformed_address_success(self):
        method = "icx_sendTransaction"
        tx_hash = create_block_hash()
        from_addr = create_address()
        to_addr = ""
        value = 10 * self.icx_factor
        data_type = "deploy"
        content_type = "application/zip"
        content = self.content

        with self.assertRaises(BaseException) as e:
            self._test_transaction_convert(method, tx_hash, from_addr, to_addr, value, data_type,
                                           content_type=content_type, content=content)
예제 #20
0
    def test_func_param_address1(self):
        value = create_address()
        params = {"value": str(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": str(value)}
        TypeConverter.adjust_params_to_method(
            self.test_score.func_param_address1, params)
        self.assertEqual(value, self.test_score.func_param_address1(**params))
예제 #21
0
    def test_hash_writable(self):
        tx_hash = bytes.hex(create_tx_hash())
        from_ = create_address(AddressPrefix.EOA)
        to_ = create_address(AddressPrefix.CONTRACT)

        request = create_request([ReqData(tx_hash, from_, to_, 'call', {})])

        data_to_hash = b'1234'

        # noinspection PyUnusedLocal
        def intercept_invoke(*args, **kwargs):
            ContextContainer._push_context(args[0])
            context_db = self._inner_task._icon_service_engine._icx_context_db
            score = SampleScore(IconScoreDatabase(to_, context_db))
            score.hash_writable(data_to_hash)
            ContextContainer._pop_context()

        IconScoreEngine.invoke = Mock(side_effect=intercept_invoke)

        result = self._inner_task_invoke(request)
        IconScoreEngine.invoke.assert_called()

        self.assertEqual(result['txResults'][tx_hash]['status'], '0x1')

        call_args_for_apply_step = self.step_counter.apply_step.call_args_list

        self.assertEqual((StepType.DEFAULT, 1), call_args_for_apply_step[0][0])
        self.assertEqual((StepType.INPUT, 0), call_args_for_apply_step[1][0])
        self.assertEqual((StepType.CONTRACT_CALL, 1),
                         call_args_for_apply_step[2][0])
        self.assertEqual(3, len(call_args_for_apply_step))

        # step_counter.consume_step() should be called in sha3_256() only if context.revision is more than 2
        self.step_counter.consume_step.assert_not_called()

        step_used = self._calc_step_used(0, len(call_args_for_apply_step))

        # check stepUsed value
        self._assert_step_used(step_used, request, tx_hash)
예제 #22
0
    def test_get_put_delete_score_fee_with_none_type(self):
        context = self.context
        score_address = create_address(AddressPrefix.CONTRACT)

        deposit_meta = DepositMeta()
        self.storage.put_deposit_meta(context, score_address, deposit_meta)

        deposit_meta_2 = self.storage.get_deposit_meta(context, score_address)
        self.assertEqual(deposit_meta, deposit_meta_2)

        self.storage.delete_deposit_meta(context, score_address)
        deposit_meta_2 = self.storage.get_deposit_meta(context, score_address)
        self.assertIsNone(deposit_meta_2)
예제 #23
0
    def setUp(self):
        self.db_name = 'icx.db'
        self.address = create_address(AddressPrefix.EOA)
        db = ContextDatabase.from_path(self.db_name)
        self.assertIsNotNone(db)

        self.storage = IcxStorage(db)

        self.factory = IconScoreContextFactory(max_size=1)
        context = self.factory.create(IconScoreContextType.DIRECT)
        context.tx_batch = TransactionBatch()
        context.block_batch = BlockBatch()
        self.context = context
예제 #24
0
    def test_transfer_step(self):
        tx_hash = bytes.hex(create_tx_hash())
        from_ = create_address(AddressPrefix.EOA)
        to_ = create_address(AddressPrefix.EOA)

        request = create_request([
            ReqData(tx_hash, from_, to_, "", ""),
        ])

        result = self._inner_task_invoke(request)
        self.assertEqual(result['txResults'][tx_hash]['status'], '0x1')

        call_args_for_apply_step = self.step_counter.apply_step.call_args_list

        self.assertEqual((StepType.DEFAULT, 1), call_args_for_apply_step[0][0])
        self.assertEqual((StepType.INPUT, 0), call_args_for_apply_step[1][0])
        self.assertEqual(2, len(call_args_for_apply_step))

        step_used = self._calc_step_used(0, len(call_args_for_apply_step))

        # check stepUsed value
        self._assert_step_used(step_used, request, tx_hash)
    def test_governance_call_about_add_deployer_already_deployer(self):
        eoa_addr = create_address()
        tx_result = self._external_call(self._admin,
                                        GOVERNANCE_SCORE_ADDRESS,
                                        'addDeployer',
                                        {"address": str(eoa_addr)})
        self.assertEqual(tx_result.status, int(True))

        tx_result = self._external_call(self._admin,
                                        GOVERNANCE_SCORE_ADDRESS,
                                        'addDeployer',
                                        {"address": str(eoa_addr)})
        self.assertEqual(tx_result.status, int(True))
예제 #26
0
    def test_call_data_convert(self):
        method = 'icx_sendTransaction'
        data_from = create_address()
        data_to = create_address()
        data_value = 1 * self.icx_factor

        request = {
            ConstantKeys.METHOD: method,
            ConstantKeys.PARAMS: {
                ConstantKeys.FROM: str(data_from),
                ConstantKeys.TO: str(data_to),
                ConstantKeys.VALUE: hex(data_value)
            }
        }

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

        self.assertEqual(method, ret_params[ConstantKeys.METHOD])
        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])
예제 #27
0
    def test_v2_invoke_convert(self):
        method = "icx_sendTransaction"
        tx_hash = create_block_hash()
        from_addr = create_address()
        to_addr = create_address(1)
        value = 10 * self.icx_factor
        fee = 10 * self.icx_fee
        timestamp = 12345
        nonce = 123
        signature = self.signature

        request_params = {
            ConstantKeys.METHOD: method,
            ConstantKeys.PARAMS: {
                ConstantKeys.OLD_TX_HASH: bytes.hex(tx_hash),
                ConstantKeys.FROM: str(from_addr),
                ConstantKeys.TO: str(to_addr),
                ConstantKeys.VALUE: hex(value),
                ConstantKeys.FEE: hex(fee),
                ConstantKeys.TIMESTAMP: hex(timestamp),
                ConstantKeys.NONCE: hex(nonce),
                ConstantKeys.SIGNATURE: signature
            }
        }

        ret_params = TypeConverter.convert(request_params,
                                           ParamType.VALIDATE_TRANSACTION)

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

        params_params = ret_params[ConstantKeys.PARAMS]
        self.assertEqual(tx_hash, params_params[ConstantKeys.TX_HASH])
        self.assertEqual(from_addr, params_params[ConstantKeys.FROM])
        self.assertEqual(to_addr, params_params[ConstantKeys.TO])
        self.assertEqual(value, params_params[ConstantKeys.VALUE])
        self.assertEqual(fee, params_params[ConstantKeys.FEE])
        self.assertEqual(timestamp, params_params[ConstantKeys.TIMESTAMP])
        self.assertEqual(nonce, params_params[ConstantKeys.NONCE])
        self.assertEqual(signature, params_params[ConstantKeys.SIGNATURE])
예제 #28
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])
예제 #29
0
    def test_account_for_stake_rev_multiple_unstake1(self):
        address: 'Address' = create_address()
        context: 'IconScoreContext' = Mock(spec=IconScoreContext)
        unstake_slot_max = 10
        context.configure_mock(unstake_slot_max=unstake_slot_max)
        context.configure_mock(revision=Revision.MULTIPLE_UNSTAKE.value)

        coin_part: 'CoinPart' = CoinPart()
        stake_part: 'StakePart' = StakePart()
        account = Account(address,
                          0,
                          Revision.MULTIPLE_UNSTAKE.value,
                          coin_part=coin_part,
                          stake_part=stake_part)

        balance = 1000
        account.deposit(balance)

        stake1 = 500
        unstake_block_height = 0
        remain_balance = balance - stake1

        account.set_stake(context, stake1, 0)

        self.assertEqual(stake1, account.stake)
        self.assertEqual(0, account.unstake)
        self.assertEqual(unstake_block_height, account.unstake_block_height)
        self.assertEqual(remain_balance, account.balance)

        stake2 = 100
        block_height = 10
        unstake = stake1 - stake2
        remain_balance = balance - stake1
        account.set_stake(context, stake2, block_height)
        expected_unstake_info = [[unstake, block_height]]

        self.assertEqual(stake2, account.stake)
        self.assertEqual(0, account.unstake)
        self.assertEqual(0, account.unstake_block_height)
        self.assertEqual(expected_unstake_info, account.unstakes_info)
        self.assertEqual(remain_balance, account.balance)

        stake3 = 600
        block_height = 15
        account.set_stake(context, stake3, block_height)
        expected_unstake_info = []
        expected_balance = 400

        self.assertEqual(stake3, account.stake)
        self.assertEqual(expected_unstake_info, account.unstakes_info)
        self.assertEqual(expected_balance, account.balance)
    def test_call(self, mocked_score_engine_get_icon_score,
                  mocked_score_engine_convert_score_params_by_annotations):
        context = IconScoreContext(IconScoreContextType.INVOKE)
        context.new_icon_score_mapper = IconScoreMapper()

        def intercept_score_base_call(func_name: str, kw_params: dict):
            self.assertEqual(func_name, 'score_method')
            # should be equal to converted_params
            self.assertEqual(kw_params, converted_params)
            return "__call method called"

        score_address = Mock(spec=Address)
        score_object = Mock(spec=IconScoreBase)
        mocked_score_engine_get_icon_score.return_value = score_object

        primitive_params = {
            "address": str(create_address(AddressPrefix.EOA)),
            "integer": "0x10"
        }
        converted_params = {
            "address": create_address(AddressPrefix.EOA),
            "integer": 10
        }
        mocked_score_engine_convert_score_params_by_annotations.return_value = converted_params
        context.set_func_type_by_icon_score = Mock()
        setattr(score_object, ATTR_SCORE_CALL,
                Mock(side_effect=intercept_score_base_call))

        # set method, and params, method cannot be None as pre-validate it
        data = {'method': 'score_method', 'params': primitive_params}
        result = IconScoreEngine._call(context, score_address, data)

        self.assertEqual(result, "__call method called")
        IconScoreEngine._get_icon_score.assert_called()
        IconScoreEngine._convert_score_params_by_annotations.assert_called()
        context.set_func_type_by_icon_score.assert_called()
        call = getattr(score_object, ATTR_SCORE_CALL)
        call.assert_called()