コード例 #1
0
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
コード例 #2
0
class RegistryBlockChainUtil:
    def __init__(self, env_type):
        self.__env_type = env_type
        if env_type == EnvironmentType.MAIN.value:
            self.__network_id = NETWORK_ID
            self.__contract_path = REG_CNTRCT_PATH
            self.__executor_address = ""
            self.__contract_address_path = REG_ADDR_PATH
            self.__blockchain_util = BlockChainUtil(
                provider_type="HTTP_PROVIDER",
                provider=NETWORKS[self.__network_id]['http_provider'])

        elif env_type == EnvironmentType.TEST.value:
            self.__contract_path = TEST_REG_CNTRCT_PATH
            self.__contract_address_path = TEST_REG_ADDR_PATH
            self.__executor_address = BLOCKCHAIN_TEST_ENV["publisher_address"]
            self.__network_id = BLOCKCHAIN_TEST_ENV["network_id"]
            self.__blockchain_util = BlockChainUtil(
                provider_type="HTTP_PROVIDER",
                provider=BLOCKCHAIN_TEST_ENV['http_provider'])
        else:
            raise MethodNotImplemented()

    def is_org_published(self, org_id):
        contract = self.__blockchain_util.load_contract(
            path=self.__contract_path)
        contract_address = self.__blockchain_util.read_contract_address(
            net_id=self.__network_id,
            path=self.__contract_address_path,
            key='address')
        return self.__organization_exist_in_blockchain(org_id, contract,
                                                       contract_address)

    def __organization_exist_in_blockchain(self, org_id, contract,
                                           contract_address):
        method_name = "getOrganizationById"
        positional_inputs = (web3.Web3.toHex(text=org_id), )
        contract = self.__blockchain_util.contract_instance(
            contract_abi=contract, address=contract_address)

        org_data = self.__blockchain_util.call_contract_function(
            contract=contract,
            contract_function=method_name,
            positional_inputs=positional_inputs)

        logger.info(f"Org data :: {org_data}")
        org_found = org_data[0]
        return org_found

    def __generate_blockchain_transaction_for_test_environment(
            self, *positional_inputs, method_name):
        transaction_object = self.__blockchain_util.create_transaction_object(
            *positional_inputs,
            method_name=method_name,
            address=self.__executor_address,
            contract_path=self.__contract_path,
            contract_address_path=self.__contract_address_path,
            net_id=self.__network_id)
        return transaction_object

    def __make_trasaction(self, *positional_inputs, method_name):
        if self.__env_type == EnvironmentType.TEST.value:
            executor_key = BotoUtils(REGION_NAME).get_ssm_parameter(
                BLOCKCHAIN_TEST_ENV["publisher_private_key"])
            transaction_object = self.__generate_blockchain_transaction_for_test_environment(
                *positional_inputs, method_name=method_name)
        else:
            raise EnvironmentNotFoundException()
        raw_transaction = self.__blockchain_util.sign_transaction_with_private_key(
            transaction_object=transaction_object, private_key=executor_key)
        transaction_hash = self.__blockchain_util.process_raw_transaction(
            raw_transaction=raw_transaction)
        return transaction_hash

    def __update_organization_in_blockchain(self, org_id, metadata_uri):
        method_name = "changeOrganizationMetadataURI"
        positional_inputs = (web3.Web3.toHex(text=org_id),
                             ipfsuri_to_bytesuri(metadata_uri))
        transaction_hash = self.__make_trasaction(*positional_inputs,
                                                  method_name=method_name)
        logger.info(
            f"transaction hash {transaction_hash} generated while registering organization "
            f"{org_id} in {self.__env_type} blockchain environment.")
        return transaction_hash

    def __register_organization_in_blockchain(self, org_id, metadata_uri,
                                              members):
        method_name = "createOrganization"
        positional_inputs = (web3.Web3.toHex(text=org_id),
                             ipfsuri_to_bytesuri(metadata_uri), members)
        transaction_hash = self.__make_trasaction(*positional_inputs,
                                                  method_name=method_name)
        logger.info(
            f"transaction hash {transaction_hash} generated while registering organization "
            f"{org_id} in {self.__env_type} blockchain environment.")
        return transaction_hash

    def publish_organization_to_test_network(self, organization):
        metadata_uri = organization.metadata_ipfs_uri
        members = []
        org_id = organization.id
        if self.__env_type == EnvironmentType.TEST.value:
            if self.is_org_published(org_id):
                return self.__update_organization_in_blockchain(
                    org_id, metadata_uri)
            else:
                return self.__register_organization_in_blockchain(
                    org_id, metadata_uri, members)
        else:
            raise EnvironmentNotFoundException()

    def __service_exist_in_blockchain(self, org_id, service_id, contract,
                                      contract_address):
        method_name = "getServiceRegistrationById"
        positional_inputs = (web3.Web3.toHex(text=org_id),
                             web3.Web3.toHex(text=service_id))
        contract = self.__blockchain_util.contract_instance(
            contract_abi=contract, address=contract_address)
        service_data = self.__blockchain_util.call_contract_function(
            contract=contract,
            contract_function=method_name,
            positional_inputs=positional_inputs)
        logger.info(f"Services :: {service_data}")
        service_found = service_data[0]
        return service_found

    def update_service_in_blockchain(self, org_id, service_id, metadata_uri):
        method_name = "updateServiceRegistration"
        positional_inputs = (web3.Web3.toHex(text=org_id),
                             web3.Web3.toHex(text=service_id),
                             ipfsuri_to_bytesuri(metadata_uri))
        transaction_hash = self.__make_trasaction(*positional_inputs,
                                                  method_name=method_name)
        logger.info(
            f"transaction hash {transaction_hash} generated while "
            f"updating service {service_id} in {self.__env_type} blockchain environment."
        )
        return transaction_hash

    def register_service_in_blockchain(self, org_id, service_id, metadata_uri,
                                       tags):
        method_name = "createServiceRegistration"
        positional_inputs = (web3.Web3.toHex(text=org_id),
                             web3.Web3.toHex(text=service_id),
                             ipfsuri_to_bytesuri(metadata_uri),
                             [web3.Web3.toHex(text=tag) for tag in tags])
        transaction_hash = self.__make_trasaction(*positional_inputs,
                                                  method_name=method_name)
        logger.info(
            f"transaction hash {transaction_hash} generated while registering service {service_id} "
            f"in {self.__env_type} blockchain environment.")
        return transaction_hash

    def is_service_published(self, org_id, service_id):
        contract = self.__blockchain_util.load_contract(
            path=self.__contract_path)
        contract_address = self.__blockchain_util.read_contract_address(
            net_id=self.__network_id,
            path=self.__contract_address_path,
            key='address')
        return self.__service_exist_in_blockchain(org_id, service_id, contract,
                                                  contract_address)

    def register_or_update_service_in_blockchain(self, org_id, service_id,
                                                 metadata_uri, tags):
        if not self.is_org_published(org_id=org_id):
            raise OrganizationNotFoundException()

        if self.is_service_published(org_id=org_id, service_id=service_id):
            transaction_hash = self.update_service_in_blockchain(
                org_id, service_id, metadata_uri)
        else:
            transaction_hash = self.register_service_in_blockchain(
                org_id, service_id, metadata_uri, tags)
        return transaction_hash
コード例 #3
0
class RegistryBlockChainUtil:
    def __init__(self, env_type):
        self.__env_type = env_type
        if env_type == EnvironmentType.MAIN.value:
            self.__network_id = NETWORK_ID
            self.__contract_path = REG_CNTRCT_PATH
            self.__executor_address = ""
            self.__contract_address_path = REG_ADDR_PATH
            self.__blockchain_util = BlockChainUtil(
                provider_type="HTTP_PROVIDER",
                provider=NETWORKS[self.__network_id]['http_provider'])
        else:
            raise MethodNotImplemented()

    def is_org_published(self, org_id):
        contract = self.__blockchain_util.load_contract(
            path=self.__contract_path)
        contract_address = self.__blockchain_util.read_contract_address(
            net_id=self.__network_id,
            path=self.__contract_address_path,
            key='address')
        return self.__organization_exist_in_blockchain(org_id, contract,
                                                       contract_address)

    def __organization_exist_in_blockchain(self, org_id, contract,
                                           contract_address):
        method_name = "getOrganizationById"
        positional_inputs = (web3.Web3.toHex(text=org_id), )
        contract = self.__blockchain_util.contract_instance(
            contract_abi=contract, address=contract_address)

        org_data = self.__blockchain_util.call_contract_function(
            contract=contract,
            contract_function=method_name,
            positional_inputs=positional_inputs)

        logger.info(f"Org data :: {org_data}")
        org_found = org_data[0]
        return org_found

    def __service_exist_in_blockchain(self, org_id, service_id, contract,
                                      contract_address):
        method_name = "getServiceRegistrationById"
        positional_inputs = (web3.Web3.toHex(text=org_id),
                             web3.Web3.toHex(text=service_id))
        contract = self.__blockchain_util.contract_instance(
            contract_abi=contract, address=contract_address)
        service_data = self.__blockchain_util.call_contract_function(
            contract=contract,
            contract_function=method_name,
            positional_inputs=positional_inputs)
        logger.info(f"Services :: {service_data}")
        service_found = service_data[0]
        return service_found

    def is_service_published(self, org_id, service_id):
        contract = self.__blockchain_util.load_contract(
            path=self.__contract_path)
        contract_address = self.__blockchain_util.read_contract_address(
            net_id=self.__network_id,
            path=self.__contract_address_path,
            key='address')
        return self.__service_exist_in_blockchain(org_id, service_id, contract,
                                                  contract_address)

    def register_or_update_service_in_blockchain(self, org_id, service_id,
                                                 metadata_uri):
        if not self.is_org_published(org_id=org_id):
            raise OrganizationNotFoundException()

        if self.is_service_published(org_id=org_id, service_id=service_id):
            transaction_hash = self.update_service_in_blockchain(
                org_id, service_id, metadata_uri)
        else:
            transaction_hash = self.register_service_in_blockchain(
                org_id, service_id, metadata_uri)
        return transaction_hash
コード例 #4
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)