Ejemplo n.º 1
0
    def test_withdraw_deposit(self):
        tx_hash = os.urandom(32)
        tx_hash_hex = bytes.hex(tx_hash)
        deposit_id = self.test_add_deposit()
        amount, penalty = 4700, 300

        IconScoreContext.engine.fee.withdraw_deposit = Mock(
            return_value=(amount, penalty))
        IconScoreContext.engine.fee.charge_transaction_fee = \
            Mock(return_value={self.from_: 9000})

        data = {'action': 'withdraw', 'id': f"0x{bytes.hex(deposit_id)}"}

        expected_event_log = [{
            "scoreAddress":
            str(self.score),
            "indexed": [
                "DepositWithdrawn(bytes,Address,int,int)",
                f"0x{bytes.hex(deposit_id)}",
                str(self.from_)
            ],
            "data": [hex(amount), hex(penalty)]
        }]

        request = create_request([
            ReqData(tx_hash_hex, self.from_, self.score, 0, 'deposit', data),
        ])

        result = self._inner_task_invoke(request)
        tx_result = result['txResults'][tx_hash_hex]

        self.assertEqual('0x1', tx_result['status'])
        self.assertEqual(expected_event_log, tx_result['eventLogs'])
Ejemplo n.º 2
0
    def test_message_step(self):
        self._inner_task._icon_service_engine._validate_score_blacklist = Mock(
        )

        tx_hash1 = bytes.hex(create_tx_hash())
        from_ = create_address(AddressPrefix.EOA)
        to_ = from_
        # data size 25
        data = '0x01234abcde01234abcde01234abcde01234abcde01234abcde'

        request = create_request([
            ReqData(tx_hash1, from_, to_, 'message', data),
        ])

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

        input_length = 25

        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, input_length))
        self.assertEqual(len(self.step_counter.apply_step.call_args_list), 2)

        step_used = self._calc_step_used(0, 2)

        self._assert_step_used(step_used, request, tx_hash1)
Ejemplo n.º 3
0
    def test_transaction_result_on_sharing_fee_user_ratio100(
            self, score_invoke):
        score_invoke.side_effect = mock_score_invoke
        IconScoreContext.engine.fee.charge_transaction_fee = \
            Mock(return_value={self.from_: 9000})
        tx_hash = bytes.hex(os.urandom(32))

        data = {
            "method": "transfer",
            "params": {
                "to": f"hx{'2'*40}",
                "amount": hex(100)
            }
        }

        request = create_request(
            [ReqData(tx_hash, self.from_, str(self.score), 0, "call", data)])
        result = self._inner_task_invoke(request)

        expected_event_log = [{
            "scoreAddress":
            str(self.score),
            "indexed":
            ['Transfer(Address,Address,int)', f"hx{'1'*40}", f"hx{'2'*40}"],
            "data": [hex(100)]
        }]

        tx_result = result['txResults'][tx_hash]

        self.assertEqual('0x1', tx_result['status'])
        self.assertEqual(expected_event_log, tx_result['eventLogs'])
        self.assertFalse(tx_result.get('detailStepUsed'))
    def test_internal_transfer_step(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', {})])

        # 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.transfer()

        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')

        call_args_list = self.step_counter.apply_step.call_args_list
        self.assertEqual(call_args_list[0][0], (StepType.DEFAULT, 1))
        self.assertEqual(call_args_list[1][0], (StepType.INPUT, 0))
        self.assertEqual(call_args_list[2][0], (StepType.CONTRACT_CALL, 1))
        self.assertEqual(call_args_list[3][0], (StepType.CONTRACT_CALL, 1))
        self.assertEqual(len(call_args_list), 4)
Ejemplo n.º 5
0
    def test_request(self, score_invoke):
        inner_task = generate_inner_task(3)
        IconScoreContext.engine.prep.preps = Mock()

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

            context_db = inner_task._icon_service_engine._icx_context_db

            score_address = create_address(AddressPrefix.CONTRACT, b'address')
            score = SampleScore(IconScoreDatabase(score_address, context_db))

            address = create_address(AddressPrefix.EOA, b'address')
            score.SampleEvent(b'i_data', address, 10, b'data', 'text')

            ContextContainer._pop_context()

        score_invoke.side_effect = intercept_invoke

        from_ = create_address(AddressPrefix.EOA, b'from')
        to_ = create_address(AddressPrefix.CONTRACT, b'score')

        request = create_request([
            ReqData(bytes.hex(create_tx_hash(b'tx1')), from_, to_, 0, 'call',
                    {}),
            ReqData(bytes.hex(create_tx_hash(b'tx2')), from_, to_, 0, 'call',
                    {})
        ])
        response = inner_task._invoke(request)

        step_total = 0

        for tx_hash in response['txResults'].keys():
            result = response['txResults'][tx_hash]
            step_total += int(result['stepUsed'], 16)
            self.assertIn('status', result)
            self.assertIn('txHash', result)
            self.assertIn('txIndex', result)
            self.assertIn('blockHeight', result)
            self.assertIn('blockHash', result)
            self.assertIn('cumulativeStepUsed', result)
            self.assertIn('stepUsed', result)
            self.assertEqual(1, len(result['eventLogs']))
            self.assertEqual(step_total, int(result['cumulativeStepUsed'], 16))

        clear_inner_task()
Ejemplo n.º 6
0
    def test_get_db(self, score_invoke, before_transaction_process):
        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_, 0, 'call', {})])

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

        def charge_transaction_fee(*args, **kwargs):
            return {args[1]: args[4]}

        IconScoreContext.engine.fee.charge_transaction_fee = Mock(
            side_effect=charge_transaction_fee)

        def get_total_available_step(*args, **kwargs):
            return args[2]

        IconScoreContext.engine.fee.get_total_available_step = Mock(
            side_effect=get_total_available_step)

        # noinspection PyUnusedLocal
        def intercept_invoke(*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
            score.get_db()

            ContextContainer._pop_context()

        score_invoke.side_effect = intercept_invoke

        result = self._inner_task_invoke(request)
        score_invoke.assert_called()
        before_transaction_process.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)
    def test_out_of_step(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', {})])

        # 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()

        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

        raw_step_costs = {
            governance.STEP_TYPE_DEFAULT: 4000,
            governance.STEP_TYPE_CONTRACT_CALL: 1500,
            governance.STEP_TYPE_CONTRACT_CREATE: 20000,
            governance.STEP_TYPE_CONTRACT_UPDATE: 8000,
            governance.STEP_TYPE_CONTRACT_DESTRUCT: -7000,
            governance.STEP_TYPE_CONTRACT_SET: 1000,
            governance.STEP_TYPE_GET: 5,
            governance.STEP_TYPE_SET: 20,
            governance.STEP_TYPE_REPLACE: 5,
            governance.STEP_TYPE_DELETE: -15,
            governance.STEP_TYPE_INPUT: 20,
            governance.STEP_TYPE_EVENT_LOG: 10,
            governance.STEP_TYPE_API_CALL: 0
        }
        step_costs = {}

        factory = self._inner_task._icon_service_engine._step_counter_factory

        for key, value in raw_step_costs.items():
            try:
                step_costs[StepType(key)] = value
            except ValueError:
                # Pass the unknown step type
                pass

        self.step_counter = IconScoreStepCounter(step_costs, 100, 0)
        factory.create = Mock(return_value=self.step_counter)

        self._inner_task._icon_service_engine._icon_score_mapper.get_icon_score = Mock(
            return_value=None)
        result = self._inner_task._invoke(request)
        self.assertTrue(result['txResults'][tx_hash]['failure']
                        ['message'].startswith("Out of step"))
    def test_event_log_step(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', {})])

        # noinspection PyUnusedLocal
        def intercept_invoke(*args, **kwargs):
            ContextContainer._push_context(args[0])
            context_db = self._inner_task._icon_service_engine._icx_context_db
            address = create_address(AddressPrefix.EOA)
            score = SampleScore(IconScoreDatabase(address, context_db))
            i_data_param = b'i_data'
            data_param = b'data'
            text_param = 'text'
            score.SampleEvent(i_data_param, address, data_param, text_param)
            global event_log_data_size
            event_log_data_size = \
                len("SampleEvent(bytes,Address,bytes,str)".encode('utf-8')) + \
                len(i_data_param) + \
                len(address.body) + \
                len(data_param) + \
                len(text_param.encode('utf-8'))

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

        self._inner_task._icon_service_engine._validate_score_blacklist = Mock(
        )

        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.EVENT_LOG, event_log_data_size))
        self.assertEqual(len(self.step_counter.apply_step.call_args_list), 4)

        step_used = self._calc_step_used(
            0, len(self.step_counter.apply_step.call_args_list))

        # check stepUsed value
        self._assert_step_used(step_used, request, tx_hash)
Ejemplo n.º 9
0
    def test_event_log_step_revision3(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', {})])

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

            context_db = self._inner_task._icon_service_engine._icx_context_db
            address = create_address(AddressPrefix.EOA)
            score = SampleScore(IconScoreDatabase(address, context_db))
            i_data_param = b'i_data'
            data_param = b'data'
            text_param = 'text'
            score.SampleEvent(i_data_param, address, data_param, text_param)
            global event_log_data_size
            event_log_data_size = \
                len("SampleEvent(bytes,Address,bytes,str)".encode('utf-8')) + \
                len(i_data_param) + \
                ICON_CONTRACT_ADDRESS_BYTES_SIZE + \
                len(data_param) + \
                len(text_param.encode('utf-8'))

            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.EVENT_LOG, event_log_data_size),
                         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)
    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')

        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(len(self.step_counter.apply_step.call_args_list), 2)
Ejemplo n.º 11
0
    def test_set_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', {})])

        # 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.set_db(100)
            ContextContainer._pop_context()

        IconScoreEngine.invoke = Mock(side_effect=intercept_invoke)

        # for StepType.SET
        result = self._inner_task_invoke(request)
        self.assertEqual(result['txResults'][tx_hash]['status'], '0x1')
        # for StepType.REPLACE
        result = self._inner_task_invoke(request)
        self.assertEqual(result['txResults'][tx_hash]['status'], '0x1')
        IconScoreEngine.invoke.assert_called()

        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.SET, 100), call_args_for_apply_step[3][0])
        self.assertEqual((StepType.DEFAULT, 1), call_args_for_apply_step[4][0])
        self.assertEqual((StepType.INPUT, 0), call_args_for_apply_step[5][0])
        self.assertEqual((StepType.CONTRACT_CALL, 1),
                         call_args_for_apply_step[6][0])
        self.assertEqual((StepType.REPLACE, 100),
                         call_args_for_apply_step[7][0])
        self.assertEqual(8, len(call_args_for_apply_step))

        step_used_replace = self._calc_step_used(4, 4)

        # check stepUsed value
        self._assert_step_used(step_used_replace, request, tx_hash)
Ejemplo n.º 12
0
    def test_add_deposit(self):
        tx_hash = os.urandom(32)
        tx_hash_hex = bytes.hex(tx_hash)
        term, amount = hex(BLOCKS_IN_ONE_MONTH), 5000

        mock_score_info = Mock(spec=DepositInfo)
        mock_score_info.configure_mock(sharing_ratio=50)
        # transfer, get_balance
        IconScoreContext.engine.fee.charge_transaction_fee = Mock(
            return_value={})
        IconScoreContext.engine.fee.can_charge_fee_from_score = Mock()

        IconScoreContext.engine.fee.add_deposit = Mock(
            return_value=[tx_hash, self.score, self.from_, amount, term])
        IconScoreContext.engine.fee.charge_transaction_fee = \
            Mock(return_value={self.from_: 9000})

        data = {
            'action': 'add',
        }

        expected_event_log = [{
            "scoreAddress":
            str(self.score),
            "indexed": [
                "DepositAdded(bytes,Address,int,int)", f"0x{tx_hash_hex}",
                str(self.from_)
            ],
            "data": [hex(amount), term]
        }]

        request = create_request([
            ReqData(tx_hash_hex, self.from_, self.score, amount, 'deposit',
                    data),
        ])

        result = self._inner_task_invoke(request)
        tx_result = result['txResults'][tx_hash_hex]

        self.assertEqual('0x1', tx_result['status'])
        self.assertEqual(expected_event_log, tx_result['eventLogs'])

        return tx_hash
Ejemplo n.º 13
0
    def test_validate_transaction(self, validate_score_blacklist,
                                  is_service_flag_on):
        mock_block = Mock(spec=Block)
        mock_block.configure_mock(height=3)
        IconScoreContext.storage.icx._last_block = mock_block
        self._inner_task._icon_service_engine._icon_pre_validator._is_inactive_score = Mock(
            return_value=False)
        tx_hash = bytes.hex(os.urandom(32))

        data = {
            "method": "transfer",
            "params": {
                "to": f"hx{'2'*40}",
                "amount": hex(100)
            }
        }

        request = create_transaction_req(
            ReqData(tx_hash, self.from_, str(self.score), 0, "call", data))
        result = self._validate_transaction(request)
Ejemplo n.º 14
0
    def test_hash_readonly(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_readonly(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 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)
Ejemplo n.º 15
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_install_step(self):
        # Ignores deploy
        deploy_engine_invoke = Mock()
        deploy_storage_get_deploy_info = Mock(return_value=None)
        self._inner_task._icon_service_engine._validate_score_blacklist = Mock(
        )
        self._inner_task._icon_service_engine. \
            _icon_score_deploy_engine.invoke = deploy_engine_invoke
        self._inner_task._icon_service_engine.\
            _icon_score_deploy_storage.get_deploy_info = deploy_storage_get_deploy_info

        tx_hash = bytes.hex(create_tx_hash())
        from_ = create_address(AddressPrefix.EOA)
        to_ = Address.from_string('cx0000000000000000000000000000000000000000')
        content_type = 'application/zip'
        data = {
            'contentType': content_type,
            'content': '0x1867291283973610982301923812873419826abcdef9182731',
        }

        request = create_request([
            ReqData(tx_hash, from_, to_, 'deploy', data),
        ])

        result = self._inner_task._invoke(request)
        deploy_engine_invoke.assert_called()
        input_length = (len(content_type.encode('utf-8')) + 25)

        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, input_length))
        self.assertEqual(self.step_counter.apply_step.call_args_list[2][0],
                         (StepType.CONTRACT_CREATE, 1))
        self.assertEqual(self.step_counter.apply_step.call_args_list[3][0],
                         (StepType.CONTRACT_SET, 25))
        self.assertEqual(len(self.step_counter.apply_step.call_args_list), 4)
Ejemplo n.º 17
0
    def test_internal_transfer_step(self, score_invoke):
        IconScoreContext.engine.icx.transfer = Mock()
        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_, 0, 'call', {})])

        # 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.transfer()
            ContextContainer._pop_context()

        score_invoke.side_effect = intercept_invoke

        result = self._inner_task_invoke(request)
        score_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.CONTRACT_CALL, 1),
                         call_args_for_apply_step[3][0])
        self.assertEqual(4, len(call_args_for_apply_step))

        step_used = self._calc_step_used(
            0, len(self.step_counter.apply_step.call_args_list))

        # check stepUsed value
        self._assert_step_used(step_used, request, tx_hash)
Ejemplo n.º 18
0
    def test_install_step(self):
        # Ignores deploy
        deploy_engine_invoke = Mock()
        self._inner_task._icon_service_engine. \
            _icon_score_deploy_engine.invoke = deploy_engine_invoke

        tx_hash1 = bytes.hex(create_tx_hash())
        from_ = create_address(AddressPrefix.EOA)
        to_ = Address.from_string('cx0000000000000000000000000000000000000000')
        content_type = 'application/zip'
        data = {
            'contentType': content_type,
            'content': '0x1867291283973610982301923812873419826abcdef9182731',
        }

        request1 = create_request([
            ReqData(tx_hash1, from_, to_, 'deploy', data),
        ])

        # for StepType.CONTRACT_CREATE
        result = self._inner_task_invoke(request1)
        self.assertEqual(result['txResults'][tx_hash1]['status'], '0x1')

        # for StepType.CONTRACT_UPDATE
        to_ = result['txResults'][tx_hash1]['scoreAddress']
        tx_hash2 = bytes.hex(create_tx_hash())

        request2 = create_request([
            ReqData(tx_hash2, from_, to_, 'deploy', data),
        ])

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

        deploy_engine_invoke.assert_called()

        input_length = (len(content_type.encode('utf-8')) + 25)

        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, input_length),
                         call_args_for_apply_step[1][0])
        self.assertEqual((StepType.CONTRACT_CREATE, 1),
                         call_args_for_apply_step[2][0])
        self.assertEqual((StepType.CONTRACT_SET, 25),
                         call_args_for_apply_step[3][0])
        self.assertEqual((StepType.DEFAULT, 1), call_args_for_apply_step[4][0])
        self.assertEqual((StepType.INPUT, input_length),
                         call_args_for_apply_step[5][0])
        self.assertEqual((StepType.CONTRACT_UPDATE, 1),
                         call_args_for_apply_step[6][0])
        self.assertEqual((StepType.CONTRACT_SET, 25),
                         call_args_for_apply_step[7][0])
        self.assertEqual(8, len(call_args_for_apply_step))

        step_used_create = self._calc_step_used(0, 4)
        step_used_update = self._calc_step_used(4, 4)

        # check SCORE install stepUsed value
        self._assert_step_used(step_used_create, request1, tx_hash1)

        # check SCORE update stepUsed value
        self._assert_step_used(step_used_update, request2, tx_hash2)