class MPEEventConsumer(EventConsumer):
    _mpe_repository = MPERepository(Repository(NETWORK_ID, NETWORKS=NETWORKS))

    def __init__(self, ws_provider):
        self.blockchain_util = BlockChainUtil("WS_PROVIDER", ws_provider)

    def on_event(self, event):
        net_id = NETWORK_ID
        base_contract_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', '..', 'node_modules',
                         'singularitynet-platform-contracts'))
        mpe_contract = self.blockchain_util.get_contract_instance(
            base_contract_path, "MPE", net_id)

        logger.info(f"processing mpe event {event}")
        event_name = event["name"]
        event_data = event["data"]
        mpe_data = eval(event_data['json_str'])
        channel_id = int(mpe_data['channelId'])

        if event_name == 'ChannelOpen':
            self._mpe_repository.create_channel(mpe_data)
        else:

            channel_data = mpe_contract.functions.channels(channel_id).call()
            group_id = base64.b64encode(channel_data[4]).decode('utf8')
            self._mpe_repository.update_channel(channel_id=channel_id,
                                                group_id=group_id,
                                                channel_data=channel_data)
class RFAIEventConsumer(EventConsumer):
    def __init__(self, net_id, ws_provider, ipfs_url, ipfs_port):
        self._ipfs_util = IPFSUtil(ipfs_url, ipfs_port)
        self._blockchain_util = BlockChainUtil("WS_PROVIDER", ws_provider)
        self._net_id = net_id

    def on_event(self, event):
        pass

    def _get_rfai_contract(self):
        base_contract_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', '..', 'node_modules',
                         'singularitynet-rfai-contracts'))
        rfai_contract = self._blockchain_util.get_contract_instance(
            base_contract_path, "RFAI", self._net_id)

        return rfai_contract

    def _get_rfai_metadata_from_ipfs(self, ipfs_hash):
        return self._ipfs_util.read_file_from_ipfs(ipfs_hash)

    def _get_event_data(self, event):
        return eval(event['data']['json_str'])

    def _get_metadata_hash(self, metadata_uri):
        return metadata_uri.decode("utf-8")

    def _get_rfai_service_request_by_id(self, request_id):
        rfai_contract = self._get_rfai_contract()
        result = self._blockchain_util.call_contract_function(
            rfai_contract, "getServiceRequestById", request_id)
        return result
Exemplo n.º 3
0
class ServiceEventConsumer(EventConsumer):
    _connection = Repository(NETWORK_ID, NETWORKS=NETWORKS)
    _service_repository = ServiceRepository(_connection)

    def __init__(self, ws_provider, ipfs_url, ipfs_port):
        self._blockchain_util = BlockChainUtil("WS_PROVIDER", ws_provider)
        self._s3_util = S3Util(S3_BUCKET_ACCESS_KEY, S3_BUCKET_SECRET_KEY)
        self._ipfs_util = IPFSUtil(ipfs_url, ipfs_port)

    def on_event(self, event):
        pass

    def _fetch_tags(self, registry_contract, org_id_hex, service_id_hex):
        tags_data = registry_contract.functions.getServiceRegistrationById(
            org_id_hex, service_id_hex).call()
        return tags_data

    def _get_org_id_from_event(self, event):
        event_data = event['data']
        service_data = eval(event_data['json_str'])
        org_id_bytes = service_data['orgId']
        org_id = Web3.toText(org_id_bytes).rstrip("\x00")
        return org_id

    def _get_service_id_from_event(self, event):
        event_data = event['data']
        service_data = eval(event_data['json_str'])
        service_id_bytes = service_data['serviceId']
        service_id = Web3.toText(service_id_bytes).rstrip("\x00")
        return service_id

    def _get_metadata_uri_from_event(self, event):
        event_data = event['data']
        service_data = eval(event_data['json_str'])
        metadata_uri = Web3.toText(
            service_data['metadataURI'])[7:].rstrip("\u0000")
        return metadata_uri

    def _get_registry_contract(self):
        net_id = NETWORK_ID
        base_contract_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', '..', 'node_modules',
                         'singularitynet-platform-contracts'))
        registry_contract = self._blockchain_util.get_contract_instance(
            base_contract_path, "REGISTRY", net_id)
        return registry_contract

    def _get_service_details_from_blockchain(self, event):
        logger.info(f"processing service event {event}")

        registry_contract = self._get_registry_contract()
        org_id = self._get_org_id_from_event(event)
        service_id = self._get_service_id_from_event(event)

        tags_data = self._fetch_tags(registry_contract=registry_contract,
                                     org_id_hex=org_id.encode("utf-8"),
                                     service_id_hex=service_id.encode("utf-8"))

        return org_id, service_id, tags_data
Exemplo n.º 4
0
class BlockchainEventProducer(EventProducer):
    def __init__(
        self,
        ws_provider,
        repository=None,
    ):
        self._blockchain_util = BlockChainUtil("WS_PROVIDER", ws_provider)
        self._event_repository = EventRepository(repository)

    def _get_base_contract_path(self):
        pass

    def _get_events_from_blockchain(self, start_block_number, end_block_number,
                                    net_id):

        base_contract_path = self._get_base_contract_path()
        contract = self._blockchain_util.get_contract_instance(
            base_contract_path, self._contract_name, net_id=net_id)
        contract_events = contract.events
        all_blockchain_events = []

        for attributes in contract_events.abi:
            if attributes['type'] == 'event':
                event_name = attributes['name']
                event_object = getattr(contract.events, event_name)
                blockchain_events = event_object.createFilter(
                    fromBlock=start_block_number,
                    toBlock=end_block_number).get_all_entries()
                all_blockchain_events.extend(blockchain_events)

        return all_blockchain_events

    def _produce_contract_events(self, start_block_number, end_block_number,
                                 net_id):

        events = self._get_events_from_blockchain(start_block_number,
                                                  end_block_number, net_id)
        logger.info(f"read no of events {len(events)}")
        return events

    def _get_end_block_number(self, last_processed_block_number, batch_limit):
        current_block_number = self._blockchain_util.get_current_block_no()
        end_block_number = last_processed_block_number + batch_limit
        if current_block_number <= end_block_number:
            end_block_number = current_block_number

        return end_block_number

    def produce_event(self, net_id):
        pass
class OrganizationEventConsumer(EventConsumer):
    _connection = Repository(NETWORK_ID, NETWORKS=NETWORKS)
    _organization_repository = OrganizationRepository(_connection)
    _service_repository = ServiceRepository(_connection)

    def __init__(self, ws_provider, ipfs_url, ipfs_port):
        self._ipfs_util = IPFSUtil(ipfs_url, ipfs_port)
        self._blockchain_util = BlockChainUtil("WS_PROVIDER", ws_provider)
        self._s3_util = S3Util(S3_BUCKET_ACCESS_KEY, S3_BUCKET_SECRET_KEY)

    def on_event(self, event):
        pass

    def _push_asset_to_s3_using_hash(self, hash, org_id, service_id):
        io_bytes = self._ipfs_util.read_bytesio_from_ipfs(
            hash.lstrip("ipfs://"))
        filename = hash.split("/")[1]
        if service_id:
            s3_filename = ASSETS_PREFIX + "/" + org_id + "/" + service_id + "/" + filename
        else:
            s3_filename = ASSETS_PREFIX + "/" + org_id + "/" + filename

        new_url = self._s3_util.push_io_bytes_to_s3(s3_filename,
                                                    ASSETS_BUCKET_NAME,
                                                    io_bytes)
        return new_url

    def _get_new_assets_url(self, org_id, new_ipfs_data):
        new_assets_hash = new_ipfs_data.get('assets', {})
        existing_assets_hash = {}
        existing_assets_url = {}

        existing_organization = self._organization_repository.get_organization(
            org_id)
        if existing_organization:
            existing_assets_hash = json.loads(
                existing_organization["assets_hash"])
            existing_assets_url = json.loads(
                existing_organization["org_assets_url"])
        new_assets_url_mapping = self._comapre_assets_and_push_to_s3(
            existing_assets_hash, new_assets_hash, existing_assets_url, org_id,
            "")
        return new_assets_url_mapping

    def _get_org_id_from_event(self, event):
        event_org_data = eval(event['data']['json_str'])
        org_id_bytes = event_org_data['orgId']
        org_id = Web3.toText(org_id_bytes).rstrip("\x00")
        return org_id

    def _get_registry_contract(self):
        net_id = NETWORK_ID
        base_contract_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', '..', 'node_modules',
                         'singularitynet-platform-contracts'))
        registry_contract = self._blockchain_util.get_contract_instance(
            base_contract_path, "REGISTRY", net_id)

        return registry_contract

    def _get_org_details_from_blockchain(self, event):
        logger.info(f"processing org event {event}")

        registry_contract = self._get_registry_contract()
        org_id = self._get_org_id_from_event(event)

        blockchain_org_data = registry_contract.functions.getOrganizationById(
            org_id.encode('utf-8')).call()
        org_metadata_uri = Web3.toText(
            blockchain_org_data[2]).rstrip("\x00").lstrip("ipfs://")
        ipfs_org_metadata = self._ipfs_util.read_file_from_ipfs(
            org_metadata_uri)

        return org_id, blockchain_org_data, ipfs_org_metadata, org_metadata_uri
Exemplo n.º 6
0
class TokenStakeEventConsumer(object):
    def __init__(self, http_provider, net_id):
        self._blockchain_util = BlockChainUtil("HTTP_PROVIDER", http_provider)
        self._net_id = net_id

    def on_event(self, event):
        pass

    def _get_token_stake_contract(self):
        base_contract_path = os.path.abspath(
            os.path.join(os.path.dirname(__file__), '..', '..', 'node_modules',
                         'singularitynet-stake-contracts'))
        # logger.info(f"base_contract_path: {base_contract_path}")
        token_stake_contract = self._blockchain_util.get_contract_instance(
            base_contract_path, "TokenStake", self._net_id)

        return token_stake_contract

    @staticmethod
    def _get_event_data(event):
        return eval(event['data']['json_str'])

    @staticmethod
    def _get_metadata_hash(metadata_uri):
        return metadata_uri.decode("utf-8")

    def get_list_of_stake_holders(self):
        token_stake_contract = self._get_token_stake_contract()
        total_stake = self._blockchain_util.call_contract_function(
            token_stake_contract, "getStakeHolders", [])
        return total_stake

    def get_stake_window_total_stake(self):
        token_stake_contract = self._get_token_stake_contract()
        total_stake = self._blockchain_util.call_contract_function(
            token_stake_contract, "windowTotalStake", [])
        return total_stake

    def get_latest_stake_window(self):
        token_stake_contract = self._get_token_stake_contract()
        stake_window_id = self._blockchain_util.call_contract_function(
            token_stake_contract, "currentStakeMapIndex", [])
        return stake_window_id

    def get_stake_window_by_stake_index(self, stake_index):
        token_stake_contract = self._get_token_stake_contract()
        stake_window_data = self._blockchain_util.call_contract_function(
            token_stake_contract, "stakeMap", [stake_index])
        return stake_window_data

    def get_stake_holder_for_given_stake_index_and_address(
            self, stake_index, staker):
        token_stake_contract = self._get_token_stake_contract()
        stake_holder_data = self._blockchain_util.call_contract_function(
            token_stake_contract, "getStakeInfo", [stake_index, staker])
        return stake_holder_data

    def _add_stake_transaction(self, block_no, blockchain_id, transaction_hash,
                               event_name, event_data, staker):
        transaction_date_in_epoch = self._blockchain_util.get_created_at_for_block(block_no=block_no). \
            get("timestamp", None)
        if transaction_date_in_epoch is not None:
            transaction_date = time.strftime(
                '%Y-%m-%d %H:%M:%S', time.localtime(transaction_date_in_epoch))
        stake_transaction = StakeTransaction(blockchain_id=blockchain_id,
                                             transaction_hash=transaction_hash,
                                             event=event_name,
                                             event_data=event_data,
                                             block_no=block_no,
                                             transaction_date=transaction_date,
                                             staker=staker)
        stake_transaction_repo.add_stake_transaction(stake_transaction)