def registry_set_status(self, org_id, status): if (self.__contract_instance is not None): if (is_valid_hex_str(binascii.hexlify(org_id).decode("utf8")) is False): logging.info("Invalid Org id {}".format(org_id)) return construct_message("failed", "Invalid argument") if not isinstance(status, RegistryStatus): logging.info("Invalid registry status {}".format(status)) return construct_message( "failed", "Invalid worker status {}".format(status)) txn_hash = self.__contract_instance.functions.registrySetStatus( org_id, status.value).buildTransaction({ "chainId": self.__eth_client.get_channel_id(), "gas": self.__eth_client.get_gas_limit(), "gasPrice": self.__eth_client.get_gas_price(), "nonce": self.__eth_client.get_txn_nonce() }) tx = self.__eth_client.execute_transaction(txn_hash) return tx else: logging.error( "direct registry contract instance is not initialized") return construct_message( "failed", "direct registry contract instance is not initialized")
def worker_lookup(self, worker_type, org_id, application_id): """ Lookup a worker identified worker_type, org_id and application_id all fields are optional and if present condition should match for all fields. If none passed it should return all workers. """ if (self.__contract_instance is not None): if not isinstance(worker_type, WorkerType): logging.info("Invalid workerType {}".format(worker_type)) return construct_message( "failed", "Invalid workerType {}".format(worker_type)) if not is_valid_hex_str(binascii.hexlify(org_id).decode("utf8")): logging.info("Invalid organization id {}".format(org_id)) return construct_message( "failed", "Invalid organization id {}".format(org_id)) if not is_valid_hex_str( binascii.hexlify(application_id).decode("utf8")): logging.info( "Invalid application id {}".format(application_id)) return construct_message( "failed", "Invalid application id {}".format(application_id)) lookupResult = self.__contract_instance.functions.workerLookUp( worker_type.value, org_id, application_id).call() return lookupResult else: logging.error( "worker registry contract instance is not initialized") return construct_message( "failed", "worker registry contract instance is not initialized")
def worker_set_status(self, worker_id, status): """ Set the registry status identified by worker id status is worker type enum type """ if (self.__contract_instance is not None): if not is_valid_hex_str( binascii.hexlify(worker_id).decode("utf8")): logging.info("Invalid worker id {}".format(worker_id)) return construct_message( "failed", "Invalid worker id {}".format(worker_id)) if not isinstance(status, WorkerStatus): logging.info("Invalid worker status {}".format(status)) return construct_message( "failed", "Invalid worker status {}".format(status)) txn_hash = self.__contract_instance.functions.workerSetStatus( worker_id, status.value).buildTransaction({ "chainId": self.__eth_client.get_channel_id(), "gas": self.__eth_client.get_gas_limit(), "gasPrice": self.__eth_client.get_gas_price(), "nonce": self.__eth_client.get_txn_nonce() }) tx = self.__eth_client.execute_transaction(txn_hash) return tx else: logging.error( "worker registry contract instance is not initialized") return construct_message( "failed", "worker registry contract instance is not initialized")
def worker_lookup_next(self, worker_type, org_id, application_id, lookup_tag): if (self.__contract_instance is not None): if not isinstance(worker_type, WorkerType): logging.info("Invalid workerType {}".format(worker_type)) return construct_message( "failed", "Invalid workerType {}".format(worker_type)) if not is_valid_hex_str(binascii.hexlify(org_id).decode("utf")): logging.info("Invalid organization id {}".format(org_id)) return construct_message( "failed", "Invalid organization id {}".format(org_id)) if not is_valid_hex_str( binascii.hexlify(application_id).decode("utf8")): logging.info("Invalid application id {}".format(org_id)) return construct_message( "failed", "Invalid application id {}".format(org_id)) lookupResult = self.__contract_instance.functions.workerLookUpNext( worker_type.value, org_id, application_id, lookup_tag).call() return lookupResult else: logging.error( "worker registry contract instance is not initialized") return construct_message( "failed", "worker registry contract instance \ is not initialized")
def worker_update(self, worker_id, details): """ Update the worker with details data which is json string """ if (self.__contract_instance is not None): if not is_valid_hex_str( binascii.hexlify(worker_id).decode("utf8")): logging.error("Invalid worker id {}".format(worker_id)) return construct_message( "failed", "Invalid worker id {}".format(worker_id)) if details is not None: is_valid = validate_details(details) if is_valid is not None: logging.error(is_valid) return construct_message("failed", is_valid) txn_hash = self.__contract_instance.functions.workerUpdate( worker_id, details).buildTransaction({ "chainId": self.__eth_client.get_channel_id(), "gas": self.__eth_client.get_gas_limit(), "gasPrice": self.__eth_client.get_gas_price(), "nonce": self.__eth_client.get_txn_nonce() }) tx = self.__eth_client.execute_transaction(txn_hash) return tx else: logging.error( "worker registry contract instance is not initialized") return construct_message( "failed", "worker registry contract instance is not initialized")
def registry_update(self, org_id, uri, sc_addr, app_type_ids): if (self.__contract_instance is not None): if (is_valid_hex_str(binascii.hexlify(org_id).decode("utf8")) is False): logging.error("Invalid Org id {}".format(org_id)) return construct_message("failed", "Invalid Org id") if (sc_addr is not None and is_valid_hex_str( binascii.hexlify(sc_addr).decode("utf8")) is False): logging.error( "Invalid smart contract address {}".format(sc_addr)) return construct_message( "failed", "Invalid smart contract address") if (not uri): logging.error("Empty uri {}".format(uri)) return construct_message("failed", "Empty uri") for aid in app_type_ids: if (is_valid_hex_str(binascii.hexlify(aid).decode("utf8")) is False): logging.error("Invalid application id {}".format(aid)) return construct_message( "failed", "Invalid application id") txn_hash = self.__contract_instance.functions.registryUpdate( org_id, uri, sc_addr, app_type_ids).buildTransaction( self.__eth_client.get_transaction_params() ) tx = self.__eth_client.execute_transaction(txn_hash) return tx else: logging.error( "direct registry contract instance is not initialized") return construct_message( "failed", "direct registry contract instance is not initialized")
def execute_transaction(self, tx_dict): """ Sign the raw transaction with private key, send it and wait for receipts """ signed_txn = self.__w3.eth.account.signTransaction( tx_dict, private_key=self.__eth_private_key) tx_hash = self.__w3.eth.sendRawTransaction(signed_txn.rawTransaction) tx_receipt = self.__w3.eth.waitForTransactionReceipt( tx_hash.hex(), 120) logging.info("executed transaction hash: %s, receipt: %s", format(tx_hash.hex()), format(tx_receipt)) if tx_receipt is None: return construct_message("failed", "timeout") if tx_receipt.status == 0: return construct_message("failed", "transaction reverted") return {'status': 'added', 'txn_receipt': tx_receipt}
def registry_retrieve(self, org_id): if (self.__contract_instance is not None): if (is_valid_hex_str(binascii.hexlify(org_id).decode("utf8")) is False): logging.info("Invalid Org id {}".format(org_id)) return construct_message("failed", "Invalid Org id") else: registryDetails = \ self.__contract_instance.functions.registryRetrieve( org_id).call() return registryDetails else: logging.error( "direct registry contract instance is not initialized") return construct_message( "failed", "direct registry contract instance is not initialized")
def registry_lookup_next(self, app_type_id, lookup_tag): if (self.__contract_instance is not None): if is_valid_hex_str(binascii.hexlify(app_type_id).decode("utf8")): lookupResult = \ self.__contract_instance.functions.registryLookUpNext( app_type_id, lookup_tag).call() return lookupResult else: logging.info( "Invalid application type id {}".format(app_type_id)) return construct_message( "failed", "Invalid application type id") else: logging.error( "direct registry contract instance is not initialized") return construct_message( "failed", "direct registry contract instance is not initialized")
def worker_retrieve(self, worker_id): """ Retrieve the worker identified by worker id """ if (self.__contract_instance is not None): if not is_valid_hex_str( binascii.hexlify(worker_id).decode("utf8")): logging.info("Invalid worker id {}".format(worker_id)) return construct_message( "failed", "Invalid worker id {}".format(worker_id)) workerDetails = self.__contract_instance.functions.workerRetrieve( worker_id).call() return workerDetails else: logging.error( "worker registry contract instance is not initialized") return construct_message( "failed", "worker registry contract instance is not initialized")
def workOrder_getResult(self, workOrder_id): """ Lookup a worker identified worker_type, org_id and application_id all fields are optional and if present condition should match for all fields. If none passed it should return all workers. """ if (self.__contract_instance != None): if not is_hex(binascii.hexlify(workOrder_id).decode("utf8")): logging.info("Invalid workOrder id {}".format(workOrder_id)) return construct_message( "failed", "Invalid workOrder id {}".format(workOrder_id)) workOrderDetails = self.__contract_instance.functions.workOrderGetResult( workOrder_id).call() return workOrderDetails else: logging.error( "work_Order_get_Result contract instance is not initialized") return construct_message( "failed", "work_Order_get_Result contract instance is not initialized")
def registry_add(self, org_id, uri, sc_addr, app_type_ids): if (self.__contract_instance is not None): if (is_valid_hex_str(binascii.hexlify(org_id).decode("utf8")) is False): logging.info("Invalid Org id {}".format(org_id)) return construct_message("failed", "Invalid Org id") if (sc_addr is not None and is_valid_hex_str( binascii.hexlify(sc_addr).decode("utf8")) is False): logging.info("Invalid smart contract address {}") return construct_message("failed", "Invalid smart contract address") if (not uri): logging.info("Empty uri {}".format(uri)) return construct_message("failed", "Empty uri") for aid in app_type_ids: if (is_valid_hex_str(binascii.hexlify(aid).decode("utf8")) is False): logging.info("Invalid application id {}".format(aid)) return construct_message("failed", "Invalid application id") txn_hash = self.__contract_instance.functions.registryAdd( org_id, uri, org_id, app_type_ids).buildTransaction({ "chainId": self.__eth_client.get_channel_id(), "gas": self.__eth_client.get_gas_limit(), "gasPrice": self.__eth_client.get_gas_price(), "nonce": self.__eth_client.get_txn_nonce() }) tx = self.__eth_client.execute_transaction(txn_hash) return tx else: logging.error( "direct registry contract instance is not initialized") return construct_message( "failed", "direct registry contract instance is not initialized")
def workOrder_submit(self, workOrder_id, worker_id, requester_id, workOrder_request): """ Registry new worker with details of worker """ if (self.__contract_instance != None): if not is_hex(binascii.hexlify(worker_id).decode("utf8")): logging.info("Invalid worker id {}".format(worker_id)) return construct_message( "failed", "Invalid worker id {}".format(worker_id)) if not is_hex(binascii.hexlify(workOrder_id).decode("utf8")): logging.info("Invalid workOrder id {}".format(workOrder_id)) return construct_message( "failed", "Invalid workerType {}".format(workeOrder_id)) if not is_hex(binascii.hexlify(requester_id).decode("utf8")): logging.info("Invalid Requester id {}".format(requester_id)) return construct_message( "failed", "Invalid oRequester id {}".format(requester_id)) txn_hash = self.__contract_instance.functions.workOrderSubmit( workOrder_id, worker_id, requester_id, workOrder_request).buildTransaction({ "chainId": self.__eth_client.get_channel_id(), "gas": self.__eth_client.get_gas_limit(), "gasPrice": self.__eth_client.get_gas_price(), "nonce": self.__eth_client.get_txn_nonce() }) tx = self.__eth_client.execute_transaction(txn_hash) return tx else: logging.error( "Submit registry contract instance is not initialized") return construct_message( "failed", "Submit contract instance is not initialized")
def worker_register(self, worker_id, worker_type, org_id, application_ids, details): """ Register new worker with details of worker """ if (self.__contract_instance is not None): if not is_valid_hex_str( binascii.hexlify(worker_id).decode("utf8")): logging.info("Invalid worker id {}".format(worker_id)) return construct_message( "failed", "Invalid worker id {}".format(worker_id)) if not isinstance(worker_type, WorkerType): logging.info("Invalid workerType {}".format(worker_type)) return construct_message( "failed", "Invalid workerType {}".format(worker_type)) if not is_valid_hex_str(binascii.hexlify(org_id).decode("utf8")): logging.info("Invalid organization id {}".format(org_id)) return construct_message( "failed", "Invalid organization id {}".format(org_id)) for aid in application_ids: if not is_valid_hex_str(binascii.hexlify(aid).decode("utf8")): logging.info("Invalid application id {}".format(aid)) return construct_message( "failed", "Invalid application id {}".format(aid)) if details is not None: is_valid = validate_details(details) if is_valid is not None: return construct_message("failed", is_valid) txn_hash = self.__contract_instance.functions.workerRegister( worker_id, worker_type.value, org_id, application_ids, details).buildTransaction({ "chainId": self.__eth_client.get_channel_id(), "gas": self.__eth_client.get_gas_limit(), "gasPrice": self.__eth_client.get_gas_price(), "nonce": self.__eth_client.get_txn_nonce() }) tx = self.__eth_client.execute_transaction(txn_hash) return tx else: logging.error( "worker registry contract instance is not initialized") return construct_message( "failed", "worker registry contract instance is not initialized")