예제 #1
0
    def test_score_invoke_with_revert(self):

        table = {
            ConfigKey.SERVICE_FEE: True,
            ConfigKey.SERVICE_AUDIT: False,
            ConfigKey.SERVICE_DEPLOYER_WHITELIST: False,
            ConfigKey.SERVICE_SCORE_PACKAGE_VALIDATOR: False
        }
        self._engine._flag = self._engine._make_service_flag(table)

        block_height = 1
        block_hash = create_block_hash(b'block')
        block_timestamp = 0
        tx_hash = create_tx_hash()
        value = 1 * 10**18

        self._to = create_address(AddressPrefix.CONTRACT)

        tx_v3 = {
            'method': 'icx_sendTransaction',
            'params': {
                'version': 3,
                'from': self._genesis_address,
                'to': self._to,
                'value': value,
                'stepLimit': 20000,
                'timestamp': 1234567890,
                'txHash': tx_hash
            }
        }

        block = Block(block_height, block_hash, block_timestamp,
                      self.genesis_block.hash)

        before_from_balance: int = \
            self._engine._icx_engine.get_balance(None, self.from_)

        self._engine._handle_score_invoke = \
            Mock(return_value=None, side_effect=RevertException("force revert"))
        self._engine._validate_score_blacklist = Mock()

        raise_exception_start_tag("test_score_invoke_with_revert")
        tx_results, state_root_hash = self._engine.invoke(block, [tx_v3])
        raise_exception_end_tag("test_score_invoke_with_revert")
        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.assertIsNotNone(tx_result.failure)
        self.assertIsNone(tx_result.score_address)
        self.assertEqual(tx_result.status, 0)
        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
        step_unit = self._engine._step_counter_factory.get_step_cost(
            StepType.DEFAULT)

        self.assertEqual(tx_result.step_used, step_unit)

        step_price = self._engine._get_step_price()
        if IconScoreContext._is_flag_on(IconScoreContext.icon_service_flag,
                                        IconServiceFlag.fee):
            # step_price MUST BE 10**10 on protocol v2
            self.assertEqual(
                step_price,
                self._engine._step_counter_factory.get_step_price())
        else:
            self.assertEqual(step_price, 0)
        self.assertEqual(tx_result.step_price, step_price)

        self._engine.commit(block)

        # Check whether fee charging works well
        after_from_balance: int = \
            self._engine._icx_engine.get_balance(None, self.from_)

        fee = tx_result.step_price * tx_result.step_used
        self.assertEqual(after_from_balance, before_from_balance - fee)
예제 #2
0
    def test_invoke_v3_without_fee(self):
        block_height = 1
        block_hash = create_block_hash()
        block_timestamp = 0
        tx_hash = create_tx_hash()
        value = 1 * 10**18

        tx_v3 = {
            'method': 'icx_sendTransaction',
            'params': {
                'nid': 3,
                'version': 3,
                'from': self._genesis_address,
                'to': self._to,
                'value': value,
                'stepLimit': 1000000,
                'timestamp': 1234567890,
                'txHash': tx_hash
            }
        }

        block = Block(block_height, block_hash, block_timestamp,
                      self.genesis_block.hash)

        tx_results, state_root_hash = self._engine.invoke(block, [tx_v3])
        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
        step_unit = self._engine._step_counter_factory.get_step_cost(
            StepType.DEFAULT)

        self.assertEqual(tx_result.step_used, step_unit)

        step_price = self._engine._get_step_price()
        if IconScoreContext._is_flag_on(IconScoreContext.icon_service_flag,
                                        IconServiceFlag.fee):
            # step_used MUST BE 10**10 on protocol v2
            self.assertEqual(step_price, 10**10)
        else:
            self.assertEqual(step_price, 0)
        self.assertEqual(tx_result.step_price, step_price)

        self._engine.commit(block)

        # Check whether fee charging works well
        from_balance: int = \
            self._engine._icx_engine.get_balance(None, self.from_)
        fee = tx_result.step_price * tx_result.step_used
        self.assertEqual(fee, 0)
        self.assertEqual(from_balance, self._total_supply - value - fee)
예제 #3
0
    def test_invoke_v3_with_fee(self):

        table = {
            ConfigKey.SERVICE_FEE: True,
            ConfigKey.SERVICE_AUDIT: False,
            ConfigKey.SERVICE_DEPLOYER_WHITELIST: False,
            ConfigKey.SERVICE_SCORE_PACKAGE_VALIDATOR: False
        }
        self._engine._flag = self._engine._make_service_flag(table)

        block_height = 1
        block_hash = create_block_hash()
        block_timestamp = 0
        tx_hash = create_tx_hash()
        value = 1 * 10**18
        step_limit = 1000000

        tx_v3 = {
            'method': 'icx_sendTransaction',
            'params': {
                'nid': 3,
                'version': 3,
                'from': self._genesis_address,
                'to': self._to,
                'value': value,
                'stepLimit': step_limit,
                'timestamp': 1234567890,
                'txHash': tx_hash
            }
        }

        block = Block(block_height, block_hash, block_timestamp,
                      self.genesis_block.hash)

        before_from_balance: int = \
            self._engine._icx_engine.get_balance(None, self.from_)

        tx_results, state_root_hash = self._engine.invoke(block, [tx_v3])
        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
        step_cost = self._engine._step_counter_factory.get_step_cost(
            StepType.DEFAULT)

        self.assertEqual(tx_result.step_used, step_cost)

        step_price = self._engine._get_step_price()
        if IconScoreContext._is_flag_on(IconScoreContext.icon_service_flag,
                                        IconServiceFlag.fee):
            # step_price MUST BE 10**10 on protocol v2
            self.assertEqual(
                step_price,
                self._engine._step_counter_factory.get_step_price())
        else:
            self.assertEqual(step_price, 0)
        self.assertEqual(tx_result.step_price, step_price)

        self._engine.commit(block)

        # Check whether fee charging works well
        after_from_balance: int = \
            self._engine._icx_engine.get_balance(None, self.from_)
        fee = tx_result.step_price * tx_result.step_used
        value = value if tx_result.status == TransactionResult.SUCCESS else 0
        self.assertEqual(after_from_balance, before_from_balance - value - fee)
예제 #4
0
    def test_invoke(self):
        block_height = 1
        block_hash = create_block_hash()
        block_timestamp = 0
        tx_hash = create_tx_hash()
        value = 1 * 10**18

        step_limit = 200000000000000
        tx_v3 = {
            'method': 'icx_sendTransaction',
            'params': {
                'version': 3,
                'from': self._genesis_address,
                'to': self._to,
                'value': value,
                'stepLimit': step_limit,
                'timestamp': 1234567890,
                'txHash': tx_hash
            }
        }

        block = Block(block_height, block_hash, block_timestamp,
                      self.genesis_block.hash)

        original_invoke_request = self._engine._invoke_request

        # noinspection PyUnusedLocal
        def intercept_invoke_req(*args, **kwargs):
            context: 'IconScoreContext' = args[0]
            request: dict = args[1]
            index: int = args[2]
            ret = original_invoke_request(context, request, index)

            # Requesting the very big number of step limit,
            # but there's a max step limit,
            # asserts max step limit is applied to step counting.
            self.assertNotEqual(step_limit, context.step_counter.step_limit)
            self.assertEqual(
                self._engine._step_counter_factory.get_max_step_limit(
                    context.type), context.step_counter.step_limit)
            return ret

        self._engine._invoke_request = Mock(side_effect=intercept_invoke_req)

        tx_results, state_root_hash = self._engine.invoke(block, [tx_v3])
        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
        step_unit = self._engine._step_counter_factory.get_step_cost(
            StepType.DEFAULT)

        self.assertEqual(tx_result.step_used, step_unit)

        step_price = self._engine._get_step_price()

        if IconScoreContext._is_flag_on(IconScoreContext.icon_service_flag,
                                        IconServiceFlag.fee):
            # step_price MUST BE 10**10 on protocol v2
            self.assertEqual(step_price, 10**10)
        else:
            self.assertEqual(step_price, 0)
        self.assertEqual(tx_result.step_price, step_price)

        self._engine.commit(block)

        # Check whether fee charging works well
        from_balance: int = \
            self._engine._icx_engine.get_balance(None, self.from_)
        fee = tx_result.step_price * tx_result.step_used
        self.assertEqual(fee, 0)
        self.assertEqual(from_balance, self._total_supply - value - fee)