def make_ocean_instance(account_index): ocn = Ocean(ExampleConfig.get_config()) account = ocn.accounts.list()[account_index] if account_index == 0: account.password = ExampleConfig.get_config().get( 'keeper-contracts', 'parity.password') else: account.password = ExampleConfig.get_config().get( 'keeper-contracts', 'parity.password1') return ocn
def search_assets(): ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create( Metadata.get_example(), account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info( f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}' ) ddo_list = ocn.assets.search('bonding curve') logging.info( f'found {len(ddo_list)} assets that contain `bonding curve` in their metadata.' ) ddo_list = ocn.assets.query( {"query": { "text": ['Ocean protocol white paper'] }}) logging.info( f'found {len(ddo_list)} assets with name that contains `Ocean protocol white paper`' )
def setup_all(): 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) network = Web3Helper.get_network_name() wallet = get_ganache_wallet() if network in ["ganache", "development"] and wallet: print( f"sender: {wallet.key}, {wallet.address}, {wallet.password}, {wallet.keysStr()}" ) print( f"sender balance: {Web3Helper.from_wei(Web3Helper.get_ether_balance(wallet.address))}" ) assert Web3Helper.from_wei(Web3Helper.get_ether_balance( wallet.address)) > 10 from ocean_lib.models.data_token import DataToken OCEAN_token = DataToken(get_ocean_token_address(network)) amt_distribute = 1000 amt_distribute_base = to_base_18(float(amt_distribute)) for w in (get_publisher_wallet(), get_consumer_wallet()): if Web3Helper.from_wei(Web3Helper.get_ether_balance( w.address)) < 2: Web3Helper.send_ether(wallet, w.address, 4) if OCEAN_token.token_balance(w.address) < 100: OCEAN_token.transfer(w.address, amt_distribute_base, from_wallet=wallet)
def search_assets(): ConfigProvider.set_config(ExampleConfig.get_config()) nevermined = Nevermined() account = get_account(0) ddo = nevermined.assets.create( example_metadata.metadata, account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = nevermined.assets.resolve(ddo.did) logging.info( f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}' ) ddo_list = nevermined.assets.search('bonding curve') logging.info( f'found {len(ddo_list)} assets that contain `bonding curve` in their metadata.' ) ddo_list = nevermined.assets.query( {"query": { "text": ['Nevermined protocol white paper'] }}) logging.info( f'found {len(ddo_list)} assets with name that contains `Nevermined protocol white paper`' )
def register_compute_asset(): # get ocean instance config = ExampleConfig.get_config() ConfigProvider.set_config(config) ocean = Ocean() keeper = Keeper.get_instance() consumer_account = get_account(0) publisher_account = get_account(1) # get descriptor for compute service compute_descriptor = build_compute_descriptor(ocean) # create an asset with compute service ddo = ocean.assets.create(example_metadata.metadata, publisher_account, providers=[config.provider_address], use_secret_store=False, service_descriptors=[compute_descriptor]) event = keeper.did_registry.subscribe_to_event( keeper.did_registry.DID_REGISTRY_EVENT_NAME, 15, event_filter={ '_did': Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id), '_owner': publisher_account.address }, wait=True) assert event, 'There was a problem registering an asset' logging.info(f'Registered asset: did={ddo.did}') # buy an asset access agreement_id = ocean.compute.order(ddo.did, consumer_account) event = keeper.compute_execution_condition.subscribe_condition_fulfilled( agreement_id, 15, None, (), wait=True) assert event, 'There was a problem creating an agreement' logging.info(f'Created asset agreement: agreement_id={agreement_id}') # supply metadata describing the algorithm to run against the dataset # you can also use an algorithm published as an Ocean asset instead algo_meta = AlgorithmMetadata(example_metadata.algo_metadata) # whether to publish the algorithm results as an Ocean assets output_dict = { 'publishOutput': False, 'publishAlgorithmLog': False, } # start the compute job job_id = ocean.compute.start(agreement_id, consumer_account, algorithm_meta=algo_meta, output=output_dict) logging.info(f'Started compute job: job_id={job_id}') # query operator service for job status status = ocean.compute.status(agreement_id, job_id, consumer_account) logging.info(f'Job status: {status}')
def buy_asset(): """ Requires all ocean services running. """ ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() # make ocean instance ocn = Ocean() acc = get_publisher_account(config) if not acc: acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] # Register ddo ddo = ocn.assets.create(Metadata.get_example(), acc, providers=[acc.address]) logging.info(f'registered ddo: {ddo.did}') # 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 keeper = Keeper.get_instance() if 'TEST_LOCAL_NILE' in os.environ and os.environ['TEST_LOCAL_NILE'] == '1': provider = keeper.did_registry.to_checksum_address( '0x413c9ba0a05b8a600899b41b0c62dd661e689354' ) keeper.did_registry.add_provider(ddo.asset_id, provider, acc) logging.debug(f'is did provider: ' f'{keeper.did_registry.is_did_provider(ddo.asset_id, provider)}') cons_ocn = Ocean() consumer_account = get_account_from_config(config, 'parity.address1', 'parity.password1') # sign agreement using the registered asset did above service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) # This will send the purchase request to Brizo which in turn will execute the agreement on-chain cons_ocn.accounts.request_tokens(consumer_account, 100) sa = ServiceAgreement.from_service_dict(service.as_dictionary()) agreement_id = cons_ocn.assets.order( ddo.did, sa.service_definition_id, consumer_account) logging.info('placed order: %s, %s', ddo.did, agreement_id) i = 0 while ocn.agreements.is_access_granted( agreement_id, ddo.did, consumer_account.address) is not True and i < 30: time.sleep(1) i += 1 assert ocn.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address) ocn.assets.consume( agreement_id, ddo.did, sa.service_definition_id, consumer_account, config.downloads_path) logging.info('Success buying asset.')
def test_publisher_download_compute(metadata): config = ExampleConfig.get_config() publisher_instance = get_publisher_instance(True, False, False) publisher = publisher_instance.main_account ddo = publisher_instance.assets.create_compute(metadata, publisher) assert ddo service_agreement = ddo.get_service("compute") assert publisher_instance.assets.download( ddo.did, service_agreement.index, publisher, config.downloads_path ) publisher_instance.assets.retire(ddo.did)
def resolve_asset(): ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create( Metadata.get_example(), account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info(f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}')
def test_publisher_download_algorithm(ddo_algorithm): config = ExampleConfig.get_config() publisher_instance = get_publisher_instance(True, False, False) publisher = publisher_instance.main_account metadata = ddo_algorithm["service"][0] ddo = publisher_instance.assets.create(metadata["attributes"], publisher) assert ddo service_agreement = ddo.get_service("access") assert publisher_instance.assets.download( ddo.did, service_agreement.index, publisher, config.downloads_path ) publisher_instance.assets.retire(ddo.did)
def resolve_asset(): ConfigProvider.set_config(ExampleConfig.get_config()) nevermined = Nevermined() account = get_account(0) ddo = nevermined.assets.create( example_metadata.metadata, account, ) sleep(ASYNC_DELAY) logging.info(f'Registered asset: did={ddo.did}, ddo={ddo.as_text()}') resolved_ddo = nevermined.assets.resolve(ddo.did) logging.info(f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}')
def sign_service_agreement(): ConfigProvider.set_config(ExampleConfig.get_config()) # make ocean instance and register an asset ocn = Ocean() acc = get_account(0) ddo = ocn.assets.create(example_metadata.metadata, acc) consumer_account = get_account(1) agreement_id, signature = ocn.agreements.prepare(ddo.did, consumer_account) sleep(ASYNC_DELAY) logging.info(f'service agreement signed: ' f'\nservice agreement id: {agreement_id}, ' f'\nsignature: {signature}')
def test_get_condition_name_by_address(): config = ExampleConfig.get_config() Web3Provider.init_web3(config.keeper_url) ContractHandler.set_artifacts_path(config.keeper_path) keeper = Keeper.get_instance() name = keeper.get_condition_name_by_address( keeper.lock_reward_condition.address) assert name == 'lockReward' name = keeper.get_condition_name_by_address( keeper.access_secret_store_condition.address) assert name == 'accessSecretStore' name = keeper.get_condition_name_by_address( keeper.escrow_reward_condition.address) assert name == 'escrowReward'
def ocean_agreements(): config = ExampleConfig.get_config() Web3Provider.init_web3(config.keeper_url) ContractHandler.set_artifacts_path(config.keeper_path) keeper = Keeper.get_instance() w3 = Web3Provider.get_web3() did_resolver = Mock() ddo = get_ddo_sample() service = ddo.get_service(ServiceTypes.ASSET_ACCESS) service.update_value( ServiceAgreementTemplate.TEMPLATE_ID_KEY, w3.toChecksumAddress("0x00bd138abd70e2f00903268f3db08f2d25677c9e")) did_resolver.resolve = MagicMock(return_value=ddo) # consumer_class = Mock # consumer_class.download = MagicMock(return_value='') return OceanAgreements(keeper, did_resolver, AssetConsumer, ConfigProvider.get_config())
def register_asset(): # make ocean instance ConfigProvider.set_config(ExampleConfig.get_config()) ocn = Ocean() account = get_account(0) # account = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create( example_metadata.metadata, account, providers=['0xfEF2d5e1670342b9EF22eeeDcb287EC526B48095']) sleep(ASYNC_DELAY) logging.info( f'Registered asset: did={ddo.did}, ddo-services={ddo.services}') resolved_ddo = ocn.assets.resolve(ddo.did) logging.info( f'resolved asset ddo: did={resolved_ddo.did}, ddo={resolved_ddo.as_text()}' )
def test_provider_download_algorithm(ddo_algorithm): config = ExampleConfig.get_config() publisher_instance = get_publisher_instance(True, False, False) provider = publisher_instance.main_account consumer_instance = get_consumer_instance(True, False, False) consumer = consumer_instance.main_account metadata = ddo_algorithm["service"][0] # consumer creates algorithm asset (for a compute job) ddo = consumer_instance.assets.create( metadata["attributes"], consumer, providers=[provider.address] ) assert ddo # provider downloads the asset service_agreement = ddo.get_service("access") assert publisher_instance.assets.download( ddo.did, service_agreement.index, consumer, config.downloads_path ) consumer_instance.assets.retire(ddo.did)
def sign_service_agreement(): ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() # make ocean instance and register an asset ocn = Ocean() acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] ddo = ocn.assets.create(Metadata.get_example(), acc) consumer_account = get_account_from_config(config, 'parity.address1', 'parity.password1') service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) agreement_id, signature = ocn.agreements.prepare( ddo.did, service.service_definition_id, consumer_account) sleep(ASYNC_DELAY) logging.info(f'service agreement signed: ' f'\nservice agreement id: {agreement_id}, ' f'\nsignature: {signature}')
def test_consumer_download_compute(metadata): config = ExampleConfig.get_config() publisher_instance = get_publisher_instance(True, False, False) publisher = publisher_instance.main_account consumer_instance = get_consumer_instance(True, False, False) consumer = consumer_instance.main_account # publisher creates compute asset ddo = publisher_instance.assets.create_compute(metadata, publisher) assert ddo # consumer orders compute asset service_agreement = ddo.get_service("compute") _agreement_id = consumer_instance.assets.order( ddo.did, service_agreement.index, consumer, consumer ) # consumer tries to download the asset with pytest.raises(ValueError): consumer_instance.assets.download( ddo.did, service_agreement.index, consumer, config.downloads_path ) publisher_instance.assets.retire(ddo.did)
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 compute_example(verbose=False): print("Setting up...") if verbose: configure_logging() date_created = dates_generator() # Setup nevermined ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() nevermined = Nevermined() keeper = Keeper.get_instance() provider = "0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0" asset_rewards = { "_amounts": ["10", "2"], "_receivers": ["0x00Bd138aBD70e2F00903268F3Db08f2D25677C9e", "0x068ed00cf0441e4829d9784fcbe7b9e26d4bd8d0"] } # Setup accounts acc = Account( Web3.toChecksumAddress(PROVIDER_ADDRESS), PROVIDER_PASSWORD, PROVIDER_KEYFILE ) nevermined.accounts.request_tokens(acc, 10) provider_acc = acc consumer_acc = acc # Publish compute example_metadata.compute_ddo["main"]["dateCreated"] = next(date_created) ddo_compute = nevermined.assets.create_compute( example_metadata.metadata, provider_acc, asset_rewards, providers=[provider] ) assert ddo_compute is not None, "Creating compute asset on-chain failed." print( f"[PROVIDER --> NEVERMINED] Publishing compute to the data asset: {ddo_compute.did}" ) # Publish algorithm example_metadata.algo_metadata["main"]["dateCreated"] = next(date_created) ddo_transformation = nevermined.assets.create( example_metadata.algo_metadata, consumer_acc, providers=[provider] ) assert ( ddo_transformation is not None ), "Creating asset transformation on-chain failed." print( f"[CONSUMER --> NEVERMINED] Publishing algorithm asset: {ddo_transformation.did}" ) # Publish workflows workflow_metadata = example_metadata.workflow_ddo workflow_metadata["main"]["workflow"]["stages"][0]["input"][0]["id"] = ddo_compute.did workflow_metadata["main"]["workflow"]["stages"][0]["transformation"][ "id" ] = ddo_transformation.did ddo_workflow = nevermined.assets.create( workflow_metadata, consumer_acc, providers=[provider] ) assert ddo_workflow is not None, "Creating asset workflow on-chain failed." print(f"[CONSUMER --> NEVERMINED] Publishing compute workflow: {ddo_workflow.did}") # Order computation service = ddo_compute.get_service(service_type=ServiceTypes.CLOUD_COMPUTE) service_agreement = ServiceAgreement.from_service_dict(service.as_dictionary()) agreement_id = nevermined.assets.order( ddo_compute.did, service_agreement.index, consumer_acc ) print( f"[CONSUMER --> PROVIDER] Requesting an agreement for compute to the data: {agreement_id}" ) event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event is not None, "Reward condition is not found" event = keeper.compute_execution_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event is not None, "Execution condition not found" # Execute workflow nevermined.assets.execute( agreement_id, ddo_compute.did, service_agreement.index, consumer_acc, ddo_workflow.did, ) print("[CONSUMER --> PROVIDER] Requesting execution of the compute workflow") event = keeper.escrow_payment_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event is not None, "Escrow Reward condition not found" print("Workflow successfully executed")
def web3_instance(): config = ExampleConfig.get_config() return Web3Provider.get_web3(config.network_url)
def test_nfts_flow(publisher_instance_no_init, consumer_instance_no_init): config = ExampleConfig.get_config() ConfigProvider.set_config(config) keeper = Keeper.get_instance() pub_acc = get_publisher_account() # Register ddo ddo = get_registered_ddo_nft(publisher_instance_no_init, pub_acc) asset_id = ddo.asset_id nft_amounts = 1 assert isinstance(ddo, DDO) consumer_account = get_consumer_account() consumer_instance_no_init.accounts.request_tokens(consumer_account, 100) service_sales = ddo.get_service(service_type=ServiceTypes.NFT_SALES) sa_sales = ServiceAgreement.from_service_dict( service_sales.as_dictionary()) amounts = sa_sales.get_amounts_int() receivers = sa_sales.get_receivers() number_nfts = sa_sales.get_number_nfts() token_address = keeper.token.address sales_agreement_id = consumer_instance_no_init.assets.order( ddo.did, sa_sales.index, consumer_account, consumer_account) sales_agreement = keeper.agreement_manager.get_agreement( sales_agreement_id) assert sales_agreement.did == asset_id, '' lock_cond_id = sales_agreement.condition_ids[0] access_cond_id = sales_agreement.condition_ids[1] escrow_cond_id = sales_agreement.condition_ids[2] # transfer the nft keeper.transfer_nft_condition.fulfill(sales_agreement_id, asset_id, consumer_account.address, nft_amounts, lock_cond_id, pub_acc) # escrow payment keeper.escrow_payment_condition.fulfill( sales_agreement_id, asset_id, amounts, receivers, keeper.escrow_payment_condition.address, token_address, lock_cond_id, access_cond_id, pub_acc) assert keeper.condition_manager.get_condition_state(lock_cond_id) == 2, '' assert keeper.condition_manager.get_condition_state( access_cond_id) == 2, '' assert keeper.condition_manager.get_condition_state( escrow_cond_id) == 2, '' assert keeper.did_registry.balance(consumer_account.address, asset_id) >= number_nfts # CHECK ACCESS CREATING THE AGREEMENT nft_access_service_agreement = ServiceAgreement.from_ddo( ServiceTypes.NFT_ACCESS, ddo) sa_access = ddo.get_service(service_type=ServiceTypes.NFT_ACCESS) nft_access_agreement_id = consumer_instance_no_init.assets.order( ddo.did, sa_access.index, consumer_account, consumer_account) event = keeper.nft_access_template.subscribe_agreement_created( nft_access_agreement_id, 10, log_event(keeper.nft_access_template.AGREEMENT_CREATED_EVENT), (), wait=True) assert event, 'no event for AgreementCreated ' time.sleep(3) keeper.nft_holder_condition.fulfill(nft_access_agreement_id, asset_id, consumer_account.address, number_nfts, consumer_account) time.sleep(3) keeper.nft_access_condition.fulfill(nft_access_agreement_id, asset_id, consumer_account.address, pub_acc) time.sleep(3) access_agreement = keeper.agreement_manager.get_agreement( nft_access_agreement_id) assert access_agreement.did == asset_id, '' nft_holder_cond_id = access_agreement.condition_ids[0] nft_access_cond_id = access_agreement.condition_ids[1] assert keeper.condition_manager.get_condition_state( nft_access_cond_id) == 2, '' assert keeper.condition_manager.get_condition_state( nft_holder_cond_id) == 2, '' # AND HERE TESTING WITHOUT CREATING THE AGREEMENT service_access = ddo.get_service(service_type=ServiceTypes.NFT_ACCESS) no_agreement_id = '0x' assert consumer_instance_no_init.assets.access( no_agreement_id, ddo.did, service_access.index, consumer_account, config.downloads_path, service_type=ServiceTypes.NFT_ACCESS) # AND HERE CHECKING CREATING AN AGREEMENT FIRST nft_access_service_agreement = ServiceAgreement.from_ddo( ServiceTypes.NFT_ACCESS, ddo) nft_access_agreement_id = ServiceAgreement.create_new_agreement_id() (nft_access_cond_id, nft_holder_cond_id ) = nft_access_service_agreement.generate_agreement_condition_ids( nft_access_agreement_id, asset_id, consumer_account.address, keeper) keeper.nft_access_template.create_agreement( nft_access_agreement_id, asset_id, [nft_holder_cond_id, nft_access_cond_id], nft_access_service_agreement.conditions_timelocks, nft_access_service_agreement.conditions_timeouts, consumer_account.address, pub_acc) event = keeper.nft_access_template.subscribe_agreement_created( nft_access_agreement_id, 10, log_event(keeper.nft_access_template.AGREEMENT_CREATED_EVENT), (), wait=True) assert event, 'no event for AgreementCreated ' # This is because in this test the gateway is executed using the same account than the consumer keeper.did_registry.add_provider(asset_id, consumer_account.address, pub_acc) time.sleep(3) assert consumer_instance_no_init.assets.access( nft_access_agreement_id, ddo.did, service_access.index, consumer_account, config.downloads_path, index=0, service_type=ServiceTypes.NFT_ACCESS)
def setup_all(): config = ExampleConfig.get_config() Web3Provider.init_web3(config.keeper_url) ContractHandler.set_artifacts_path(config.keeper_path) Keeper.get_instance()
from ocean_keeper.web3_provider import Web3Provider from ocean_utils.aquarius import AquariusProvider from examples import ExampleConfig from squid_py import ConfigProvider from squid_py.ocean.keeper import SquidKeeper as Keeper from tests.resources.helper_functions import ( get_consumer_ocean_instance, get_metadata, get_publisher_account, get_publisher_ocean_instance, get_registered_ddo, setup_logging) from tests.resources.mocks.secret_store_mock import SecretStoreMock from tests.resources.tiers import should_run_test setup_logging() if should_run_test('e2e'): ConfigProvider.set_config(ExampleConfig.get_config()) @pytest.fixture def setup_all(): config = ExampleConfig.get_config() Web3Provider.init_web3(config.keeper_url) ContractHandler.set_artifacts_path(config.keeper_path) Keeper.get_instance() @pytest.fixture def secret_store(): return SecretStoreMock
def buy_asset(): """ Requires all ocean services running. """ setup_logging(default_level=logging.INFO) ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() providers = { 'duero': '0xfEF2d5e1670342b9EF22eeeDcb287EC526B48095', 'nile': '0x4aaab179035dc57b35e2ce066919048686f82972' } # make ocean instance ocn = Ocean() acc = get_account(1) if not acc: acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0] Diagnostics.verify_contracts() metadata = example_metadata.metadata.copy() metadata['main']['dateCreated'] = get_timestamp() keeper = Keeper.get_instance() # Register ddo did = '' if did: ddo = ocn.assets.resolve(did) logging.info(f'using ddo: {did}') else: ddo = ocn.assets.create(metadata, acc, providers=[], use_secret_store=True) assert ddo is not None, f'Registering asset on-chain failed.' did = ddo.did logging.info(f'registered ddo: {did}') # 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 test_net = os.environ.get('TEST_NET', '') if test_net.startswith('nile'): provider = keeper.did_registry.to_checksum_address(providers['nile']) elif test_net.startswith('duero'): provider = keeper.did_registry.to_checksum_address(providers['duero']) else: provider = '0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0' # Wait for did registry event event = keeper.did_registry.subscribe_to_event( keeper.did_registry.DID_REGISTRY_EVENT_NAME, 30, event_filter={ '_did': Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id), '_owner': acc.address}, wait=True ) if not event: logging.warning(f'Failed to get the did registry event for asset with did {did}.') assert keeper.did_registry.get_block_number_updated(ddo.asset_id) > 0, \ f'There is an issue in registering asset {did} on-chain.' keeper.did_registry.add_provider(ddo.asset_id, provider, acc) logging.info(f'is {provider} set as did provider: ' f'{keeper.did_registry.is_did_provider(ddo.asset_id, provider)}') _providers = keeper.did_registry.get_did_providers(ddo.asset_id) access_template_name = keeper.template_manager.SERVICE_TO_TEMPLATE_NAME['access'] template_id = keeper.template_manager.create_template_id(access_template_name) approved = keeper.template_manager.is_template_approved(template_id) print(f'agreement template approved: {approved}') template_data = keeper.template_manager.get_template(template_id) print(f'access agreement template: {template_data}') cons_ocn = Ocean() consumer_account = get_account(0) # sign agreement using the registered asset did above service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) # This will send the order request to Brizo which in turn will execute the agreement on-chain cons_ocn.accounts.request_tokens(consumer_account, 10) sa = ServiceAgreement.from_json(service.as_dictionary()) agreement_id = '' if not agreement_id: agreement_id = cons_ocn.assets.order( did, sa.index, consumer_account) logging.info('placed order: %s, %s', did, agreement_id) event = keeper.agreement_manager.subscribe_agreement_created( agreement_id, 60, None, (), wait=True ) assert event, "Agreement event is not found, check the keeper node's logs" logging.info(f'Got agreement event, next: lock reward condition') event = keeper.lock_reward_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event, "Lock reward condition fulfilled event is not found, check the keeper node's logs" logging.info('Got lock reward event, next: wait for the access condition..') event = keeper.access_secret_store_condition.subscribe_condition_fulfilled( agreement_id, 15, None, (), wait=True ) logging.info(f'Got access event {event}') i = 0 while ocn.agreements.is_access_granted( agreement_id, did, consumer_account.address) is not True and i < 30: time.sleep(1) i += 1 assert ocn.agreements.is_access_granted(agreement_id, did, consumer_account.address) ocn.assets.consume( agreement_id, did, sa.index, consumer_account, config.downloads_path) logging.info('Success buying asset.') event = keeper.escrow_reward_condition.subscribe_condition_fulfilled( agreement_id, 30, None, (), wait=True ) assert event, 'no event for EscrowReward.Fulfilled' logging.info(f'got EscrowReward.FULFILLED event: {event}') logging.info('Done buy asset.')
def test_buy_asset_no_secret_store(publisher_instance_gateway, consumer_instance_gateway): config = ExampleConfig.get_config() ConfigProvider.set_config(config) keeper = Keeper.get_instance() w3 = Web3Provider.get_web3() pub_acc = get_publisher_account() for method in [ServiceAuthorizationTypes.SECRET_STORE, ServiceAuthorizationTypes.PSK_ECDSA, ServiceAuthorizationTypes.PSK_RSA]: # Register ddo ddo = get_registered_with_psk(publisher_instance_gateway, pub_acc, auth_method=method) 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_gateway._config.downloads_path)) if os.path.exists( consumer_instance_gateway._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_gateway.accounts.request_tokens(consumer_account, 100) agreement_id = consumer_instance_gateway.assets.order( ddo.did, sa.index, consumer_account, consumer_account) event_wait_time = 10 event = keeper.access_template.subscribe_agreement_created( agreement_id, event_wait_time, log_event(keeper.access_template.AGREEMENT_CREATED_EVENT), (), wait=True ) assert event, 'no event for EscrowAccessSecretStoreTemplate.AgreementCreated' 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_gateway.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_gateway.agreements.is_access_granted(agreement_id, ddo.did, consumer_account.address) publisher_instance_gateway.agreements.conditions.release_reward( agreement_id, ddo.asset_id, sa.get_amounts_int(), sa.get_receivers(), pub_acc) assert consumer_instance_gateway.assets.access( agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path) assert len(os.listdir(config.downloads_path)) == downloads_path_elements + 1 with pytest.raises(AssertionError): consumer_instance_gateway.assets.access( agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, -2 ) with pytest.raises(AssertionError): consumer_instance_gateway.assets.access( agreement_id, ddo.did, sa.index, consumer_account, config.downloads_path, 3 )
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 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 buy_asset(): """ Requires all Nevermined services running. """ ConfigProvider.set_config(ExampleConfig.get_config()) config = ConfigProvider.get_config() providers = { 'duero': '0xfEF2d5e1670342b9EF22eeeDcb287EC526B48095', 'nile': '0x4aaab179035dc57b35e2ce066919048686f82972' } # make nevermined instance nevermined = Nevermined() Diagnostics.verify_contracts() acc = get_account(1) if not acc: acc = ([acc for acc in nevermined.accounts.list() if acc.password] or nevermined.accounts.list())[0] keeper = Keeper.get_instance() # Register ddo did = '' # 'did:nv:7648596b60f74301ae1ef9baa5d637255d517ff362434754a3779e1de4c8219b' if did: ddo = nevermined.assets.resolve(did) logging.info(f'using ddo: {did}') else: ddo = nevermined.assets.create(example_metadata.metadata, acc, providers=[], authorization_type='SecretStore', use_secret_store=True) assert ddo is not None, f'Registering asset on-chain failed.' did = ddo.did logging.info(f'registered ddo: {did}') # 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 test_net = os.environ.get('TEST_NET', '') if test_net.startswith('nile'): provider = keeper.did_registry.to_checksum_address(providers['nile']) elif test_net.startswith('duero'): provider = keeper.did_registry.to_checksum_address(providers['duero']) else: provider = '0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0' # Wait for did registry event event = keeper.did_registry.subscribe_to_event( keeper.did_registry.DID_REGISTRY_EVENT_NAME, 30, event_filter={ '_did': Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id), '_owner': acc.address}, wait=True ) if not event: logging.warning(f'Failed to get the did registry event for asset with did {did}.') assert keeper.did_registry.get_block_number_updated(ddo.asset_id) > 0, \ f'There is an issue in registering asset {did} on-chain.' keeper.did_registry.add_provider(ddo.asset_id, provider, acc) logging.info(f'is {provider} set as did provider: ' f'{keeper.did_registry.is_did_provider(ddo.asset_id, provider)}') nevermined_cons = Nevermined() consumer_account = get_account(0) # sign agreement using the registered asset did above service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS) # This will send the order request to Gateway which in turn will execute the agreement on-chain nevermined_cons.accounts.request_tokens(consumer_account, 10) sa = ServiceAgreement.from_service_dict(service.as_dictionary()) agreement_id = '' if not agreement_id: # Use these 2 lines to request new agreement from Gateway # agreement_id, signature = nevermined_cons.agreements.prepare(did, sa.service_definition_id, # consumer_account) # nevermined_cons.agreements.send(did, agreement_id, sa.service_definition_id, signature, # consumer_account) # assets.order now creates agreement directly using consumer account. agreement_id = nevermined_cons.assets.order( did, sa.index, consumer_account) logging.info('placed order: %s, %s', did, agreement_id) event = keeper.access_template.subscribe_agreement_created( agreement_id, 60, None, (), wait=True ) assert event, "Agreement event is not found, check the keeper node's logs" logging.info(f'Got agreement event, next: lock reward condition') event = keeper.lock_payment_condition.subscribe_condition_fulfilled( agreement_id, 60, None, (), wait=True ) assert event, "Lock reward condition fulfilled event is not found, check the keeper node's logs" logging.info('Got lock reward event, next: wait for the access condition..') event = keeper.access_condition.subscribe_condition_fulfilled( agreement_id, 15, None, (), wait=True ) logging.info(f'Got access event {event}') i = 0 while nevermined.agreements.is_access_granted( agreement_id, did, consumer_account.address) is not True and i < 15: time.sleep(1) i += 1 assert nevermined.agreements.is_access_granted(agreement_id, did, consumer_account.address) nevermined.assets.access( agreement_id, did, sa.index, consumer_account, config.downloads_path, index=0) logging.info('Success buying asset.') event = keeper.escrow_payment_condition.subscribe_condition_fulfilled( agreement_id, 30, None, (), wait=True ) assert event, 'no event for EscrowReward.Fulfilled' logging.info(f'got EscrowReward.FULFILLED event: {event}') logging.info('Done buy asset.')