def deploy(self, address: Address, data: bytes, tx_hash: bytes): """Deploy SCORE; Stores SCORE on the root path :param address: SCORE address :param data: Bytes of the zip file. :param tx_hash: Transaction hash """ score_root_path = os.path.join(self.score_root_path, address.to_bytes().hex()) converted_tx_hash = f'0x{bytes.hex(tx_hash)}' install_path = os.path.join(score_root_path, converted_tx_hash) try: if os.path.isfile(install_path): raise ScoreInstallException( f'{install_path} is a file. Check your path.') if os.path.isdir(install_path): raise ScoreInstallException( f'{install_path} is a directory. Check {install_path}') if not os.path.exists(install_path): os.makedirs(install_path) file_info_generator = IconScoreDeployer._extract_files_gen(data) for name, file_info, parent_dir in file_info_generator: if not os.path.exists(os.path.join(install_path, parent_dir)): os.makedirs(os.path.join(install_path, parent_dir)) with file_info as file_info_context, open( os.path.join(install_path, name), 'wb') as dest: contents = file_info_context.read() dest.write(contents) except BaseException as e: shutil.rmtree(install_path, ignore_errors=True) raise e
def test_array_db(self, context, key_value_db, values, value_type): context_db = ContextDatabase(key_value_db, is_shared=False) address = Address(AddressPrefix.CONTRACT, os.urandom(20)) score_db = IconScoreDatabase(address, context_db) self._init_context(context, score_db.address) self._set_revision(context, Revision.USE_RLP.value - 1) name = "array_db" array_db = ArrayDB(name, score_db, value_type) for i in range(2): array_db.put(values[i]) assert len(array_db) == 2 self._set_revision(context, Revision.USE_RLP.value) array_db.put(values[2]) array_db.put(values[3]) assert len(array_db) == 4 for i, value in enumerate(array_db): assert value == values[i] final_key: bytes = _get_final_key(address, ContainerTag.ARRAY, name.encode(), use_rlp=True) assert key_value_db.get(final_key) == int_to_bytes(len(array_db)) for i, use_rlp in enumerate((False, False, True, True)): key = _get_final_key( address.to_bytes(), ContainerTag.ARRAY.value, name.encode(), int_to_bytes(i), use_rlp=use_rlp, ) assert key_value_db.get(key) == ContainerUtil.encode_value(values[i]) for v in reversed(values): assert v == array_db.pop() assert len(array_db) == 0 # 2 values for array_db size still remain # even though all items in array_db have been popped. assert len(key_value_db) == 2