def __init__(self, repo): self.request_dao = RequestDAO(repo=repo) self.vote_dao = VoteDAO(repo=repo) self.solution_dao = SolutionDAO(repo=repo) self.stake_dao = StakeDAO(repo=repo) self.foundation_member_dao = FoundationMemberDAO(repo=repo) self.rfai_request_dao = RFAIRequestRepository(repo=repo)
class RFAIClaimBackRequestEventConsumer(RFAIEventConsumer): _connection = Repository(NETWORKS=NETWORK) _request_dao = RequestDAO(_connection) _stake_dao = StakeDAO(_connection) def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port): super().__init__(net_id, ws_provider, ipfs_url, ipfs_port) def on_event(self, event): event_data = self._get_event_data(event) request_id = event_data['requestId'] [ found, request_id, requester, total_fund, document_uri, expiration, end_submission, end_evaluation, status, stake_members, submitters ] = self._get_rfai_service_request_by_id(request_id) claim_back_amount = event_data["amount"] request_data = self._request_dao.get_request_data_for_given_requester_and_status( filter_parameter={"request_id": request_id}) self._connection.begin_transaction() try: self._request_dao.update_request_for_given_request_id( request_id=request_id, update_parameters={"fund_total": total_fund}) self._stake_dao.update_stake_for_given_request_id( request_id=request_id, update_parameters={"claim_back_amount": 0}) self._connection.commit_transaction() except Exception as e: logger.info( f"Transaction Rollback for event {event}. Error::{repr(e)}") self._connection.rollback_transaction() raise e
class RFAIApproveRequestEventConsumer(RFAIEventConsumer): _connection = Repository(NETWORKS=NETWORK) _request_dao = RequestDAO(_connection) def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port): super().__init__(net_id, ws_provider, ipfs_url, ipfs_port) def on_event(self, event): # need to change this whenever we clean up it should nto be tied with db column name event_data = self._get_event_data(event) request_id = event_data['requestId'] approver = event_data['approver'] end_submission = event_data['endSubmission'] end_evaluation = event_data['endEvaluation'] expiration = event_data['expiration'] filter_params = { "status": RFAIStatusCodes.APPROVED.value, "request_actor": approver, "end_submission": end_submission, "end_evaluation": end_evaluation, "expiration": expiration } self._request_dao.update_request_for_given_request_id( request_id, filter_params)
class RFAICloseRequestEventConsumer(RFAIEventConsumer): _connection = Repository(NETWORKS=NETWORK) _request_dao = RequestDAO(_connection) def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port): super().__init__(net_id, ws_provider, ipfs_url, ipfs_port) def on_event(self, event): event_data = self._get_event_data(event) request_id = event_data['requestId'] [ found, request_id, requester, total_fund, document_uri, expiration, end_submission, end_evaluation, status, stake_members, submitters ] = self._get_rfai_service_request_by_id(request_id) self._update_rfai_request_status_and_actor( request_id=request_id, status=RFAIStatusCodes.CLOSED.value, request_actor=event_data["actor"]) def _update_rfai_request_status_and_actor(self, request_id, status, request_actor): self._request_dao.update_request_for_given_request_id( request_id=request_id, update_parameters={ "status": status, "request_actor": request_actor })
class RFAIFundRequestEventConsumer(RFAIEventConsumer): _connection = Repository(NETWORKS=NETWORK) _rfai_request_repository = RequestDAO(_connection) _stake_dao_repository = StakeDAO(_connection) def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port): super().__init__(net_id, ws_provider, ipfs_url, ipfs_port) def on_event(self, event): event_data = self._get_event_data(event) request_id = event_data['requestId'] staker = event_data['staker'] amount = event_data['amount'] [ found, request_id, requester, total_fund, document_uri, expiration, end_submission, end_evaluation, status, stake_members, submitters ] = self._get_rfai_service_request_by_id(request_id) metadata_hash = self._get_metadata_hash(document_uri) rfai_metadata = eval(self._get_rfai_metadata_from_ipfs(metadata_hash)) created_at = rfai_metadata['created'] # from where we will get claim back amount self._stake_dao_repository.create_stake( request_id, staker, amount, 0, event["data"]["transactionHash"], created_at)
class RFAIFundRequestEventConsumer(RFAIEventConsumer): _connection = Repository(NETWORKS=NETWORK) _request_dao = RequestDAO(_connection) _stake_dao = StakeDAO(_connection) _fund_request_trxn_dao = FundRequestTransactionDAO(_connection) def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port): super().__init__(net_id, ws_provider, ipfs_url, ipfs_port) def on_event(self, event): created_at_in_epoch = self._blockchain_util.get_created_at_for_block( block_no=event["data"]["block_no"]).get("timestamp", None) if created_at_in_epoch is not None: created_at = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(created_at_in_epoch)) event_data = self._get_event_data(event) request_id = event_data['requestId'] staker = event_data['staker'] funded_amount = event_data['amount'] [ found, request_id, requester, total_fund, document_uri, expiration, end_submission, end_evaluation, status, stake_members, submitters ] = self._get_rfai_service_request_by_id(request_id) found, stake_amount = self._get_stake_by_id(request_id=request_id, staker=staker) if not found: stake_amount = 0 request_fund = self._request_dao.get_request_data_for_given_requester_and_status( filter_parameter={"request_id": request_id })[0]["request_fund"] + funded_amount self._connection.begin_transaction() try: self._stake_dao.create_or_update_stake( request_id=request_id, stake_member=staker, stake_amount=0, claim_back_amount=stake_amount, created_at=created_at) self._stake_dao.add_stake_amount(request_id=request_id, stake_member=staker, stake_amount=funded_amount) self._fund_request_trxn_dao.persist_transaction( stake_member=requester, transaction_hash=event["data"]["transactionHash"], created_at=created_at) self._request_dao.update_request_for_given_request_id( request_id=request_id, update_parameters={ "request_fund": request_fund, "fund_total": total_fund }) self._connection.commit_transaction() except Exception as e: logger.info( f"Transaction Rollback for event {event}. Error::{repr(e)}") self._connection.rollback_transaction() raise e
class RFAIClaimBackRequestEventConsumer(RFAIEventConsumer): _connection = Repository(NETWORKS=NETWORK) _rfai_request_repository = RequestDAO(_connection) def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port): super().__init__(net_id, ws_provider, ipfs_url, ipfs_port) def on_event(self, event): event_data = self._get_event_data(event) request_id = event_data['requestId'] [ found, request_id, requester, total_fund, document_uri, expiration, end_submission, end_evaluation, status, stake_members, submitters ] = self._get_rfai_service_request_by_id(request_id) print("reached")
class RFAIExtendRequestEventConsumer(RFAIEventConsumer): _connection = Repository(NETWORKS=NETWORK) _request_dao = RequestDAO(_connection) def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port): super().__init__(net_id, ws_provider, ipfs_url, ipfs_port) def on_event(self, event): # need to change this whenever we clean up it should nto be tied with db column name event_data = self._get_event_data(event) expiration = event_data['expiration'] requester = event_data['requester'] request_id = event_data['requestId'] filter_params = {"expiration": expiration} self._request_dao.update_request_for_given_request_id( request_id, filter_params)
class RFAICreateRequestEventConsumer(RFAIEventConsumer): _connection = Repository(NETWORKS=NETWORK) _rfai_request_repository = RequestDAO(_connection) def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port): super().__init__(net_id, ws_provider, ipfs_url, ipfs_port) def on_event(self, event): event_data = self._get_event_data(event) request_id = event_data['requestId'] requester = event_data['requester'] expiration = event_data['expiration'] amount = event_data['amount'] metadata_hash = self._get_metadata_hash(event_data['documentURI']) self._process_create_request_event(request_id, requester, expiration, amount, metadata_hash) self._stake_dao_repository.create_stake( request_id, requester, amount, 0, event["data"]["transactionHash"], dt.utcnow()) def _process_create_request_event(self, request_id, requester, expiration, amount, metadata_hash): [ found, request_id, requester, total_fund, document_uri, expiration, end_submission, end_evaluation, status, stake_members, submitters ] = self._get_rfai_service_request_by_id(request_id) rfai_metadata = eval(self._get_rfai_metadata_from_ipfs(metadata_hash)) title = rfai_metadata['title'] requester_name = requester description = rfai_metadata['description'] git_hub_link = '' training_data_set_uri = rfai_metadata['training-dataset'] acceptance_criteria = rfai_metadata['acceptance-criteria'] request_actor = '' created_at = rfai_metadata['created'] self._rfai_request_repository.create_request( request_id, requester, total_fund, metadata_hash, expiration, end_submission, end_evaluation, status, title, requester_name, description, git_hub_link, training_data_set_uri, acceptance_criteria, request_actor, created_at)
class RFAIService: def __init__(self, repo): self.request_dao = RequestDAO(repo=repo) self.vote_dao = VoteDAO(repo=repo) self.solution_dao = SolutionDAO(repo=repo) self.stake_dao = StakeDAO(repo=repo) self.foundation_member_dao = FoundationMemberDAO(repo=repo) self.rfai_request_dao = RFAIRequestRepository(repo=repo) def _format_filter_params(self, query_parameters): filter_params = {} # if "requester" in query_parameters.keys(): # filter_params["requester"] = query_parameters["requester"] if "request_id" in query_parameters.keys(): filter_params["request_id"] = query_parameters["request_id"] return filter_params def get_requests(self, query_string_parameters): status = query_string_parameters.get("status", None) status = status.upper() filter_parameter = self._format_filter_params( query_parameters=query_string_parameters) if status is not None and status in RFAIStatusCodes.__members__: status_code = RFAIStatusCodes[status].value query_string_parameters["status_code"] = status_code current_block_no = obj_blockchain_utils.get_current_block_no() if status_code == RFAIStatusCodes.ACTIVE.value: tmp_requests_data = self.request_dao.get_approved_active_request( current_block_no=current_block_no, filter_parameter=filter_parameter) elif status_code == RFAIStatusCodes.SOLUTION_VOTE.value: tmp_requests_data = self.request_dao.get_approved_solution_vote_request( current_block_no=current_block_no, filter_parameter=filter_parameter) elif status_code == RFAIStatusCodes.COMPLETED.value: tmp_requests_data = self.request_dao.get_approved_completed_request( current_block_no=current_block_no, filter_parameter=filter_parameter) elif status_code == RFAIStatusCodes.PENDING.value: tmp_requests_data = self.request_dao.get_open_active_request( current_block_no=current_block_no, requester=query_string_parameters["requester"], filter_parameter=filter_parameter) elif status_code == RFAIStatusCodes.CLOSED.value: tmp_requests_data = self.request_dao.get_closed_request(requester=query_string_parameters["requester"], filter_parameter=filter_parameter) + \ self.request_dao.get_request_data_for_given_requester_and_status( filter_parameter={"status": RFAIStatusCodes.REJECTED.value}) elif status_code == RFAIStatusCodes.INCOMPLETE.value: tmp_requests_data = self.request_dao.get_open_expired_request(current_block_no=current_block_no, filter_parameter=filter_parameter) + \ self.request_dao.get_approved_expired_request(current_block_no=current_block_no, filter_parameter=filter_parameter) + \ self.request_dao.get_approved_request_with_no_votes( current_block_no=current_block_no, filter_parameter=filter_parameter) + \ self.request_dao.get_approved_request_with_no_solution( current_block_no=current_block_no, filter_parameter=filter_parameter) else: filter_parameter.update( {"status": getattr(RFAIStatusCodes, status).value}) tmp_requests_data = self.request_dao.get_request_data_for_given_requester_and_status( filter_parameter=filter_parameter) elif status is None: tmp_requests_data = self.request_dao.get_request_data_for_given_requester_and_status( filter_parameter=filter_parameter) my_request = query_string_parameters.get("my_request", "false") requests = [] for record in tmp_requests_data: if my_request.lower() == "true" and query_string_parameters[ "requester"] != record["requester"]: continue vote_count = self.vote_dao.get_votes_count_for_given_request( request_id=record["request_id"]) stake_count = self.stake_dao.get_stake_count_for_given_request( request_id=record["request_id"]) solution_count = self.solution_dao.get_solution_count_for_given_request( request_id=record["request_id"]) record.update({"vote_count": vote_count["vote_count"]}) record.update({"stake_count": stake_count["stake_count"]}) record.update({"solution_count": solution_count["solution_count"]}) record["created_at"] = str(record["created_at"]) requests.append(record) logger.info(requests) return requests def get_rfai_summary(self, requester, my_request): request_summary = self.generate_rfai_summary(requester=requester, my_request=my_request) return request_summary def get_vote_details_for_given_request_id(self, request_id): vote_details = self.rfai_request_dao.get_vote_details_for_given_request_id( request_id=request_id) for record in vote_details: record["created_at"] = str(record["created_at"]) return vote_details def get_stake_details_for_given_request_id(self, request_id): stake_data = self.stake_dao.get_stake_details_for_given_request_id( request_id=request_id) for record in stake_data: record["created_at"] = str(record["created_at"]) return stake_data def get_solution_details_for_given_request_id(self, request_id): solution_data = self.solution_dao.get_solution_details_for_given_request_id( request_id=request_id) for record in solution_data: record["created_at"] = str(record["created_at"]) return solution_data def get_foundation_members(self): foundation_members_data = self.foundation_member_dao.get_foundation_members( ) for record in foundation_members_data: record["status"] = obj_utils.bits_to_integer(record["status"]) record["role"] = obj_utils.bits_to_integer(record["role"]) record["created_at"] = str(record["created_at"]) return foundation_members_data def generate_rfai_summary(self, requester, my_request): filter_parameter = {} current_block_no = obj_blockchain_utils.get_current_block_no() rfai_summary = { "PENDING": len( self.get_requests( query_string_parameters={ "requester": requester, "status": RFAIStatusCodes.PENDING.name, "my_request": my_request })), "INCOMPLETE": len( self.get_requests( query_string_parameters={ "requester": requester, "status": RFAIStatusCodes.INCOMPLETE.name, "my_request": my_request })), "ACTIVE": len( self.get_requests( query_string_parameters={ "requester": requester, "status": RFAIStatusCodes.ACTIVE.name, "my_request": my_request })), "SOLUTION_VOTE": len( self.get_requests( query_string_parameters={ "requester": requester, "status": RFAIStatusCodes.SOLUTION_VOTE.name, "my_request": my_request })), "COMPLETED": len( self.get_requests( query_string_parameters={ "requester": requester, "status": RFAIStatusCodes.COMPLETED.name, "my_request": my_request })), "REJECTED": len( self.get_requests( query_string_parameters={ "requester": requester, "status": RFAIStatusCodes.REJECTED.name, "my_request": my_request })), "CLOSED": len( self.get_requests( query_string_parameters={ "requester": requester, "status": RFAIStatusCodes.CLOSED.name, "my_request": my_request })) } return rfai_summary def get_claims_data_for_solution_provider(self, user_address): current_block_no = obj_blockchain_utils.get_current_block_no() logger.info(f"current_block_no {current_block_no}") solution_data = self.rfai_request_dao.get_claims_data_for_solution_provider( submitter=user_address, current_block_no=current_block_no) for record in solution_data: request_id = record["request_id"] request_data = self.request_dao.get_request_data_for_given_requester_and_status( filter_parameter={"request_id": request_id}) votes = self.vote_dao.get_vote_details_for_given_rfai_solution_id( rfai_solution_id=record["row_id"]) claim_amount_data = self.rfai_request_dao.get_claim_amount_for_solution_provider( rfai_solution_id=record["row_id"]) tokens = claim_amount_data[0]["claim_amount_for_soln_provider"] if tokens is None: tokens = 0 record.update({ "request_title": request_data[0]["request_title"], "votes": votes["votes"], "expiration": request_data[0]["expiration"], "tokens": int(tokens), "end_evaluation": request_data[0]["end_evaluation"] }) return solution_data def get_claims_data_for_stake_provider(self, user_address): current_block_no = obj_blockchain_utils.get_current_block_no() stake_data = self.rfai_request_dao.get_claim_details_for_stakers( stake_member=user_address, current_block_no=current_block_no) for record in stake_data: record["status"] = RFAIStatusCodes(record["status"]).name record[ "claim_back_amount"] = self.stake_dao.get_stake_details_for_given_request_id_and_stake_member( request_id=record["request_id"], stake_member=user_address)[0]["claim_back_amount"] return stake_data
class RFAIService: def __init__(self, repo): self.request_dao = RequestDAO(repo=repo) self.vote_dao = VoteDAO(repo=repo) self.solution_dao = SolutionDAO(repo=repo) self.stake_dao = StakeDAO(repo=repo) self.foundation_member_dao = FoundationMemberDAO(repo=repo) def _format_filter_params(self, query_parameters): filter_params = {} # if "requester" in query_parameters.keys(): # filter_params["requester"] = query_parameters["requester"] if "request_id" in query_parameters.keys(): filter_params["request_id"] = query_parameters["request_id"] return filter_params def get_requests(self, query_string_parameters): status = query_string_parameters.get("status", None) status = status.upper() filter_parameter = self._format_filter_params( query_parameters=query_string_parameters) if status is not None and status in RFAIStatusCodes.__members__: status_code = RFAIStatusCodes[status].value query_string_parameters["status_code"] = status_code current_block_no = obj_blockchain_utils.get_current_block_no() if status_code == RFAIStatusCodes.ACTIVE.value: tmp_requests_data = self.request_dao.get_approved_active_request( current_block_no=current_block_no, filter_parameter=filter_parameter) elif status_code == RFAIStatusCodes.SOLUTION_VOTE.value: tmp_requests_data = self.request_dao.get_approved_solution_vote_request( current_block_no=current_block_no, filter_parameter=filter_parameter) elif status_code == RFAIStatusCodes.COMPLETED.value: tmp_requests_data = self.request_dao.get_approved_completed_request( current_block_no=current_block_no, filter_parameter=filter_parameter) elif status_code == RFAIStatusCodes.PENDING.value: tmp_requests_data = self.request_dao.get_open_active_request( current_block_no=current_block_no, requester=query_string_parameters["requester"], filter_parameter=filter_parameter) elif status_code == RFAIStatusCodes.INCOMPLETE.value: tmp_requests_data = self.request_dao.get_open_expired_request(current_block_no=current_block_no, filter_parameter=filter_parameter) + \ self.request_dao.get_approved_expired_request(current_block_no=current_block_no, filter_parameter=filter_parameter) else: filter_parameter.update( {"status": getattr(RFAIStatusCodes, status).value}) tmp_requests_data = self.request_dao.get_request_data_for_given_requester_and_status( filter_parameter=filter_parameter) elif status is None: tmp_requests_data = self.request_dao.get_request_data_for_given_requester_and_status( filter_parameter=filter_parameter) my_request = query_string_parameters.get("my_request", False) requests = [] for record in tmp_requests_data: if my_request and query_string_parameters["requester"] != record[ "requester"]: continue vote_count = self.vote_dao.get_votes_count_for_given_request( request_id=record["request_id"]) stake_count = self.stake_dao.get_stake_count_for_given_request( request_id=record["request_id"]) solution_count = self.solution_dao.get_solution_count_for_given_request( request_id=record["request_id"]) record.update({"vote_count": vote_count["vote_count"]}) record.update({"stake_count": stake_count["stake_count"]}) record.update({"solution_count": solution_count["solution_count"]}) record["created_at"] = str(record["created_at"]) requests.append(record) return requests def get_rfai_summary(self, requester, my_request): request_summary = self.generate_rfai_summary(requester=requester, my_request=my_request) return request_summary def get_vote_details_for_given_request_id(self, request_id): vote_data = self.vote_dao.get_vote_details_for_given_request_id( request_id=request_id) return vote_data def get_stake_details_for_given_request_id(self, request_id): stake_data = self.stake_dao.get_stake_details_for_given_request_id( request_id=request_id) for record in stake_data: record["created_at"] = str(record["created_at"]) return stake_data def get_solution_details_for_given_request_id(self, request_id): solution_data = self.solution_dao.get_solution_details_for_given_request_id( request_id=request_id) for record in solution_data: record["created_at"] = str(record["created_at"]) return solution_data def get_foundation_members(self): foundation_members_data = self.foundation_member_dao.get_foundation_members( ) for record in foundation_members_data: record["status"] = obj_utils.bits_to_integer(record["status"]) record["role"] = obj_utils.bits_to_integer(record["role"]) record["created_at"] = str(record["created_at"]) return foundation_members_data def generate_rfai_summary(self, requester, my_request): filter_parameter = {} current_block_no = obj_blockchain_utils.get_current_block_no() rfai_summary = { "PENDING": len( self.request_dao.get_open_active_request( current_block_no=current_block_no, requester=requester, filter_parameter=filter_parameter)), "INCOMPLETE": len( self.request_dao.get_open_expired_request( current_block_no=current_block_no, filter_parameter=filter_parameter)) + len( self.request_dao.get_approved_expired_request( current_block_no=current_block_no, filter_parameter=filter_parameter)), "ACTIVE": len( self.request_dao.get_approved_active_request( current_block_no=current_block_no, filter_parameter=filter_parameter)), "SOLUTION_VOTE": len( self.request_dao.get_approved_solution_vote_request( current_block_no=current_block_no, filter_parameter=filter_parameter)), "COMPLETED": len( self.request_dao.get_approved_completed_request( current_block_no=current_block_no, filter_parameter=filter_parameter)), "REJECTED": len( self.request_dao. get_request_data_for_given_requester_and_status( filter_parameter={ "status": RFAIStatusCodes.REJECTED.value })), "CLOSED": len( self.request_dao. get_request_data_for_given_requester_and_status( filter_parameter={"status": RFAIStatusCodes.CLOSED.value})) } return rfai_summary
class RFAICreateRequestEventConsumer(RFAIEventConsumer): _connection = Repository(NETWORKS=NETWORK) _request_dao = RequestDAO(_connection) _stake_dao = StakeDAO(_connection) _fund_request_trxn_dao = FundRequestTransactionDAO(_connection) def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port): super().__init__(net_id, ws_provider, ipfs_url, ipfs_port) def on_event(self, event): created_at_in_epoch = self._blockchain_util.get_created_at_for_block( block_no=event["data"]["block_no"]).get("timestamp", None) if created_at_in_epoch is not None: created_at = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(created_at_in_epoch)) event_data = self._get_event_data(event) request_id = event_data['requestId'] requester = event_data['requester'] expiration = event_data['expiration'] amount = event_data['amount'] metadata_hash = self._get_metadata_hash(event_data['documentURI']) self._process_create_request_event(request_id, requester, expiration, amount, metadata_hash, created_at, event) def _process_create_request_event(self, request_id, requester, expiration, amount, metadata_hash, created_at, event): [ found, request_id, requester, total_fund, document_uri, expiration, end_submission, end_evaluation, status, stake_members, submitters ] = self._get_rfai_service_request_by_id(request_id) rfai_metadata = eval(self._get_rfai_metadata_from_ipfs(metadata_hash)) title = rfai_metadata['title'] requester_name = requester description = rfai_metadata['description'] git_hub_link = rfai_metadata['documentURI'] training_data_set_uri = rfai_metadata['training-dataset'] acceptance_criteria = rfai_metadata['acceptance-criteria'] request_actor = '' self._connection.begin_transaction() try: self._request_dao.create_request( request_id=request_id, requester=requester, request_fund=amount, fund_total=total_fund, document_uri=metadata_hash, expiration=expiration, end_submission=end_submission, end_evaluation=end_evaluation, status=status, request_title=title, requester_name=requester_name, description=description, git_hub_link=git_hub_link, training_data_set_uri=training_data_set_uri, acceptance_criteria=acceptance_criteria, request_actor=request_actor, created_at=created_at) self._stake_dao.create_stake(request_id=request_id, stake_member=requester, stake_amount=amount, claim_back_amount=amount, created_at=created_at) self._fund_request_trxn_dao.persist_transaction( stake_member=requester, transaction_hash=event["data"]["transactionHash"], created_at=created_at) self._connection.commit_transaction() except Exception as e: logger.info( f"Transaction Rollback for event {event}. Error::{repr(e)}") self._connection.rollback_transaction() raise e