def _retrieve_uri_from_registry_list(config): # Retrieve Http JSON RPC listener uri from registry logger.info("\n Retrieve Http JSON RPC listener uri from registry \n") # Get block chain type blockchain_type = config['blockchain']['type'] if blockchain_type == "ethereum": worker_registry_list = EthereumWorkerRegistryListImpl(config) else: worker_registry_list = None logger.error("\n Worker registry list is currently supported only for " "ethereum block chain \n") return None # Lookup returns tuple, first element is number of registries and # second is element is lookup tag and # third is list of organization ids. registry_count, lookup_tag, registry_list = \ worker_registry_list.registry_lookup() logger.info("\n Registry lookup response: registry count: {} " "lookup tag: {} registry list: {}\n".format( registry_count, lookup_tag, registry_list)) if (registry_count == 0): logger.error("No registries found") return None # Retrieve the fist registry details. registry_retrieve_result = worker_registry_list.registry_retrieve( registry_list[0]) logger.info( "\n Registry retrieve response: {}\n".format(registry_retrieve_result)) return registry_retrieve_result[0]
def __init__(self, config_file): super(TestEthereumWorkerRegistryListImpl, self).__init__() if not path.isfile(config_file): raise FileNotFoundError("File not found at path: {0}".format( path.realpath(config_file))) try: with open(config_file) as fd: self.__config = toml.load(fd) except IOError as e: if e.errno != errno.ENOENT: raise Exception('Could not open config file: %s', e) self.__eth_conn = EthereumWorkerRegistryListImpl(self.__config)
def __init__(self, config_file=None, config=None): """ Parameters: config_file Optional configuration file path as a string config Optional dictionary loaded from config_file Either one of config_file or config needs to be passed. If both are passed, then config takes precedence. """ if (config is not None): self.__config = config else: if not isfile(config_file): raise FileNotFoundError("File not found at path: {0}".format( realpath(config_file))) try: with open(config_file) as fd: self.__config = toml.load(fd) except IOError as e: """ Catch the exception related to toml file format except for the File does not exist exception. """ if e.errno != errno.ENOENT: raise Exception('Could not open config file: %s' % e) self.__blockchain_type = self.__config['blockchain']['type'] if self.__blockchain_type.lower() == "ethereum": self.__worker_registry_list = EthereumWorkerRegistryListImpl( self.__config) else: self.__worker_registry_list = None self.__worker_registry = JRPCWorkerRegistryImpl(self.__config) self.__work_order = JRPCWorkOrderImpl(self.__config) self.__work_order_receipts = JRPCWorkOrderReceiptImpl(self.__config)
class DirectModelGenericClient(BaseGenericClient): """ Generic client class to test end to end flow for direct model. """ def __init__(self, config): super().__init__() self._config = config self._worker_registry_list = None self._worker_instance = JRPCWorkerRegistryImpl(self._config) self._work_order_instance = JRPCWorkOrderImpl(self._config) self._work_order_receipt = JRPCWorkOrderReceiptImpl(self._config) def _get_random_jrpc_id(self): return random.randint(1, 10000) def retrieve_uri_from_registry_list(self, config): # Retrieve Http JSON RPC listener uri from registry # in case of direct model logging.info("\n Retrieve Http JSON RPC listener uri from registry \n") # Get block chain type blockchain_type = config['blockchain']['type'] if blockchain_type == "Ethereum": self._worker_registry_list = EthereumWorkerRegistryListImpl(config) else: logging.error("\n Worker registry list is currently " "supported only for " "ethereum block chain \n") return None # Lookup returns tuple, first element is number of registries and # second is element is lookup tag and # third is list of organization ids. registry_count, lookup_tag, registry_list = \ self._worker_registry_list.registry_lookup() logging.info("\n Registry lookup response: registry count: {} " "lookup tag: {} registry list: {}\n".format( registry_count, lookup_tag, registry_list)) if (registry_count == 0): logging.error("No registries found") return None # Retrieve the fist registry details. registry_retrieve_result = \ self._worker_registry_list.registry_retrieve( registry_list[0]) logging.info("\n Registry retrieve response: {}\n".format( registry_retrieve_result)) return registry_retrieve_result[0] def get_worker_details(self, worker_id): """ Fetch worker details for given worker id """ worker_retrieve_res = self._worker_instance.worker_retrieve( worker_id, self._get_random_jrpc_id()) if worker_retrieve_res and "result" in worker_retrieve_res: status = worker_retrieve_res["result"]["status"] details = worker_retrieve_res["result"]["details"] logging.info("\n Worker retrieve: worker status {} " "details : {}\n".format(status, json.dumps(details, indent=4))) if status == WorkerStatus.ACTIVE.value: # Initializing Worker Object worker_obj = SGXWorkerDetails() worker_obj.load_worker(details) return worker_obj else: logging.error("Worker is not active") else: return None def get_work_order_result(self, work_order_id): """ Retrieve work order result for given work order id """ work_order_res = self._work_order_instance.work_order_get_result( work_order_id, self._get_random_jrpc_id()) logging.info("Work order get result {}".format( json.dumps(work_order_res, indent=4))) if work_order_res and "result" in work_order_res: return True, work_order_res return False, work_order_res def submit_work_order(self, wo_params): """ Submit work order request """ jrpc_id = self._get_random_jrpc_id() wo_request = wo_params.to_string() logging.info("\n Work order sumbit request {}\n".format( wo_params.to_jrpc_string(jrpc_id))) wo_submit_res = self._work_order_instance.work_order_submit( wo_params.get_work_order_id(), wo_params.get_worker_id(), wo_params.get_requester_id(), wo_request, jrpc_id) logging.info("Work order submit response : {}\n ".format( json.dumps(wo_submit_res, indent=4))) if wo_submit_res: # in asynchronous mode if "error" in wo_submit_res: if wo_submit_res["error"]["code"] == WorkOrderStatus.PENDING: return True, wo_submit_res # in synchronous mode elif "result" in wo_submit_res: return True, wo_submit_res else: return False, wo_submit_res else: return False, wo_submit_res def create_work_order_receipt(self, wo_params, client_private_key): # Create a work order receipt object using # WorkOrderReceiptRequest class. # This function will send a WorkOrderReceiptCreate # JSON RPC request. wo_request = json.loads( wo_params.to_jrpc_string(self._get_random_jrpc_id())) wo_receipt_request_obj = WorkOrderReceiptRequest() wo_create_receipt = wo_receipt_request_obj.create_receipt( wo_request, ReceiptCreateStatus.PENDING.value, client_private_key) logging.info("Work order create receipt request : {} \n \n ".format( json.dumps(wo_create_receipt, indent=4))) # Submit work order create receipt jrpc request wo_receipt_resp = self._work_order_receipt.work_order_receipt_create( wo_create_receipt["workOrderId"], wo_create_receipt["workerServiceId"], wo_create_receipt["workerId"], wo_create_receipt["requesterId"], wo_create_receipt["receiptCreateStatus"], wo_create_receipt["workOrderRequestHash"], wo_create_receipt["requesterGeneratedNonce"], wo_create_receipt["requesterSignature"], wo_create_receipt["signatureRules"], wo_create_receipt["receiptVerificationKey"], self._get_random_jrpc_id()) logging.info("Work order create receipt response : {} \n \n ".format( wo_receipt_resp)) def retrieve_work_order_receipt(self, wo_id): """ Retrieve work order receipt for given work order id """ receipt_res = self._work_order_receipt.work_order_receipt_retrieve( wo_id, id=self._get_random_jrpc_id()) logging.info("\n Retrieve receipt response:\n {}".format( json.dumps(receipt_res, indent=4))) # Retrieve last update to receipt by passing 0xFFFFFFFF receipt_update_retrieve = \ self._work_order_receipt.work_order_receipt_update_retrieve( wo_id, None, 1 << 32, id=self._get_random_jrpc_id()) logging.info("\n Last update to receipt receipt is:\n {}".format( json.dumps(receipt_update_retrieve, indent=4))) return receipt_update_retrieve def verify_receipt_signature(self, receipt_update_retrieve): """ Verify work order receipt signature """ signer = worker_signing.WorkerSign() status = signer.verify_update_receipt_signature( receipt_update_retrieve['result']) if status == SignatureStatus.PASSED: logging.info( "Work order receipt retrieve signature verification " + "successful") else: logging.error( "Work order receipt retrieve signature verification failed!!") return False return True
class TestEthereumWorkerRegistryListImpl(unittest.TestCase): def __init__(self, config_file): super(TestEthereumWorkerRegistryListImpl, self).__init__() if not path.isfile(config_file): raise FileNotFoundError("File not found at path: {0}".format( path.realpath(config_file))) try: with open(config_file) as fd: self.__config = toml.load(fd) except IOError as e: if e.errno != errno.ENOENT: raise Exception('Could not open config file: %s', e) self.__eth_conn = EthereumWorkerRegistryListImpl(self.__config) def test_registry_add(self): self.__org_id = urandom(32) self.__uri = "http://127.0.0.1:1947" self.__sc_addr = urandom(32) self.__app_type_ids = [urandom(32), urandom(32)] logging.info( 'Calling registry_add contract..\n org_id: %s\n ' + 'uri: %s\n ' + 'sc_addr: %s\n application_ids: %s', hex_to_utf8(self.__org_id), self.__uri, hex_to_utf8(self.__sc_addr), pretty_ids(self.__app_type_ids)) result = self.__eth_conn.registry_add( self.__org_id, self.__uri, self.__sc_addr, self.__app_type_ids) logging.info( "registry_add contract status \n %s", result) self.assertIsNotNone( result, "Registry add response not matched") def test_registry_update(self): self.__new_app_id = [urandom(32)] self.__new_uri = 'http://localhost:1947' logging.info( 'Calling registry_update contract..\n org_id: %s\n uri: %s\n ' + 'sc_addr: %s\n application_ids: %s', hex_to_utf8(self.__org_id), self.__new_uri, hex_to_utf8(self.__sc_addr), pretty_ids(self.__new_app_id)) result = self.__eth_conn.registry_update( self.__org_id, self.__new_uri, self.__sc_addr, self.__new_app_id) logging.info( "registry_update contract status \n%s", result) self.assertIsNotNone( result, "Registry update response not matched") def test_registry_set_status(self): self.__new_status = RegistryStatus.OFF_LINE logging.info( 'Calling registry_set_status contract..\n org_id: %s\n status: %d', hex_to_utf8(self.__org_id), self.__new_status.value) result = self.__eth_conn.registry_set_status( self.__org_id, self.__new_status) logging.info( "registry_set_status contract status \n%s", result) self.assertIsNotNone( result, "Registry set status response not matched") def test_registry_lookup(self): logging.info( 'Calling registry_lookup..\n application_id: %s', hex_to_utf8(self.__app_type_ids[0])) result = self.__eth_conn.registry_lookup(self.__app_type_ids[0]) logging.info( 'registry_lookup contract status [%d, %s, %s]', result[0], result[1], pretty_ids(result[2])) self.assertEqual( result[0], 1, "Registry lookup response total count not matched") self.assertEqual( result[2][0], self.__org_id, "Registry lookup response not matched for org id") def test_registry_retrieve(self): logging.info('Calling registry_retrieve..\n org_id: %s', hex_to_utf8(self.__org_id)) result = self.__eth_conn.registry_retrieve(self.__org_id) logging.info( 'registry_retrieve contract status [%s, %s, %s, %d]', result[0], hex_to_utf8(result[1]), pretty_ids(result[2]), result[3]) self.assertEqual( result[0], self.__new_uri, "Registry retrieve response not matched for uri") self.assertEqual( hex_to_utf8(result[1]), hex_to_utf8(self.__sc_addr), "Registry retrieve response not matched for " + "smart contract address") self.assertEqual( result[2][0], self.__app_type_ids[0], "Registry retrieve response not matched for app id type list " + "index 0") self.assertEqual( result[2][1], self.__app_type_ids[1], "Registry retrieve response not matched for app id type list " + "index 1") self.assertEqual( result[2][2], self.__new_app_id[0], "Registry retrieve response not matched for app id type list " + "index 2") self.assertEqual( result[3], self.__new_status.value, "Registry retrieve response not matched for status") def test_registry_lookup_next(self): lookup_tag = "test" logging.info( 'Calling registry_lookup_next..\n application_id: %s\n ' + 'lookup_tag: %s', hex_to_utf8(self.__app_type_ids[0]), lookup_tag) result = self.__eth_conn.registry_lookup_next( self.__app_type_ids[0], lookup_tag) logging.info('registry_lookup_next contract status [%d, %s, %s]', result[0], result[1], pretty_ids(result[2]))