def test_address_file(monkeypatch): """Tests the Config.address_file property.""" # Test default value when ADDRESS_FILE envvar and address.file config option not set ENV_ADDRESS_FILE = environ_names_and_sections[NAME_ADDRESS_FILE][0] monkeypatch.delenv(ENV_ADDRESS_FILE) config_text_empty = "" config = Config(text=config_text_empty) assert config.address_file.endswith("site-packages/artifacts/address.json") # Test when address.file config option is set config_text = """ [eth-network] address.file = custom/address.json """ config = Config(text=config_text) assert config.address_file.endswith("custom/address.json") # Test when ADDRESS_FILE envvar is set monkeypatch.setenv(ENV_ADDRESS_FILE, "another/custom/address.json") config = Config() assert config.address_file.endswith("another/custom/address.json") # Test when both ADDRESS_FILE envvar and address.file config option are set config = Config(text=config_text) assert config.address_file.endswith("another/custom/address.json")
def test_metadata_cache_uri_set_via_env_vars(monkeypatch, caplog): """Tests the metadata_cache_uri property fallback logic when set via an environment variable""" ENV_METADATA_CACHE_URI = environ_names_and_sections[ NAME_METADATA_CACHE_URI][0] ENV_AQUARIUS_URL = deprecated_environ_names[NAME_AQUARIUS_URL][0] monkeypatch.delenv(ENV_METADATA_CACHE_URI, raising=False) monkeypatch.delenv(ENV_AQUARIUS_URL, raising=False) config = Config() metadata_cache_uri = config.metadata_cache_uri assert metadata_cache_uri == "https://aquarius.marketplace.oceanprotocol.com" monkeypatch.setenv(ENV_METADATA_CACHE_URI, "https://custom-aqua.uri") config = Config() assert config.metadata_cache_uri == "https://custom-aqua.uri" monkeypatch.setenv(ENV_AQUARIUS_URL, "https://another-aqua.url") with pytest.raises(ValueError): config = Config() monkeypatch.delenv(ENV_METADATA_CACHE_URI) config = Config() assert config.metadata_cache_uri == "https://another-aqua.url" assert ( "Config: AQUARIUS_URL envvar is deprecated. Use METADATA_CACHE_URI instead." in caplog.text)
def initialize(private_key): load_dotenv(".env") config = Config(os.getenv('config.ini')) config = Config(os.getenv('config.ini')) print(config.network_url) # config.network_url="https://rinkeby.infura.io/v3/31d95be121a545b688a0e07e4de4d256" ConfigProvider.set_config(config) Web3Provider.init_web3( provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path) ocean = Ocean() wallet = Wallet(ocean.web3, private_key=private_key) return ocean, wallet
def publish_asset(metadata, publisher_wallet): ocean = Ocean(config=Config(options_dict=get_config_dict())) # create compute service compute_descriptor = build_compute_descriptor(ocean, publisher_wallet.address) # create asset DDO and datatoken try: asset = ocean.assets.create(metadata, publisher_wallet, [compute_descriptor], dt_name='Dataset with Compute', dt_symbol='DT-Compute') print(f'Dataset asset created successfully: did={asset.did}, datatoken={asset.data_token_address}') except Exception as e: print(f'Publishing asset failed: {e}') return None, None dt = DataToken(asset.data_token_address) txid = dt.mint_tokens(publisher_wallet.address, 100, publisher_wallet) receipt = dt.get_tx_receipt(txid) assert receipt and receipt.status == 1, f'datatoken mint failed: tx={txid}, txReceipt={receipt}' # Create datatoken liquidity pool for the new asset pool = ocean.pool.create(asset.data_token_address, 50, 50, publisher_wallet, 5) print(f'datatoken liquidity pool was created at address {pool.address}') # Now the asset can be discovered and consumed dt_cost = ocean.pool.calcInGivenOut(pool.address, ocean.OCEAN_address, asset.data_token_address, 1.0) print(f'Asset {asset.did} can now be purchased from pool @{pool.address} ' f'at the price of {dt_cost} OCEAN tokens.') return asset, pool
def run_events_monitor(): setup_logging() logger.info('EventsMonitor: preparing') required_env_vars = ['EVENTS_RPC', 'CONFIG_FILE'] for envvar in required_env_vars: if not os.getenv(envvar): raise AssertionError( f'env var {envvar} is missing, make sure to set the following ' f'environment variables before starting the events monitor: {required_env_vars}' ) network_rpc = os.environ.get('EVENTS_RPC', 'http:127.0.0.1:8545') config_file = os.getenv('CONFIG_FILE', 'config.ini') logger.info( f'EventsMonitor: starting with the following values: rpc={network_rpc}' ) ConfigProvider.set_config(Config(config_file)) from ocean_lib.ocean.util import get_web3_connection_provider Web3Provider.init_web3(provider=get_web3_connection_provider(network_rpc)) ContractHandler.set_artifacts_path(get_artifacts_path()) if get_network_name().lower() == 'rinkeby': from web3.middleware import geth_poa_middleware Web3Provider.get_web3().middleware_stack.inject(geth_poa_middleware, layer=0) monitor = EventsMonitor(Web3Provider.get_web3(), config_file) monitor.start_events_monitor() logger.info(f'EventsMonitor: started') while True: time.sleep(5)
def init_components(config=None): if config is None: config = Config(os.getenv(ENV_CONFIG_FILE)) ConfigProvider.set_config(config) Web3Provider.init_web3(provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path)
def main(did, pool_address, order_tx_id=None): ocean = Ocean(config=Config(options_dict=get_config_dict())) publisher = Wallet(ocean.web3, private_key='0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58') # 0xe2DD09d719Da89e5a3D0F2549c7E24566e947260 #consumer = Wallet(ocean.web3, private_key='0x9bf5d7e4978ed5206f760e6daded34d657572bd49fa5b3fe885679329fb16b16') # 0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0 publisher_wallet = Wallet(ocean.web3, private_key=os.getenv('Publisher_Key')) #addr: 0xc966Ba2a41888B6B4c5273323075B98E27B9F364 consumer = Wallet(ocean.web3, private_key=os.getenv('Consumer_Key')) #addr: 0xEF5dc33A53DD2ED3F670B53F07cEc5ADD4D80504 if not (did and pool_address): metadata_file = './examples/data/metadata.json' with open(metadata_file) as f: metadata = json.load(f) asset, pool = publish_asset(metadata, publisher) #Dataset asset created successfully: did=did:op:784Cc17176533cc962cf659B9f49349ba6F9df3b, datatoken=0x784Cc17176533cc962cf659B9f49349ba6F9df3b #pool_address = 0x3490DDd035B2e1DA30Af09AB6090Bf71fdb94898 else: asset = ocean.assets.resolve(did) pool = BPool(pool_address) if not asset: print(f'publish asset failed, cannot continue with running compute.') return print(f'Requesting compute using asset {asset.did} and pool {pool.address}') algo_file = './examples/data/algorithm.py' order_tx_id= job_id, status = run_compute(asset.did, consumer, algo_file, pool.address, order_tx_id) print(f'Compute started on asset {asset.did}: job_id={job_id}, status={status}')
def publish_asset(metadata, publisher_wallet): ocean = Ocean(config=Config(options_dict=get_config_dict())) # create compute service compute_descriptor = build_compute_descriptor(ocean, publisher_wallet.address) # create asset DDO and datatoken try: asset = ocean.assets.create(metadata, publisher_wallet, [compute_descriptor], dt_name='Compute with data6', dt_symbol='DT-Testx7') print(f'Dataset asset created successfully: did={asset.did}, datatoken={asset.data_token_address}') #Dataset asset created successfully: did=did:op:2cbDb0Aaa1F546829E31267d1a7F74d926Bb5B1B, datatoken=0x2cbDb0Aaa1F546829E31267d1a7F74d926Bb5B1B except Exception as e: print(f'Publishing asset failed: {e}') return None, None dt = DataToken(asset.data_token_address) txid = dt.mint_tokens(publisher_wallet.address, 100, publisher_wallet) receipt = dt.get_tx_receipt(txid) assert receipt and receipt.status == 1, f'datatoken mint failed: tx={txid}, txReceipt={receipt}' # Create datatoken liquidity pool for the new asset pool = ocean.pool.create(asset.data_token_address, 50, 5, publisher_wallet, 5) #50 datatokens - 5 ocean in pool print(f'datatoken liquidity pool was created at address {pool.address}') #datatoken liquidity pool was created at address 0xeaD638506951B4a4c3575bbC0c7D1491c17B7A08 # Now the asset can be discovered and consumed dt_cost = ocean.pool.calcInGivenOut(pool.address, ocean.OCEAN_address, asset.data_token_address, 1.0) print(f'Asset {asset.did} can now be purchased from pool @{pool.address} ' f'at the price of {dt_cost} OCEAN tokens.') return asset, pool
def main(did, pool_address, order_tx_id=None): ocean = Ocean(config=Config(options_dict=get_config_dict())) publisher = Wallet( ocean.web3, private_key="0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58", ) # 0xe2DD09d719Da89e5a3D0F2549c7E24566e947260 consumer = Wallet( ocean.web3, private_key="0x9bf5d7e4978ed5206f760e6daded34d657572bd49fa5b3fe885679329fb16b16", ) # 0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0 if not (did and pool_address): metadata_file = "./examples/data/metadata.json" with open(metadata_file) as f: metadata = json.load(f) asset, pool = publish_asset(metadata, publisher) else: asset = ocean.assets.resolve(did) pool = BPool(pool_address) if not asset: print("publish asset failed, cannot continue with running compute.") return print(f"Requesting compute using asset {asset.did} and pool {pool.address}") algo_file = "./examples/data/algorithm.py" job_id, status = run_compute( asset.did, consumer, algo_file, pool.address, order_tx_id ) print(f"Compute started on asset {asset.did}: job_id={job_id}, status={status}")
def test_config_from_text(): """Tests that a Config object can be set from raw text.""" config_text = """ [resources] aquarius.url = https://another-aqua.url """ config = Config(text=config_text) assert config.aquarius_url == "https://another-aqua.url"
def test_config_from_text_malformed_content(): """Tests creating Config object. Setup: from raw text, content is malformed Expect: complain """ config_text = "Malformed content inside config text" with pytest.raises(Exception): Config(text=config_text)
def test_config_filename_not_given_envvar_is_empty(monkeypatch): """Test creating a Config object. Setup: filename not given, envvar is empty Expect: complain """ monkeypatch.delenv(ENV_CONFIG_FILE) with pytest.raises(ValueError): Config()
def run_compute(did, consumer_wallet, algorithm_file, pool_address, order_id=None): ocean = Ocean(config=Config(options_dict=get_config_dict())) # Get asset DDO/metadata and service asset = ocean.assets.resolve(did) service = asset.get_service(ServiceTypes.CLOUD_COMPUTE) # check the price in ocean tokens num_ocean = ocean.pool.calcInGivenOut(pool_address, ocean.OCEAN_address, asset.data_token_address, 1.0) # buy datatoken to be able to run the compute service dt = DataToken(asset.asset_id) dt_balance = dt.token_balance(consumer_wallet.address) if dt_balance < 1.0: pool = BPool(pool_address) txid = ocean.pool.buy_data_tokens(pool_address, 1.0, num_ocean+0.1, consumer_wallet) receipt = pool.get_tx_receipt(txid) if not receipt or receipt.status != 1: print(f'buying data token failed: txId={txid}, txReceipt={receipt}') return None, None tx_id = order_id if not tx_id: tx_id = ocean.assets.pay_for_service(1.0, asset.data_token_address, did, service.index, fee_receiver=asset.publisher, from_wallet=consumer_wallet) # load python algorithm to run in the compute job with open(algorithm_file) as f: algorithm_text = f.read() # whether to publish the algorithm results as an Ocean assets output_dict = { 'publishOutput': False, 'publishAlgorithmLog': False, } # start the compute job (submit the compute service request) algorithm_meta = AlgorithmMetadata( { 'language': 'python', 'rawcode': algorithm_text, 'container': { 'tag': 'latest', 'image': 'amancevice/pandas', 'entrypoint': 'python $ALGO' } } ) job_id = ocean.compute.start(did, consumer_wallet, tx_id, algorithm_meta=algorithm_meta, output=output_dict) # check the status of the compute job status = ocean.compute.status(did, job_id, consumer_wallet) print(f'status of compute job {job_id}: {status}') # get the result of the compute run result = ocean.compute.result(did, job_id, consumer_wallet) print(f'got result of compute job {job_id}: {result}') return job_id, status
def test_config_from_filename(): """Tests that a Config object can be set from a filename.""" config_file_name = get_resource_path("config", "test_config.ini") config = Config(filename=config_file_name) assert config.aquarius_url == "https://custom-aqua.url" assert config.artifacts_path is not None assert config.metadata_store_url == config.aquarius_url assert config.provider_address == "0x00bd138abd70e2f00903268f3db08f2d25677c9e" assert isinstance(config.gas_limit, int)
def test_config_filename_given_file_exists_malformed_content(monkeypatch, tmp_path): """Test creating a Config object. Setup: filename given, file exists, malformed content Expect: complain """ config_file_name = _create_malformed_conffile(tmp_path) monkeypatch.setenv(ENV_CONFIG_FILE, config_file_name) with pytest.raises(Exception): Config()
def test_config_filename_given_file_doesnt_exist(): """Test creating a Config object. Setup: filename given, file doesn't exist Expect: complain """ config_file_name = "i_dont_exist.ini" assert not os.path.exists(config_file_name) with pytest.raises(Exception): Config(filename=config_file_name)
def test_config_filename_not_given_file_exists_wellformed_content(monkeypatch): """Test creating a Config object. Setup: filename not given, default file exists, content is well-formed Expect: success. Uses config file at ENV_CONFIG_FILE. """ config_file_name = get_resource_path("config", "test_config.ini") monkeypatch.setenv(ENV_CONFIG_FILE, str(config_file_name)) config = Config() assert config.provider_address == "0x00bd138abd70e2f00903268f3db08f2d25677c9e"
def test_get_contracts_addresses(): config = Config(os.getenv(ENV_CONFIG_FILE)) ConfigProvider.set_config(config) addresses = util.get_contracts_addresses("ganache", config) assert addresses assert isinstance(addresses, dict) assert ("DTFactory" and "BFactory" and "FixedRateExchange" and "Metadata" and "Ocean" in addresses) assert len(addresses) == 5 for _, value in addresses.items(): assert value.startswith("0x")
def test_config_filename_given_file_exists_wellformed_content(): """Test creating a Config object. Setup: filename given, file exists, content is well-formed Expect: success """ config_file_name = get_resource_path("config", "test_config.ini") config = Config(filename=config_file_name) assert config.metadata_cache_uri == "https://custom-aqua.url" assert config.provider_address == "0x00bd138abd70e2f00903268f3db08f2d25677c9e" assert isinstance(config.gas_limit, int)
def test_config_filename_not_given_file_doesnt_exist(monkeypatch): """Test creating a Config object. Setup: filename not given, default file doesn't exist Expect: complain """ config_file_name = "i_dont_exist.ini" assert not os.path.exists(config_file_name) monkeypatch.setenv(ENV_CONFIG_FILE, config_file_name) with pytest.raises(Exception): Config()
def test_config_from_text_wellformed_content(): """Tests creating Config object. Setup: from raw text, content is well-formed Expect: success """ config_text = """ [resources] metadata_cache_uri = https://another-aqua.url """ config = Config(text=config_text) assert config.metadata_cache_uri == "https://another-aqua.url"
def test_metadata_cache_uri_set_via_config_options(caplog): """Tests the metadata_cache_uri property fallback logic when set via a config dict""" config_dict = {"resources": {"metadata_cache_uri": "https://custom-aqua.uri"}} config = Config(options_dict=config_dict) assert config.metadata_cache_uri == "https://custom-aqua.uri" config_dict = { "resources": { "metadata_cache_uri": "https://custom-aqua.uri", "aquarius.url": "https://another-aqua.url", } } with pytest.raises(ValueError): Config(options_dict=config_dict) config_dict = {"resources": {"aquarius.url": "https://another-aqua.url"}} config = Config(options_dict=config_dict) assert config.metadata_cache_uri == "https://another-aqua.url" assert ( "Config: resources.aquarius.url option is deprecated. " "Use resources.metadata_cache_uri instead." in caplog.text )
def get_config() -> Config: """Return `Config` containing default values for a given network. Chain ID is determined by querying the RPC specified by `OCEAN_NETWORK_URL` envvar. """ network_url = os.getenv("OCEAN_NETWORK_URL") assert ( network_url is not None ), "Cannot use ocean-lib without a specified network URL." w3 = get_web3(network_url) chain_id = w3.eth.chain_id config = get_config_dict(chain_id) config[SECTION_ETH_NETWORK][NAME_NETWORK_URL] = network_url return Config(options_dict=config)
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 events_object(): global EVENTS_INSTANCE if not EVENTS_INSTANCE: config_file = os.getenv('CONFIG_FILE', 'config.ini') network_rpc = os.environ.get('EVENTS_RPC', 'http://127.0.0.1:8545') ConfigProvider.set_config(Config(config_file)) from ocean_lib.ocean.util import get_web3_connection_provider Web3Provider.init_web3( provider=get_web3_connection_provider(network_rpc)) ContractHandler.set_artifacts_path(get_artifacts_path()) EVENTS_INSTANCE = EventsMonitor(Web3Provider.get_web3(), app.config['CONFIG_FILE']) EVENTS_INSTANCE.store_last_processed_block(0) return EVENTS_INSTANCE
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 run_scenario(): config = Config('config.ini') ocean = Ocean(config) wallet = Wallet( ocean.web3, 0x376e05899a4ae00463a3a607c774069b7d6a647860dba723f39b735c91238ddf, None, "EARLYTOBEDANDEARLYTORISE") ConfigProvider.set_config(config) Web3Provider.init_web3( provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path) print(dir(wallet)) print(wallet.address) print(config.network_url) print(config.provider_url) print(config.network_url) print(config.artifacts_path) data_token = ocean.create_data_token('S1Seven', 'S1SV', from_wallet=wallet) print(f'created new datatoken with address {data_token.address}') token_address = data_token.address print(token_address) '''
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)
import os from ocean_lib.ocean.ocean import Ocean from ocean_lib.web3_internal.wallet import Wallet from ocean_lib.config import Config from ocean_lib.config_provider import ConfigProvider from ocean_lib.data_provider.data_service_provider import DataServiceProvider from ocean_utils.agreements.service_factory import ServiceDescriptor config = Config('config.ini') ocean = Ocean(config) alice_wallet = Wallet(ocean.web3, 0x376e05899a4ae00463a3a607c774069b7d6a647860dba723f39b735c91238ddf) b'Uau3\xc8\x96`\xe5J\xa1\x88x\xeaT\xa6[}\x01kE\xcb\xf4AQ\xdd\xb0\xc2\xb3\xb9Q\xc1\xd5' web3 = Web #data_token = ocean.create_data_token('DataToken1', 'DT1', alice_wallet, blob=ocean.config.metadata_store_url) #data_token = ocean.create_data_token('DataToken1', 'DT1', alice_wallet) #token_address = data_token.address
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: ")