def _send_request(self, suffix, data=None, content_type=None, jobId=None): if self._base_url.startswith("http://"): url = "{}/{}".format(self._base_url, suffix) else: url = "http://{}/{}".format(self._base_url, suffix) headers = {} if content_type is not None: headers['Content-Type'] = content_type try: if data is not None: result = requests.post(url, headers=headers, data=data) else: result = requests.get(url, headers=headers) if result.status_code == 404: raise JobException("No such job: {}".format(jobId)) if not result.ok: raise JobException("Error {}: {}".format( result.status_code, result.reason)) except requests.ConnectionError as err: raise JobException('Failed to connect to {}: {}'.format( url, str(err))) except BaseException as err: raise JobException(err) return result.text
def main(prog_name=os.path.basename(sys.argv[0]), args=None): if args is None: args = sys.argv[1:] parser = create_parser(prog_name) args = parser.parse_args(args) # if args.verbose is None: # verbose_level = 0 # else: # verbose_level = args.verbose # setup_loggers(verbose_level=verbose_level) if args.command == 'create': do_create(args) elif args.command == 'list': do_list(args) elif args.command == 'workers': do_workers(args) elif args.command == 'broadcast': do_broadcast(args) # elif args.command == 'take': # do_take(args) else: raise JobException("invalid command: {}".format(args.command))
def _get_status(self, batch_id, wait): try: result = self._send_request( 'batch_statuses?id={}&wait={}'.format(batch_id, wait), ) return yaml.safe_load(result)['data'][0]['status'] except BaseException as err: raise JobException(err)
def __init__(self, base_url, keyfile=None): self._base_url = base_url if keyfile is None: self._signer = None return try: with open(keyfile) as fd: private_key_str = fd.read().strip() except OSError as err: raise JobException('Failed to read private key {}: {}'.format( keyfile, str(err))) try: private_key = Secp256k1PrivateKey.from_hex(private_key_str) except ParseError as e: raise JobException('Unable to load private key: {}'.format(str(e))) self._signer = CryptoFactory(create_context('secp256k1')) \ .new_signer(private_key)
def do_show(args): jobId = args.jobId url = _get_url(args) # auth_user, auth_password = _get_auth_info(args) client = JobClient(base_url=url, keyfile=None) data = client.show(jobId) if data is not None: print('job'.data) else: raise JobException("Job not found: {}".format(jobId))
def do_list(args): url = _get_url(args) # auth_user, auth_password = _get_auth_info(args) client = JobClient(base_url=url, keyfile=None) job_list = [ job.split(',') for jobs in client.list() for job in jobs.decode().split('|') ] if job_list is not None: for job_data in job_list: jobId, workerId, publisherId, start_time, end_time, deadline, base_rewards, extra_rewards = job_data print('{}, {}, {}, {}, {}, {}, {}, {}'.format( jobId, workerId, publisherId, start_time, end_time, deadline, base_rewards, extra_rewards)) else: raise JobException("Could not retrieve game listing.")
def do_list(args): url = _get_url(args) # auth_user, auth_password = _get_auth_info(args) client = JobClient(base_url=url, keyfile=None) job_list = [ job.split(',') for jobs in client.list() for job in jobs.decode().split('|') ] if job_list is not None: for job_data in job_list: jobId, receiverId, publisherId, data_size, start_time, duration, guaranteed_rt, test_rt, base_rewards, extra_rewards, is_integrity = job_data print('{}, {}, {}, {}, {}, {}, {}, {}'.format( jobId, receiverId, publisherId, data_size, start_time, duration, guaranteed_rt, test_rt, base_rewards, extra_rewards, is_integrity)) else: raise JobException("Could not retrieve game listing.")
def computeReputation(self, receiverIds): # current time in millisecond # current_time = time.time() # current_time = 1593871200000 # store each node's available rewards, used for validation phase print('++++++++create log file++++++++') logger = logging.getLogger() hdlr = logging.FileHandler('/home/ubuntu/reputation.log') logger.addHandler(hdlr) logger.setLevel(logging.INFO) # get all job from chain job_list = [ job.split(',') for jobs in self.list() for job in jobs.decode().split('|') ] print('job_list: ', job_list) # construct job record for reputation computation # required: start_time # extra_rewards job_record = {} jobs = [] if job_list is not None: for job_data in job_list: jobId, receiverId, publisherId, data_size, start_time, duration, guaranteed_rt, test_rt, base_rewards, extra_rewards, is_integrity = job_data # store jobs according to receiverId job_record.setdefault(receiverId, []).append({ 'start_time': start_time, 'extra_rewards': extra_rewards }) jobs.append({ 'receiverId': receiverId, 'publisherId': publisherId, 'base_rewards': float(base_rewards), 'extra_rewards': float(extra_rewards) }) else: raise JobException("Could not retrieve game listing.") print('++++++++job record in chain++++++++') print(job_record) # based on extra rewards, reflecting histroy performance # only compute for who has expressed interests reputation_receivers = self.reputationBasedOnRewards(receiverIds, job_record) # or # reputation_receivers = self.reputationWithPunishment(receiverIds, job_record) print('++++++++ reputation_receivers n++++++++') print(reputation_receivers) for receiverId in receiverIds: if not receiverId in reputation_receivers.keys(): reputation_receivers[receiverId] = 0.0 recvBaseRewards = {} recvExtraRewards = {} # initialize for receiverId in receiverIds: recvBaseRewards[receiverId] = 0 recvExtraRewards[receiverId] = 0 for job in jobs: recvBaseRewards[job['receiverId']] += job['base_rewards'] recvExtraRewards[job['receiverId']] += job['extra_rewards'] if recvBaseRewards and recvExtraRewards: for receiverId in receiverIds: info = receiverId + ' - ' +str(recvBaseRewards[receiverId]) + ' - ' + str(recvExtraRewards[receiverId]) + ' - ' + str(round(reputation_receivers[receiverId], 3)) print('++++++++write log++++++++') print(info) logger.info(info) return reputation_receivers
def computeReputation(self, workerIds): # current time in millisecond # current_time = time.time() current_time = 1593871200000 print('++++++++create log file++++++++') logger = logging.getLogger() hdlr = logging.FileHandler('/home/ubuntu/reputation.log') logger.addHandler(hdlr) logger.setLevel(logging.INFO) # get all job from chain job_list = [ job.split(',') for jobs in self.list() for job in jobs.decode().split('|') ] # construct job record for reputation computation # required: start_time # end_time # extra_rewards job_record = {} jobs = [] if job_list is not None: for job_data in job_list: jobId, workerId, publisherId, start_time, end_time, deadline, base_rewards, extra_rewards = job_data job_record.setdefault(workerId, []).append({ 'start_time': start_time, 'end_time': end_time, 'extra_rewards': extra_rewards }) jobs.append({ 'workerId': workerId, 'publisherId': publisherId, 'base_rewards': float(base_rewards), 'extra_rewards': float(extra_rewards) }) else: raise JobException("Could not retrieve game listing.") # print('++++++++job record in chain++++++++') # print(job_record) # rewards based reputation calculation # based on extra rewards, reflecting histroy performance reputation_workers = self.computeBasedOnRewards(workerIds, job_record) # print('++++++++ score_rewards_based n++++++++') # print(score_rewards_based) recvBaseRewards = {} recvExtraRewards = {} # initialize for job in jobs: recvBaseRewards[job['workerId']] = 0 recvExtraRewards[job['workerId']] = 0 for job in jobs: recvBaseRewards[job['workerId']] += job['base_rewards'] recvExtraRewards[job['workerId']] += job['extra_rewards'] for workerId in workerIds: info = workerId + ' - ' + str( recvBaseRewards[workerId]) + ' - ' + str( recvExtraRewards[workerId]) + ' - ' + str( round(reputation_workers[workerId], 3)) print('++++++++write log++++++++') print(info) logger.info(info) return reputation_workers