コード例 #1
0
    def test_deploy_when_score_depth_is_different_above_revision3(self):
        """
        Reads all files from the depth lower than where the file 'package.json' is
        and test deploying successfully.
        """
        zip_list = [('score_registry.zip', [
            'interfaces/__init__.py', 'interfaces/abc_owned.py',
            'interfaces/abc_score_registry.py', 'package.json',
            'score_registry/__init__.py', 'score_registry/score_registry.py',
            'utility/__init__.py', 'utility/owned.py', 'utility/utils.py'
        ]), ('fakedir.zip', ['__init__.py', 'call_class1.py', 'package.json']),
                    ('nodir.zip',
                     ['__init__.py', 'package.json', 'sample_token.py'])]

        for zip_file, expected_list in zip_list:
            address: 'Address' = create_address(AddressPrefix.CONTRACT)
            self.archive_path = os.path.join(DIRECTORY_PATH, 'sample',
                                             zip_file)
            tx_hash1 = create_tx_hash()
            score_deploy_path: str = get_score_deploy_path(
                self.score_root_path, address, tx_hash1)

            IconScoreDeployer.deploy(
                score_deploy_path,
                self.read_zipfile_as_byte(self.archive_path),
                Revision.THREE.value)
            self.assertEqual(True, os.path.exists(score_deploy_path))

            installed_files = self.get_installed_files(score_deploy_path)
            installed_files.sort()
            self.assertEqual(installed_files, expected_list)

            score_path: str = get_score_path(self.score_root_path, address)
            remove_path(score_path)
コード例 #2
0
    def test_deploy_raise_no_package_above_revision3(self):
        """
        if package doesn't contain package.json, raise exception(no package.json) above revision 3
        """
        zip_list = ['nodir_nopackage.zip', 'normal_nopackage.zip']

        for zip_item in zip_list:
            address: 'Address' = create_address(AddressPrefix.CONTRACT)
            self.archive_path = os.path.join(DIRECTORY_PATH, 'sample',
                                             zip_item)
            tx_hash1 = create_tx_hash()
            score_deploy_path: str = get_score_deploy_path(
                self.score_root_path, address, tx_hash1)

            with self.assertRaises(BaseException) as e:
                IconScoreDeployer.deploy(
                    score_deploy_path,
                    self.read_zipfile_as_byte(self.archive_path),
                    Revision.THREE.value)
            self.assertEqual(e.exception.code, ExceptionCode.INVALID_PACKAGE)
            self.assertEqual(e.exception.message, "package.json not found")
            self.assertTrue(os.path.exists(score_deploy_path))

            score_path: str = get_score_path(self.score_root_path, address)
            remove_path(score_path)
コード例 #3
0
    def test_deploy_when_score_depth_is_different(self):
        """
        Reads all files from the depth lower than where the file 'package.json' is
        and test deploying successfully.
        """
        zip_list = ['score_registry.zip', 'fakedir.zip', 'nodir.zip']

        for zip_item in zip_list:
            address: 'Address' = create_address(AddressPrefix.CONTRACT)
            self.archive_path = os.path.join(DIRECTORY_PATH, 'sample',
                                             zip_item)
            tx_hash1 = create_tx_hash()
            score_deploy_path: str = get_score_deploy_path(
                self.score_root_path, address, tx_hash1)

            IconScoreDeployer.deploy(
                score_deploy_path,
                self.read_zipfile_as_byte(self.archive_path))
            self.assertEqual(True, os.path.exists(score_deploy_path))

            zip_file_info_gen = IconScoreDeployer._extract_files_gen(
                self.read_zipfile_as_byte(self.archive_path))
            file_path_list = [
                name for name, info, parent_dir in zip_file_info_gen
            ]

            installed_contents = self.get_installed_files(score_deploy_path)
            self.assertTrue(
                self.check_package_json_validity(installed_contents))
            installed_contents.sort()
            file_path_list.sort()
            self.assertEqual(installed_contents, file_path_list)

            score_path: str = get_score_path(self.score_root_path, address)
            remove_path(score_path)
コード例 #4
0
    def tearDown(self):
        self.engine = None
        self.icx_storage.close(self._context)
        ContextDatabaseFactory.close()

        path = os.path.join(TEST_ROOT_PATH, self._SCORE_ROOT_PATH)
        remove_path(path)
        path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH)
        remove_path(path)

        IconScoreClassLoader.exit(self._SCORE_ROOT_PATH)
コード例 #5
0
    def test_remove_existing_score(self):
        tx_hash: bytes = create_tx_hash()
        score_deploy_path: str = get_score_deploy_path(self.score_root_path,
                                                       self.address, tx_hash)

        self.normal_score_path = os.path.join(DIRECTORY_PATH, 'sample',
                                              'normal_score.zip')
        IconScoreDeployer.deploy(
            score_deploy_path,
            self.read_zipfile_as_byte(self.normal_score_path))
        remove_path(score_deploy_path)
        self.assertFalse(os.path.exists(score_deploy_path))
コード例 #6
0
    def tearDown(self):
        self._engine = None
        ContextContainer._pop_context()
        self._icon_score_mapper.close()

        path = os.path.join(TEST_ROOT_PATH, 'tests')
        remove_path(path)
        path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH)
        remove_path(path)
        IconScoreContextUtil.validate_score_blacklist = VALIDATE_SCORE_BLACK_LIST
        IconScoreContextUtil.get_owner = GET_OWNER
        IconScoreContextUtil.get_icon_score = GET_ICON_SCORE
        IconScoreContextUtil.is_service_flag_on = IS_SERVICE_FLAG_ON
コード例 #7
0
    def _deploy_score(context: 'IconScoreContext', score_name: str,
                      score_address: 'Address',
                      builtin_score_owner: 'Address'):

        score_source_path_in_iconservice: str = os.path.join(
            IconBuiltinScoreLoader._pre_builtin_score_root_path(), score_name)

        # Save deploy_info for a builtin score to storage.
        deploy_info = IconScoreDeployInfo(score_address=score_address,
                                          deploy_state=DeployState.ACTIVE,
                                          owner=builtin_score_owner,
                                          current_tx_hash=ZERO_TX_HASH,
                                          next_tx_hash=ZERO_TX_HASH)
        context.storage.deploy.put_deploy_info(context, deploy_info)

        tx_hash: bytes = deploy_info.current_tx_hash

        # score_path is score_root_path/score_address/ directory.
        score_path: str = os.path.join(context.score_root_path,
                                       score_address.to_bytes().hex())

        # Make a directory for a builtin score with a given score_address.
        os.makedirs(score_path, exist_ok=True)

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

            # remove_path() supports directory as well as file.
            remove_path(score_deploy_path)
            # Copy builtin score source files from iconservice package to score_deploy_path.
            copytree(score_source_path_in_iconservice, score_deploy_path)
        except FileExistsError:
            pass

        try:
            # Import score class from deployed builtin score sources
            score_info: 'IconScoreInfo' =\
                IconScoreContextUtil.create_score_info(context, score_address, tx_hash)

            # Create a score instance from the imported score class.
            score = score_info.create_score()

            # Call on_install() to initialize the score database of the builtin score.
            score.on_install()
        except BaseException as e:
            Logger.exception(
                f'Failed to deploy a builtin score: {score_address}\n{str(e)}',
                ICON_DEPLOY_LOG_TAG)
            raise e
コード例 #8
0
 def tearDown(self):
     remove_path(self.score_path)