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_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(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_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 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 worker_retrieve(self, worker_id): """ Retrieve the worker identified by worker id """ if (self.__contract_instance != 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 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")