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 setUp(self): rmtree(self._SCORE_ROOT_PATH) rmtree(self._TEST_DB_PATH) archive_path = 'tests/sample/normal_score.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._SCORE_ROOT_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(deploy_storage) IconScoreClassLoader.init(self._SCORE_ROOT_PATH) IconScoreMapper.deploy_storage = deploy_storage self.icon_score_mapper = IconScoreMapper() self.engine = IconScoreEngine() # Use mock to prevent an exception from IconScoreEngine._validate_score_blacklist(). IconScoreEngine._validate_score_blacklist = mock.Mock() self._from = create_address(AddressPrefix.EOA) self._icon_score_address = create_address(AddressPrefix.CONTRACT) IconScoreContext.icon_score_deploy_engine = deploy_engine self._context = IconScoreContext(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 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_DB_NAME) 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 = IconScoreClassLoader(score_path) self._icon_score_mapper = IconScoreMapper() self._addr1 = create_address(AddressPrefix.EOA) self._score_deploy_engine.open( score_root_path=score_path, score_deploy_storage=self._deploy_storage) self.make_context()
def setUp(self): self.storage = IconScoreDeployStorage(Mock(spec=ContextDatabase))
class TestIconScoreDeployStorage(unittest.TestCase): def setUp(self): self.storage = IconScoreDeployStorage(Mock(spec=ContextDatabase)) def test_put_deploy_info_and_tx_params(self): self.storage._put_deploy_tx_params = Mock() self.storage.get_deploy_tx_params = Mock(return_value=bytes()) context = Mock(spec=IconScoreContext) score_address = create_address(1) deploy_type = DeployType.INSTALL owner = create_address() tx_hash = create_tx_hash() deploy_data = {} with self.assertRaises(ServerErrorException) as e: self.storage.put_deploy_info_and_tx_params(context, score_address, deploy_type, owner, tx_hash, deploy_data) self.assertEqual(e.exception.code, ExceptionCode.SERVER_ERROR) self.assertEqual(e.exception.message, f'deploy_params already exists: {tx_hash}') self.storage._put_deploy_tx_params.assert_not_called() with patch( 'iconservice.deploy.icon_score_deploy_storage.IconScoreDeployTXParams' ) as MockTxParams: with patch( 'iconservice.deploy.icon_score_deploy_storage.IconScoreDeployInfo' ) as MockDeployInfos: self.storage._put_deploy_tx_params.reset_mock() self.storage.get_deploy_tx_params.reset_mock() self.storage.get_deploy_tx_params.return_value = None self.storage.get_deploy_info = Mock(return_value=None) self.storage._put_deploy_info = Mock() context = Mock(spec=IconScoreContext) score_address = create_address(1) deploy_type = DeployType.INSTALL owner = create_address() tx_hash = create_tx_hash() deploy_data = {} tx_params = IconScoreDeployTXParams(tx_hash, deploy_type, score_address, deploy_data) MockTxParams.return_value = tx_params deploy_info = IconScoreDeployInfo(score_address, DeployState.INACTIVE, owner, None, tx_hash) MockDeployInfos.return_value = deploy_info self.storage.put_deploy_info_and_tx_params( context, score_address, deploy_type, owner, tx_hash, deploy_data) self.storage.get_deploy_tx_params.assert_called_once_with( context, tx_hash) self.storage._put_deploy_tx_params.assert_called_once_with( context, tx_params) self.storage.get_deploy_info.assert_called_once_with( context, score_address) self.storage._put_deploy_info.assert_called_once_with( context, deploy_info) with patch( 'iconservice.deploy.icon_score_deploy_storage.IconScoreDeployTXParams' ) as MockTxParams: self.storage._put_deploy_tx_params.reset_mock() self.storage.get_deploy_tx_params.reset_mock() self.storage.get_deploy_tx_params.return_value = None self.storage._put_deploy_info = Mock() context = Mock(spec=IconScoreContext) score_address = create_address(1) deploy_type = DeployType.INSTALL owner = create_address() tx_hash = create_tx_hash() deploy_data = {} deploy_info = IconScoreDeployInfo(score_address, DeployState.INACTIVE, owner, None, tx_hash) tx_params = IconScoreDeployTXParams(tx_hash, deploy_type, score_address, deploy_data) self.storage.get_deploy_info = Mock(return_value=deploy_info) MockTxParams.return_value = tx_params other_owner = create_address() with self.assertRaises(ServerErrorException) as e: self.storage.put_deploy_info_and_tx_params( context, score_address, deploy_type, other_owner, tx_hash, deploy_data) self.assertEqual(e.exception.code, ExceptionCode.SERVER_ERROR) self.assertEqual( e.exception.message, f'invalid owner: {deploy_info.owner} != {other_owner}') self.storage.get_deploy_tx_params.assert_called_once_with( context, tx_hash) self.storage._put_deploy_tx_params.assert_called_once_with( context, tx_params) self.storage.get_deploy_info.assert_called_once_with( context, score_address) with patch( 'iconservice.deploy.icon_score_deploy_storage.IconScoreDeployTXParams' ) as MockTxParams: self.storage._put_deploy_tx_params.reset_mock() self.storage.get_deploy_tx_params.reset_mock() self.storage.get_deploy_tx_params.return_value = None self.storage._put_deploy_info = Mock() self.storage._db.delete = Mock() context = Mock(spec=IconScoreContext) context.get_revision = Mock(return_value=0) score_address = create_address(1) deploy_type = DeployType.INSTALL owner = create_address() tx_hash = create_tx_hash() deploy_data = {} already_tx_hash = create_tx_hash() deploy_info = IconScoreDeployInfo(score_address, DeployState.INACTIVE, owner, None, already_tx_hash) tx_params = IconScoreDeployTXParams(tx_hash, deploy_type, score_address, deploy_data) self.storage.get_deploy_info = Mock( return_value=deepcopy(deploy_info)) self.storage._create_db_key = Mock( return_value=deploy_info.next_tx_hash) MockTxParams.return_value = tx_params self.storage.put_deploy_info_and_tx_params(context, score_address, deploy_type, owner, tx_hash, deploy_data) self.storage.get_deploy_tx_params.assert_called_once_with( context, tx_hash) self.storage._put_deploy_tx_params.assert_called_once_with( context, tx_params) self.storage.get_deploy_info.assert_called_once_with( context, score_address) self.storage._db.delete.assert_called_once_with( context, deploy_info.next_tx_hash) self.storage._put_deploy_info.assert_called_once() ret_context, ret_deployinfo = self.storage._put_deploy_info.call_args[ 0] self.assertEqual(ret_context, context) self.assertEqual(ret_deployinfo.next_tx_hash, tx_hash) def test_put_deploy_info_and_tx_params_for_builtin(self): with patch( 'iconservice.deploy.icon_score_deploy_storage.IconScoreDeployInfo' ) as MockDeployInfos: self.storage._put_deploy_info = Mock() context = Mock(spec=IconScoreContext) score_address = create_address(1) owner = create_address() deploy_info = IconScoreDeployInfo(score_address, DeployState.ACTIVE, owner, None, None) MockDeployInfos.return_value = deploy_info self.storage.put_deploy_info_and_tx_params_for_builtin( context, score_address, owner) self.storage._put_deploy_info.assert_called_once_with( context, deploy_info) def test_update_score_info(self): context = Mock(spec=IconScoreContext) score_address = create_address(1) tx_hash = create_tx_hash() self.storage.get_deploy_info = Mock(return_value=None) with self.assertRaises(ServerErrorException) as e: self.storage.update_score_info(context, score_address, tx_hash) self.assertEqual(e.exception.code, ExceptionCode.SERVER_ERROR) self.assertEqual(e.exception.message, f'deploy_info is None: {score_address}') self.storage.get_deploy_info.assert_called_once_with( context, score_address) current_tx_hash = create_tx_hash() next_tx_hash = create_tx_hash() deploy_info = IconScoreDeployInfo(score_address, DeployState.INACTIVE, create_address(), current_tx_hash, next_tx_hash) self.storage.get_deploy_info = Mock(return_value=deepcopy(deploy_info)) with self.assertRaises(ServerErrorException) as e: self.storage.update_score_info(context, score_address, tx_hash) self.assertEqual(e.exception.code, ExceptionCode.SERVER_ERROR) self.assertEqual( e.exception.message, f'Invalid update tx_hash: ' f'tx_hash({tx_hash}) != next_tx_hash({next_tx_hash})') self.storage.get_deploy_info.assert_called_once_with( context, score_address) owner = create_address() current_tx_hash = create_tx_hash() deploy_info = IconScoreDeployInfo(score_address, DeployState.INACTIVE, owner, current_tx_hash, tx_hash) self.storage.get_deploy_info = Mock(return_value=deepcopy(deploy_info)) self.storage._put_deploy_info = Mock() self.storage.get_deploy_tx_params = Mock(return_value=None) with self.assertRaises(ServerErrorException) as e: self.storage.update_score_info(context, score_address, tx_hash) self.assertEqual(e.exception.code, ExceptionCode.SERVER_ERROR) self.assertEqual(e.exception.message, f'tx_params is None: {tx_hash}') self.storage.get_deploy_info.assert_called_once_with( context, score_address) self.storage._put_deploy_info.assert_called_once() ret_context, ret_deploy_info = self.storage._put_deploy_info.call_args[ 0] self.assertEqual(ret_context, context) expected = IconScoreDeployInfo(score_address, DeployState.ACTIVE, owner, tx_hash, None) self.assertEqual(ret_deploy_info.to_bytes(), expected.to_bytes()) self.storage.get_deploy_info = Mock(return_value=deepcopy(deploy_info)) self.storage._put_deploy_info = Mock() self.storage.get_deploy_tx_params = Mock(return_value=Mock( spec=IconScoreDeployTXParams)) self.storage.update_score_info(context, score_address, tx_hash) self.storage.get_deploy_info.assert_called_once_with( context, score_address) self.storage._put_deploy_info.assert_called_once() ret_context, ret_deploy_info = self.storage._put_deploy_info.call_args[ 0] self.assertEqual(ret_context, context) expected = IconScoreDeployInfo(score_address, DeployState.ACTIVE, owner, tx_hash, None) self.assertEqual(ret_deploy_info.to_bytes(), expected.to_bytes()) self.storage.get_deploy_tx_params.assert_called_once_with( context, tx_hash) def test_put_deploy_info(self): context = Mock(spec=IconScoreContext) score_address = create_address(1) deploy_info = IconScoreDeployInfo(score_address, DeployState.INACTIVE, create_address(), None, create_tx_hash()) self.storage._create_db_key = Mock( return_value=score_address.to_bytes()) self.storage._db.put = Mock() self.storage._put_deploy_info(context, deploy_info) self.storage._db.put.assert_called_once_with(context, score_address.to_bytes(), deploy_info.to_bytes()) def test_get_deploy_info(self): context = Mock(spec=IconScoreContext) score_address = create_address(1) self.storage._create_db_key = Mock( return_value=score_address.to_bytes()) self.storage._db.get = Mock(return_value=None) self.assertEqual(None, self.storage.get_deploy_info(context, score_address)) score_address = create_address(1) deploy_info = IconScoreDeployInfo(score_address, DeployState.INACTIVE, create_address(), None, create_tx_hash()) self.storage._create_db_key = Mock( return_value=score_address.to_bytes()) self.storage._db.get = Mock(return_value=deploy_info.to_bytes()) self.assertEqual( deploy_info.to_bytes(), self.storage.get_deploy_info(context, score_address).to_bytes()) def test_put_deploy_tx_params(self): context = Mock(spec=IconScoreContext) tx_hash = create_tx_hash() tx_params = IconScoreDeployTXParams(tx_hash, DeployType.INSTALL, create_address(1), {}) self.storage._create_db_key = Mock(return_value=tx_hash) self.storage._db.put = Mock() self.storage._put_deploy_tx_params(context, tx_params) self.storage._db.put.assert_called_once_with(context, tx_params.tx_hash, tx_params.to_bytes()) def test_get_deploy_tx_params(self): context = Mock(spec=IconScoreContext) tx_hash = create_tx_hash() self.storage._create_db_key = Mock(return_value=tx_hash) self.storage._db.get = Mock(return_value=None) self.assertEqual(None, self.storage.get_deploy_tx_params(context, tx_hash)) tx_hash = create_tx_hash() tx_params = IconScoreDeployTXParams(tx_hash, DeployType.INSTALL, create_address(1), {}) self.storage._create_db_key = Mock(return_value=tx_hash) self.storage._db.get = Mock(return_value=tx_params.to_bytes()) self.assertEqual( tx_params.to_bytes(), self.storage.get_deploy_tx_params(context, tx_hash).to_bytes()) def test_create_db_key(self): prefix = b'prefix' src_key = b'src_key' self.assertEqual(prefix + src_key, self.storage._create_db_key(prefix, src_key)) prefix = b'' src_key = b'' self.assertEqual(prefix + src_key, self.storage._create_db_key(prefix, src_key)) def test_is_score_active(self): context = Mock(spec=IconScoreContext) score_address = create_address(1) self.storage.get_deploy_info = Mock(return_value=None) self.assertEqual(False, self.storage.is_score_active(context, score_address)) self.storage.get_deploy_info.assert_called_once_with( context, score_address) deploy_info = Mock(spec=IconScoreDeployInfo) deploy_info.deploy_state = DeployState.ACTIVE self.storage.get_deploy_info = Mock(return_value=deploy_info) self.assertEqual(True, self.storage.is_score_active(context, score_address)) self.storage.get_deploy_info.assert_called_once_with( context, score_address) deploy_info.reset_mock() deploy_info.deploy_state = DeployState.INACTIVE self.storage.get_deploy_info = Mock(return_value=deploy_info) self.assertEqual(False, self.storage.is_score_active(context, score_address)) self.storage.get_deploy_info.assert_called_once_with( context, score_address) def test_get_score_owner(self): context = Mock(spec=IconScoreContext) score_address = create_address(1) self.storage.get_deploy_info = Mock(return_value=None) self.assertEqual(False, self.storage.is_score_active(context, score_address)) self.storage.get_deploy_info.assert_called_once_with( context, score_address) deploy_info = Mock(spec=IconScoreDeployInfo) deploy_info.attach_mock(Mock(), 'owner') self.storage.get_deploy_info = Mock(return_value=deploy_info) self.assertEqual(deploy_info.owner, self.storage.get_score_owner(context, score_address)) self.storage.get_deploy_info.assert_called_once_with( context, score_address) def test_get_tx_hashes_by_score_address(self): context = Mock(spec=IconScoreContext) score_address = create_address(1) self.storage.get_deploy_info = Mock(return_value=None) self.assertEqual((None, None), self.storage.get_tx_hashes_by_score_address( context, score_address)) self.storage.get_deploy_info.assert_called_once_with( context, score_address) deploy_info = Mock(spec=IconScoreDeployInfo) deploy_info.attach_mock(Mock(), 'current_tx_hash') deploy_info.attach_mock(Mock(), 'next_tx_hash') self.storage.get_deploy_info = Mock(return_value=deploy_info) self.assertEqual( (deploy_info.current_tx_hash, deploy_info.next_tx_hash), self.storage.get_tx_hashes_by_score_address( context, score_address)) self.storage.get_deploy_info.assert_called_once_with( context, score_address) def test_get_score_address_by_tx_hash(self): context = Mock(spec=IconScoreContext) tx_hash = create_tx_hash() self.storage.get_deploy_tx_params = Mock(return_value=None) self.assertIsNone( self.storage.get_score_address_by_tx_hash(context, tx_hash)) self.storage.get_deploy_tx_params.assert_called_once_with( context, tx_hash) tx_params = Mock(spec=IconScoreDeployTXParams) self.storage.get_deploy_tx_params = Mock(return_value=tx_params) self.assertEqual( tx_params.score_address, self.storage.get_score_address_by_tx_hash(context, tx_hash)) self.storage.get_deploy_tx_params.assert_called_once_with( context, tx_hash)