コード例 #1
0
class TestIntegrateBase(TestCase):
    @classmethod
    def setUpClass(cls):
        cls._score_root_path = '.score'
        cls._state_db_root_path = '.statedb'
        cls._test_sample_root = "test_samples"
        cls._signature = "VAia7YZ2Ji6igKWzjR2YsGa2m53nKPrfK7uXYW78QLE+ATehAVZPC40szvAiA6NEU5gCYB4c4qaQzqDh2ugcHgA="

        cls._version = 3
        cls._step_limit = 1 * 10**12
        cls._icx_factor = 10**18

        cls._admin: 'Address' = create_address()
        cls._genesis: 'Address' = create_address()
        cls._fee_treasury: 'Address' = create_address()

        cls._addr_array = [create_address() for _ in range(10)]

    def setUp(self):
        root_clear(self._score_root_path, self._state_db_root_path)

        self._block_height = 0
        self._prev_block_hash = None

        config = IconConfig("", default_icon_config)
        config.load()
        config.update_conf({ConfigKey.BUILTIN_SCORE_OWNER: str(self._admin)})
        config.update_conf({
            ConfigKey.SERVICE: {
                ConfigKey.SERVICE_AUDIT: False,
                ConfigKey.SERVICE_FEE: False,
                ConfigKey.SERVICE_DEPLOYER_WHITELIST: False,
                ConfigKey.SERVICE_SCORE_PACKAGE_VALIDATOR: False
            }
        })
        config.update_conf({
            ConfigKey.SCORE_ROOT_PATH:
            self._score_root_path,
            ConfigKey.STATE_DB_ROOT_PATH:
            self._state_db_root_path
        })
        config.update_conf(self._make_init_config())

        self.icon_service_engine = IconServiceEngine()
        self.icon_service_engine.open(config)

        self._genesis_invoke()

    def tearDown(self):
        self.icon_service_engine.close()
        root_clear(self._score_root_path, self._state_db_root_path)

    def _make_init_config(self) -> dict:
        return {}

    def _genesis_invoke(self) -> dict:
        tx_hash = create_tx_hash()
        timestamp_us = create_timestamp()
        request_params = {
            'txHash': tx_hash,
            'version': self._version,
            'timestamp': timestamp_us
        }

        tx = {
            'method': 'icx_sendTransaction',
            'params': request_params,
            'genesisData': {
                "accounts": [{
                    "name": "genesis",
                    "address": self._genesis,
                    "balance": 100 * self._icx_factor
                }, {
                    "name": "fee_treasury",
                    "address": self._fee_treasury,
                    "balance": 0
                }, {
                    "name": "_admin",
                    "address": self._admin,
                    "balance": 1_000_000 * self._icx_factor
                }]
            },
        }
コード例 #2
0
class IconServiceSyncer(object):
    _TAG = "SYNC"

    def __init__(self):
        self._block_reader = BinBlockDatabaseReader()
        self._engine = IconServiceEngine()
        self._timer = Timer()

    def open(
        self,
        config_path: str,
        fee: bool = True,
        audit: bool = True,
        deployer_whitelist: bool = False,
        score_package_validator: bool = False,
        builtin_score_owner: str = "",
    ):
        conf = IconConfig("", default_icon_config)

        if config_path != "":
            conf.load(config_path)

        conf.update_conf({
            "builtinScoreOwner": builtin_score_owner,
            "service": {
                "fee": fee,
                "audit": audit,
                "scorePackageValidator": score_package_validator,
                "deployerWhiteList": deployer_whitelist,
            },
        })

        Logger.load_config(conf)
        self._engine.open(conf)

    def run(self, *args, **kwargs) -> int:
        Logger.debug(tag=self._TAG, msg=f"run() start: {args} {kwargs}")

        loop = asyncio.get_event_loop()
        future = asyncio.Future()

        try:
            asyncio.ensure_future(
                self._wait_for_complete(future, *args, **kwargs))
            loop.run_until_complete(future)
        finally:
            for task in asyncio.Task.all_tasks():
                task.cancel()
            loop.close()

        ret = future.result()
        Logger.debug(tag=self._TAG, msg=f"run() end: {ret}")

        return ret

    async def _wait_for_complete(self, result_future: asyncio.Future, *args,
                                 **kwargs):
        Logger.debug(tag=self._TAG, msg="_wait_for_complete() start")

        # Wait for rc to be ready
        future = self._engine.get_ready_future()
        await future

        with ThreadPoolExecutor(max_workers=1) as executor:
            # Call IconServiceEngine.hello()
            f = executor.submit(self._hello)
            future = asyncio.wrap_future(f)
            await future

            # Start to sync blocks
            f = executor.submit(self._run, *args, **kwargs)
            f = asyncio.wrap_future(f)
            for fut in asyncio.as_completed([f]):
                try:
                    ret = await fut
                except Exception as exc:
                    self._engine.close()
                    # Wait to stop ipc_server for 1s
                    await asyncio.sleep(1)
                    result_future.set_exception(exc)
                else:
                    self._engine.close()
                    # Wait to stop ipc_server for 1s
                    await asyncio.sleep(1)
                    Logger.debug(tag=self._TAG,
                                 msg="_wait_for_complete() end1")
                    result_future.set_result(ret)
                    Logger.debug(tag=self._TAG,
                                 msg="_wait_for_complete() end2")

    def _hello(self):
        self._engine.hello()

    def _run(
        self,
        db_path: str,
        channel: str,
        start_height: int = 0,
        count: int = 99_999_999,
        stop_on_error: bool = True,
        no_commit: bool = False,
        backup_period: int = 0,
        write_precommit_data: bool = False,
        print_block_height: int = 1,
        iiss_db_backup_path: Optional[str] = None,
    ) -> int:
        """Begin to synchronize IconServiceEngine with blocks from loopchain db

        :param db_path: loopchain db path
        :param channel: channel name used as a key to get commit_state in loopchain db
        :param start_height: start height to sync
        :param count: The number of blocks to sync
        :param stop_on_error: If error happens, stop syncing
        :param no_commit: Do not commit
        :param backup_period: state backup period in block
        :param write_precommit_data:
        :param print_block_height: print every this block height
        :return: 0(success), otherwise(error)
        """
        Logger.debug(tag=self._TAG, msg="_run() start")

        word_detector = WordDetector(
            filename="iconservice.log",
            block_word=r"CALCULATE\(",
            release_word=r"CALCULATE_DONE\(",
        )
        ret: int = 0
        self._block_reader.open(db_path)

        print("block_height | commit_state | state_root_hash | tx_count")

        prev_bin_block: Optional["BinBlock"] = None
        prev_block: Optional["Block"] = None
        main_preps: Optional['NodeContainer'] = None
        next_main_preps: Optional["NodeContainer"] = None

        if start_height > 0:
            prev_bin_block: Optional[
                BinBlock] = self._block_reader.get_block_by_height(
                    start_height - 1)
            # init main_preps
            preps: list = self._block_reader.load_main_preps(
                reps_hash=prev_bin_block.reps_hash)
            main_preps: Optional['NodeContainer'] = NodeContainer.from_list(
                preps=preps)

            # when sync from the first block of term, have to initialize next_main_preps here
            # in that case, invoke_result[3] will be None on first block and can not update next_main_preps
            cur_bin_block: Optional[
                BinBlock] = self._block_reader.get_block_by_height(
                    start_height)
            block: "Block" = create_iconservice_block(cur_bin_block)
            if self._check_calculation_block(block):
                preps: list = self._block_reader.load_main_preps(
                    reps_hash=cur_bin_block.reps_hash)
                next_main_preps: Optional[
                    'NodeContainer'] = NodeContainer.from_list(preps=preps)

        end_height = start_height + count - 1

        self._timer.start()

        for height in range(start_height, start_height + count):
            bin_block = self._block_reader.get_block_by_height(height)
            if bin_block is None:
                print(f"last block: {height - 1}")
                break

            block: "Block" = create_iconservice_block(bin_block)

            tx_requests: List[Dict[str, Any]] = create_transaction_requests(
                bin_block.transactions)
            prev_block_generator: Optional[
                "Address"] = prev_bin_block.leader if prev_bin_block else None
            prev_block_validators: Optional[
                List["Address"]] = create_block_validators(
                    bin_block.prev_votes, prev_block_generator)
            prev_block_votes: Optional[List[
                Tuple["Address",
                      int]]] = create_prev_block_votes(bin_block.prev_votes,
                                                       prev_block_generator,
                                                       main_preps)

            Logger.info(tag=self._TAG,
                        msg=f"prev_block_generator={prev_block_generator}")
            Logger.info(tag=self._TAG,
                        msg=f"prev_block_validators={prev_block_validators}")
            Logger.info(tag=self._TAG,
                        msg=f"prev_block_votes={prev_block_votes}")

            if prev_block is not None and prev_block.hash != block.prev_hash:
                raise Exception(f"Invalid prev_block_hash: height={height}")

            invoke_result = self._engine.invoke(
                block,
                tx_requests,
                prev_block_generator,
                prev_block_validators,
                prev_block_votes,
            )
            tx_results, state_root_hash = invoke_result[0], invoke_result[1]
            main_preps_as_dict: Optional[Dict] = invoke_result[3]

            commit_state: bytes = bin_block.state_hash

            # "commit_state" is the field name of state_root_hash in loopchain block
            if (height - start_height) % print_block_height == 0:
                self._print_status(height, start_height, count, commit_state,
                                   state_root_hash, len(tx_requests))

            if write_precommit_data:
                self._print_precommit_data(block)

            try:
                if stop_on_error:
                    if commit_state:
                        if commit_state != state_root_hash:
                            raise Exception("state_root_hash mismatch")

                    if height > 0 and not self._check_invoke_result(
                            tx_results):
                        raise Exception("tx_result mismatch")
            except Exception as e:
                logging.exception(e)

                self._print_precommit_data(block)
                ret: int = 1
                break

            is_calculation_block = self._check_calculation_block(block)

            if is_calculation_block:
                word_detector.start()
                time.sleep(0.5)
                if iiss_db_backup_path is not None:
                    self._backup_iiss_db(iiss_db_backup_path, block.height)

            # If no_commit is set to True, the config only affects to the last block to commit
            if not no_commit or height < end_height:
                while word_detector.get_hold():
                    time.sleep(0.5)

                # Call IconServiceEngine.commit() with a block
                self._commit(block)

            while word_detector.get_hold():
                time.sleep(0.5)
            # Prepare the next iteration
            self._backup_state_db(block, backup_period)
            prev_block: 'Block' = block
            prev_bin_block: 'BinBlock' = bin_block

            if next_main_preps:
                main_preps = next_main_preps
                next_main_preps = None

            if main_preps_as_dict is not None:
                next_main_preps = NodeContainer.from_dict(main_preps_as_dict)

        self._block_reader.close()
        word_detector.stop()

        Logger.debug(tag=self._TAG, msg=f"_run() end: {ret}")
        return ret
コード例 #3
0
class TestIntegrateBase(TestCase):
    @classmethod
    def setUpClass(cls):
        cls._score_root_path = '.score'
        cls._state_db_root_path = '.statedb'
        cls._test_sample_root = ""
        cls._signature = "VAia7YZ2Ji6igKWzjR2YsGa2m53nKPrfK7uXYW78QLE+ATehAVZPC40szvAiA6NEU5gCYB4c4qaQzqDh2ugcHgA="

        cls._version = 3
        cls._step_limit = 1 * 10**9
        cls._icx_factor = 10**18

        cls.admin: 'Address' = create_address()
        cls.genesis: 'Address' = create_address()
        cls.owner1: 'Address' = create_address()
        cls.owner2: 'Address' = create_address()
        cls.owner3: 'Address' = create_address()

        cls._fee_treasury: 'Address' = create_address()

    def setUp(self):
        root_clear(self._score_root_path, self._state_db_root_path)

        self._block_height = 0
        self._prev_block_hash = None

        config = IconConfig("", default_icon_config)
        config.load()
        config.update_conf({ConfigKey.BUILTIN_SCORE_OWNER: str(self.admin)})
        config.update_conf({
            ConfigKey.SERVICE: {
                ConfigKey.SERVICE_AUDIT: False,
                ConfigKey.SERVICE_FEE: False,
                ConfigKey.SERVICE_DEPLOYER_WHITE_LIST: False,
                ConfigKey.SERVICE_SCORE_PACKAGE_VALIDATOR: False
            }
        })
        config.update_conf({
            ConfigKey.SCORE_ROOT_PATH:
            self._score_root_path,
            ConfigKey.STATE_DB_ROOT_PATH:
            self._state_db_root_path
        })
        config.update_conf(self._make_init_config())

        self.icon_service_engine = IconServiceEngine()
        self.icon_service_engine.open(config)

        self._genesis_invoke()

    def tearDown(self):
        self.icon_service_engine.close()
        root_clear(self._score_root_path, self._state_db_root_path)

    def _make_init_config(self) -> dict:
        return {}

    def _genesis_invoke(self) -> list:
        tx_hash = create_tx_hash()
        timestamp_us = create_timestamp()
        request_params = {
            'txHash': tx_hash,
            'version': self._version,
            'timestamp': timestamp_us
        }

        tx = {
            'method': 'icx_sendTransaction',
            'params': request_params,
            'genesisData': {
                "accounts": [
                    {
                        "name": "genesis",
                        "address": self.genesis,
                        "balance": 100 * self._icx_factor
                    },
                    {
                        "name": "fee_treasury",
                        "address": self._fee_treasury,
                        "balance": 0
                    },
                    {
                        "name": "owner1",
                        "address": self.owner1,
                        "balance": 100 * self._icx_factor
                    },
                    {
                        "name": "owner2",
                        "address": self.owner2,
                        "balance": 100 * self._icx_factor
                    },
                    {
                        "name": "owner3",
                        "address": self.owner3,
                        "balance": 100 * self._icx_factor
                    },
                ]
            },
        }

        block_hash = create_block_hash()
        block = Block(self._block_height, block_hash, timestamp_us, None)

        invoke_response, state_root_hash, added_transactions, next_preps = \
            self.icon_service_engine.invoke(block=block,
                                            tx_requests=[tx])

        self.icon_service_engine.commit(block.height, block.hash, None)
        self._block_height += 1
        self._prev_block_hash = block_hash

        return invoke_response

    def deploy_score(self,
                     package_name: str,
                     deployer_address: 'Address',
                     deploy_params: dict = None):
        tx1 = self._make_deploy_tx("",
                                   package_name,
                                   deployer_address,
                                   ZERO_SCORE_ADDRESS,
                                   deploy_params=deploy_params)

        prev_block, tx_results = self._make_and_req_block([tx1])
        self._write_precommit_state(prev_block)

        self.assertEqual(tx_results[0].status, int(True))
        score_address = tx_results[0].score_address

        return score_address

    def _make_deploy_tx(self,
                        score_root: str,
                        score_name: str,
                        addr_from: Union['Address', None],
                        addr_to: 'Address',
                        deploy_params: dict = None,
                        timestamp_us: int = None,
                        data: bytes = None,
                        is_sys: bool = False):

        if deploy_params is None:
            deploy_params = {}

        score_path = get_score_path(score_root, score_name)

        if is_sys:
            deploy_data = {
                'contentType': 'application/tbears',
                'content': score_path,
                'params': deploy_params
            }
        else:
            if data is None:
                mz = InMemoryZip()
                mz.zip_in_memory(score_path)
                data = f'0x{mz.data.hex()}'
            else:
                data = f'0x{bytes.hex(data)}'
            deploy_data = {
                'contentType': 'application/zip',
                'content': data,
                'params': deploy_params
            }

        if timestamp_us is None:
            timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "version": self._version,
            "from": addr_from,
            "to": addr_to,
            "stepLimit": self._step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature,
            "dataType": "deploy",
            "data": deploy_data
        }

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {'method': method, 'params': request_params}

        self.icon_service_engine.validate_transaction(tx)
        return tx

    def query(self,
              score_address: 'Address',
              method: str,
              params: dict = None):
        params = {} if params is None else params
        query_request = {
            "version": self._version,
            "from": self.admin,
            "to": score_address,
            "dataType": "call",
            "data": {
                "method": method,
                "params": params
            }
        }
        return self._query(query_request)

    def send_tx(self,
                addr_from: Optional['Address'],
                addr_to: 'Address',
                method: str,
                params: dict,
                value: int = 0):
        tx = self._make_score_call_tx(addr_from, addr_to, method, params,
                                      value)
        prev_block, tx_results = self._make_and_req_block([tx])
        self.assertEqual(int(True), tx_results[0].status)

        self._write_precommit_state(prev_block)
        return tx_results[0]

    def send_message(self,
                     addr_from: Optional['Address'],
                     addr_to: 'Address',
                     data: str,
                     value: int = 0):
        tx = self._make_score_message_tx(addr_from, addr_to, data, value)
        prev_block, tx_results = self._make_and_req_block([tx])
        self.assertEqual(int(True), tx_results[0].status)

        self._write_precommit_state(prev_block)
        return tx_results[0]

    def _make_score_message_tx(self,
                               addr_from: Optional['Address'],
                               addr_to: 'Address',
                               data: str,
                               value: int = 0):
        timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "version": self._version,
            "from": addr_from,
            "to": addr_to,
            "value": value,
            "stepLimit": self._step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature,
            "dataType": "message",
            "data": data
        }

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {'method': method, 'params': request_params}

        self.icon_service_engine.validate_transaction(tx)
        return tx

    def _make_score_call_tx(self,
                            addr_from: Optional['Address'],
                            addr_to: 'Address',
                            method: str,
                            params: dict,
                            value: int = 0):

        timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "version": self._version,
            "from": addr_from,
            "to": addr_to,
            "value": value,
            "stepLimit": self._step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature,
            "dataType": "call",
            "data": {
                "method": method,
                "params": params
            }
        }

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {'method': method, 'params': request_params}

        self.icon_service_engine.validate_transaction(tx)
        return tx

    def icx_send_tx(self,
                    addr_from: Optional['Address'],
                    addr_to: Union['Address', 'MalformedAddress'],
                    value: int,
                    disable_pre_validate: bool = False,
                    support_v2: bool = False):
        tx = self._make_icx_send_tx(addr_from, addr_to, value, support_v2)
        prev_block, tx_results = self._make_and_req_block([tx])
        self.assertEqual(tx_results[0].status, int(True))

        self._write_precommit_state(prev_block)
        return tx_results

    def _make_icx_send_tx(self,
                          addr_from: Optional['Address'],
                          addr_to: Union['Address', 'MalformedAddress'],
                          value: int,
                          disable_pre_validate: bool = False,
                          support_v2: bool = False):

        timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "from": addr_from,
            "to": addr_to,
            "value": value,
            "stepLimit": self._step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature
        }

        if support_v2:
            request_params["fee"] = 10**16
        else:
            request_params["version"] = self._version

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {'method': method, 'params': request_params}

        if not disable_pre_validate:
            self.icon_service_engine.validate_transaction(tx)
        return tx

    def _make_and_req_block(self,
                            tx_list: list,
                            block_height: int = None) -> tuple:
        if block_height is None:
            block_height: int = self._block_height
        block_hash = create_block_hash()
        timestamp_us = create_timestamp()

        block = Block(block_height, block_hash, timestamp_us,
                      self._prev_block_hash)

        tx_results, state_root_hash, added_transactions, next_preps = \
            self.icon_service_engine.invoke(block=block,
                                            tx_requests=tx_list)
        return block, tx_results

    def _write_precommit_state(self, block: 'Block') -> None:
        self.icon_service_engine.commit(block.height, block.hash, None)
        self._block_height += 1
        self._prev_block_hash = block.hash

    def _remove_precommit_state(self, block: 'Block') -> None:
        self.icon_service_engine.rollback(block.height, block.hash)

    def _query(self, request: dict, method: str = 'icx_call') -> Any:
        response = self.icon_service_engine.query(method, request)
        return response

    def _create_invalid_block(self, block_height: int = None) -> 'Block':
        if block_height is None:
            block_height: int = self._block_height
        block_hash = create_block_hash()
        timestamp_us = create_timestamp()

        return Block(block_height, block_hash, timestamp_us,
                     self._prev_block_hash)
コード例 #4
0
class TestIntegrateBase(TestCase):

    @classmethod
    def setUpClass(cls):
        cls._score_root_path = '.score'
        cls._state_db_root_path = '.statedb'
        cls._iiss_db_root_path = '.iissdb'

        cls._test_sample_root = "samples"
        cls._signature = "VAia7YZ2Ji6igKWzjR2YsGa2m53nKPrfK7uXYW78QLE+ATehAVZPC40szvAiA6NEU5gCYB4c4qaQzqDh2ugcHgA="

        cls._version = 3

        cls._admin: 'EOAAccount' = cls.create_eoa_accounts(1)[0]
        cls._genesis: 'Address' = create_address()
        cls._fee_treasury: 'Address' = create_address()

        cls._accounts = cls.create_eoa_accounts(100)

        cls._tx_results: dict = {}

    def setUp(self):
        root_clear(self._score_root_path, self._state_db_root_path, self._iiss_db_root_path)

        self._block_height = -1
        self._prev_block_hash = None

        config = IconConfig("", copy.deepcopy(default_icon_config))

        config.load()
        config.update_conf({ConfigKey.BUILTIN_SCORE_OWNER: str(self._admin.address)})
        config.update_conf({ConfigKey.SERVICE: {ConfigKey.SERVICE_AUDIT: False,
                                                ConfigKey.SERVICE_FEE: False,
                                                ConfigKey.SERVICE_DEPLOYER_WHITE_LIST: False,
                                                ConfigKey.SERVICE_SCORE_PACKAGE_VALIDATOR: False}})
        config.update_conf({ConfigKey.SCORE_ROOT_PATH: self._score_root_path,
                            ConfigKey.STATE_DB_ROOT_PATH: self._state_db_root_path})
        config.update_conf(self._make_init_config())

        self._config: 'IconConfig' = config

        self.icon_service_engine = IconServiceEngine()

        self._mock_ipc()
        self.icon_service_engine.open(config)

        self._genesis_invoke()

    def get_block_height(self) -> int:
        return self._block_height

    def mock_calculate(self, _path, _block_height):
        context: 'IconScoreContext' = IconScoreContext(IconScoreContextType.QUERY)
        end_block_height_of_calc: int = context.storage.iiss.get_end_block_height_of_calc(context)
        calc_period: int = context.storage.iiss.get_calc_period(context)
        response = CalculateDoneNotification(0, True, end_block_height_of_calc - calc_period, 0, b'mocked_response')
        self._calculate_done_callback(response)

    @classmethod
    def _mock_ipc(cls, mock_calculate: callable = mock_calculate):
        RewardCalcProxy.open = Mock()
        RewardCalcProxy.start = Mock()
        RewardCalcProxy.stop = Mock()
        RewardCalcProxy.close = Mock()
        RewardCalcProxy.get_version = Mock()
        RewardCalcProxy.calculate = mock_calculate
        RewardCalcProxy.claim_iscore = Mock()
        RewardCalcProxy.query_iscore = Mock()
        RewardCalcProxy.commit_block = Mock()
        RewardCalcProxy.commit_claim = Mock()
        RewardCalcProxy.query_calculate_result = Mock(return_value=(RCCalculateResult.SUCCESS, 0, 0, bytes()))

    def tearDown(self):
        self.icon_service_engine.close()
        root_clear(self._score_root_path, self._state_db_root_path, self._iiss_db_root_path)

    def _make_init_config(self) -> dict:
        return {}

    def _genesis_invoke(self) -> tuple:
        tx_hash = create_tx_hash()
        timestamp_us = create_timestamp()
        request_params = {
            'txHash': tx_hash,
            'version': self._version,
            'timestamp': timestamp_us
        }

        tx = {
            'method': 'icx_sendTransaction',
            'params': request_params,
            'genesisData': {
                "accounts": [
                    {
                        "name": "genesis",
                        "address": self._genesis,
                        "balance": 0
                    },
                    {
                        "name": "fee_treasury",
                        "address": self._fee_treasury,
                        "balance": 0
                    },
                    {
                        "name": "_admin",
                        "address": self._admin.address,
                        "balance": icx_to_loop(TOTAL_SUPPLY)
                    }
                ]
            },
        }

        block_hash = create_block_hash()
        block = Block(self._block_height + 1, block_hash, timestamp_us, None, 0)
        invoke_response: tuple = self.icon_service_engine.invoke(
            block,
            [tx]
        )
        self.icon_service_engine.commit(block.height, block.hash, None)
        self._block_height += 1
        self._prev_block_hash = block_hash

        return invoke_response

    def get_tx_results(self, hash_list: List[bytes]):
        tx_results: List['TransactionResult'] = []
        for tx_hash in hash_list:
            tx_results.append(self._tx_results[tx_hash])
        return tx_results

    @classmethod
    def get_hash_list_from_tx_list(cls, tx_list: list) -> List[bytes]:
        hash_list: list = []
        for tx in tx_list:
            hash_list.append(tx['params']['txHash'])
        return hash_list

    def add_tx_result(self, tx_results: List['TransactionResult']):
        for tx_result in tx_results:
            self._tx_results[tx_result.tx_hash] = tx_result

    def make_and_req_block(self,
                           tx_list: list,
                           block_height: int = None,
                           prev_block_generator: Optional['Address'] = None,
                           prev_block_validators: Optional[List['Address']] = None,
                           prev_block_votes: Optional[List[Tuple['Address', int]]] = None,
                           block_hash: bytes = None) \
            -> Tuple['Block', List[bytes]]:
        if block_height is None:
            block_height: int = self._block_height + 1
        if block_hash is None:
            block_hash = create_block_hash()
        timestamp_us = create_timestamp()

        block = Block(block_height, block_hash, timestamp_us, self._prev_block_hash, 0)
        context = IconScoreContext(IconScoreContextType.DIRECT)

        is_block_editable = False
        self.icon_service_engine._set_revision_to_context(context)
        if context.is_decentralized():
            is_block_editable = True

        tx_results, state_root_hash, added_transactions, main_prep_as_dict = \
            self.icon_service_engine.invoke(block=block,
                                            tx_requests=tx_list,
                                            prev_block_generator=prev_block_generator,
                                            prev_block_validators=prev_block_validators,
                                            prev_block_votes=prev_block_votes,
                                            is_block_editable=is_block_editable)

        self.add_tx_result(tx_results)
        return block, self.get_hash_list_from_tx_list(tx_list)

    def debug_make_and_req_block(self,
                                 tx_list: list,
                                 prev_block_generator: Optional['Address'] = None,
                                 prev_block_validators: Optional[List['Address']] = None,
                                 prev_block_votes: Optional[List[Tuple['Address', int]]] = None,
                                 block: 'Block' = None) -> tuple:

        # Prevent a base transaction from being added to the original tx_list
        tx_list = copy.copy(tx_list)

        if block is None:
            block_height: int = self._block_height + 1
            block_hash = create_block_hash()
            timestamp_us = create_timestamp()
            block = Block(block_height, block_hash, timestamp_us, self._prev_block_hash, 0)

        context = IconScoreContext(IconScoreContextType.DIRECT)

        is_block_editable = False
        self.icon_service_engine._set_revision_to_context(context)
        if context.is_decentralized():
            is_block_editable = True

        tx_results, state_root_hash, added_transactions, main_prep_as_dict = \
            self.icon_service_engine.invoke(block=block,
                                            tx_requests=tx_list,
                                            prev_block_generator=prev_block_generator,
                                            prev_block_validators=prev_block_validators,
                                            prev_block_votes=prev_block_votes,
                                            is_block_editable=is_block_editable)

        return block, tx_results, state_root_hash, added_transactions, main_prep_as_dict

    def _make_and_req_block_for_issue_test(self,
                                           tx_list: list,
                                           block_height: int = None,
                                           prev_block_generator: Optional['Address'] = None,
                                           prev_block_validators: Optional[List['Address']] = None,
                                           prev_block_votes: Optional[List[Tuple['Address', int]]] = None,
                                           is_block_editable=False,
                                           cumulative_fee: int = 0) -> Tuple['Block', List[bytes]]:
        if block_height is None:
            block_height: int = self._block_height + 1
        block_hash = create_block_hash()
        timestamp_us = create_timestamp()

        block = Block(block_height, block_hash, timestamp_us, self._prev_block_hash, cumulative_fee)

        tx_results, _, added_transactions, main_prep_as_dict = \
            self.icon_service_engine.invoke(block=block,
                                            tx_requests=tx_list,
                                            prev_block_generator=prev_block_generator,
                                            prev_block_validators=prev_block_validators,
                                            prev_block_votes=prev_block_votes,
                                            is_block_editable=is_block_editable)

        self.add_tx_result(tx_results)

        return block, self.get_hash_list_from_tx_list(tx_list)

    def _write_precommit_state(self, block: 'Block') -> None:
        self.icon_service_engine.commit(block.height, block.hash, None)
        self._block_height += 1
        assert block.height == self._block_height
        self._prev_block_hash = block.hash

    def _remove_precommit_state(self, block: 'Block') -> None:
        self.icon_service_engine.rollback(block.height, block.hash)

    def _query(self, request: dict, method: str = 'icx_call') -> Any:
        response = self.icon_service_engine.query(method, request)
        return response

    def inner_call(self, request: dict) -> Any:
        response = self.icon_service_engine.inner_call(request)
        return response

    def _create_invalid_block(self, block_height: int = None) -> 'Block':
        if block_height is None:
            block_height: int = self._block_height
        block_hash = create_block_hash()
        timestamp_us = create_timestamp()

        return Block(block_height, block_hash, timestamp_us, self._prev_block_hash, 0)

    @classmethod
    def _convert_address_from_address_type(cls,
                                           from_: Union[
                                               'EOAAccount',
                                               'Address',
                                               'MalformedAddress',
                                               None]
                                           ) -> Union['Address', 'MalformedAddress', None]:
        if isinstance(from_, EOAAccount):
            return from_.address
        elif isinstance(from_, (Address, MalformedAddress)):
            return from_
        else:
            return None

    # ====== API ===== #
    def process_confirm_block_tx(self,
                                 tx_list: list,
                                 expected_status: bool = True,
                                 prev_block_generator: Optional['Address'] = None,
                                 prev_block_validators: Optional[List['Address']] = None,
                                 prev_block_votes: Optional[List[Tuple['Address', int]]] = None,
                                 block_height: int = None) -> List['TransactionResult']:

        prev_block, hash_list = self.make_and_req_block(tx_list,
                                                        block_height,
                                                        prev_block_generator,
                                                        prev_block_validators,
                                                        prev_block_votes)
        self._write_precommit_state(prev_block)
        tx_results: List['TransactionResult'] = self.get_tx_results(hash_list)
        for tx_result in tx_results:
            self.assertEqual(int(expected_status), tx_result.status)
        return tx_results

    def process_confirm_block(self,
                              tx_list: list,
                              prev_block_generator: Optional['Address'] = None,
                              prev_block_validators: Optional[List['Address']] = None,
                              prev_block_votes: Optional[List[Tuple['Address', int]]] = None,
                              block_height: int = None) -> List['TransactionResult']:

        prev_block, hash_list = self.make_and_req_block(tx_list,
                                                        block_height,
                                                        prev_block_generator,
                                                        prev_block_validators,
                                                        prev_block_votes)
        self._write_precommit_state(prev_block)
        tx_results: List['TransactionResult'] = self.get_tx_results(hash_list)
        return tx_results

    def create_deploy_score_tx(self,
                               score_root: str,
                               score_name: str,
                               from_: Union['EOAAccount', 'Address', None],
                               to_: Union['EOAAccount', 'Address'],
                               deploy_params: dict = None,
                               timestamp_us: int = None,
                               data: bytes = None,
                               is_sys: bool = False,
                               pre_validation_enabled: bool = True,
                               step_limit: int = DEFAULT_DEPLOY_STEP_LIMIT) -> dict:

        addr_from: Optional['Address'] = self._convert_address_from_address_type(from_)
        addr_to: 'Address' = self._convert_address_from_address_type(to_)

        if deploy_params is None:
            deploy_params = {}

        score_path = get_score_path(score_root, score_name)

        if is_sys:
            deploy_data = {'contentType': 'application/tbears', 'content': score_path, 'params': deploy_params}
        else:
            if data is None:
                mz = InMemoryZip()
                mz.zip_in_memory(score_path)
                data = f'0x{mz.data.hex()}'
            else:
                data = f'0x{bytes.hex(data)}'
            deploy_data = {'contentType': 'application/zip', 'content': data, 'params': deploy_params}

        if timestamp_us is None:
            timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "version": self._version,
            "from": addr_from,
            "to": addr_to,
            "stepLimit": step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature,
            "dataType": "deploy",
            "data": deploy_data
        }

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {
            'method': method,
            'params': request_params
        }

        if pre_validation_enabled:
            self.icon_service_engine.validate_transaction(tx)

        return tx

    def create_score_call_tx(self,
                             from_: Union['EOAAccount', 'Address', None],
                             to_: 'Address',
                             func_name: str,
                             params: Optional[dict] = None,
                             value: int = 0,
                             pre_validation_enabled: bool = True,
                             step_limit: int = DEFAULT_BIG_STEP_LIMIT) -> dict:

        from_: Optional['Address'] = self._convert_address_from_address_type(from_)
        if params is None:
            params: dict = {}

        timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "version": self._version,
            "from": from_,
            "to": to_,
            "value": value,
            "stepLimit": step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature,
            "dataType": "call",
            "data": {
                "method": func_name,
                "params": params
            }
        }

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {
            'method': method,
            'params': request_params
        }

        if pre_validation_enabled:
            self.icon_service_engine.validate_transaction(tx)

        return tx

    def create_transfer_icx_tx(self,
                               from_: Union['EOAAccount', 'Address', None],
                               to_: Union['EOAAccount', 'Address', 'MalformedAddress'],
                               value: int,
                               disable_pre_validate: bool = False,
                               support_v2: bool = False,
                               step_limit: int = DEFAULT_STEP_LIMIT) -> dict:

        addr_from: Optional['Address'] = self._convert_address_from_address_type(from_)
        addr_to: Optional['Address', 'MalformedAddress'] = self._convert_address_from_address_type(to_)

        timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "from": addr_from,
            "to": addr_to,
            "value": value,
            "stepLimit": step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature
        }

        if support_v2:
            request_params["fee"] = 10 ** 16
        else:
            request_params["version"] = self._version

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {
            'method': method,
            'params': request_params
        }

        if not disable_pre_validate:
            self.icon_service_engine.validate_transaction(tx)
        return tx

    def create_message_tx(self,
                          from_: Union['EOAAccount', 'Address', None],
                          to_: Union['EOAAccount', 'Address', 'MalformedAddress'],
                          data: bytes = None,
                          value: int = 0) -> dict:

        addr_from: Optional['Address'] = self._convert_address_from_address_type(from_)
        addr_to: Optional['Address', 'MalformedAddress'] = self._convert_address_from_address_type(to_)

        timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "version": self._version,
            "from": addr_from,
            "to": addr_to,
            "value": value,
            "stepLimit": DEFAULT_BIG_STEP_LIMIT,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature,
            "dataType": "message",
            "data": '0x' + data.hex(),
        }

        method = 'icx_sendTransaction'
        # Inserts txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {
            'method': method,
            'params': request_params
        }

        self.icon_service_engine.validate_transaction(tx)
        return tx

    def create_deposit_tx(self,
                          from_: Union['EOAAccount', 'Address', None],
                          to_: 'Address',
                          action: str,
                          params: dict,
                          value: int = 0,
                          pre_validation_enabled: bool = True,
                          step_limit: int = DEFAULT_BIG_STEP_LIMIT) -> dict:

        addr_from: Optional['Address'] = self._convert_address_from_address_type(from_)
        addr_to: 'Address' = self._convert_address_from_address_type(to_)

        timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "version": self._version,
            "from": addr_from,
            "to": addr_to,
            "value": value,
            "stepLimit": step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature,
            "dataType": "deposit",
            "data": {
                "action": action,
            }
        }

        for k, v in params.items():
            request_params["data"][k] = v

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {
            'method': method,
            'params': request_params
        }

        if pre_validation_enabled:
            self.icon_service_engine.validate_transaction(tx)

        return tx

    def create_register_proposal_tx(self,
                                    from_: 'Address',
                                    title: str,
                                    description: str,
                                    type_: int,
                                    value: Union[str, int, 'Address'],
                                    step_limit: int = DEFAULT_BIG_STEP_LIMIT) -> dict:
        text = '{"address":"%s"}' % value
        json_data: bytes = text.encode("utf-8")

        method = "registerProposal"
        score_params = {
            "title": title,
            "description": description,
            "type": hex(type_),
            "value": bytes_to_hex(json_data)
        }

        return self.create_score_call_tx(from_=from_,
                                         to_=GOVERNANCE_SCORE_ADDRESS,
                                         func_name=method,
                                         params=score_params,
                                         step_limit=step_limit)

    def create_vote_proposal_tx(self,
                                from_: 'Address',
                                id_: bytes,
                                vote: bool,
                                step_limit=DEFAULT_BIG_STEP_LIMIT) -> dict:
        method = "voteProposal"
        score_params = {
            "id": bytes_to_hex(id_, "0x"),
            "vote": hex(vote)
        }

        return self.create_score_call_tx(from_=from_,
                                         to_=GOVERNANCE_SCORE_ADDRESS,
                                         func_name=method,
                                         params=score_params,
                                         step_limit=step_limit)

    @staticmethod
    def _convert_tx_for_estimating_step_from_origin_tx(tx: dict):
        tx = copy.deepcopy(tx)
        tx["method"] = "debug_estimateStep"
        del tx["params"]["nonce"]
        del tx["params"]["stepLimit"]
        del tx["params"]["timestamp"]
        del tx["params"]["txHash"]
        del tx["params"]["signature"]
        return tx

    # ===== wrapping API ===== #
    def estimate_step(self, tx: dict):
        converted_tx = self._convert_tx_for_estimating_step_from_origin_tx(tx)
        return self.icon_service_engine.estimate_step(request=converted_tx)

    def update_governance(self,
                          version: str = "latest_version",
                          expected_status: bool = True) -> List['TransactionResult']:

        tx = self.create_deploy_score_tx("sample_builtin",
                                         f"{version}/governance",
                                         self._admin,
                                         GOVERNANCE_SCORE_ADDRESS)
        return self.process_confirm_block_tx([tx], expected_status)

    def transfer_icx(self,
                     from_: Union['EOAAccount', 'Address', None],
                     to_: Union['EOAAccount', 'Address', 'MalformedAddress'],
                     value: int,
                     disable_pre_validate: bool = False,
                     support_v2: bool = False,
                     step_limit: int = DEFAULT_STEP_LIMIT,
                     expected_status: bool = True) -> List['TransactionResult']:
        tx = self.create_transfer_icx_tx(from_=from_,
                                         to_=to_,
                                         value=value,
                                         disable_pre_validate=disable_pre_validate,
                                         support_v2=support_v2,
                                         step_limit=step_limit)
        return self.process_confirm_block_tx([tx], expected_status)

    def deploy_score(self,
                     score_root: str,
                     score_name: str,
                     from_: Union['EOAAccount', 'Address', None],
                     deploy_params: dict = None,
                     step_limit: int = DEFAULT_DEPLOY_STEP_LIMIT,
                     expected_status: bool = True,
                     to_: Union['EOAAccount', 'Address'] = ZERO_SCORE_ADDRESS,
                     data: bytes = None) -> List['TransactionResult']:

        tx = self.create_deploy_score_tx(score_root=score_root,
                                         score_name=score_name,
                                         from_=from_,
                                         to_=to_,
                                         deploy_params=deploy_params,
                                         step_limit=step_limit,
                                         data=data)
        return self.process_confirm_block_tx([tx], expected_status)

    def score_call(self,
                   from_: Union['EOAAccount', 'Address', None],
                   to_: 'Address',
                   func_name: str,
                   params: dict = None,
                   value: int = 0,
                   step_limit: int = DEFAULT_BIG_STEP_LIMIT,
                   expected_status: bool = True) -> List['TransactionResult']:

        tx = self.create_score_call_tx(from_=from_,
                                       to_=to_,
                                       func_name=func_name,
                                       params=params,
                                       value=value,
                                       step_limit=step_limit)
        return self.process_confirm_block_tx([tx], expected_status)

    def set_revision(self,
                     revision: int,
                     expected_status: bool = True) -> List['TransactionResult']:

        return self.score_call(from_=self._admin,
                               to_=GOVERNANCE_SCORE_ADDRESS,
                               func_name="setRevision",
                               params={"code": hex(revision), "name": f"1.1.{revision}"},
                               expected_status=expected_status)

    def accept_score(self,
                     tx_hash: Union[bytes, str],
                     warning_message: str = None,
                     expected_status: bool = True) -> List['TransactionResult']:
        if isinstance(tx_hash, bytes):
            tx_hash_str = f'0x{bytes.hex(tx_hash)}'
        else:
            tx_hash_str = tx_hash

        params: dict = {"txHash": tx_hash_str}
        if warning_message is not None:
            params["warning"] = warning_message

        return self.score_call(from_=self._admin,
                               to_=GOVERNANCE_SCORE_ADDRESS,
                               func_name="acceptScore",
                               params=params,
                               expected_status=expected_status)

    def reject_score(self,
                     tx_hash: Union[bytes, str],
                     reason: str = "reason",
                     expected_status: bool = True) -> List['TransactionResult']:

        if isinstance(tx_hash, bytes):
            tx_hash_str = f'0x{bytes.hex(tx_hash)}'
        else:
            tx_hash_str = tx_hash

        return self.score_call(from_=self._admin,
                               to_=GOVERNANCE_SCORE_ADDRESS,
                               func_name="rejectScore",
                               params={"txHash": tx_hash_str,
                                       "reason": reason},
                               expected_status=expected_status)

    def deposit_icx(self,
                    score_address: 'Address',
                    amount: int,
                    period: int,
                    sender: Union['EOAAccount', 'Address', None] = None,
                    expected_status: bool = True) -> List['TransactionResult']:

        if sender is None:
            sender = self._admin
        if FIXED_TERM:
            tx: dict = self.create_deposit_tx(from_=sender,
                                              to_=score_address,
                                              action="add",
                                              params={},
                                              value=amount)
        else:
            tx: dict = self.create_deposit_tx(from_=sender,
                                              to_=score_address,
                                              action="add",
                                              params={"term": hex(period)},
                                              value=amount)
        return self.process_confirm_block_tx([tx], expected_status)

    def withdraw_deposit(self,
                         deposit_id: bytes,
                         score_address: 'Address',
                         sender: Union['EOAAccount', 'Address', None] = None,
                         expected_status: bool = True) -> List['TransactionResult']:
        if sender is None:
            sender = self._admin
        tx: dict = self.create_deposit_tx(from_=sender,
                                          to_=score_address,
                                          action="withdraw",
                                          params={"id": f"0x{bytes.hex(deposit_id)}"})
        return self.process_confirm_block_tx([tx], expected_status)

    def register_proposal(self,
                          from_: 'Address',
                          title: str,
                          description: str,
                          type_: int,
                          value: Union[str, int, 'Address'],
                          expected_status: bool = True) -> 'TransactionResult':
        tx: dict = self.create_register_proposal_tx(from_, title, description, type_, value)

        # 0: base transaction, 1: register proposal
        tx_results = self.process_confirm_block_tx([tx], expected_status)

        return tx_results[1]

    def vote_proposal(self,
                      from_: 'Address',
                      id_: bytes,
                      vote: bool,
                      expected_status: bool = True) -> List['TransactionResult']:
        tx: dict = self.create_vote_proposal_tx(from_, id_, vote)
        return self.process_confirm_block_tx([tx], expected_status)

    def get_balance(self,
                    account: Union['EOAAccount', 'Address']) -> int:

        address: Optional['Address'] = self._convert_address_from_address_type(account)

        return self._query(
            request={
                "address": address
            },
            method="icx_getBalance"
        )

    def get_total_supply(self) -> int:
        return self._query(request={}, method="icx_getTotalSupply")

    def get_score_api(self, address: 'Address'):
        return self._query(
            request={
                "address": address
            },
            method='icx_getScoreApi'
        )

    def query_score(self,
                    from_: Union['EOAAccount', 'Address', None],
                    to_: 'Address',
                    func_name: str,
                    params: dict = None):
        query_request = {
            "version": self._version,
            "from": from_,
            "to": to_,
            "dataType": "call",
            "data": {
                "method": func_name,
                "params": {} if params is None else params
            }
        }
        return self._query(query_request)

    def get_score_status(self, to_: 'Address'):
        query_request = {
            "version": self._version,
            "from": self._admin,
            "to": GOVERNANCE_SCORE_ADDRESS,
            "dataType": "call",
            "data": {
                "method": "getScoreStatus",
                "params": {"address": str(to_)}
            }
        }
        return self._query(query_request)

    def get_step_price(self) -> int:
        query_request = {
            "version": self._version,
            "from": self._admin,
            "to": GOVERNANCE_SCORE_ADDRESS,
            "dataType": "call",
            "data": {
                "method": "getStepPrice",
                "params": {}
            }
        }
        return self._query(query_request)

    @staticmethod
    def create_eoa_accounts(count: int) -> List['EOAAccount']:
        accounts: list = []
        wallets: List['KeyWallet'] = [KeyWallet.create() for _ in range(count)]
        for wallet in wallets:
            accounts.append(EOAAccount(wallet))
        return accounts
コード例 #5
0
class IconScoreInnerTask(object):
    def __init__(self, conf: 'IconConfig'):
        self._conf = conf
        self._thread_flag = ENABLE_THREAD_FLAG

        self._icon_service_engine = IconServiceEngine()
        self._open()

        self._thread_pool = {
            THREAD_INVOKE: ThreadPoolExecutor(1),
            THREAD_QUERY: ThreadPoolExecutor(1),
            THREAD_VALIDATE: ThreadPoolExecutor(1)
        }

    def _open(self):
        Logger.info("icon_score_service open", ICON_INNER_LOG_TAG)
        self._icon_service_engine.open(self._conf)

    def _is_thread_flag_on(self, flag: 'EnableThreadFlag') -> bool:
        return (self._thread_flag & flag) == flag

    def _log_exception(self,
                       e: BaseException,
                       tag: str = ICON_INNER_LOG_TAG) -> None:
        Logger.exception(e, tag)
        Logger.error(e, tag)

    @message_queue_task
    async def hello(self):
        Logger.info('icon_score_hello', ICON_INNER_LOG_TAG)

    def _close(self):
        Logger.info("icon_score_service close", ICON_INNER_LOG_TAG)

        if self._icon_service_engine:
            self._icon_service_engine.close()
            self._icon_service_engine = None
        MessageQueueService.loop.stop()

    @message_queue_task
    async def close(self):
        self._close()

    @message_queue_task
    async def invoke(self, request: dict):
        Logger.info(f'invoke request with {request}', ICON_INNER_LOG_TAG)
        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_INVOKE],
                                              self._invoke, request)
        else:
            return self._invoke(request)

    def _invoke(self, request: dict):
        """Process transactions in a block

        :param request:
        :return:
        """

        response = None
        try:
            params = TypeConverter.convert(request, ParamType.INVOKE)
            converted_block_params = params['block']
            block = Block.from_dict(converted_block_params)

            converted_tx_requests = params['transactions']
            tx_results, state_root_hash = self._icon_service_engine.invoke(
                block=block, tx_requests=converted_tx_requests)

            convert_tx_results = \
                {bytes.hex(tx_result.tx_hash): tx_result.to_dict(to_camel_case) for tx_result in tx_results}
            results = {
                'txResults': convert_tx_results,
                'stateRootHash': bytes.hex(state_root_hash)
            }
            response = MakeResponse.make_response(results)
        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.SERVER_ERROR, str(e))
        finally:
            Logger.info(f'invoke response with {response}', ICON_INNER_LOG_TAG)
            return response

    @message_queue_task
    async def query(self, request: dict):
        Logger.info(f'query request with {request}', ICON_INNER_LOG_TAG)
        if self._is_thread_flag_on(EnableThreadFlag.QUERY):
            loop = get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_QUERY],
                                              self._query, request)
        else:
            return self._query(request)

    def _query(self, request: dict):
        response = None

        try:
            method = request['method']

            if method == 'debug_estimateStep':
                converted_request = TypeConverter.convert(
                    request, ParamType.INVOKE_TRANSACTION)
                value = self._icon_service_engine.estimate_step(
                    converted_request)
            else:
                converted_request = TypeConverter.convert(
                    request, ParamType.QUERY)
                value = self._icon_service_engine.query(
                    method, converted_request['params'])

            if isinstance(value, Address):
                value = str(value)
            response = MakeResponse.make_response(value)
        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.SERVER_ERROR, str(e))
        finally:
            Logger.info(f'query response with {response}', ICON_INNER_LOG_TAG)
            return response

    @message_queue_task
    async def write_precommit_state(self, request: dict):
        Logger.info(f'write_precommit_state request with {request}',
                    ICON_INNER_LOG_TAG)
        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_INVOKE],
                                              self._write_precommit_state,
                                              request)
        else:
            return self._write_precommit_state(request)

    def _write_precommit_state(self, request: dict):
        response = None
        try:
            converted_block_params = TypeConverter.convert(
                request, ParamType.WRITE_PRECOMMIT)
            block = Block.from_dict(converted_block_params)

            self._icon_service_engine.commit(block)
            response = MakeResponse.make_response(ExceptionCode.OK)
        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.SERVER_ERROR, str(e))
        finally:
            Logger.info(f'write_precommit_state response with {response}',
                        ICON_INNER_LOG_TAG)
            return response

    @message_queue_task
    async def remove_precommit_state(self, request: dict):
        Logger.info(f'remove_precommit_state request with {request}',
                    ICON_INNER_LOG_TAG)
        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_INVOKE],
                                              self._remove_precommit_state,
                                              request)
        else:
            return self._remove_precommit_state(request)

    def _remove_precommit_state(self, request: dict):
        response = None
        try:
            converted_block_params = TypeConverter.convert(
                request, ParamType.WRITE_PRECOMMIT)
            block = Block.from_dict(converted_block_params)

            self._icon_service_engine.rollback(block)
            response = MakeResponse.make_response(ExceptionCode.OK)
        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.SERVER_ERROR, str(e))
        finally:
            Logger.info(f'remove_precommit_state response with {response}',
                        ICON_INNER_LOG_TAG)
            return response

    @message_queue_task
    async def validate_transaction(self, request: dict):
        Logger.info(f'pre_validate_check request with {request}',
                    ICON_INNER_LOG_TAG)
        if self._is_thread_flag_on(EnableThreadFlag.VALIDATE):
            loop = get_event_loop()
            return await loop.run_in_executor(
                self._thread_pool[THREAD_VALIDATE], self._validate_transaction,
                request)
        else:
            return self._validate_transaction(request)

    def _validate_transaction(self, request: dict):
        response = None
        try:
            converted_request = TypeConverter.convert(
                request, ParamType.VALIDATE_TRANSACTION)
            self._icon_service_engine.validate_transaction(converted_request)
            response = MakeResponse.make_response(ExceptionCode.OK)
        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.SERVER_ERROR, str(e))
        finally:
            Logger.info(f'pre_validate_check response with {response}',
                        ICON_INNER_LOG_TAG)
            return response

    @message_queue_task
    async def change_block_hash(self, params):
        return ExceptionCode.OK
コード例 #6
0
ファイル: icon_integrate_test.py プロジェクト: xgenvn/t-bears
class IconIntegrateTestBase(TestCase):
    @classmethod
    def setUpClass(cls):
        cls._score_root_path = '.testscore'
        cls._state_db_root_path = '.teststatedb'

        cls._icx_factor = 10**18

        cls._genesis: 'KeyWallet' = KeyWallet.create()
        cls._fee_treasury: 'KeyWallet' = KeyWallet.create()
        cls._test1: 'KeyWallet' = KeyWallet.load(
            bytes.fromhex(TEST1_PRIVATE_KEY))
        cls._wallet_array = [KeyWallet.load(v) for v in TEST_ACCOUNTS]

    def setUp(self,
              genesis_accounts: List[Account] = None,
              block_confirm_interval: int = tbears_server_config[
                  TbConf.BLOCK_CONFIRM_INTERVAL],
              network_only: bool = False,
              network_delay_ms: int = tbears_server_config[
                  TbConf.NETWORK_DELAY_MS]):

        self._block_height = -1
        self._prev_block_hash = None
        self._block_confirm_interval = block_confirm_interval
        self._network_only: bool = network_only
        self._network_delay: float = network_delay_ms / 1000

        if self._network_only:
            return

        root_clear(self._score_root_path, self._state_db_root_path)

        config = IconConfig("", default_icon_config)
        config.load()
        config.update_conf(
            {ConfigKey.BUILTIN_SCORE_OWNER: self._test1.get_address()})
        config.update_conf({
            ConfigKey.SCORE_ROOT_PATH:
            self._score_root_path,
            ConfigKey.STATE_DB_ROOT_PATH:
            self._state_db_root_path
        })
        config.update_conf(self._make_init_config())

        self.icon_service_engine = IconServiceEngine()
        self._mock_rc_proxy()
        self.icon_service_engine.open(config)

        self._genesis_invoke(genesis_accounts)
        self._tx_results: dict = {}

    def tearDown(self):
        if not self._network_only:
            self.icon_service_engine.close()
        root_clear(self._score_root_path, self._state_db_root_path)

    def _make_init_config(self) -> dict:
        return {}

    @staticmethod
    def _append_list(tx: dict, genesis_accounts: List[Account]) -> None:
        """Appends additional genesis account list to genesisData

        :param genesis_accounts: additional genesis account list consisted of namedtuple named Account
        of which keys are name, address and balance
        :return: None
        """
        for account_as_namedtuple in genesis_accounts:
            tx["genesisData"]['accounts'].append({
                "name":
                account_as_namedtuple.name,
                "address":
                account_as_namedtuple.address,
                "balance":
                account_as_namedtuple.balance
            })

    def _genesis_invoke(self, genesis_accounts: List[Account]) -> list:
        tx_hash = create_tx_hash()
        timestamp_us = create_timestamp()
        request_params = {
            'txHash': tx_hash,
            'version': 3,
            'timestamp': timestamp_us
        }

        tx = {
            'method': 'icx_sendTransaction',
            'params': request_params,
            'genesisData': {
                "accounts": [{
                    "name":
                    "genesis",
                    "address":
                    Address.from_string(self._genesis.get_address()),
                    "balance":
                    100 * self._icx_factor
                }, {
                    "name":
                    "fee_treasury",
                    "address":
                    Address.from_string(self._fee_treasury.get_address()),
                    "balance":
                    0
                }, {
                    "name":
                    "_admin",
                    "address":
                    Address.from_string(self._test1.get_address()),
                    "balance":
                    1_000_000 * self._icx_factor
                }]
            },
        }
コード例 #7
0
class IconScoreInnerTask(object):
    def __init__(self, conf: dict):
        self._conf = conf
        self._thread_flag = ENABLE_THREAD_FLAG

        self._icon_service_engine = IconServiceEngine()
        self._open()

        self._thread_pool = {
            THREAD_INVOKE: ThreadPoolExecutor(1),
            THREAD_STATUS: ThreadPoolExecutor(1),
            THREAD_QUERY: ThreadPoolExecutor(1),
            THREAD_ESTIMATE: ThreadPoolExecutor(1),
            THREAD_VALIDATE: ThreadPoolExecutor(1)
        }

    def _open(self):
        Logger.info(tag=_TAG, msg="_open() start")
        self._icon_service_engine.open(self._conf)
        Logger.info(tag=_TAG, msg="_open() end")

    def _is_thread_flag_on(self, flag: 'EnableThreadFlag') -> bool:
        return (self._thread_flag & flag) == flag

    def _check_icon_service_ready(self):
        if not self._icon_service_engine.is_reward_calculator_ready():
            raise ServiceNotReadyException("Reward Calculator is not ready")

    @staticmethod
    def _log_exception(e: BaseException, tag: str) -> None:
        Logger.exception(str(e), tag)
        Logger.error(str(e), tag)

    @message_queue_task
    async def hello(self):
        Logger.info(tag=_TAG, msg='hello() start')

        ready_future = self._icon_service_engine.get_ready_future()
        await ready_future

        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = asyncio.get_event_loop()
            ret = await loop.run_in_executor(self._thread_pool[THREAD_INVOKE], self._hello)
        else:
            ret = self._hello()

        Logger.info(tag=_TAG, msg='hello() end')

        return ret

    def _hello(self):
        return self._icon_service_engine.hello()

    def cleanup(self):
        Logger.info(tag=_TAG, msg="cleanup() start")

        # shutdown thread pool executors
        for executor in self._thread_pool.values():
            executor.shutdown()

        # close ICON Service
        if self._icon_service_engine:
            self._icon_service_engine.close()
            self._icon_service_engine = None

        Logger.info(tag=_TAG, msg="cleanup() end")

    @message_queue_task
    async def close(self):
        Logger.info(tag=_TAG, msg="close() stop event loop")
        self._close()

    @staticmethod
    def _close():
        asyncio.get_event_loop().stop()

    @message_queue_task
    async def invoke(self, request: dict) -> dict:
        Logger.debug(tag=_TAG, msg=f'invoke() start')

        try:
            self._check_icon_service_ready()
        except ServiceNotReadyException as e:
            return MakeResponse.make_error_response(e.code, str(e))

        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = asyncio.get_event_loop()
            ret: dict = await loop.run_in_executor(self._thread_pool[THREAD_INVOKE],
                                                   self._invoke, request)
        else:
            ret: dict = self._invoke(request)

        Logger.debug(tag=_TAG, msg=f'invoke() end')
        return ret

    def _invoke(self, request: dict):
        """Process transactions in a block

        :param request:
        :return:
        """

        Logger.info(tag=_TAG, msg=f'INVOKE Request: {request}')

        try:
            params = TypeConverter.convert(request, ParamType.INVOKE)
            converted_block_params = params['block']
            block = Block.from_dict(converted_block_params)
            Logger.info(tag=_TAG, msg=f'INVOKE: BH={block.height}')

            converted_tx_requests = params['transactions']

            convert_tx_result_to_dict: bool = 'isBlockEditable' in params

            converted_is_block_editable = params.get('isBlockEditable', False)
            converted_prev_block_generator = params.get('prevBlockGenerator')
            converted_prev_block_validators = params.get('prevBlockValidators')
            converted_prev_votes = params.get('prevBlockVotes')

            tx_results, state_root_hash, added_transactions, next_preps, is_shutdown = \
                self._icon_service_engine.invoke(
                    block=block,
                    tx_requests=converted_tx_requests,
                    prev_block_generator=converted_prev_block_generator,
                    prev_block_validators=converted_prev_block_validators,
                    prev_block_votes=converted_prev_votes,
                    is_block_editable=converted_is_block_editable
                )

            if convert_tx_result_to_dict:
                convert_tx_results = [tx_result.to_dict(to_camel_case) for tx_result in tx_results]
            else:
                # old version
                convert_tx_results = {bytes.hex(tx_result.tx_hash): tx_result.to_dict(to_camel_case)
                                      for tx_result in tx_results}
            results = {
                'txResults': convert_tx_results,
                'stateRootHash': bytes.hex(state_root_hash),
                'addedTransactions': added_transactions
            }

            if next_preps:
                results["prep"] = next_preps
            if is_shutdown:
                results["is_shutdown"] = True

            response = MakeResponse.make_response(results)
        except FatalException as e:
            self._log_exception(e, _TAG)
            response = MakeResponse.make_error_response(ExceptionCode.SYSTEM_ERROR, str(e))
            self._close()
        except InvalidBaseTransactionException as e:
            self._log_exception(e, _TAG)
            response = MakeResponse.make_error_response(ExceptionCode.SYSTEM_ERROR, str(e))
        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))
        finally:
            if self._icon_service_engine:
                self._icon_service_engine.clear_context_stack()

        Logger.info(tag=_TAG, msg=f'INVOKE Response: {json.dumps(response, cls=BytesToHexJSONEncoder)}')
        return response

    @message_queue_task
    async def query(self, request: dict) -> dict:
        try:
            self._check_icon_service_ready()
        except ServiceNotReadyException as e:
            return MakeResponse.make_error_response(e.code, str(e))

        return await self._get_query_response(request)

    async def _get_query_response(self, request: dict) -> dict:
        try:
            value = await self._execute_query(request)
            if isinstance(value, Address):
                value = str(value)
            response = MakeResponse.make_response(value)
        except FatalException as e:
            self._log_exception(e, _TAG)
            response = MakeResponse.make_error_response(ExceptionCode.SYSTEM_ERROR, str(e))
        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))

        self._icon_service_engine.clear_context_stack()
        return response

    async def _execute_query(self, request: dict):
        method_name: str = request['method']
        if method_name == RPCMethod.DEBUG_ESTIMATE_STEP:
            method: callable = self._estimate
            args = [request]
        else:
            method: callable = self._query
            args = [request, method_name]

        if self._is_thread_flag_on(EnableThreadFlag.QUERY):
            return await asyncio.get_event_loop(). \
                run_in_executor(self._thread_pool[QUERY_THREAD_MAPPER[method_name]],
                                method, *args)
        else:
            return method(*args)

    def _estimate(self, request: dict):
        converted_request = TypeConverter.convert(request, ParamType.INVOKE_TRANSACTION)
        return self._icon_service_engine.estimate_step(converted_request)

    def _query(self, request: dict, method: str):
        converted_request = TypeConverter.convert(request, ParamType.QUERY)
        return self._icon_service_engine.query(method, converted_request['params'])

    @message_queue_task
    async def call(self, request: dict):
        Logger.info(tag=_TAG, msg=f'call() start: {request}')

        try:
            self._check_icon_service_ready()
        except ServiceNotReadyException as e:
            return MakeResponse.make_error_response(e.code, str(e))

        if self._is_thread_flag_on(EnableThreadFlag.QUERY):
            loop = asyncio.get_event_loop()
            ret = await loop.run_in_executor(self._thread_pool[THREAD_QUERY],
                                             self._call, request)
        else:
            ret = self._call(request)

        Logger.info(tag=_TAG, msg=f'call() end: {ret}')
        return ret

    def _call(self, request: dict):
        try:
            response = self._icon_service_engine.inner_call(request)

            if isinstance(response, Address):
                response = str(response)
        except FatalException as e:
            self._log_exception(e, _TAG)
            response = MakeResponse.make_error_response(ExceptionCode.SYSTEM_ERROR, str(e))
        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))

        return response

    @message_queue_task
    async def write_precommit_state(self, request: dict):
        try:
            self._check_icon_service_ready()
        except ServiceNotReadyException as e:
            return MakeResponse.make_error_response(e.code, str(e))

        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = asyncio.get_event_loop()
            ret = await loop.run_in_executor(self._thread_pool[THREAD_INVOKE],
                                             self._write_precommit_state, request)
        else:
            ret = self._write_precommit_state(request)

        return ret

    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

    @message_queue_task
    async def remove_precommit_state(self, request: dict):
        Logger.info(tag=_TAG, msg=f'remove_precommit_state() start')

        try:
            self._check_icon_service_ready()
        except ServiceNotReadyException as e:
            return MakeResponse.make_error_response(e.code, str(e))

        """
        Unused API
        """
        return {}

    @message_queue_task
    async def rollback(self, request: dict):
        """Go back to the state of the given previous block

        :param request:
        :return:
        """

        Logger.info(tag=_TAG, msg=f"rollback() start")

        try:
            self._check_icon_service_ready()
        except ServiceNotReadyException as e:
            return MakeResponse.make_error_response(e.code, str(e))

        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = asyncio.get_event_loop()
            response = await loop.run_in_executor(self._thread_pool[THREAD_INVOKE], self._rollback, request)
        else:
            response = self._rollback(request)

        Logger.info(tag=_TAG, msg=f"rollback() end")

        return response

    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

    @message_queue_task
    async def validate_transaction(self, request: dict):
        try:
            self._check_icon_service_ready()
        except ServiceNotReadyException as e:
            return MakeResponse.make_error_response(e.code, str(e))

        if self._is_thread_flag_on(EnableThreadFlag.VALIDATE):
            loop = asyncio.get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_VALIDATE],
                                              self._validate_transaction, request)
        else:
            return self._validate_transaction(request)

    def _validate_transaction(self, request: dict):
        try:
            Logger.info(tag=_TAG, msg=f'validate_transaction Request: {request}')
            converted_request = TypeConverter.convert(request, ParamType.VALIDATE_TRANSACTION)
            self._icon_service_engine.validate_transaction(converted_request, request)
            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))
        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))

        self._icon_service_engine.clear_context_stack()
        return response

    @message_queue_task
    async def change_block_hash(self, _params):
        try:
            self._check_icon_service_ready()
        except ServiceNotReadyException as e:
            return MakeResponse.make_error_response(e.code, str(e))

        return ExceptionCode.OK

    @message_queue_task
    async def dos_guard(self, params: dict) -> dict:
        try:
            Logger.info(tag=_TAG, msg=f'dos_guard: params: {params}')
            self._check_icon_service_ready()
        except ServiceNotReadyException as e:
            return MakeResponse.make_error_response(e.code, str(e))

        try:
            self._icon_service_engine.dos_guard.run(_from=params["from"])
            response = MakeResponse.make_response(ExceptionCode.OK)
        except Exception as e:
            self._log_exception(e, _TAG)
            response = MakeResponse.make_error_response(ExceptionCode.SYSTEM_ERROR, str(e))
        return response
コード例 #8
0
class IconScoreInnerTask(object):
    def __init__(self, conf: 'IconConfig'):
        self._conf = conf
        self._thread_flag = ENABLE_THREAD_FLAG

        self._icon_service_engine = IconServiceEngine()
        self._open()

        self._thread_pool = {
            THREAD_INVOKE: ThreadPoolExecutor(1),
            THREAD_QUERY: ThreadPoolExecutor(1),
            THREAD_VALIDATE: ThreadPoolExecutor(1)
        }

    def _open(self):
        Logger.info("icon_score_service open", ICON_INNER_LOG_TAG)
        self._icon_service_engine.open(self._conf)

    def _is_thread_flag_on(self, flag: 'EnableThreadFlag') -> bool:
        return (self._thread_flag & flag) == flag

    def _check_icon_service_ready(self):
        if not self._icon_service_engine.is_reward_calculator_ready():
            raise ServiceNotReadyException("Reward Calculator is not ready")

    @staticmethod
    def _log_exception(e: BaseException,
                       tag: str = ICON_INNER_LOG_TAG) -> None:
        Logger.exception(str(e), tag)
        Logger.error(str(e), tag)

    @message_queue_task
    async def hello(self):
        Logger.info('hello() start', ICON_INNER_LOG_TAG)

        ready_future = self._icon_service_engine.get_ready_future()
        await ready_future

        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = asyncio.get_event_loop()
            ret = await loop.run_in_executor(self._thread_pool[THREAD_INVOKE],
                                             self._hello)
        else:
            ret = self._hello()

        Logger.info('hello() end', ICON_INNER_LOG_TAG)

        return ret

    def _hello(self):
        return self._icon_service_engine.hello()

    def _close(self):
        Logger.info(tag=_TAG, msg="_close() start")

        if self._icon_service_engine:
            self._icon_service_engine.close()
            self._icon_service_engine = None
        MessageQueueService.loop.stop()

        Logger.info(tag=_TAG, msg="_close() end")

    @message_queue_task
    async def close(self):
        Logger.info(tag=_TAG, msg="close() start")

        self._close()

        Logger.info(tag=_TAG, msg="close() end")

    @message_queue_task
    async def invoke(self, request: dict):
        Logger.info(f'invoke request with {request}', ICON_INNER_LOG_TAG)

        self._check_icon_service_ready()

        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = asyncio.get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_INVOKE],
                                              self._invoke, request)
        else:
            return self._invoke(request)

    def _invoke(self, request: dict):
        """Process transactions in a block

        :param request:
        :return:
        """

        response = None
        try:
            params = TypeConverter.convert(request, ParamType.INVOKE)
            converted_block_params = params['block']
            block = Block.from_dict(converted_block_params)

            converted_tx_requests = params['transactions']

            convert_tx_result_to_dict: bool = 'isBlockEditable' in params

            converted_is_block_editable = params.get('isBlockEditable', False)
            converted_prev_block_generator = params.get('prevBlockGenerator')
            converted_prev_block_validators = params.get('prevBlockValidators')
            converted_prev_votes = params.get('prevBlockVotes')

            tx_results, state_root_hash, added_transactions, main_prep_as_dict = self._icon_service_engine.invoke(
                block=block,
                tx_requests=converted_tx_requests,
                prev_block_generator=converted_prev_block_generator,
                prev_block_validators=converted_prev_block_validators,
                prev_block_votes=converted_prev_votes,
                is_block_editable=converted_is_block_editable)

            if convert_tx_result_to_dict:
                convert_tx_results = [
                    tx_result.to_dict(to_camel_case)
                    for tx_result in tx_results
                ]
            else:
                # old version
                convert_tx_results = {
                    bytes.hex(tx_result.tx_hash):
                    tx_result.to_dict(to_camel_case)
                    for tx_result in tx_results
                }
            results = {
                'txResults': convert_tx_results,
                'stateRootHash': bytes.hex(state_root_hash),
                'addedTransactions': added_transactions
            }

            if main_prep_as_dict:
                results["prep"] = main_prep_as_dict

            Logger.info(f'invoke origin response with {results}',
                        ICON_INNER_LOG_TAG)
            response = MakeResponse.make_response(results)
        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 InvalidBaseTransactionException as e:
            self._log_exception(e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SYSTEM_ERROR, str(e))
        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:
            if self._icon_service_engine:
                self._icon_service_engine.clear_context_stack()
            return response

    @message_queue_task
    async def query(self, request: dict):
        Logger.debug(f'query request with {request}', ICON_INNER_LOG_TAG)

        self._check_icon_service_ready()

        if self._is_thread_flag_on(EnableThreadFlag.QUERY):
            loop = asyncio.get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_QUERY],
                                              self._query, request)
        else:
            return self._query(request)

    def _query(self, request: dict):
        response = None

        try:
            method = request['method']

            if method == 'debug_estimateStep':
                converted_request = TypeConverter.convert(
                    request, ParamType.INVOKE_TRANSACTION)
                value = self._icon_service_engine.estimate_step(
                    converted_request)
            else:
                converted_request = TypeConverter.convert(
                    request, ParamType.QUERY)
                value = self._icon_service_engine.query(
                    method, converted_request['params'])

            if isinstance(value, Address):
                value = str(value)
            response = MakeResponse.make_response(value)
        except FatalException as e:
            self._log_exception(e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SYSTEM_ERROR, str(e))
        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.debug(f'query response with {response}', ICON_INNER_LOG_TAG)
            self._icon_service_engine.clear_context_stack()
            return response

    @message_queue_task
    async def call(self, request: dict):
        Logger.info(f'call request with {request}', ICON_INNER_LOG_TAG)

        self._check_icon_service_ready()

        if self._is_thread_flag_on(EnableThreadFlag.QUERY):
            loop = asyncio.get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_QUERY],
                                              self._call, request)
        else:
            return self._call(request)

    def _call(self, request: dict):
        response = None

        try:
            response = self._icon_service_engine.inner_call(request)

            if isinstance(response, Address):
                response = str(response)
        except FatalException as e:
            self._log_exception(e, ICON_SERVICE_LOG_TAG)
            response = MakeResponse.make_error_response(
                ExceptionCode.SYSTEM_ERROR, str(e))
        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'call response with {response}', ICON_INNER_LOG_TAG)
            return response

    @message_queue_task
    async def write_precommit_state(self, request: dict):
        Logger.info(f'write_precommit_state request with {request}',
                    ICON_INNER_LOG_TAG)

        self._check_icon_service_ready()

        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = asyncio.get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_INVOKE],
                                              self._write_precommit_state,
                                              request)
        else:
            return self._write_precommit_state(request)

    @staticmethod
    def _get_block_info_for_precommit_state(
            converted_block_params: dict
    ) -> Tuple[int, bytes, Optional[bytes]]:
        block_height: int = converted_block_params[ConstantKeys.BLOCK_HEIGHT]
        block_hash: Optional[bytes] = None
        if ConstantKeys.BLOCK_HASH in converted_block_params:
            instant_block_hash: bytes = converted_block_params[
                ConstantKeys.BLOCK_HASH]
        else:
            instant_block_hash: bytes = converted_block_params[
                ConstantKeys.OLD_BLOCK_HASH]
            block_hash = converted_block_params[ConstantKeys.NEW_BLOCK_HASH]

        return block_height, instant_block_hash, block_hash

    def _write_precommit_state(self, request: dict):
        response = None
        try:
            converted_block_params = TypeConverter.convert(
                request, ParamType.WRITE_PRECOMMIT)
            block_height, instant_block_hash, block_hash = \
                self._get_block_info_for_precommit_state(converted_block_params)

            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, 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'write_precommit_state response with {response}',
                        ICON_INNER_LOG_TAG)
            return response

    @message_queue_task
    async def remove_precommit_state(self, request: dict):
        Logger.info(f'remove_precommit_state request with {request}',
                    ICON_INNER_LOG_TAG)

        self._check_icon_service_ready()

        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = asyncio.get_event_loop()
            return await loop.run_in_executor(self._thread_pool[THREAD_INVOKE],
                                              self._remove_precommit_state,
                                              request)
        else:
            return self._remove_precommit_state(request)

    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

    @message_queue_task
    async def rollback(self, request: dict):
        """Go back to the state of the given previous block

        :param request:
        :return:
        """

        Logger.info(tag=ICON_INNER_LOG_TAG, msg=f"rollback() start: {request}")

        self._check_icon_service_ready()

        if self._is_thread_flag_on(EnableThreadFlag.INVOKE):
            loop = asyncio.get_event_loop()
            response = await loop.run_in_executor(
                self._thread_pool[THREAD_INVOKE], self._rollback, request)
        else:
            response = self._rollback(request)

        Logger.info(tag=ICON_INNER_LOG_TAG, msg=f"rollback() end: {response}")

        return response

    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

    @message_queue_task
    async def validate_transaction(self, request: dict):
        Logger.debug(f'pre_validate_check request with {request}',
                     ICON_INNER_LOG_TAG)

        self._check_icon_service_ready()

        if self._is_thread_flag_on(EnableThreadFlag.VALIDATE):
            loop = asyncio.get_event_loop()
            return await loop.run_in_executor(
                self._thread_pool[THREAD_VALIDATE], self._validate_transaction,
                request)
        else:
            return self._validate_transaction(request)

    def _validate_transaction(self, request: dict):
        response = None
        try:
            converted_request = TypeConverter.convert(
                request, ParamType.VALIDATE_TRANSACTION)
            self._icon_service_engine.validate_transaction(converted_request)
            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))
        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.debug(f'pre_validate_check response with {response}',
                         ICON_INNER_LOG_TAG)
            self._icon_service_engine.clear_context_stack()
            return response

    @message_queue_task
    async def change_block_hash(self, _params):

        self._check_icon_service_ready()

        return ExceptionCode.OK
コード例 #9
0
class TestIntegrateBase(TestCase):
    @classmethod
    def setUpClass(cls):
        cls._score_root_path = '.score'
        cls._state_db_root_path = '.statedb'
        cls._test_sample_root = ""
        cls._signature = "VAia7YZ2Ji6igKWzjR2YsGa2m53nKPrfK7uXYW78QLE+ATehAVZPC40szvAiA6NEU5gCYB4c4qaQzqDh2ugcHgA="

        cls._version = 3
        cls._step_limit = 1 * 10**9
        cls._icx_factor = 10**18

        cls._admin: 'Address' = create_address()
        cls._genesis: 'Address' = create_address()
        cls._fee_treasury: 'Address' = create_address()
        cls._owner1: 'Address' = create_address()
        cls._owner2: 'Address' = create_address()
        cls._owner3: 'Address' = create_address()
        cls._owner4: 'Address' = create_address()
        cls._owner5: 'Address' = create_address()
        cls._owner6: 'Address' = create_address()

        cls._addr_array = [create_address() for _ in range(10)]

    def setUp(self):
        root_clear(self._score_root_path, self._state_db_root_path)

        self._block_height = 0
        self._prev_block_hash = None

        config = IconConfig("", default_icon_config)
        config.load()
        config.update_conf({ConfigKey.BUILTIN_SCORE_OWNER: str(self._admin)})
        config.update_conf({
            ConfigKey.SERVICE: {
                ConfigKey.SERVICE_AUDIT: False,
                ConfigKey.SERVICE_FEE: False,
                ConfigKey.SERVICE_DEPLOYER_WHITELIST: False,
                ConfigKey.SERVICE_SCORE_PACKAGE_VALIDATOR: False
            }
        })
        config.update_conf({
            ConfigKey.SCORE_ROOT_PATH:
            self._score_root_path,
            ConfigKey.STATE_DB_ROOT_PATH:
            self._state_db_root_path
        })
        config.update_conf(self._make_init_config())

        self.icon_service_engine = IconServiceEngine()
        self.icon_service_engine.open(config)

        self._genesis_invoke()

    def tearDown(self):
        self.icon_service_engine.close()
        root_clear(self._score_root_path, self._state_db_root_path)

    def _make_init_config(self) -> dict:
        return {}

    def _genesis_invoke(self) -> dict:
        tx_hash = create_tx_hash()
        timestamp_us = create_timestamp()
        request_params = {
            'txHash': tx_hash,
            'version': self._version,
            'timestamp': timestamp_us
        }

        tx = {
            'method': 'icx_sendTransaction',
            'params': request_params,
            'genesisData': {
                "accounts": [
                    {
                        "name": "genesis",
                        "address": self._genesis,
                        "balance": 100 * self._icx_factor
                    },
                    {
                        "name": "fee_treasury",
                        "address": self._fee_treasury,
                        "balance": 0
                    },
                    {
                        "name": "owner1",
                        "address": self._owner1,
                        "balance": 100 * self._icx_factor
                    },
                    {
                        "name": "owner2",
                        "address": self._owner2,
                        "balance": 100 * self._icx_factor
                    },
                    {
                        "name": "owner3",
                        "address": self._owner3,
                        "balance": 100 * self._icx_factor
                    },
                ]
            },
        }

        block_hash = create_block_hash()
        block = Block(self._block_height, block_hash, timestamp_us, None)
        invoke_response: dict = self.icon_service_engine.invoke(block, [tx])
        self.icon_service_engine.commit(block)
        self._block_height += 1
        self._prev_block_hash = block_hash

        return invoke_response

    #Todo: need to be refactoring
    def _deploy_multisig_wallet(self):
        tx1 = self._make_deploy_tx(
            "",
            "multisig_wallet",
            self._addr_array[0],
            ZERO_SCORE_ADDRESS,
            deploy_params={
                "_walletOwners":
                str("%s,%s,%s" %
                    (str(self._owner1), str(self._owner2), str(self._owner3))),
                "_required":
                "0x02"
            })

        prev_block, tx_results = self._make_and_req_block([tx1])
        self._write_precommit_state(prev_block)

        self.assertEqual(tx_results[0].status, int(True))
        multisig_score_addr = tx_results[0].score_address

        # check wallet owners
        query_request = {
            "version": self._version,
            "from": self._admin,
            "to": multisig_score_addr,
            "dataType": "call",
            "data": {
                "method": "getWalletOwners",
                "params": {
                    "_offset": "0",
                    "_count": "10"
                }
            }
        }
        response = self._query(query_request)
        expected_owners = [
            str(self._owner1),
            str(self._owner2),
            str(self._owner3)
        ]
        self.assertEqual(response, expected_owners)

        # check requirements
        query_request = {
            "version": self._version,
            "from": self._admin,
            "to": multisig_score_addr,
            "dataType": "call",
            "data": {
                "method": "getRequirements",
                "params": {}
            }
        }
        response = self._query(query_request)
        expected_requirements = 2
        self.assertEqual(response, expected_requirements)

        return multisig_score_addr

    # Todo: need to be refactoring
    def _deploy_multisig_wallet_and_token_score(self, token_total_supply: int,
                                                token_owner: Address):
        tx1 = self._make_deploy_tx(
            "",
            "multisig_wallet",
            self._addr_array[0],
            ZERO_SCORE_ADDRESS,
            deploy_params={
                "_walletOwners":
                str("%s,%s,%s" %
                    (str(self._owner1), str(self._owner2), str(self._owner3))),
                "_required":
                "0x02"
            })

        tx2 = self._make_deploy_tx("",
                                   "standard_token",
                                   token_owner,
                                   ZERO_SCORE_ADDRESS,
                                   deploy_params={
                                       "initialSupply":
                                       str(hex(token_total_supply)),
                                       "decimals":
                                       str(hex(0))
                                   })

        prev_block, tx_results = self._make_and_req_block([tx1, tx2])
        self._write_precommit_state(prev_block)

        self.assertEqual(tx_results[0].status, int(True))
        self.assertEqual(tx_results[1].status, int(True))
        multisig_score_addr = tx_results[0].score_address
        token_score_addr = tx_results[1].score_address

        # check wallet owners
        query_request = {
            "version": self._version,
            "from": self._admin,
            "to": multisig_score_addr,
            "dataType": "call",
            "data": {
                "method": "getWalletOwners",
                "params": {
                    "_offset": "0",
                    "_count": "10"
                }
            }
        }
        response = self._query(query_request)
        expected_owners = [
            str(self._owner1),
            str(self._owner2),
            str(self._owner3)
        ]
        self.assertEqual(response, expected_owners)

        # check requirements
        query_request = {
            "version": self._version,
            "from": self._admin,
            "to": multisig_score_addr,
            "dataType": "call",
            "data": {
                "method": "getRequirements",
                "params": {}
            }
        }
        response = self._query(query_request)
        expected_requirements = 2
        self.assertEqual(response, expected_requirements)

        # check token supply
        query_request = {
            "version": self._version,
            "from": self._admin,
            "to": token_score_addr,
            "dataType": "call",
            "data": {
                "method": "balanceOf",
                "params": {
                    '_owner': str(token_owner)
                }
            }
        }
        response = self._query(query_request)
        self.assertEqual(response, token_total_supply)

        return multisig_score_addr, token_score_addr

    def _make_deploy_tx(self,
                        score_root: str,
                        score_name: str,
                        addr_from: Union['Address', None],
                        addr_to: 'Address',
                        deploy_params: dict = None,
                        timestamp_us: int = None,
                        data: bytes = None,
                        is_sys: bool = False):

        if deploy_params is None:
            deploy_params = {}

        score_path = get_score_path(score_root, score_name)

        if is_sys:
            deploy_data = {
                'contentType': 'application/tbears',
                'content': score_path,
                'params': deploy_params
            }
        else:
            if data is None:
                mz = InMemoryZip()
                mz.zip_in_memory(score_path)
                data = f'0x{mz.data.hex()}'
            else:
                data = f'0x{bytes.hex(data)}'
            deploy_data = {
                'contentType': 'application/zip',
                'content': data,
                'params': deploy_params
            }

        if timestamp_us is None:
            timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "version": self._version,
            "from": addr_from,
            "to": addr_to,
            "stepLimit": self._step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature,
            "dataType": "deploy",
            "data": deploy_data
        }

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {'method': method, 'params': request_params}

        self.icon_service_engine.validate_transaction(tx)
        return tx

    def _make_score_call_tx(self,
                            addr_from: Optional['Address'],
                            addr_to: 'Address',
                            method: str,
                            params: dict,
                            value: int = 0):

        timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "version": self._version,
            "from": addr_from,
            "to": addr_to,
            "value": value,
            "stepLimit": self._step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature,
            "dataType": "call",
            "data": {
                "method": method,
                "params": params
            }
        }

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {'method': method, 'params': request_params}

        self.icon_service_engine.validate_transaction(tx)
        return tx

    def _make_icx_send_tx(self,
                          addr_from: Optional['Address'],
                          addr_to: Union['Address', 'MalformedAddress'],
                          value: int,
                          disable_pre_validate: bool = False,
                          support_v2: bool = False):

        timestamp_us = create_timestamp()
        nonce = 0

        request_params = {
            "from": addr_from,
            "to": addr_to,
            "value": value,
            "stepLimit": self._step_limit,
            "timestamp": timestamp_us,
            "nonce": nonce,
            "signature": self._signature
        }

        if support_v2:
            request_params["fee"] = 10**16
        else:
            request_params["version"] = self._version

        method = 'icx_sendTransaction'
        # Insert txHash into request params
        request_params['txHash'] = create_tx_hash()
        tx = {'method': method, 'params': request_params}

        if not disable_pre_validate:
            self.icon_service_engine.validate_transaction(tx)
        return tx

    def _make_and_req_block(self,
                            tx_list: list,
                            block_height: int = None) -> tuple:
        if block_height is None:
            block_height: int = self._block_height
        block_hash = create_block_hash()
        timestamp_us = create_timestamp()

        block = Block(block_height, block_hash, timestamp_us,
                      self._prev_block_hash)

        invoke_response, _ = self.icon_service_engine.invoke(block, tx_list)
        return block, invoke_response

    def _write_precommit_state(self, block: 'Block') -> None:
        self.icon_service_engine.commit(block)
        self._block_height += 1
        self._prev_block_hash = block.hash

    def _remove_precommit_state(self, block: 'Block') -> None:
        self.icon_service_engine.rollback(block)

    def _query(self, request: dict, method: str = 'icx_call') -> Any:
        response = self.icon_service_engine.query(method, request)
        return response

    def _create_invalid_block(self, block_height: int = None) -> 'Block':
        if block_height is None:
            block_height: int = self._block_height
        block_hash = create_block_hash()
        timestamp_us = create_timestamp()

        return Block(block_height, block_hash, timestamp_us,
                     self._prev_block_hash)
コード例 #10
0
class IconIntegrateTestBase(TestCase):
    @classmethod
    def setUpClass(cls):
        cls._score_root_path = '.testscore'
        cls._state_db_root_path = '.teststatedb'

        cls._icx_factor = 10 ** 18

        cls._genesis: 'KeyWallet' = KeyWallet.create()
        cls._fee_treasury: 'KeyWallet' = KeyWallet.create()
        cls._test1: 'KeyWallet' = KeyWallet.load(bytes.fromhex(TEST1_PRIVATE_KEY))

        cls._wallet_array = [KeyWallet.create() for _ in range(10)]

    def setUp(self):
        root_clear(self._score_root_path, self._state_db_root_path)

        self._block_height = 0
        self._prev_block_hash = None

        config = IconConfig("", default_icon_config)
        config.load()
        config.update_conf({ConfigKey.BUILTIN_SCORE_OWNER: self._test1.get_address()})
        config.update_conf({ConfigKey.SCORE_ROOT_PATH: self._score_root_path,
                            ConfigKey.STATE_DB_ROOT_PATH: self._state_db_root_path})
        config.update_conf(self._make_init_config())

        self.icon_service_engine = IconServiceEngine()
        self.icon_service_engine.open(config)

        self._genesis_invoke()

    def tearDown(self):
        self.icon_service_engine.close()
        root_clear(self._score_root_path, self._state_db_root_path)

    def _make_init_config(self) -> dict:
        return {}

    def _genesis_invoke(self) -> dict:
        tx_hash = create_tx_hash()
        timestamp_us = create_timestamp()
        request_params = {
            'txHash': tx_hash,
            'version': 3,
            'timestamp': timestamp_us
        }

        tx = {
            'method': 'icx_sendTransaction',
            'params': request_params,
            'genesisData': {
                "accounts": [
                    {
                        "name": "genesis",
                        "address": Address.from_string(self._genesis.get_address()),
                        "balance": 100 * self._icx_factor
                    },
                    {
                        "name": "fee_treasury",
                        "address": Address.from_string(self._fee_treasury.get_address()),
                        "balance": 0
                    },
                    {
                        "name": "_admin",
                        "address": Address.from_string(self._test1.get_address()),
                        "balance": 1_000_000 * self._icx_factor
                    }
                ]
            },
        }
コード例 #11
0
class IconIntegrateTestBase(TestCase):
    @classmethod
    def setUpClass(cls):
        cls._score_root_path = '.testscore'
        cls._state_db_root_path = '.teststatedb'

        cls._icx_factor = 10 ** 18

        cls._genesis: 'KeyWallet' = KeyWallet.create()
        cls._fee_treasury: 'KeyWallet' = KeyWallet.create()
        cls._test1: 'KeyWallet' = KeyWallet.load(bytes.fromhex(TEST1_PRIVATE_KEY))

        cls._wallet_array = [KeyWallet.create() for _ in range(10)]

    def setUp(self, genesis_accounts: List[Account] = None):
        root_clear(self._score_root_path, self._state_db_root_path)

        self._block_height = 0
        self._prev_block_hash = None

        config = IconConfig("", default_icon_config)
        config.load()
        config.update_conf({ConfigKey.BUILTIN_SCORE_OWNER: self._test1.get_address()})
        config.update_conf({ConfigKey.SCORE_ROOT_PATH: self._score_root_path,
                            ConfigKey.STATE_DB_ROOT_PATH: self._state_db_root_path})
        config.update_conf(self._make_init_config())
        self.icon_service_engine = IconServiceEngine()
        self.icon_service_engine.open(config)

        self._genesis_invoke(genesis_accounts)

    def tearDown(self):
        self.icon_service_engine.close()
        root_clear(self._score_root_path, self._state_db_root_path)

    def _make_init_config(self) -> dict:
        return {}

    @staticmethod
    def _append_list(tx: dict, genesis_accounts: List[Account]) -> None:
        """Appends additional genesis account list to genesisData

        :param genesis_accounts: additional genesis account list consisted of namedtuple named Account
        of which keys are name, address and balance
        :return: None
        """
        for account_as_namedtuple in genesis_accounts:
            tx["genesisData"]['accounts'].append({
                "name": account_as_namedtuple.name,
                "address": account_as_namedtuple.address,
                "balance": account_as_namedtuple.balance
            })

    def _genesis_invoke(self, genesis_accounts: List[Account]) -> list:
        tx_hash = create_tx_hash()
        timestamp_us = create_timestamp()
        request_params = {
            'txHash': tx_hash,
            'version': 3,
            'timestamp': timestamp_us
        }

        tx = {
            'method': 'icx_sendTransaction',
            'params': request_params,
            'genesisData': {
                "accounts": [
                    {
                        "name": "genesis",
                        "address": Address.from_string(self._genesis.get_address()),
                        "balance": 100 * self._icx_factor
                    },
                    {
                        "name": "fee_treasury",
                        "address": Address.from_string(self._fee_treasury.get_address()),
                        "balance": 0
                    },
                    {
                        "name": "_admin",
                        "address": Address.from_string(self._test1.get_address()),
                        "balance": 1_000_000 * self._icx_factor
                    }
                ]
            },
        }
コード例 #12
0
class IconServiceSyncer(object):
    _TAG = "SYNC"

    def __init__(self):
        self._block_reader = BlockDatabaseReader()
        self._engine = IconServiceEngine()

    def open(self,
             config_path: str,
             fee: bool = True,
             audit: bool = True,
             deployer_whitelist: bool = False,
             score_package_validator: bool = False,
             builtin_score_owner: str = ''):
        conf = IconConfig("", default_icon_config)

        if config_path != "":
            conf.load(config_path)

        conf.update_conf({
            "builtinScoreOwner": builtin_score_owner,
            "service": {
                "fee": fee,
                "audit": audit,
                "scorePackageValidator": score_package_validator,
                "deployerWhiteList": deployer_whitelist
            }
        })

        Logger.load_config(conf)
        self._engine.open(conf)

    def run(self, *args, **kwargs) -> int:
        Logger.debug(tag=self._TAG, msg=f"run() start: {args} {kwargs}")

        loop = asyncio.get_event_loop()
        future = asyncio.Future()

        try:
            asyncio.ensure_future(
                self._wait_for_complete(future, *args, **kwargs))
            loop.run_until_complete(future)
        finally:
            self._engine.close()
            loop.close()

        ret = future.result()
        Logger.debug(tag=self._TAG, msg=f"run() end: {ret}")

        return ret

    async def _wait_for_complete(self, result_future: asyncio.Future, *args,
                                 **kwargs):
        Logger.debug(tag=self._TAG, msg="_wait_for_complete() start")

        executor = ThreadPoolExecutor(max_workers=1)
        f = executor.submit(self._run, *args, **kwargs)
        future = asyncio.wrap_future(f)

        await future

        Logger.debug(tag=self._TAG, msg="_wait_for_complete() end1")
        result_future.set_result(future.result())

        Logger.debug(tag=self._TAG, msg="_wait_for_complete() end2")

    def _run(self,
             db_path: str,
             channel: str,
             start_height: int = 0,
             count: int = 99999999,
             stop_on_error: bool = True,
             no_commit: bool = False,
             backup_period: int = 0,
             write_precommit_data: bool = False) -> int:
        """Begin to synchronize IconServiceEngine with blocks from loopchain db

        :param db_path: loopchain db path
        :param channel: channel name used as a key to get commit_state in loopchain db
        :param start_height: start height to sync
        :param count: The number of blocks to sync
        :param stop_on_error: If error happens, stop syncing
        :param no_commit: Do not commit
        :param backup_period: state backup period in block
        :param write_precommit_data:
        :return: 0(success), otherwise(error)
        """
        Logger.debug(tag=self._TAG, msg="_run() start")

        ret: int = 0
        self._block_reader.open(db_path)

        print('block_height | commit_state | state_root_hash | tx_count')

        prev_block: Optional['Block'] = None

        for height in range(start_height, start_height + count):
            block_dict: dict = self._block_reader.get_block_by_block_height(
                height)
            if block_dict is None:
                print(f'last block: {height - 1}')
                break

            loopchain_block = LoopchainBlock.from_dict(block_dict)
            block: 'Block' = utils.create_block(loopchain_block)
            tx_requests: list = utils.create_transaction_requests(
                loopchain_block)

            if prev_block is not None:
                # print(f'prev_block({prev_block.hash.hex()}) == block({block.prev_hash.hex()})')
                if prev_block.hash != block.prev_hash:
                    raise Exception()

            invoke_result = self._engine.invoke(block, tx_requests)
            tx_results, state_root_hash = invoke_result[0], invoke_result[1]
            commit_state: bytes = self._block_reader.get_commit_state(
                block_dict, channel, b'')

            # "commit_state" is the field name of state_root_hash in loopchain block
            print(
                f'{height} | {commit_state.hex()[:6]} | {state_root_hash.hex()[:6]} | {len(tx_requests)}'
            )

            if write_precommit_data:
                self._print_precommit_data(block)

            try:
                if stop_on_error:
                    if commit_state:
                        if commit_state != state_root_hash:
                            raise Exception()

                    if height > 0 and not self._check_invoke_result(
                            tx_results):
                        raise Exception()
            except Exception as e:
                logging.exception(e)

                print(block_dict)
                self._print_precommit_data(block)
                ret: int = 1
                break

            if not no_commit:
                if 'block' in inspect.signature(
                        self._engine.commit).parameters:
                    self._engine.commit(block)
                else:
                    self._engine.commit(block.height, block.hash, None)

            self._backup_state_db(block, backup_period)
            prev_block = block

        self._block_reader.close()

        Logger.debug(tag=self._TAG, msg=f"_run() end: {ret}")
        return ret

    def _check_invoke_result(self, tx_results: list):
        """Compare the transaction results from IconServiceEngine
        with the results stored in loopchain db

        If transaction result is not compatible to protocol v3, pass it

        :param tx_results: the transaction results that IconServiceEngine.invoke() returns
        :return: True(same) False(different)
        """

        for tx_result in tx_results:
            tx_info_in_db: dict =\
                self._block_reader.get_transaction_result_by_hash(
                    tx_result.tx_hash.hex())
            tx_result_in_db = tx_info_in_db['result']

            # tx_v2 dose not have transaction result_v3
            if 'status' not in tx_result_in_db:
                continue

            # information extracted from db
            status: int = int(tx_result_in_db['status'], 16)
            tx_hash: bytes = bytes.fromhex(tx_result_in_db['txHash'])
            step_used: int = int(tx_result_in_db['stepUsed'], 16)
            step_price: int = int(tx_result_in_db['stepPrice'], 16)
            event_logs: list = tx_result_in_db['eventLogs']
            step: int = step_used * step_price

            if tx_hash != tx_result.tx_hash:
                print(f'tx_hash: {tx_hash.hex()} != {tx_result.tx_hash.hex()}')
                return False
            if status != tx_result.status:
                print(f'status: {status} != {tx_result.status}')
                return False
            if step_used != tx_result.step_used:
                print(f'step_used: {step_used} != {tx_result.step_used}')
                return False

            tx_result_step: int = tx_result.step_used * tx_result.step_price
            if step != tx_result_step:
                print(f'step: {step} != {tx_result_step}')
                return False
            if step_price != tx_result.step_price:
                print(f'step_price: {step_price} != {tx_result.step_price}')
                return False

            if not self._check_event_logs(event_logs, tx_result.event_logs):
                return False

        return True

    @staticmethod
    def _check_event_logs(event_logs_in_db: list,
                          event_logs_in_tx_result: list):
        for event_log, _tx_result_event_log in zip(event_logs_in_db,
                                                   event_logs_in_tx_result):
            tx_result_event_log: dict = _tx_result_event_log.to_dict()

            # convert Address to str
            if 'score_address' in tx_result_event_log:
                score_address: 'Address' = tx_result_event_log['score_address']
                del tx_result_event_log['score_address']
                tx_result_event_log['scoreAddress'] = str(score_address)

            # convert Address objects to str objects in 'indexes'
            indexed: list = tx_result_event_log['indexed']
            for i in range(len(indexed)):
                value = indexed[i]
                indexed[i] = utils.object_to_str(value)

            data: list = tx_result_event_log['data']
            for i in range(len(data)):
                value = data[i]
                data[i] = utils.object_to_str(value)

            if event_log != tx_result_event_log:
                print(f'{event_log} != {tx_result_event_log}')
                return False

        return True

    def _print_precommit_data(self, block: 'Block'):
        """Print the latest updated states stored in IconServiceEngine

        :return:
        """
        precommit_data_manager: PrecommitDataManager =\
            getattr(self._engine, '_precommit_data_manager')

        precommit_data: PrecommitData = precommit_data_manager.get(block.hash)
        block_batch: BlockBatch = precommit_data.block_batch
        state_root_hash: bytes = block_batch.digest()

        filename = f'{block.height}-precommit-data.txt'
        with open(filename, 'wt') as f:
            for i, key in enumerate(block_batch):
                value: bytes = block_batch[key]

                if value:
                    line = f'{i}: {key.hex()} - {value.hex()}'
                else:
                    line = f'{i}: {key.hex()} - None'

                print(line)
                f.write(f'{line}\n')

            f.write(f'state_root_hash: {state_root_hash.hex()}\n')

    @staticmethod
    def _backup_state_db(block: 'Block', backup_period: int):
        if backup_period <= 0:
            return
        if block.height == 0:
            return

        if block.height % backup_period == 0:
            print(f"----------- Backup statedb: {block.height} ------------")
            dirname: str = f"block-{block.height}"

            for basename in (".score", ".statedb"):
                try:
                    shutil.copytree(basename, f"{dirname}/{basename}/")
                except FileExistsError:
                    pass

    def close(self):
        pass
コード例 #13
0
class IconServiceSyncer(object):
    _TAG = "SYNC"

    def __init__(self):
        self._block_reader = BlockDatabaseReader()
        self._engine = IconServiceEngine()

    def open(
        self,
        config_path: str,
        fee: bool = True,
        audit: bool = True,
        deployer_whitelist: bool = False,
        score_package_validator: bool = False,
        builtin_score_owner: str = "",
    ):
        conf = IconConfig("", default_icon_config)

        if config_path != "":
            conf.load(config_path)

        conf.update_conf({
            "builtinScoreOwner": builtin_score_owner,
            "service": {
                "fee": fee,
                "audit": audit,
                "scorePackageValidator": score_package_validator,
                "deployerWhiteList": deployer_whitelist,
            },
        })

        Logger.load_config(conf)
        self._engine.open(conf)

    def run(self, *args, **kwargs) -> int:
        Logger.debug(tag=self._TAG, msg=f"run() start: {args} {kwargs}")

        loop = asyncio.get_event_loop()
        future = asyncio.Future()

        try:
            asyncio.ensure_future(
                self._wait_for_complete(future, *args, **kwargs))
            loop.run_until_complete(future)
        finally:
            for task in asyncio.Task.all_tasks():
                task.cancel()
            loop.close()

        ret = future.result()
        Logger.debug(tag=self._TAG, msg=f"run() end: {ret}")

        return ret

    async def _wait_for_complete(self, result_future: asyncio.Future, *args,
                                 **kwargs):
        Logger.debug(tag=self._TAG, msg="_wait_for_complete() start")

        # Wait for rc to be ready
        future = self._engine.get_ready_future()
        await future

        with ThreadPoolExecutor(max_workers=1) as executor:
            # Call IconServiceEngine.hello()
            f = executor.submit(self._hello)
            future = asyncio.wrap_future(f)
            await future

            # Start to sync blocks
            f = executor.submit(self._run, *args, **kwargs)
            f = asyncio.wrap_future(f)
            for fut in asyncio.as_completed([f]):
                try:
                    ret = await fut
                except Exception as exc:
                    self._engine.close()
                    # Wait to stop ipc_server for 1s
                    await asyncio.sleep(1)
                    result_future.set_exception(exc)
                else:
                    self._engine.close()
                    # Wait to stop ipc_server for 1s
                    await asyncio.sleep(1)
                    Logger.debug(tag=self._TAG,
                                 msg="_wait_for_complete() end1")
                    result_future.set_result(ret)
                    Logger.debug(tag=self._TAG,
                                 msg="_wait_for_complete() end2")

    def _hello(self):
        self._engine.hello()

    def _run(
        self,
        db_path: str,
        channel: str,
        start_height: int = 0,
        count: int = 99999999,
        stop_on_error: bool = True,
        no_commit: bool = False,
        backup_period: int = 0,
        write_precommit_data: bool = False,
        print_block_height: int = 1,
        iiss_db_backup_path: Optional[str] = None,
    ) -> int:
        """Begin to synchronize IconServiceEngine with blocks from loopchain db

        :param db_path: loopchain db path
        :param channel: channel name used as a key to get commit_state in loopchain db
        :param start_height: start height to sync
        :param count: The number of blocks to sync
        :param stop_on_error: If error happens, stop syncing
        :param no_commit: Do not commit
        :param backup_period: state backup period in block
        :param write_precommit_data:
        :param print_block_height: print every this block height
        :return: 0(success), otherwise(error)
        """
        Logger.debug(tag=self._TAG, msg="_run() start")

        word_detector = WordDetector(
            filename="iconservice.log",
            block_word=r"CALCULATE\(",
            release_word=r"CALCULATE_DONE\(",
        )
        ret: int = 0
        self._block_reader.open(db_path)

        print("block_height | commit_state | state_root_hash | tx_count")

        prev_block: Optional["Block"] = None
        prev_loopchain_block: Optional["LoopchainBlock"] = None
        main_preps: Optional['NodeContainer'] = None
        next_main_preps: Optional["NodeContainer"] = None

        if start_height > 0:
            prev_block_dict = self._block_reader.get_block_by_block_height(
                start_height - 1)
            prev_loopchain_block = LoopchainBlock.from_dict(prev_block_dict)

            # init main_preps
            preps: list = self._block_reader.load_main_preps(prev_block_dict)
            main_preps: Optional['NodeContainer'] = NodeContainer.from_list(
                preps=preps)

            # when sync from the first block of term, have to initialize next_main_preps here
            # in that case, invoke_result[3] will be None on first block and can not update next_main_preps
            block_dict = self._block_reader.get_block_by_block_height(
                start_height)
            loopchain_block = LoopchainBlock.from_dict(block_dict)
            block = _create_iconservice_block(loopchain_block)
            if self._check_calculation_block(block):
                preps: list = self._block_reader.load_main_preps(block_dict)
                next_main_preps: Optional[
                    'NodeContainer'] = NodeContainer.from_list(preps=preps)

        end_height = start_height + count - 1

        for height in range(start_height, start_height + count):
            block_dict: dict = self._block_reader.get_block_by_block_height(
                height)

            if block_dict is None:
                print(f"last block: {height - 1}")
                break

            loopchain_block: 'LoopchainBlock' = LoopchainBlock.from_dict(
                block_dict)
            block: 'Block' = _create_iconservice_block(loopchain_block)

            tx_requests: list = create_transaction_requests(loopchain_block)
            prev_block_generator: Optional[
                "Address"] = prev_loopchain_block.leader if prev_loopchain_block else None
            prev_block_validators: Optional[
                List["Address"]] = _create_block_validators(
                    block_dict, prev_block_generator)
            prev_block_votes: Optional[List[Tuple[
                "Address",
                int]]] = _create_prev_block_votes(block_dict,
                                                  prev_block_generator,
                                                  main_preps)

            if prev_block is not None and prev_block.hash != block.prev_hash:
                raise Exception()

            invoke_result = self._engine.invoke(
                block,
                tx_requests,
                prev_block_generator,
                prev_block_validators,
                prev_block_votes,
            )
            tx_results, state_root_hash = invoke_result[0], invoke_result[1]
            main_preps_as_dict: Optional[Dict] = invoke_result[3]

            commit_state: bytes = self._block_reader.get_commit_state(
                block_dict, channel)

            # "commit_state" is the field name of state_root_hash in loopchain block
            if (height - start_height) % print_block_height == 0:
                print(
                    f"{height} | {commit_state.hex()[:6]} | {state_root_hash.hex()[:6]} | {len(tx_requests)}"
                )

            if write_precommit_data:
                self._print_precommit_data(block)

            try:
                if stop_on_error:
                    if commit_state:
                        if commit_state != state_root_hash:
                            raise Exception()

                    if height > 0 and not self._check_invoke_result(
                            tx_results):
                        raise Exception()
            except Exception as e:
                logging.exception(e)

                self._print_precommit_data(block)
                ret: int = 1
                break

            is_calculation_block = self._check_calculation_block(block)

            if is_calculation_block:
                word_detector.start()
                time.sleep(0.5)
                if iiss_db_backup_path is not None:
                    self._backup_iiss_db(iiss_db_backup_path, block.height)

            # If no_commit is set to True, the config only affects to the last block to commit
            if height < end_height:
                while word_detector.get_hold():
                    time.sleep(0.5)

                # Call IconServiceEngine.commit() with a block
                if not no_commit:
                    self._commit(block)

            while word_detector.get_hold():
                time.sleep(0.5)
            # Prepare the next iteration
            self._backup_state_db(block, backup_period)
            prev_block = block
            prev_loopchain_block = loopchain_block

            if next_main_preps:
                main_preps = next_main_preps
                next_main_preps = None

            if main_preps_as_dict is not None:
                next_main_preps = NodeContainer.from_dict(main_preps_as_dict)

        self._block_reader.close()
        word_detector.stop()

        Logger.debug(tag=self._TAG, msg=f"_run() end: {ret}")
        return ret

    def _commit(self, block: "Block"):
        if "block" in inspect.signature(self._engine.commit).parameters:
            self._engine.commit(block)
        else:
            self._engine.commit(block.height, block.hash, block.hash)

    def _backup_iiss_db(self, iiss_db_backup_path: Optional[str],
                        block_height: int):
        iiss_db_path: str = os.path.join(self._engine._state_db_root_path,
                                         "iiss")

        with os.scandir(iiss_db_path) as it:
            for entry in it:
                if entry.is_dir(
                ) and entry.name == Storage.CURRENT_IISS_DB_NAME:
                    dst_path: str = os.path.join(
                        iiss_db_backup_path,
                        f"{Storage.IISS_RC_DB_NAME_PREFIX}{block_height - 1}",
                    )
                    if os.path.exists(dst_path):
                        shutil.rmtree(dst_path)
                    shutil.copytree(entry.path, dst_path)
                    break

    def _check_invoke_result(self, tx_results: list):
        """Compare the transaction results from IconServiceEngine
        with the results stored in loopchain db

        If transaction result is not compatible to protocol v3, pass it

        :param tx_results: the transaction results that IconServiceEngine.invoke() returns
        :return: True(same) False(different)
        """

        for tx_result in tx_results:
            tx_info_in_db: dict = self._block_reader.get_transaction_result_by_hash(
                tx_result.tx_hash.hex())
            tx_result_in_db = tx_info_in_db["result"]

            # tx_v2 dose not have transaction result_v3
            if "status" not in tx_result_in_db:
                continue

            # information extracted from db
            status: int = int(tx_result_in_db["status"], 16)
            tx_hash: bytes = bytes.fromhex(tx_result_in_db["txHash"])
            step_used: int = int(tx_result_in_db["stepUsed"], 16)
            step_price: int = int(tx_result_in_db["stepPrice"], 16)
            event_logs: list = tx_result_in_db["eventLogs"]
            step: int = step_used * step_price

            if tx_hash != tx_result.tx_hash:
                print(f"tx_hash: {tx_hash.hex()} != {tx_result.tx_hash.hex()}")
                return False
            if status != tx_result.status:
                print(f"status: {status} != {tx_result.status}")
                return False
            if step_used != tx_result.step_used:
                print(f"step_used: {step_used} != {tx_result.step_used}")
                return False

            tx_result_step: int = tx_result.step_used * tx_result.step_price
            if step != tx_result_step:
                print(f"step: {step} != {tx_result_step}")
                return False
            if step_price != tx_result.step_price:
                print(f"step_price: {step_price} != {tx_result.step_price}")
                return False

            if not self._check_event_logs(event_logs, tx_result.event_logs):
                return False

        return True

    @staticmethod
    def _check_event_logs(event_logs_in_db: list,
                          event_logs_in_tx_result: list):

        if event_logs_in_db is None:
            event_logs_in_db = []

        if event_logs_in_tx_result is None:
            event_logs_in_tx_result = []

        if len(event_logs_in_db) != len(event_logs_in_tx_result):
            return False

        for event_log, _tx_result_event_log in zip(event_logs_in_db,
                                                   event_logs_in_tx_result):
            tx_result_event_log: dict = _tx_result_event_log.to_dict()

            # convert Address to str
            if "score_address" in tx_result_event_log:
                score_address: "Address" = tx_result_event_log["score_address"]
                del tx_result_event_log["score_address"]
                tx_result_event_log["scoreAddress"] = str(score_address)

            # convert Address objects to str objects in 'indexes'
            indexed: list = tx_result_event_log["indexed"]
            for i in range(len(indexed)):
                value = indexed[i]
                indexed[i] = object_to_str(value)

            data: list = tx_result_event_log["data"]
            for i in range(len(data)):
                value = data[i]
                data[i] = object_to_str(value)

            if event_log != tx_result_event_log:
                print(f"{event_log} != {tx_result_event_log}")
                return False

        return True

    def _print_precommit_data(self, block: "Block"):
        """Print the latest updated states stored in IconServiceEngine

        :return:
        """
        precommit_data_manager: PrecommitDataManager = getattr(
            self._engine, "_precommit_data_manager")

        precommit_data: PrecommitData = precommit_data_manager.get(block.hash)
        block_batch: BlockBatch = precommit_data.block_batch
        state_root_hash: bytes = block_batch.digest()

        filename = f"{block.height}-precommit-data.txt"
        with open(filename, "wt") as f:
            for i, key in enumerate(block_batch):
                value: "TransactionBatchValue" = block_batch[key]

                if value:
                    hex_value = value.value.hex(
                    ) if value.value is not None else None
                    include_state_root_hash = value.include_state_root_hash

                    line = f"{i}: {key.hex()} - {hex_value}, {include_state_root_hash}"
                else:
                    line = f"{i}: {key.hex()} - None"

                print(line)
                f.write(f"{line}\n")

            f.write(f"state_root_hash: {state_root_hash.hex()}\n")

    def _check_calculation_block(self, block: "Block") -> bool:
        """check calculation block"""

        context = IconScoreContext(IconScoreContextType.DIRECT)
        revision = self._engine._get_revision_from_rc(context)
        context.block = block

        if revision < Revision.IISS.value:
            return False

        start_block = context.engine.iiss.get_start_block_of_calc(context)
        return start_block == block.height

    @staticmethod
    def _backup_state_db(block: "Block", backup_period: int):
        if backup_period <= 0:
            return
        if block.height == 0:
            return

        if block.height % backup_period == 0:
            print(f"----------- Backup statedb: {block.height} ------------")
            dirname: str = f"block-{'%09d' % block.height}"
            for basename in (".score", ".statedb"):
                try:
                    shutil.copytree(basename, f"{dirname}/{basename}/")
                except FileExistsError:
                    pass

    def close(self):
        pass