1616
1717import  os 
1818import  sys 
19+ import  time 
1920import  json 
2021import  argparse 
2122import  logging 
3637from  avalon_sdk .direct .jrpc .jrpc_work_order_receipt  \
3738     import  JRPCWorkOrderReceiptImpl 
3839from  error_code .error_status  import  WorkOrderStatus , ReceiptCreateStatus 
40+ from  error_code .enclave_error  import  EnclaveError 
3941import  avalon_crypto_utils .signature  as  signature 
4042from  error_code .error_status  import  SignatureStatus 
4143from  avalon_sdk .work_order_receipt .work_order_receipt  \
@@ -291,39 +293,17 @@ def _verify_wo_res_signature(work_order_res,
291293    return  True 
292294
293295
294- def  Main (args = None ):
295-     options  =  _parse_command_line (args )
296- 
297-     config  =  _parse_config_file (options .config )
298-     if  config  is  None :
299-         logger .error ("\n  Error in parsing config file: {}\n " .format (
300-             options .config 
301-         ))
302-         sys .exit (- 1 )
303- 
304-     # mode should be one of listing or registry (default) 
305-     mode  =  options .mode 
306- 
307-     # Http JSON RPC listener uri 
308-     uri  =  options .uri 
309-     if  uri :
310-         config ["tcf" ]["json_rpc_uri" ] =  uri 
311- 
312-     # Address of smart contract 
313-     address  =  options .address 
314-     if  address :
315-         if  mode  ==  "listing" :
316-             config ["ethereum" ]["direct_registry_contract_address" ] =  \
317-                 address 
318-         elif  mode  ==  "registry" :
319-             logger .error (
320-                 "\n  Only Worker registry listing address is supported."  + 
321-                 "Worker registry address is unsupported \n " )
322-             sys .exit (- 1 )
323- 
324-     # worker id 
325-     worker_id  =  options .worker_id 
296+ def  _start_client (config , options , jrpc_req_id , worker_id , worker_registry ):
297+     """ 
298+     Generates, submits work order requests. Retrieve response for the 
299+     work order requests, verifies the response and generate receipt 
326300
301+     @param config - instance of parsed config file 
302+     @param options - command line options that are passed to the client 
303+     @param jrpc_req_id - JSON RPC request id 
304+     @param worker_id - worker id obtained from worker registry 
305+     @param worker_registry - worker registry 
306+     """ 
327307    # work load id of worker 
328308    workload_id  =  options .workload_id 
329309    if  not  workload_id :
@@ -342,42 +322,6 @@ def Main(args=None):
342322    # requester signature for work order requests 
343323    requester_signature  =  options .requester_signature 
344324
345-     # setup logging 
346-     config ["Logging" ] =  {
347-         "LogFile" : "__screen__" ,
348-         "LogLevel" : "INFO" 
349-     }
350- 
351-     plogger .setup_loggers (config .get ("Logging" , {}))
352-     sys .stdout  =  plogger .stream_to_logger (
353-         logging .getLogger ("STDOUT" ), logging .DEBUG )
354-     sys .stderr  =  plogger .stream_to_logger (
355-         logging .getLogger ("STDERR" ), logging .WARN )
356- 
357-     logger .info ("******* Hyperledger Avalon Generic client *******" )
358- 
359-     if  mode  ==  "registry"  and  address :
360-         logger .error ("\n  Worker registry contract address is unsupported \n " )
361-         sys .exit (- 1 )
362- 
363-     # Retrieve JSON RPC uri from registry list 
364-     if  not  uri  and  mode  ==  "listing" :
365-         uri  =  _retrieve_uri_from_registry_list (config )
366-         if  uri  is  None :
367-             logger .error ("\n  Unable to get http JSON RPC uri \n " )
368-             sys .exit (- 1 )
369- 
370-     # Prepare worker 
371-     # JRPC request id. Choose any integer value 
372-     jrpc_req_id  =  31 
373-     worker_registry  =  JRPCWorkerRegistryImpl (config )
374-     if  not  worker_id :
375-         # Get first worker from worker registry 
376-         worker_id  =  _lookup_first_worker (worker_registry , jrpc_req_id )
377-         if  worker_id  is  None :
378-             logger .error ("\n  Unable to get worker \n " )
379-             sys .exit (- 1 )
380- 
381325    # Retrieve worker details 
382326    jrpc_req_id  +=  1 
383327    worker_retrieve_result  =  worker_registry .worker_retrieve (
@@ -465,6 +409,12 @@ def Main(args=None):
465409                    res ['result' ], session_key , session_iv )
466410            logger .info ("\n Decrypted response:\n  {}" 
467411                        .format (decrypted_res ))
412+     # Handle worker key refresh scenario 
413+     elif  "error"  in  res  and  \
414+             res ["error" ]["code" ] ==  WorkOrderStatus .WORKER_KEY_REFRESHED :
415+         logger .error ("Worker Key refreshed. Retrieving latest Worker details" )
416+         worker_id  =  _lookup_first_worker (worker_registry , jrpc_req_id )
417+         _start_client (config , options , jrpc_req_id , worker_id , worker_registry )
468418    else :
469419        logger .error ("\n  Work order get result failed {}\n " .format (
470420            res 
@@ -483,5 +433,77 @@ def Main(args=None):
483433            sys .exit (1 )
484434
485435
436+ def  Main (args = None ):
437+     options  =  _parse_command_line (args )
438+ 
439+     config  =  _parse_config_file (options .config )
440+     if  config  is  None :
441+         logger .error ("\n  Error in parsing config file: {}\n " .format (
442+             options .config 
443+         ))
444+         sys .exit (- 1 )
445+ 
446+     # mode should be one of listing or registry (default) 
447+     mode  =  options .mode 
448+ 
449+     # Http JSON RPC listener uri 
450+     uri  =  options .uri 
451+     if  uri :
452+         config ["tcf" ]["json_rpc_uri" ] =  uri 
453+ 
454+     # Address of smart contract 
455+     address  =  options .address 
456+     if  address :
457+         if  mode  ==  "listing" :
458+             config ["ethereum" ]["direct_registry_contract_address" ] =  \
459+                 address 
460+         elif  mode  ==  "registry" :
461+             logger .error (
462+                 "\n  Only Worker registry listing address is supported."  + 
463+                 "Worker registry address is unsupported \n " )
464+             sys .exit (- 1 )
465+ 
466+     # worker id 
467+     worker_id  =  options .worker_id 
468+ 
469+     # setup logging 
470+     config ["Logging" ] =  {
471+         "LogFile" : "__screen__" ,
472+         "LogLevel" : "INFO" 
473+     }
474+ 
475+     plogger .setup_loggers (config .get ("Logging" , {}))
476+     sys .stdout  =  plogger .stream_to_logger (
477+         logging .getLogger ("STDOUT" ), logging .DEBUG )
478+     sys .stderr  =  plogger .stream_to_logger (
479+         logging .getLogger ("STDERR" ), logging .WARN )
480+ 
481+     logger .info ("******* Hyperledger Avalon Generic client *******" )
482+ 
483+     if  mode  ==  "registry"  and  address :
484+         logger .error ("\n  Worker registry contract address is unsupported \n " )
485+         sys .exit (- 1 )
486+ 
487+     # Retrieve JSON RPC uri from registry list 
488+     if  not  uri  and  mode  ==  "listing" :
489+         uri  =  _retrieve_uri_from_registry_list (config )
490+         if  uri  is  None :
491+             logger .error ("\n  Unable to get http JSON RPC uri \n " )
492+             sys .exit (- 1 )
493+ 
494+     # Prepare worker 
495+     # JRPC request id. Choose any integer value 
496+     jrpc_req_id  =  31 
497+     worker_registry  =  JRPCWorkerRegistryImpl (config )
498+     if  not  worker_id :
499+         # Get first worker from worker registry 
500+         worker_id  =  _lookup_first_worker (worker_registry , jrpc_req_id )
501+         if  worker_id  is  None :
502+             logger .error ("\n  Unable to get worker \n " )
503+             sys .exit (- 1 )
504+ 
505+     _start_client (config , options , jrpc_req_id , worker_id , worker_registry )
506+ 
507+ 
486508# ----------------------------------------------------------------------------- 
487509Main ()
0 commit comments