def test_build_cluster_attributes(config): data_provider = DataServiceProvider compute = OceanCompute(config, data_provider=data_provider) cluster_dict = compute.build_cluster_attributes( "Kubernetes", "http://10.0.0.17/my_cluster") expected_cluster_dict = { "type": "Kubernetes", "url": "http://10.0.0.17/my_cluster" } assert cluster_dict, "Cluster dictionary is None." assert isinstance(cluster_dict, dict), "The cluster is not a dict." assert (cluster_dict == expected_cluster_dict ), "The cluster values are different from the expected ones."
def test_build_container_attributes(config): data_provider = DataServiceProvider compute = OceanCompute(config, data_provider=data_provider) container_dict = compute.build_container_attributes( "node", "best_tag", "entrypoint.exe") expected_container_dict = { "image": "node", "tag": "best_tag", "entrypoint": "entrypoint.exe", } assert container_dict, "Container dictionary is None." assert isinstance(container_dict, dict), "The container is not a dict." assert (container_dict == expected_container_dict ), "The container values are different from the expected ones."
def test_build_service_privacy_attributes_no_trusted_algos(config): data_provider = DataServiceProvider compute = OceanCompute(config=config, data_provider=data_provider) privacy_dict = compute.build_service_privacy_attributes() expected_privacy_dict = { "allowRawAlgorithm": False, "allowAllPublishedAlgorithms": False, "publisherTrustedAlgorithms": [], "allowNetworkAccess": False, } assert privacy_dict assert isinstance(privacy_dict, dict) assert (privacy_dict == expected_privacy_dict ), "The privacy dict is not the expected one."
def trying_algorithm_run(did, wallet, order_tx_id, nonce): load_dotenv(".env") config = Config(os.getenv('config.ini')) print(config.network_url) # config.network_url="https://rinkeby.infura.io/v3/31d95be121a545b688a0e07e4de4d256" ConfigProvider.set_config(config) ocean_auth = OceanAuth("./storage_tokens") token = ocean_auth.get(wallet) print(config.provider_url) ocean_compute = OceanCompute(ocean_auth, config, config.provider_url) job_id = ocean_compute.start(did, wallet, order_tx_id, nonce, algorithm_meta=algo_metadata) return job_id
def test_build_server_attributes(config): data_provider = DataServiceProvider compute = OceanCompute(config, data_provider=data_provider) server_dict = compute.build_server_attributes("test_server_id_123", "test_server_type", 4, 4, "20", "20", 30) expected_server_dict = { "serverId": "test_server_id_123", "serverType": "test_server_type", "cpu": 4, "gpu": 4, "memory": "20", "disk": "20", "maxExecutionTime": 30, } assert server_dict, "Server dictionary is None." assert isinstance(server_dict, dict), "The server is not a dict." assert (server_dict == expected_server_dict ), "The server values are different from the expected ones."
def test_get_service_endpoint(): data_provider = DataServiceProvider options_dict = {"resources": {"provider.url": "http://localhost:8030"}} config = Config(options_dict=options_dict) compute = OceanCompute(config, data_provider) ddo = get_sample_ddo_with_compute_service() compute_service = ddo.get_service(ServiceTypes.CLOUD_COMPUTE) compute_service.service_endpoint = "http://localhost:8030" with patch("ocean_lib.ocean.ocean_compute.resolve_asset") as mock: mock.return_value = ddo service_endpoint = compute._get_service_endpoint(ddo.did) assert service_endpoint, "The service endpoint is None." assert isinstance(service_endpoint, tuple), "The service endpoint is not a tuple." assert (service_endpoint[0] == "GET" ), "The http method of compute status job must be GET." assert (service_endpoint[1] == data_provider.build_compute_endpoint( config.provider_url)[1]), "Different URLs for compute status job."
def test_build_service_privacy_attributes(publisher_ocean_instance): data_provider = DataServiceProvider compute = OceanCompute(config=publisher_ocean_instance.config, data_provider=data_provider) algorithm_ddo = get_sample_algorithm_ddo() with patch("ocean_lib.assets.trusted_algorithms.resolve_asset") as mock: mock.return_value = algorithm_ddo privacy_dict = compute.build_service_privacy_attributes( trusted_algorithms=[algorithm_ddo.did], metadata_cache_uri=publisher_ocean_instance.config. metadata_cache_uri, allow_raw_algorithm=True, allow_all_published_algorithms=True, allow_network_access=True, ) expected_privacy_dict = { "allowRawAlgorithm": True, "allowAllPublishedAlgorithms": True, "publisherTrustedAlgorithms": create_publisher_trusted_algorithms( [algorithm_ddo.did], publisher_ocean_instance.config.metadata_cache_uri), "allowNetworkAccess": True, } assert privacy_dict, "Privacy dictionary is None." assert isinstance(privacy_dict, dict), "Privacy attributes do not form a dictionary." assert (privacy_dict == expected_privacy_dict ), "The privacy dict is not the expected one."
def trying_compute(wallet): load_dotenv(".env") config = Config(os.getenv('config.ini')) print(config.network_url) # config.network_url="https://rinkeby.infura.io/v3/31d95be121a545b688a0e07e4de4d256" ConfigProvider.set_config(config) ocean_auth = OceanAuth("./storage_tokens") token = ocean_auth.get(wallet) # config=ConfigProvider() print(config.provider_url) ocean_compute = OceanCompute(ocean_auth, config, config.provider_url) build_server_attributes = OceanCompute.build_server_attributes( server_id="1", server_type="homemade", cpu=2, gpu=0, memory=2, disk=10, max_run_time=5000) compute_service_cluster_attribute = OceanCompute.build_cluster_attributes( cluster_type="Kubernetes", url="http://10.96.0.1/") compute_service_container_attributes = OceanCompute.build_container_attributes( image="patanae", tag="patanae", entrypoint="python") compute_service_provider_attributes = OceanCompute.build_service_provider_attributes( "forgeeks", description="providecompute", cluster=compute_service_cluster_attribute, containers=compute_service_container_attributes, servers=build_server_attributes) created_compute_service_attributes = OceanCompute.create_compute_service_attributes( creator="hello", provider_attributes=compute_service_provider_attributes, date_published="", timeout=20000) compute_service_descriptor = ocean_compute.create_compute_service_descriptor( created_compute_service_attributes)
def __init__(self, config=None, data_provider=None): """Initialize Ocean class. >> # Make a new Ocean instance >> ocean = Ocean({...}) This class provides the main top-level functions in ocean protocol: * Publish assets metadata and associated services * Each asset is assigned a unique DID and a DID Document (DDO) * The DDO contains the asset's services including the metadata * The DID is registered on-chain with a URL of the metadata store to retrieve the DDO from >> asset = ocean.assets.create(metadata, publisher_wallet) * Discover/Search assets via the current configured metadata store (Aquarius) >> assets_list = ocean.assets.search('search text') An instance of Ocean is parameterized by a `Config` instance. :param config: Config instance :param data_provider: DataServiceProvider instance """ # Configuration information for the market is stored in the Config class # config = Config(filename=config_file, options_dict=config_dict) if not config: try: config = ConfigProvider.get_config() except AssertionError: config = Config(os.getenv(ENV_CONFIG_FILE)) ConfigProvider.set_config(config) if isinstance(config, dict): # fallback to metadataStoreUri cache_key = ("metadataCacheUri" if ("metadataCacheUri" in config) else "metadataStoreUri") aqua_url = config.get( cache_key, config.get("aquarius.url", "http://localhost:5000")) config_dict = { "eth-network": { "network": config.get("network", "") }, "resources": { "aquarius.url": aqua_url, "provider.url": config.get("providerUri", "http://localhost:8030"), }, } config = Config(options_dict=config_dict) ConfigProvider.set_config(config) self._config = config ContractHandler.set_artifacts_path(self._config.artifacts_path) Web3Provider.init_web3( provider=get_web3_connection_provider(self._config.network_url)) self._web3 = Web3Provider.get_web3() if not data_provider: data_provider = DataServiceProvider network = Web3Helper.get_network_name() addresses = get_contracts_addresses(network, self._config) self.assets = OceanAssets( self._config, data_provider, addresses.get(MetadataContract.CONTRACT_NAME)) self.services = OceanServices() self.auth = OceanAuth(self._config.storage_path) self.compute = OceanCompute(self.auth, self._config, data_provider) ocean_address = get_ocean_token_address(network) self.pool = OceanPool(ocean_address, get_bfactory_address(network)) self.exchange = OceanExchange( ocean_address, FixedRateExchange.configured_address( network or Web3Helper.get_network_name(), ConfigProvider.get_config().address_file, ), self.config, ) logger.debug("Ocean instance initialized: ")
def test_build_service_provider_attributes(config): data_provider = DataServiceProvider compute = OceanCompute(config=config, data_provider=data_provider) cluster_dict = compute.build_cluster_attributes( "Kubernetes", "http://10.0.0.17/my_cluster") expected_cluster_dict = { "type": "Kubernetes", "url": "http://10.0.0.17/my_cluster" } assert cluster_dict, "Cluster dictionary is None." assert isinstance(cluster_dict, dict), "The cluster is not a dict." assert (cluster_dict == expected_cluster_dict ), "The cluster values are different from the expected ones." container_dict = compute.build_container_attributes( "node", "best_tag", "entrypoint.exe") expected_container_dict = { "image": "node", "tag": "best_tag", "entrypoint": "entrypoint.exe", } assert container_dict, "Container dictionary is None." assert isinstance(container_dict, dict), "The container is not a dict." assert (container_dict == expected_container_dict ), "The container values are different from the expected ones." server_dict = compute.build_server_attributes("test_server_id_123", "test_server_type", 4, 4, "20", "20", 30) expected_server_dict = { "serverId": "test_server_id_123", "serverType": "test_server_type", "cpu": 4, "gpu": 4, "memory": "20", "disk": "20", "maxExecutionTime": 30, } assert server_dict, "Server dictionary is None." assert isinstance(server_dict, dict), "The server is not a dict." assert (server_dict == expected_server_dict ), "The server values are different from the expected ones." service_provider_dict = compute.build_service_provider_attributes( "My Provider", "Unit testing", cluster_dict, container_dict, server_dict) expected_service_provider_dict = { "type": "My Provider", "description": "Unit testing", "environment": { "cluster": cluster_dict, "supportedContainers": container_dict, "supportedServers": server_dict, }, } assert service_provider_dict, "Provider server is None." assert isinstance(service_provider_dict, dict), "Provider server is not a dict." assert (service_provider_dict == expected_service_provider_dict ), "The service provider is not the expected one."
def test_create_compute_service_attributes(publisher_ocean_instance): publisher = get_publisher_wallet() data_provider = DataServiceProvider config = publisher_ocean_instance.config compute = OceanCompute(config=config, data_provider=data_provider) algorithm_ddo = get_sample_algorithm_ddo() with patch("ocean_lib.assets.trusted_algorithms.resolve_asset") as mock: mock.return_value = algorithm_ddo privacy_dict = compute.build_service_privacy_attributes( trusted_algorithms=[algorithm_ddo.did], metadata_cache_uri=config.metadata_cache_uri, allow_raw_algorithm=True, allow_all_published_algorithms=True, allow_network_access=True, ) expected_privacy_dict = { "allowRawAlgorithm": True, "allowAllPublishedAlgorithms": True, "publisherTrustedAlgorithms": create_publisher_trusted_algorithms([algorithm_ddo.did], config.metadata_cache_uri), "allowNetworkAccess": True, } assert privacy_dict, "Privacy dictionary is None." assert isinstance(privacy_dict, dict), "Privacy attributes do not form a dictionary." assert (privacy_dict == expected_privacy_dict ), "The privacy dict is not the expected one." cluster_dict = compute.build_cluster_attributes( "Kubernetes", "http://10.0.0.17/my_cluster") expected_cluster_dict = { "type": "Kubernetes", "url": "http://10.0.0.17/my_cluster" } assert cluster_dict, "Cluster dictionary is None." assert isinstance(cluster_dict, dict), "The cluster is not a dict." assert (cluster_dict == expected_cluster_dict ), "The cluster values are different from the expected ones." container_dict = compute.build_container_attributes( "node", "best_tag", "entrypoint.exe") expected_container_dict = { "image": "node", "tag": "best_tag", "entrypoint": "entrypoint.exe", } assert container_dict, "Container dictionary is None." assert isinstance(container_dict, dict), "The container is not a dict." assert (container_dict == expected_container_dict ), "The container values are different from the expected ones." server_dict = compute.build_server_attributes("test_server_id_123", "test_server_type", 4, 4, "20", "20", 30) expected_server_dict = { "serverId": "test_server_id_123", "serverType": "test_server_type", "cpu": 4, "gpu": 4, "memory": "20", "disk": "20", "maxExecutionTime": 30, } assert server_dict, "Server dictionary is None." assert isinstance(server_dict, dict), "The server is not a dict." assert (server_dict == expected_server_dict ), "The server values are different from the expected ones." service_provider_dict = compute.build_service_provider_attributes( "My Provider", "Unit testing", cluster_dict, container_dict, server_dict) expected_service_provider_dict = { "type": "My Provider", "description": "Unit testing", "environment": { "cluster": cluster_dict, "supportedContainers": container_dict, "supportedServers": server_dict, }, } assert service_provider_dict, "Provider server is None." assert isinstance(service_provider_dict, dict), "Provider server is not a dict." assert (service_provider_dict == expected_service_provider_dict ), "The service provider is not the expected one." compute_attributes = compute.create_compute_service_attributes( 30, publisher.address, (datetime.utcnow().replace(microsecond=0).isoformat() + "Z"), service_provider_dict, privacy_dict, ) expected_compute_attributes = { "main": { "name": "dataAssetComputingServiceAgreement", "creator": publisher.address, "datePublished": datetime.utcnow().replace(microsecond=0).isoformat() + "Z", "cost": 1.0, "timeout": 30, "provider": service_provider_dict, "privacy": privacy_dict, } } assert compute_attributes, "Compute attributes do not exist." assert isinstance(compute_attributes, dict), "compute_attributes is not a dict." assert (compute_attributes == expected_compute_attributes ), "The compute attributes are not the expected ones."
def __init__( self, config: Union[Dict, Config], data_provider: Optional[Type] = None ) -> None: """Initialize Ocean class. Usage: Make a new Ocean instance `ocean = Ocean({...})` This class provides the main top-level functions in ocean protocol: 1. Publish assets metadata and associated services - Each asset is assigned a unique DID and a DID Document (DDO) - The DDO contains the asset's services including the metadata - The DID is registered on-chain with a URL of the metadata store to retrieve the DDO from `asset = ocean.assets.create(metadata, publisher_wallet)` 2. Discover/Search assets via the current configured metadata store (Aquarius) - Usage: `assets_list = ocean.assets.search('search text')` An instance of Ocean is parameterized by a `Config` instance. :param config: `Config` instance :param data_provider: `DataServiceProvider` instance """ if isinstance(config, dict): # fallback to metadataStoreUri cache_key = ( "metadataCacheUri" if ("metadataCacheUri" in config) else "metadataStoreUri" ) metadata_cache_uri = config.get( cache_key, config.get("metadata_cache_uri", "http://localhost:5000") ) config_dict = { "eth-network": {"network": config.get("network", "")}, "resources": { "metadata_cache_uri": metadata_cache_uri, "provider.url": config.get("providerUri", "http://localhost:8030"), }, } config = Config(options_dict=config_dict) self.config = config self.web3 = get_web3(self.config.network_url) if not data_provider: data_provider = DataServiceProvider network = get_network_name(web3=self.web3) addresses = get_contracts_addresses(self.config.address_file, network) self.assets = OceanAssets( self.config, self.web3, data_provider, addresses.get(MetadataContract.CONTRACT_NAME), ) self.compute = OceanCompute(self.config, data_provider) ocean_address = get_ocean_token_address(self.config.address_file, network) self.pool = OceanPool( self.web3, ocean_address, get_bfactory_address(self.config.address_file, network), get_dtfactory_address(self.config.address_file, network), ) self.exchange = OceanExchange( self.web3, ocean_address, FixedRateExchange.configured_address(network, self.config.address_file), self.config, ) logger.debug("Ocean instance initialized: ")