def registered_ddo(): ocn = get_publisher_ocean_instance() aqua = AquariusProvider.get_aquarius(ocn.config.aquarius_url) for did in aqua.list_assets(): aqua.retire_asset_ddo(did) return get_registered_ddo(ocn, get_publisher_account())
def test_provider_address_with_url(): """Tests that a URL version of provider address exists on the DataServiceProvider.""" p_ocean_instance = get_publisher_ocean_instance() provider_address = DataSP.get_provider_address( DataSP.get_url(p_ocean_instance.config)) assert provider_address, "Failed to get provider address." assert DataSP.get_provider_address("not a url") is None
def __init__(self, ocean_instance=None, account=None): self.ocean_instance = ocean_instance if not ocean_instance: from tests.resources.helper_functions import get_publisher_ocean_instance self.ocean_instance = get_publisher_ocean_instance( init_tokens=False, use_ss_mock=False, use_brizo_mock=False) self.account = account if not account: from tests.resources.helper_functions import get_publisher_account self.account = get_publisher_account(ConfigProvider.get_config())
def __init__(self, ocean_instance=None, wallet=None): if not ocean_instance: from tests.resources.helper_functions import get_publisher_ocean_instance ocean_instance = get_publisher_ocean_instance( use_provider_mock=True) self.ocean_instance = ocean_instance self.wallet = wallet if not wallet: from tests.resources.helper_functions import get_publisher_wallet self.wallet = get_publisher_wallet()
def mint_fake_OCEAN(): """ Does the following: 1. Mints tokens 2. Distributes tokens to TEST_PRIVATE_KEY1 and TEST_PRIVATE_KEY2 """ config = ExampleConfig.get_config() ConfigProvider.set_config(config) Web3Provider.init_web3( provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path) addresses_file = config.address_file ocean = get_publisher_ocean_instance() web3 = ocean.web3 with open(addresses_file) as f: network_addresses = json.load(f) network = "development" deployer_wallet = get_ganache_wallet() OCEAN_token = DataToken(address=network_addresses[network]["Ocean"]) amt_distribute = 1000 amt_distribute_base = to_base_18(float(amt_distribute)) OCEAN_token.mint(deployer_wallet.address, 2 * amt_distribute_base, from_wallet=deployer_wallet) for key_label in ["TEST_PRIVATE_KEY1", "TEST_PRIVATE_KEY2"]: key = os.environ.get(key_label) if not key: continue w = Wallet(web3, private_key=key) if OCEAN_token.token_balance(w.address) < 1000: OCEAN_token.transfer(w.address, amt_distribute_base, from_wallet=deployer_wallet) if from_wei(get_ether_balance(w.address)) < 2: send_ether(deployer_wallet, w.address, 4)
def test_market_flow(): pub_wallet = get_publisher_wallet() publisher_ocean = get_publisher_ocean_instance() consumer_ocean = get_consumer_ocean_instance() # Register Asset asset = get_registered_ddo(publisher_ocean, pub_wallet) assert isinstance(asset, Asset) assert asset.data_token_address consumer_wallet = get_consumer_wallet() service = asset.get_service(service_type=ServiceTypes.ASSET_ACCESS) sa = ServiceAgreement.from_json(service.as_dictionary()) # Mint data tokens and assign to publisher dt = publisher_ocean.get_data_token(asset.data_token_address) mint_tokens_and_wait(dt, pub_wallet.address, pub_wallet) ###### # Give the consumer some datatokens so they can order the service try: tx_id = dt.transfer_tokens(consumer_wallet.address, 10, pub_wallet) dt.verify_transfer_tx(tx_id, pub_wallet.address, consumer_wallet.address) except (AssertionError, Exception) as e: print(e) raise ###### # Place order for the download service order_requirements = consumer_ocean.assets.order(asset.did, consumer_wallet.address, sa.index) ###### # Pay for the service _order_tx_id = consumer_ocean.assets.pay_for_service( order_requirements.amount, order_requirements.data_token_address, asset.did, service.index, '0xF9f2DB837b3db03Be72252fAeD2f6E0b73E428b9', consumer_wallet) ###### # Download the asset files asset_folder = consumer_ocean.assets.download( asset.did, sa.index, consumer_wallet, _order_tx_id, consumer_ocean.config.downloads_path) assert len(os.listdir(asset_folder)) > 1 orders = consumer_ocean.get_user_orders(consumer_wallet.address, asset.asset_id) assert orders, f'no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}' orders = consumer_ocean.get_user_orders( consumer_wallet.address, consumer_ocean.web3.toChecksumAddress(asset.asset_id)) assert orders, f'no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}' orders = consumer_ocean.get_user_orders(consumer_wallet.address) assert orders, f'no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}'
def test_market_flow(order_type): """Tests that an order is correctly placed on the market. The parameter implicit_none sends the payload with an empty key as the delegated consumer. The parameter explicit_none sends None as the delegated consumer, explicitly.""" pub_wallet = get_publisher_wallet() publisher_ocean = get_publisher_ocean_instance() consumer_ocean = get_consumer_ocean_instance() # Register Asset asset = get_registered_ddo(publisher_ocean, get_metadata(), pub_wallet) assert isinstance(asset, Asset) assert asset.data_token_address consumer_wallet = get_consumer_wallet() service = asset.get_service(service_type=ServiceTypes.ASSET_ACCESS) sa = ServiceAgreement.from_json(service.as_dictionary()) # Mint data tokens and assign to publisher dt = publisher_ocean.get_data_token(asset.data_token_address) mint_tokens_and_wait(dt, pub_wallet.address, pub_wallet) ###### # Give the consumer some datatokens so they can order the service try: tx_id = dt.transfer_tokens(consumer_wallet.address, 10.0, pub_wallet) dt.verify_transfer_tx(tx_id, pub_wallet.address, consumer_wallet.address) except (AssertionError, Exception) as e: print(e) raise ###### # Place order for the download service order_requirements = consumer_ocean.assets.order(asset.did, consumer_wallet.address, sa.index) ###### # Pay for the service args = [ order_requirements.amount, order_requirements.data_token_address, asset.did, service.index, "0xF9f2DB837b3db03Be72252fAeD2f6E0b73E428b9", consumer_wallet, ] if order_type == "explicit_none": args.append(None) _order_tx_id = consumer_ocean.assets.pay_for_service(*args) ###### # Download the asset files asset_folder = consumer_ocean.assets.download( asset.did, sa.index, consumer_wallet, _order_tx_id, consumer_ocean.config.downloads_path, ) assert len(os.listdir(asset_folder)) >= 1 if order_type == "explicit_none": # no need to continue, order worked return orders = consumer_ocean.get_user_orders(consumer_wallet.address, asset.asset_id) assert ( orders ), f"no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}" orders = consumer_ocean.get_user_orders( consumer_wallet.address, consumer_ocean.web3.toChecksumAddress(asset.asset_id)) assert ( orders ), f"no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}" orders = consumer_ocean.get_user_orders(consumer_wallet.address) assert ( orders ), f"no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}"
def test_payer_market_flow(): """Tests that an order can be placed for a delegated consumer, other than the payer.""" pub_wallet = get_publisher_wallet() publisher_ocean = get_publisher_ocean_instance() consumer_ocean = get_consumer_ocean_instance() another_consumer_ocean = get_another_consumer_ocean_instance( use_provider_mock=True) # Register Asset asset = get_registered_ddo(publisher_ocean, get_metadata(), pub_wallet) assert isinstance(asset, Asset) assert asset.data_token_address another_consumer_wallet = get_another_consumer_wallet() consumer_wallet = get_consumer_wallet() service = asset.get_service(service_type=ServiceTypes.ASSET_ACCESS) sa = ServiceAgreement.from_json(service.as_dictionary()) # Mint data tokens and assign to publisher dt = publisher_ocean.get_data_token(asset.data_token_address) mint_tokens_and_wait(dt, pub_wallet.address, pub_wallet) ###### # Give the consumer some datatokens so they can order the service try: tx_id = dt.transfer_tokens(consumer_wallet.address, 10.0, pub_wallet) dt.verify_transfer_tx(tx_id, pub_wallet.address, consumer_wallet.address) except (AssertionError, Exception) as e: print(e) raise ###### # Place order for the download service order_requirements = consumer_ocean.assets.order( asset.did, another_consumer_wallet.address, sa.index) ###### # Pay for the service and have another_consumer_wallet as consumer _order_tx_id = consumer_ocean.assets.pay_for_service( order_requirements.amount, order_requirements.data_token_address, asset.did, service.index, "0xF9f2DB837b3db03Be72252fAeD2f6E0b73E428b9", consumer_wallet, another_consumer_wallet.address, ) asset_folder = None assert asset_folder is None if asset_folder is None: # Download the asset files asset_folder = another_consumer_ocean.assets.download( asset.did, sa.index, another_consumer_wallet, _order_tx_id, another_consumer_ocean.config.downloads_path, ) assert len(os.listdir(asset_folder)) >= 1 orders = consumer_ocean.get_user_orders(consumer_wallet.address, asset.asset_id) assert ( orders ), f"no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}" orders = consumer_ocean.get_user_orders( consumer_wallet.address, consumer_ocean.web3.toChecksumAddress(asset.asset_id)) assert ( orders ), f"no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}" orders = consumer_ocean.get_user_orders(consumer_wallet.address) assert ( orders ), f"no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}"
def test_compute_flow(): ###### # setup pub_wallet = get_publisher_wallet() p_ocean_instance = get_publisher_ocean_instance() c_ocean_instance = get_consumer_ocean_instance() cons_ocn = c_ocean_instance consumer_wallet = get_consumer_wallet() ###### # Publish Assets # Dataset with compute service sample_ddo_path = get_resource_path('ddo', 'ddo_with_compute_service.json') old_ddo = Asset(json_filename=sample_ddo_path) metadata = old_ddo.metadata metadata['main']['files'][0]['checksum'] = str(uuid.uuid4()) service = old_ddo.get_service(ServiceTypes.CLOUD_COMPUTE) compute_service = ServiceDescriptor.compute_service_descriptor( service.attributes, DataServiceProvider.get_url(p_ocean_instance.config)) block = p_ocean_instance.web3.eth.blockNumber compute_ddo = p_ocean_instance.assets.create( metadata, pub_wallet, service_descriptors=[compute_service], ) did = compute_ddo.did ddo_reg = p_ocean_instance.assets.ddo_registry() log = ddo_reg.get_event_log(ddo_reg.EVENT_METADATA_CREATED, block, compute_ddo.asset_id, 30) assert log, f'no ddo created event.' ddo = wait_for_ddo(p_ocean_instance, compute_ddo.did) assert ddo, f'resolve did {compute_ddo.did} failed.' _compute_ddo = p_ocean_instance.assets.resolve(compute_ddo.did) # algorithm with download service algorithm_ddo_path = get_resource_path('ddo', 'ddo_sample_algorithm.json') algo_main = Asset(json_filename=algorithm_ddo_path).metadata['main'] algo_meta_dict = algo_main['algorithm'].copy() algo_meta_dict['url'] = algo_main['files'][0]['url'] algorithm_meta = AlgorithmMetadata(algo_meta_dict) ###### # Mint tokens for dataset and assign to publisher dt = p_ocean_instance.get_data_token(compute_ddo.data_token_address) mint_tokens_and_wait(dt, pub_wallet.address, pub_wallet) ###### # Give the consumer some datatokens so they can order the service try: tx_id = dt.transfer_tokens(consumer_wallet.address, 10, pub_wallet) dt.verify_transfer_tx(tx_id, pub_wallet.address, consumer_wallet.address) except (AssertionError, Exception) as e: print(e) raise ###### # Order compute service from the dataset asset order_requirements = cons_ocn.assets.order( compute_ddo.did, consumer_wallet.address, service_type=ServiceTypes.CLOUD_COMPUTE) ###### # Start the order on-chain using the `order` requirements from previous step service = compute_ddo.get_service(ServiceTypes.CLOUD_COMPUTE) _order_tx_id = cons_ocn.assets.pay_for_service( order_requirements.amount, order_requirements.data_token_address, compute_ddo.did, service.index, '0xF9f2DB837b3db03Be72252fAeD2f6E0b73E428b9', consumer_wallet) ###### job_id = cons_ocn.compute.start(did, consumer_wallet, _order_tx_id, nonce=order_requirements.nonce, algorithm_meta=algorithm_meta) assert job_id, f'expected a job id, got {job_id}' status = cons_ocn.compute.status(did, job_id, consumer_wallet) print(f'got job status: {status}') assert status and status[ 'ok'], f'something not right about the compute job, got status: {status}' status = cons_ocn.compute.stop(did, job_id, consumer_wallet) print(f'got job status after requesting stop: {status}') assert status, f'something not right about the compute job, got status: {status}'
def __init__(self): """Initialise shared variables.""" self.publisher_wallet = get_publisher_wallet() self.consumer_wallet = get_consumer_wallet() self.publisher_ocean_instance = get_publisher_ocean_instance() self.consumer_ocean_instance = get_consumer_ocean_instance()
def publisher_ocean_instance(): return get_publisher_ocean_instance()
def deploy(network, addresses_file): config = ExampleConfig.get_config() ConfigProvider.set_config(config) Web3Provider.init_web3( provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path) artifacts_path = ContractHandler.artifacts_path if not addresses_file: addresses_file = config.address_file else: addresses_file = Path(addresses_file).expanduser().resolve() ocean = get_publisher_ocean_instance() web3 = ocean.web3 addresses = dict() if os.path.exists(addresses_file): with open(addresses_file) as f: network_addresses = json.load(f) else: network_addresses = {network: {}} if network == "ganache" and network not in network_addresses: network = "development" _addresses = network_addresses[network] # ****SET ENVT**** # grab vars factory_deployer_private_key = get_ganache_wallet().private_key # corner cases if invalidKey(factory_deployer_private_key): print("Need valid FACTORY_DEPLOYER_PRIVATE_KEY") sys.exit(0) # ****SEE FUNDS**** print("Keys:\n%s" % Wallet( web3=get_web3(), private_key=factory_deployer_private_key).keysStr()) print("") # ****DEPLOY**** deployer_wallet = Wallet(web3, private_key=factory_deployer_private_key) minter_addr = deployer_wallet.address # cap = 2 ** 255 not used if DTFactory.CONTRACT_NAME not in _addresses: print("****Deploy DataTokenTemplate: begin****") dt_address = DataToken.deploy( web3, deployer_wallet, artifacts_path, "Template Contract", "TEMPLATE", minter_addr, DataToken.DEFAULT_CAP_BASE, DTFactory.FIRST_BLOB, minter_addr, ) addresses[DataToken.CONTRACT_NAME] = dt_address print("****Deploy DataTokenTemplate: done****\n") print("****Deploy DTFactory: begin****") dtfactory = DTFactory( DTFactory.deploy(web3, deployer_wallet, artifacts_path, dt_address, minter_addr)) addresses[DTFactory.CONTRACT_NAME] = dtfactory.address print("****Deploy DTFactory: done****\n") if BFactory.CONTRACT_NAME not in _addresses: print("****Deploy BPool: begin****") bpool_address = BPool.deploy(web3, deployer_wallet, artifacts_path) bpool_template = BPool(bpool_address) addresses[BPool.CONTRACT_NAME] = bpool_address print("****Deploy BPool: done****\n") print("****Deploy 'BFactory': begin****") bfactory_address = BFactory.deploy(web3, deployer_wallet, artifacts_path, bpool_template.address) _ = BFactory(bfactory_address) addresses[BFactory.CONTRACT_NAME] = bfactory_address print("****Deploy 'BFactory': done****\n") if FixedRateExchange.CONTRACT_NAME not in _addresses: print("****Deploy 'FixedRateExchange': begin****") addresses[FixedRateExchange.CONTRACT_NAME] = FixedRateExchange.deploy( web3, deployer_wallet, artifacts_path) print("****Deploy 'FixedRateExchange': done****\n") if MetadataContract.CONTRACT_NAME not in _addresses: print("****Deploy 'Metadata': begin****") addresses[MetadataContract.CONTRACT_NAME] = MetadataContract.deploy( web3, deployer_wallet, artifacts_path) print("****Deploy 'Metadata': done****\n") if network in ("ganache", "development"): print("****Deploy fake OCEAN: begin****") # For simplicity, hijack DataTokenTemplate. minter_addr = deployer_wallet.address OCEAN_cap = 1410 * 10**6 # 1.41B OCEAN_cap_base = util.to_base_18(float(OCEAN_cap)) OCEAN_token = DataToken( DataToken.deploy( web3, deployer_wallet, artifacts_path, "Ocean", "OCEAN", minter_addr, OCEAN_cap_base, "", minter_addr, )) addresses["Ocean"] = OCEAN_token.address print("****Deploy fake OCEAN: done****\n") print("****Mint fake OCEAN: begin****") OCEAN_token.mint(minter_addr, OCEAN_cap_base, from_wallet=deployer_wallet) print("****Mint fake OCEAN: done****\n") print("****Distribute fake OCEAN: begin****") amt_distribute = 1000 amt_distribute_base = util.to_base_18(float(amt_distribute)) for key_label in ["TEST_PRIVATE_KEY1", "TEST_PRIVATE_KEY2"]: key = os.environ.get(key_label) if not key: continue dst_address = privateKeyToAddress(key) OCEAN_token.transfer(dst_address, amt_distribute_base, from_wallet=deployer_wallet) print("****Distribute fake OCEAN: done****\n") network_addresses[network].update(addresses) with open(addresses_file, "w") as f: json.dump(network_addresses, f, indent=2) return addresses
def test_provider_address_with_url(): p_ocean_instance = get_publisher_ocean_instance() provider_address = DataServiceProvider.get_provider_address( DataServiceProvider.get_url(p_ocean_instance.config)) assert provider_address, "Failed to get provider address."
def deploy_fake_OCEAN(): """ Does the following: 1. Deploy to ganache a new ERC20 contract having symbol OCEAN 2. Mints tokens 3. Distributes tokens to TEST_PRIVATE_KEY1 and TEST_PRIVATE_KEY2 4. In addresses.json, updates development : Ocean entry with new address """ network = "ganache" config = ExampleConfig.get_config() ConfigProvider.set_config(config) Web3Provider.init_web3( provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path) artifacts_path = ContractHandler.artifacts_path addresses_file = config.address_file ocean = get_publisher_ocean_instance() web3 = ocean.web3 addresses = dict() if os.path.exists(addresses_file): with open(addresses_file) as f: network_addresses = json.load(f) else: network_addresses = {network: {}} if network not in network_addresses: network = "development" # ****SET ENVT**** deployer_private_key = get_ganache_wallet().private_key if invalidKey(deployer_private_key): print("Need valid DEPLOYER_PRIVATE_KEY") sys.exit(0) # ****DEPLOY**** deployer_wallet = Wallet(web3, private_key=deployer_private_key) print("****Deploy fake OCEAN: begin****") # For simplicity, hijack DataTokenTemplate. deployer_addr = deployer_wallet.address OCEAN_cap = 1410 * 10**6 # 1.41B OCEAN_cap_base = util.to_base_18(float(OCEAN_cap)) OCEAN_token = DataToken( DataToken.deploy( web3, deployer_wallet, artifacts_path, "Ocean", "OCEAN", deployer_addr, OCEAN_cap_base, "", deployer_addr, )) addresses["Ocean"] = OCEAN_token.address print("****Deploy fake OCEAN: done****\n") print("****Mint fake OCEAN: begin****") OCEAN_token.mint(deployer_addr, OCEAN_cap_base, from_wallet=deployer_wallet) print("****Mint fake OCEAN: done****\n") print("****Distribute fake OCEAN: begin****") amt_distribute = 1000 amt_distribute_base = util.to_base_18(float(amt_distribute)) for key_label in ["TEST_PRIVATE_KEY1", "TEST_PRIVATE_KEY2"]: key = os.environ.get(key_label) if not key: continue dst_address = privateKeyToAddress(key) OCEAN_token.transfer(dst_address, amt_distribute_base, from_wallet=deployer_wallet) print(f"Distributed {amt_distribute} OCEAN to address {dst_address}") print("****Distribute fake OCEAN: done****\n") print("****Update addresses file: begin****\n") print(f"addresses file: {addresses_file}") print(f"network: {network}") print("") network_addresses[network].update(addresses) with open(addresses_file, "w") as f: json.dump(network_addresses, f, indent=2) _s = json.dumps(addresses, indent=4) s = "Have deployed to, and updated the following addresses\n" + _s print(s) print("****Update addresses file: done****\n")
def test_market_flow(): """Tests that an order is correctly placed on the market. The parameter implicit_none sends the payload with an empty key as the delegated consumer. The parameter explicit_none sends None as the delegated consumer, explicitly.""" pub_wallet = get_publisher_wallet() publisher_ocean = get_publisher_ocean_instance() consumer_ocean = get_consumer_ocean_instance() # Register asset asset = get_registered_ddo(publisher_ocean, get_metadata(), pub_wallet) assert isinstance(asset, V3Asset) assert asset.data_token_address, "The asset does not have a token address." consumer_wallet = get_consumer_wallet() service = asset.get_service(service_type=ServiceTypes.ASSET_ACCESS) sa = Service.from_json(service.as_dictionary()) # Mint data tokens and assign to publisher dt = publisher_ocean.get_data_token(asset.data_token_address) mint_tokens_and_wait(dt, pub_wallet.address, pub_wallet) ###### # Give the consumer some datatokens so they can order the service try: tx_id = dt.transfer(consumer_wallet.address, to_wei(10), pub_wallet) dt.verify_transfer_tx(tx_id, pub_wallet.address, consumer_wallet.address) except (AssertionError, Exception) as e: print(e) raise ###### # Place order for the download service order_requirements = consumer_ocean.assets.order(asset.did, consumer_wallet.address, sa.index) ###### # Pay for the service args = [ consumer_ocean.web3, order_requirements.amount, order_requirements.data_token_address, asset.did, service.index, "0xF9f2DB837b3db03Be72252fAeD2f6E0b73E428b9", consumer_wallet, sa.get_c2d_address(), ] _order_tx_id = consumer_ocean.assets.pay_for_service(*args) ###### # Download the asset files asset_folder = consumer_ocean.assets.download( asset.did, sa.index, consumer_wallet, _order_tx_id, consumer_ocean.config.downloads_path, ) assert len(os.listdir(asset_folder)) >= 1, "The asset folder is empty." orders = consumer_ocean.get_user_orders(consumer_wallet.address, asset.asset_id) assert ( orders ), f"no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}" orders = consumer_ocean.get_user_orders( consumer_wallet.address, consumer_ocean.web3.toChecksumAddress(asset.asset_id)) assert ( orders ), f"no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}" orders = consumer_ocean.get_user_orders(consumer_wallet.address) assert ( orders ), f"no orders found using the order history: datatoken {asset.asset_id}, consumer {consumer_wallet.address}" ###### # Publisher can get the urls of the asset asset_urls = DataServiceProvider.get_asset_urls(asset.did, str(service.index), "http://172.15.0.4:8030", pub_wallet) assert len(asset_urls) == 3 for url in asset_urls: assert "10_Monkey_Species_Small" in url
def publisher_ocean_instance_brizo(): return get_publisher_ocean_instance(use_brizo_mock=True)