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_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_buy_asset(consumer_ocean_instance, publisher_ocean_instance): config = ExampleConfig.get_config() ConfigProvider.set_config(config) keeper = Keeper.get_instance() # :TODO: enable the actual SecretStore # SecretStoreProvider.set_secret_store_class(SecretStore) w3 = Web3Provider.get_web3() pub_acc = get_publisher_account() # Register ddo ddo = get_registered_ddo(publisher_ocean_instance, pub_acc) assert isinstance(ddo, DDO) # ocn here will be used only to publish the asset. Handling the asset by the publisher # will be performed by the Brizo server running locally cons_ocn = consumer_ocean_instance # restore the http client because we want the actual Brizo server to do the work # not the BrizoMock. # Brizo.set_http_client(requests) consumer_account = get_consumer_account() downloads_path_elements = len( os.listdir( consumer_ocean_instance._config.downloads_path)) if os.path.exists( consumer_ocean_instance._config.downloads_path) else 0 # sign agreement using the registered asset did above service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) sa = ServiceAgreement.from_json(service.as_dictionary()) # This will send the consume request to Brizo which in turn will execute the agreement on-chain cons_ocn.accounts.request_tokens(consumer_account, 100) agreement_id = cons_ocn.assets.order(ddo.did, sa.index, consumer_account, auto_consume=False) event_wait_time = 10 event = keeper.escrow_access_secretstore_template.subscribe_agreement_created( agreement_id, event_wait_time, log_event( keeper.escrow_access_secretstore_template.AGREEMENT_CREATED_EVENT), (), wait=True) assert event, 'no event for EscrowAccessSecretStoreTemplate.AgreementCreated' event = keeper.lock_reward_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, log_event(keeper.lock_reward_condition.FULFILLED_EVENT), (), wait=True) assert event, 'no event for LockRewardCondition.Fulfilled' # give access publisher_ocean_instance.agreements.conditions.grant_access( agreement_id, ddo.did, consumer_account.address, pub_acc) event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, log_event(keeper.access_secret_store_condition.FULFILLED_EVENT), (), wait=True) assert event, 'no event for AccessSecretStoreCondition.Fulfilled' assert cons_ocn.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address) assert cons_ocn.assets.consume(agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path) assert len(os.listdir( config.downloads_path)) == downloads_path_elements + 1 # Check that we can consume only an specific file in passing the index. assert cons_ocn.assets.consume(agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, 2) assert len(os.listdir( config.downloads_path)) == downloads_path_elements + 1 with pytest.raises(AssertionError): cons_ocn.assets.consume(agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, -2) with pytest.raises(AssertionError): cons_ocn.assets.consume(agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, 3) # decrypt the contentUrls using the publisher account instead of consumer account. # if the secret store is working and ACL check is enabled, this should fail # since SecretStore decrypt will fail the checkPermissions check try: cons_ocn.assets.consume(agreement_id, ddo.did, sa.index, pub_acc, config.downloads_path) except RPCError: print('hooray, secret store is working as expected.') publisher_ocean_instance.agreements.conditions.release_reward( agreement_id, sa.get_price(), pub_acc) event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time + 20, log_event(keeper.escrow_reward_condition.FULFILLED_EVENT), (), wait=True) assert event, 'no event for EscrowReward.Fulfilled' assert w3.toHex(event.args['_agreementId']) == agreement_id
def test_payer_market_flow(): 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, 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, 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, ) downloaded_file = os.path.join( asset_folder, os.path.join(f"datafile.{asset.asset_id}.{sa.index}") ) assert os.path.exists(asset_folder) assert downloaded_file.split("/")[-1] in os.listdir(asset_folder) 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_buy_asset(publisher_instance_no_init, consumer_instance_no_init): config = ExampleConfig.get_config() ConfigProvider.set_config(config) keeper = Keeper.get_instance() # :TODO: enable the actual SecretStore # SecretStoreProvider.set_secret_store_class(SecretStore) w3 = Web3Provider.get_web3() pub_acc = get_publisher_account() # Register ddo ddo = get_registered_ddo(publisher_instance_no_init, pub_acc) assert isinstance(ddo, DDO) # nevermined here will be used only to publish the asset. Handling the asset by the publisher # will be performed by the Gateway server running locally # restore the http client because we want the actual Gateway server to do the work # not the GatewayMock. # Gateway.set_http_client(requests) consumer_account = get_consumer_account() downloads_path_elements = len( os.listdir(consumer_instance_no_init._config.downloads_path)) if os.path.exists( consumer_instance_no_init._config.downloads_path) else 0 # sign agreement using the registered asset did above service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) sa = ServiceAgreement.from_service_dict(service.as_dictionary()) # This will send the access request to Gateway which in turn will execute the agreement on-chain consumer_instance_no_init.accounts.request_tokens(consumer_account, 100) agreement_id = consumer_instance_no_init.assets.order( ddo.did, sa.index, consumer_account, consumer_account) event_wait_time = 10 event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, log_event(keeper.lock_payment_condition.FULFILLED_EVENT), (), wait=True ) assert event, 'no event for LockRewardCondition.Fulfilled' # give access publisher_instance_no_init.agreements.conditions.grant_access( agreement_id, ddo.asset_id, consumer_account.address, pub_acc) event = keeper.access_condition.subscribe_condition_fulfilled( agreement_id, event_wait_time, log_event(keeper.access_condition.FULFILLED_EVENT), (), wait=True ) assert event, 'no event for AccessSecretStoreCondition.Fulfilled' assert consumer_instance_no_init.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address) amounts = list(map(int, service.get_param_value_by_name('_amounts'))) receivers = service.get_param_value_by_name('_receivers') publisher_instance_no_init.agreements.conditions.release_reward( agreement_id, ddo.asset_id, amounts, receivers, pub_acc) assert consumer_instance_no_init.assets.access( agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path) assert len(os.listdir(config.downloads_path)) == downloads_path_elements + 1 # Check that we can access only an specific file in passing the index. assert consumer_instance_no_init.assets.access( agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, 1 ) assert len(os.listdir(config.downloads_path)) == downloads_path_elements + 1 with pytest.raises(AssertionError): consumer_instance_no_init.assets.access( agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, -2 ) with pytest.raises(AssertionError): consumer_instance_no_init.assets.access( agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, 3 ) # decrypt the contentUrls using the publisher account instead of consumer account. # if the secret store is working and ACL check is enabled, this should fail # since SecretStore decrypt will fail the checkPermissions check and the gateway will return # an http error code back with pytest.raises(ValueError) as e: consumer_instance_no_init.assets.access( agreement_id, ddo.did, sa.index, pub_acc, config.downloads_path ) print(e)
def registered_ddo(): return get_registered_ddo(get_publisher_instance(), get_publisher_account())