def register_transaction_type_v1(transaction_type_structure: Dict[str, Any]) -> None: """ Takes in a transaction type structure and attempts to register """ model = transaction_type_model.new_from_user_input(transaction_type_structure) try: transaction_type_dao.get_registered_transaction_type(cast(str, model.txn_type)) # This will be a string after creation except exceptions.NotFound: pass else: _log.error("Transaction type is already registered") raise exceptions.TransactionTypeConflict(f"A transaction type of {model.txn_type} is already registered") _log.debug("Uploading new type to datastore") transaction_type_dao.store_registered_transaction_type(model)
def register_transaction_type_v1( transaction_type_structure: Dict[str, Any]) -> None: """ Takes in a transaction type structure and attempts to register """ model = transaction_type_model.new_from_user_input( transaction_type_structure) try: transaction_type_dao.get_registered_transaction_type(model.txn_type) except exceptions.NotFound: pass else: _log.error("Transaction type is already registered") raise exceptions.TransactionTypeConflict( f"A transaction type of {model.txn_type} is already registered") _log.debug("Queuing transaction type for creation") transaction_type_dao.create_new_transaction_type(model)
def register_smart_contract_transaction_type( smart_contract_model: "smart_contract_model.SmartContractModel" ) -> None: """Creates a new transaction type for a contract. Throws ContractConflict exception if name exists Args: model (obj): The model of the contract Returns: None if no duplicates are found, or throws ContractConflict Raises: TransactionTypeConflict exception if name exists """ try: get_registered_transaction_type(smart_contract_model.txn_type) raise exceptions.TransactionTypeConflict( "Transaction type already registered") except exceptions.NotFound: pass _log.debug("Uploading new contract type to datastore") txn_type_model = transaction_type_model.new_from_contract_create( smart_contract_model.txn_type, smart_contract_model.id) store_registered_transaction_type(txn_type_model)
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 redisearch.get_document_count( redisearch.Indexes.smartcontract.value) >= 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) # Check that this transaction type isn't already taken try: transaction_type_dao.get_registered_transaction_type(contract.txn_type) raise exceptions.TransactionTypeConflict( f"Transaction type {contract.txn_type} already registered") except exceptions.NotFound: pass # Start build task job_processor.begin_task( contract, task_type=smart_contract_model.ContractActions.CREATE) try: # Register new transaction type for smart contract _log.info("Registering new smart contract transaction type") smart_contract_dao.add_smart_contract_index(contract) transaction_type_dao.register_smart_contract_transaction_type( contract, body.get("custom_indexes")) return contract.export_as_at_rest() except Exception: # Try to cleanup if contract doesn't create successfully _log.exception( "Error creating contract index or transaction type. Reverting") job_processor.begin_task( contract, task_type=smart_contract_model.ContractActions.DELETE) raise
def test_webserver_error_handler_transaction_type_conflict(self, mock_http_response, mock_report_exception): exception = exceptions.TransactionTypeConflict() helpers.webserver_error_handler(exception) mock_report_exception.assert_not_called() mock_http_response.assert_called_once_with(409, ANY)