def create_contract_v1(body: dict) -> dict: """Deploy a new contract on the chain Args: body: The parsed body, supplied by user. This is used to create a data model Returns: DTO of the contract at rest which is being created """ # Before anything else, check to see if this chain has too many contracts if elasticsearch.get_count(folder=smart_contract_dao.FOLDER) >= MAX_CONTRACT_LIMIT: raise exceptions.ContractLimitExceeded(MAX_CONTRACT_LIMIT) # Create model and validate fields _log.info(f"Creating data model for {body['txn_type']}") contract = smart_contract_model.new_contract_from_user(body) # Register new trasnsaction type for smart contract _log.info("Registering new smart contract transaction type") transaction_type_dao.register_smart_contract_transaction_type(contract) # Start build task try: job_processor.begin_task(contract, task_type=smart_contract_model.ContractActions.CREATE) except RuntimeError: transaction_type_dao.remove_existing_transaction_type(contract.txn_type) raise return contract.export_as_at_rest()
def delete_contract_data(self) -> None: """Remove all stored information on this smart contract""" try: _log.info(f"Deleting contract data for contract {self.model.id}") storage.delete_directory(f"SMARTCONTRACT/{self.model.id}") _log.info("Removing index") smart_contract_dao.remove_smart_contract_index(self.model.id) _log.info(f"Deleting txn type {self.model.txn_type}") transaction_type_dao.remove_existing_transaction_type(self.model.txn_type) key = f"KEYS/{self.model.auth_key_id}" _log.info(f"Deleting HMAC key {key}") storage.delete(key) except Exception: _log.exception("Error deleting contract data") self.model.set_state(state=self.end_error_state, msg="Error deleting contract data")
def delete_transaction_type_v1(transaction_type: str) -> None: """ Takes in a transaction type Checks if the transaction type is of smart contract Deletes type """ try: existing_type_structure = transaction_type_dao.get_registered_transaction_type(transaction_type) if existing_type_structure is None: raise exceptions.NotFound(transaction_type) if existing_type_structure.contract_id: raise exceptions.ActionForbidden("Cannot delete smart contract transaction type") transaction_type_dao.remove_existing_transaction_type(transaction_type) except exceptions.NotFound: pass
def delete_transaction_type_v1(transaction_type: str) -> None: """ Takes in a transaction type Checks if the transaction type is of smart contract Deletes type """ try: existing_txn_type = transaction_type_dao.get_registered_transaction_type( transaction_type) # If txn type is a smart contract that exists, don't allow it to be deleted if existing_txn_type.contract_id and smart_contract_dao.contract_does_exist( existing_txn_type.contract_id): raise exceptions.ActionForbidden( "Cannot delete smart contract transaction type") transaction_type_dao.remove_existing_transaction_type(transaction_type) except exceptions.NotFound: return
def delete_contract_data(self) -> None: """Remove all stored information on this smart contract Returns: None """ _log.info("Deleting contract data") try: storage.delete_directory(f"SMARTCONTRACT/{self.model.id}") _log.info("Removing index") elasticsearch.remove_index(doc_id=self.model.id, folder="SMARTCONTRACT") _log.info("Deleting txn type") transaction_type_dao.remove_existing_transaction_type( self.model.txn_type) key = f"KEYS/{self.model.auth_key_id}" _log.info(f"Deleting HMAC key {key}") storage.delete(key) except Exception: _log.exception("Error deleting contract data") self.model.set_state(state=self.end_error_state, msg="Error deleting contract data")
def test_delete_registered_txn_type_succeeds(self, storage_delete_mock, rsearch_delete_mock): transaction_type_dao.remove_existing_transaction_type("randomTxn") storage_delete_mock.assert_called_with( "TRANSACTION_TYPES/TYPES/randomTxn") rsearch_delete_mock.assert_called_once_with("randomTxn")