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_deploy_info_from_bytes_to_bytes_none_check(self): score_address = create_address(1) owner_address = create_address() tx_hash1 = create_tx_hash() tx_hash2 = None deploy_state = DeployState.INACTIVE info1 = IconScoreDeployInfo(score_address, deploy_state, owner_address, tx_hash1, tx_hash2) data = IconScoreDeployInfo.to_bytes(info1) self.assertTrue(isinstance(data, bytes)) info2 = IconScoreDeployInfo.from_bytes(data) self.assertEqual(info2.score_address, score_address) self.assertEqual(info2.deploy_state, deploy_state) self.assertEqual(info2.owner, owner_address) self.assertEqual(info2.current_tx_hash, tx_hash1) self.assertEqual(info2.next_tx_hash, tx_hash2)
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_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)