def setUp(self):
        self._score_path = self._ROOT_SCORE_PATH
        self._loader = IconScoreLoader(self._score_path)

        self._factory = IconScoreContextFactory(max_size=1)
        IconScoreContext.icon_score_deploy_engine = Mock()
        self._context = self._factory.create(IconScoreContextType.DIRECT)
        ContextContainer._push_context(self._context)
Example #2
0
    def setUp(self):
        db_path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH)
        score_path = os.path.join(TEST_ROOT_PATH, self._ROOT_SCORE_PATH)

        self._tx_index = 0
        self.__ensure_dir(db_path)

        self._icx_db = ContextDatabaseFactory.create_by_name('icx_db')
        self._icx_db.address = ICX_ENGINE_ADDRESS
        self._icx_storage = IcxStorage(self._icx_db)
        self._icon_deploy_storage = IconScoreDeployStorage(self._icx_db)

        self._engine = IconScoreDeployEngine()
        self._icon_score_loader = IconScoreLoader(score_path, 0)
        IconScoreMapper.icon_score_loader = self._icon_score_loader
        IconScoreMapper.deploy_storage = self._icon_deploy_storage
        self._icon_score_mapper = IconScoreMapper()

        IconScoreContext.icon_score_manager = Mock(spec=IconScoreManager)

        self._engine.open(
            score_root_path=score_path,
            flag=0,
            icon_deploy_storage=self._icon_deploy_storage)

        self.from_address = create_address(AddressPrefix.EOA)

        self.sample_token_address = create_address(AddressPrefix.CONTRACT)

        self._factory = IconScoreContextFactory(max_size=1)
        self.make_context()

        self._one_icx = 1 * 10 ** 18
        self._one_icx_to_token = 1
    def setUp(self):
        rmtree(self._ROOT_SCORE_PATH)
        rmtree(self._TEST_DB_PATH)

        archive_path = 'tests/sample/valid.zip'
        archive_path = os.path.join(TEST_ROOT_PATH, archive_path)
        zip_bytes = self.read_zipfile_as_byte(archive_path)
        install_path = os.path.join(TEST_ROOT_PATH, self._ROOT_SCORE_PATH)
        self.__unpack_zip_file(install_path, zip_bytes)

        db_path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH)
        ContextDatabaseFactory.open(
            db_path, ContextDatabaseFactory.Mode.SINGLE_DB)

        self.__ensure_dir(db_path)

        icx_db = ContextDatabaseFactory.create_by_name('icx_db')
        self.icx_storage = IcxStorage(icx_db)
        deploy_storage = IconScoreDeployStorage(self.icx_storage.db)
        deploy_engine = IconScoreDeployEngine()
        deploy_engine.open(self._ROOT_SCORE_PATH, 0, deploy_storage)
        IconScoreContext.icon_score_manager = IconScoreManager(deploy_engine)

        self.icon_score_loader = IconScoreLoader(self._ROOT_SCORE_PATH, 0)

        IconScoreMapper.icon_score_loader = self.icon_score_loader
        IconScoreMapper.deploy_storage = deploy_storage
        self.icon_score_mapper = IconScoreMapper()

        self.engine = IconScoreEngine()
        self.engine.open(
            self.icx_storage,
            self.icon_score_mapper)

        self._from = create_address(AddressPrefix.EOA)
        self._icon_score_address = create_address(AddressPrefix.CONTRACT)

        self.factory = IconScoreContextFactory(max_size=1)
        IconScoreContext.icon_score_mapper = self.icon_score_mapper
        self._context = self.factory.create(IconScoreContextType.DIRECT)
        self._context.msg = Message(self._from, 0)
        tx_hash = create_tx_hash()
        self._context.tx = Transaction(
            tx_hash, origin=create_address(AddressPrefix.EOA))
        block_hash = create_block_hash()
        self._context.block = Block(1, block_hash, 0, None)
    def test_make_pkg_root_import(self):
        address = '010cb2b5d7cca1dec18c51de595155a4468711d4f4'
        tx_hash = '0x49485e08589256a68e02a63fa3484b16edd322a729394fbd6b543d77a7f68621'
        score_root_path = './.score'
        score_path = f'{score_root_path}/{address}/{tx_hash}'
        expected_import_name: str = f'{address}.{tx_hash}'

        loader = IconScoreLoader(score_root_path)
        import_name: str = loader._make_pkg_root_import(score_path)
        self.assertEqual(import_name, expected_import_name)

        score_root_path = '/haha/hoho/hehe/score/'
        score_path = f'{score_root_path}/{address}/{tx_hash}'
        loader = IconScoreLoader(score_root_path)
        import_name: str = loader._make_pkg_root_import(score_path)
        self.assertEqual(import_name, expected_import_name)
Example #5
0
    def setUp(self):
        db_path = os.path.join(TEST_ROOT_PATH, self._TEST_DB_PATH)
        score_path = os.path.join(TEST_ROOT_PATH, self._ROOT_SCORE_PATH)

        self._tx_index = 0

        self.__ensure_dir(db_path)
        self._icx_db = ContextDatabaseFactory.create_by_name('icon_dex')
        self._icx_db.address = ICX_ENGINE_ADDRESS
        self._icx_storage = IcxStorage(self._icx_db)
        self._score_deploy_engine = IconScoreDeployEngine()
        self._deploy_storage = IconScoreDeployStorage(self._icx_db)

        self._icon_score_loader = IconScoreLoader(score_path)
        self._icon_score_mapper = IconScoreMapper()

        self._addr1 = create_address(AddressPrefix.EOA)
        self._score_deploy_engine.open(
            score_root_path=score_path,
            icon_deploy_storage=self._deploy_storage)

        self._factory = IconScoreContextFactory(max_size=1)
        self.make_context()
class TestIconScoreLoader(unittest.TestCase):
    _ROOT_SCORE_PATH = '.score'

    def setUp(self):
        self._score_path = self._ROOT_SCORE_PATH
        self._loader = IconScoreLoader(self._score_path)

        self._factory = IconScoreContextFactory(max_size=1)
        IconScoreContext.icon_score_deploy_engine = Mock()
        self._context = self._factory.create(IconScoreContextType.DIRECT)
        ContextContainer._push_context(self._context)

    def tearDown(self):
        ContextContainer._pop_context()
        rmtree(self._score_path)

    @staticmethod
    def __ensure_dir(dir_path):
        if not path.exists(dir_path):
            makedirs(dir_path)

    def load_proj(self, proj: str) -> callable:
        addr_score = create_address(1, data=proj.encode())
        target_path = path.join(self._score_path, addr_score.to_bytes().hex())
        makedirs(target_path, exist_ok=True)
        tx_hash = create_tx_hash()
        converted_tx_hash = f'0x{bytes.hex(tx_hash)}'
        target_path = path.join(target_path, converted_tx_hash)

        ref_path = path.join(TEST_ROOT_PATH, 'tests/sample/{}'.format(proj))
        symlink(ref_path, target_path, target_is_directory=True)
        score_path = self._loader.make_score_path(addr_score, tx_hash)
        return self._loader.load_score(score_path)

    def test_install(self):
        self.__ensure_dir(self._score_path)

        score = self.load_proj('test_score01')
        print('test_score01', score.get_api())
        score = self.load_proj('test_score02')
        print('test_score02', score.get_api())

        ins_score = score(Mock())

        ins_score.print_test()
        self.assertTrue(IconScoreBase in inspect.getmro(score))

    def test_make_pkg_root_import(self):
        address = '010cb2b5d7cca1dec18c51de595155a4468711d4f4'
        tx_hash = '0x49485e08589256a68e02a63fa3484b16edd322a729394fbd6b543d77a7f68621'
        score_root_path = './.score'
        score_path = f'{score_root_path}/{address}/{tx_hash}'
        expected_import_name: str = f'{address}.{tx_hash}'

        loader = IconScoreLoader(score_root_path)
        import_name: str = loader._make_pkg_root_import(score_path)
        self.assertEqual(import_name, expected_import_name)

        score_root_path = '/haha/hoho/hehe/score/'
        score_path = f'{score_root_path}/{address}/{tx_hash}'
        loader = IconScoreLoader(score_root_path)
        import_name: str = loader._make_pkg_root_import(score_path)
        self.assertEqual(import_name, expected_import_name)