コード例 #1
0
    def _process_service_descriptors(self, service_descriptors, metadata,
                                     account):
        brizo = BrizoProvider.get_brizo()
        ddo_service_endpoint = self._get_aquarius().get_service_endpoint()

        service_type_to_descriptor = {sd[0]: sd for sd in service_descriptors}
        _service_descriptors = []
        metadata_service_desc = service_type_to_descriptor.pop(
            ServiceTypes.METADATA,
            ServiceDescriptor.metadata_service_descriptor(
                metadata, ddo_service_endpoint))
        auth_service_desc = service_type_to_descriptor.pop(
            ServiceTypes.AUTHORIZATION,
            ServiceDescriptor.authorization_service_descriptor(
                self._config.secret_store_url))
        _service_descriptors = [metadata_service_desc, auth_service_desc]

        # Always dafault to creating a ServiceTypes.ASSET_ACCESS service if no services are specified
        access_service_descriptor = service_type_to_descriptor.pop(
            ServiceTypes.ASSET_ACCESS,
            ServiceDescriptor.access_service_descriptor(
                self._build_access_service(metadata, account),
                brizo.get_consume_endpoint(self._config),
                self._keeper.escrow_access_secretstore_template.address))
        compute_service_descriptor = service_type_to_descriptor.pop(
            ServiceTypes.CLOUD_COMPUTE, None)

        if access_service_descriptor:
            _service_descriptors.append(access_service_descriptor)
        if compute_service_descriptor:
            _service_descriptors.append(compute_service_descriptor)

        _service_descriptors.extend(service_type_to_descriptor.values())
        return ServiceFactory.build_services(_service_descriptors)
コード例 #2
0
ファイル: ocean_assets.py プロジェクト: dmtree2/ocean.py
    def _process_service_descriptors(self, service_descriptors: list,
                                     metadata: dict, provider_uri: str,
                                     wallet: Wallet) -> list:
        ddo_service_endpoint = self._get_aquarius().get_service_endpoint()

        service_type_to_descriptor = {sd[0]: sd for sd in service_descriptors}
        _service_descriptors = []
        metadata_service_desc = service_type_to_descriptor.pop(
            ServiceTypes.METADATA,
            ServiceDescriptor.metadata_service_descriptor(
                metadata, ddo_service_endpoint))
        _service_descriptors = [
            metadata_service_desc,
        ]

        # Always dafault to creating a ServiceTypes.ASSET_ACCESS service if no services are specified
        access_service_descriptor = service_type_to_descriptor.pop(
            ServiceTypes.ASSET_ACCESS, None)
        compute_service_descriptor = service_type_to_descriptor.pop(
            ServiceTypes.CLOUD_COMPUTE, None)
        # Make an access service only if no services are given by the user.
        if not access_service_descriptor and not compute_service_descriptor:
            access_service_descriptor = ServiceDescriptor.access_service_descriptor(
                self._build_access_service(metadata, 1.0, wallet.address),
                self._data_provider.get_url(self._config))

        if access_service_descriptor:
            _service_descriptors.append(access_service_descriptor)
        if compute_service_descriptor:
            _service_descriptors.append(compute_service_descriptor)

        _service_descriptors.extend(service_type_to_descriptor.values())
        return ServiceFactory.build_services(_service_descriptors)
コード例 #3
0
def test_service_factory():
    ddo = DDO(dictionary=_get_sample_ddo('ddo_sample1.json'))
    type_to_service = {s.type: s for s in ddo.services}
    metadata = ddo.metadata

    md_descriptor = ServiceDescriptor.metadata_service_descriptor(metadata, type_to_service[ServiceTypes.METADATA].service_endpoint)
    access_service = type_to_service[ServiceTypes.ASSET_ACCESS]
    access_descriptor = ServiceDescriptor.access_service_descriptor(access_service.attributes, access_service.service_endpoint, access_service.template_id)
    compute_descriptor = ServiceDescriptor.compute_service_descriptor(access_service.attributes, access_service.service_endpoint, access_service.template_id)

    services = ServiceFactory.build_services([md_descriptor, access_descriptor, compute_descriptor])
    assert len(services) == 3
    assert services[0].type == ServiceTypes.METADATA
    assert services[1].type == ServiceTypes.ASSET_ACCESS
    assert services[2].type == ServiceTypes.CLOUD_COMPUTE

    keeper = Keeper.get_instance()
    name_to_address = {name: i.address for name, i in keeper.contract_name_to_instance.items()}
    s = services[1]
    s.init_conditions_values(ddo.did, name_to_address)
    s = services[2]
    s.init_conditions_values(ddo.did, name_to_address)

    assert isinstance(services[1], ServiceAgreement)
    assert isinstance(services[2], ServiceAgreement)
コード例 #4
0
def create_asset(ocean, wallet, service_attributes, metadata, token_address):

    service_endpoint = DataServiceProvider.get_url(ocean.config)
    download_service = ServiceDescriptor.access_service_descriptor(
        service_attributes, service_endpoint)
    compute_attributes = trying_compute(wallet)
    computer_service = ServiceDescriptor.compute_service_descriptor(
        compute_attributes, service_endpoint)
    print(download_service)
    print(computer_service)
    print("kjashdfkhaskdfhkjhasdjkfhkjasdhfjkasdhjkfhajkdshf")
    asset = ocean.assets.create(metadata,
                                wallet,
                                service_descriptors=[download_service],
                                data_token_address=token_address)
    return asset
コード例 #5
0
ファイル: ddo_helpers.py プロジェクト: akshay-ap/ocean.py
def get_registered_ddo_with_compute_service(ocean_instance,
                                            wallet,
                                            provider_uri=None,
                                            trusted_algorithms=None):
    old_ddo = get_sample_ddo_with_compute_service()
    metadata = old_ddo.metadata
    metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    service = old_ddo.get_service(ServiceTypes.CLOUD_COMPUTE)
    compute_attributes = ocean_instance.compute.create_compute_service_attributes(
        service.attributes["main"]["timeout"],
        service.attributes["main"]["creator"],
        service.attributes["main"]["datePublished"],
        service.attributes["main"]["provider"],
        privacy_attributes=ocean_instance.compute.
        build_service_privacy_attributes(
            trusted_algorithms,
            allow_raw_algorithm=True,
            allow_all_published_algorithms=not bool(trusted_algorithms),
        ),
    )
    compute_service = ServiceDescriptor.compute_service_descriptor(
        compute_attributes, DataServiceProvider.get_url(ocean_instance.config))

    return get_registered_ddo(ocean_instance,
                              metadata,
                              wallet,
                              compute_service,
                              provider_uri=provider_uri)
コード例 #6
0
def test_create_asset_with_different_secret_store(publisher_ocean_instance):
    ocn = publisher_ocean_instance

    sample_ddo_path = get_resource_path('ddo', 'ddo_sa_sample.json')
    assert sample_ddo_path.exists(), "{} does not exist!".format(
        sample_ddo_path)

    acct = ocn.main_account

    aqua = AquariusProvider.get_aquarius(ocn.config.aquarius_url)
    aqua.retire_all_assets()

    asset = DDO(json_filename=sample_ddo_path)
    my_secret_store = 'http://myownsecretstore.com'
    auth_service = ServiceDescriptor.authorization_service_descriptor(
        my_secret_store)
    asset.metadata['main']['name'] += str(datetime.now().timestamp())
    new_asset = ocn.assets.create(asset.metadata, acct, [auth_service])
    assert new_asset.get_service(
        ServiceTypes.AUTHORIZATION).service_endpoint == my_secret_store
    assert new_asset.get_service(ServiceTypes.ASSET_ACCESS)
    assert new_asset.get_service(ServiceTypes.METADATA)
    publisher_ocean_instance.assets.retire(new_asset.did)

    access_service = ServiceDescriptor.access_service_descriptor(
        {
            "main": {
                "name": "dataAssetAccessServiceAgreement",
                "creator": '0x1234',
                "price": '1',
                "timeout": 3600,
                "datePublished": '2019-08-30T12:19:54Z'
            }
        }, 'service/endpoint', '0x0011001100110011')
    asset.metadata['main']['name'] += str(datetime.now().timestamp())
    new_asset = ocn.assets.create(asset.metadata, acct, [access_service])
    assert new_asset.get_service(ServiceTypes.AUTHORIZATION)
    assert new_asset.get_service(ServiceTypes.ASSET_ACCESS)
    assert new_asset.get_service(ServiceTypes.METADATA)
    publisher_ocean_instance.assets.retire(new_asset.did)

    asset.metadata['main']['name'] += str(datetime.now().timestamp())
    new_asset = ocn.assets.create(asset.metadata, acct)
    assert new_asset.get_service(ServiceTypes.AUTHORIZATION)
    assert new_asset.get_service(ServiceTypes.ASSET_ACCESS)
    assert new_asset.get_service(ServiceTypes.METADATA)
    publisher_ocean_instance.assets.retire(new_asset.did)
コード例 #7
0
def create_asset(ocean, publisher):
    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    assert sample_ddo_path.exists(), "{} does not exist!".format(sample_ddo_path)

    asset = DDO(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    my_secret_store = "http://myownsecretstore.com"
    auth_service = ServiceDescriptor.authorization_service_descriptor(my_secret_store)
    return ocean.assets.create(asset.metadata, publisher, [auth_service])
コード例 #8
0
ファイル: ocean_compute.py プロジェクト: surajsjain/ocean.py
    def create_compute_service_descriptor(self, attributes):
        """
        Return a service descriptor (tuple) for service of type ServiceTypes.CLOUD_COMPUTE
        and having the required attributes and service endpoint.

        :param attributes: dict as created in `create_compute_service_attributes`
        """
        compute_endpoint = self._data_provider.get_url(self._config)
        return ServiceDescriptor.compute_service_descriptor(
            attributes=attributes, service_endpoint=compute_endpoint)
コード例 #9
0
ファイル: ddo_helpers.py プロジェクト: fleischr/ocean.py
def get_access_service_descriptor(
    ocean_instance, address, date_created, provider_uri=None, timeout=3600
):
    if not provider_uri:
        provider_uri = DataServiceProvider.get_url(ocean_instance.config)

    return ServiceDescriptor.access_service_descriptor(
        ocean_instance.assets.build_access_service(date_created, 1.0, address, timeout),
        DataServiceProvider.build_download_endpoint(provider_uri)[1],
    )
コード例 #10
0
def get_registered_ddo(ocean_instance, wallet: Wallet):
    metadata = get_metadata()
    metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    ServiceDescriptor.access_service_descriptor(
        ocean_instance.assets._build_access_service(metadata, to_base_18(1),
                                                    wallet),
        DataServiceProvider.get_url(ocean_instance.config),
    )

    block = ocean_instance.web3.eth.blockNumber
    asset = ocean_instance.assets.create(metadata, wallet)
    ddo_reg = ocean_instance.assets.ddo_registry()
    log = ddo_reg.get_event_log(ddo_reg.EVENT_METADATA_CREATED, block,
                                asset.asset_id, 30)
    assert log, "no ddo created event."

    ddo = wait_for_ddo(ocean_instance, asset.did)
    assert ddo, f"resolve did {asset.did} failed."

    return asset
コード例 #11
0
    def create_access_service(attributes, service_endpoint):
        """
        Publish an asset with an `Access` service according to the supplied attributes.

        :param attributes: attributes of the access service, dict
        :param service_endpoint: str URL for initiating service access request
        :return: Service instance or None
        """
        service = ServiceDescriptor.access_service_descriptor(
            attributes, service_endpoint,
            Keeper.get_instance().escrow_access_secretstore_template.address)
        return service
コード例 #12
0
ファイル: ddo_helpers.py プロジェクト: fleischr/ocean.py
def get_registered_ddo_with_compute_service(ocean_instance, wallet, provider_uri=None):
    old_ddo = get_sample_ddo_with_compute_service()
    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(ocean_instance.config)
    )

    return get_registered_ddo(
        ocean_instance, metadata, wallet, compute_service, provider_uri=provider_uri
    )
コード例 #13
0
ファイル: ocean_services.py プロジェクト: opscientia/ocean.py
    def create_access_service(attributes, provider_uri=None):
        """Publish an asset with an `Access` service according to the supplied attributes.

        :param attributes: attributes of the access service, dict
        :param provider_uri: str URL of service provider. This will be used as base to
            construct the serviceEndpoint for the `access` (download) service
        :return: Service instance or None
        """
        service_endpoint = provider_uri or DataServiceProvider.get_url(
            ConfigProvider.get_config())
        service = ServiceDescriptor.access_service_descriptor(
            attributes, service_endpoint)
        return service
コード例 #14
0
def create_asset(publisher_ocean_instance):
    ocn = publisher_ocean_instance
    sample_ddo_path = get_resource_path('ddo', 'ddo_sa_sample.json')
    assert sample_ddo_path.exists(), "{} does not exist!".format(
        sample_ddo_path)

    acct = ocn.main_account

    asset = DDO(json_filename=sample_ddo_path)
    my_secret_store = 'http://myownsecretstore.com'
    auth_service = ServiceDescriptor.authorization_service_descriptor(
        my_secret_store)
    return ocn.assets.create(asset.metadata, acct, [auth_service])
コード例 #15
0
def get_compute_service_descriptor(address, price, metadata):
    compute_service_attributes = {
        "main": {
            "name": "dataAssetComputeServiceAgreement",
            "creator": address,
            "cost": price,
            "timeout": 3600,
            "datePublished": metadata[MetadataMain.KEY]['dateCreated']
        }
    }

    return ServiceDescriptor.compute_service_descriptor(
        compute_service_attributes,
        f'http://localhost:8030{BaseURLs.ASSETS_URL}/compute')
コード例 #16
0
def get_access_service_descriptor(address, metadata):
    access_service_attributes = {
        "main": {
            "name": "dataAssetAccessServiceAgreement",
            "creator": address,
            "cost": metadata[MetadataMain.KEY]['cost'],
            "timeout": 3600,
            "datePublished": metadata[MetadataMain.KEY]['dateCreated']
        }
    }

    return ServiceDescriptor.access_service_descriptor(
        access_service_attributes,
        f'http://localhost:8030{BaseURLs.ASSETS_URL}/download')
コード例 #17
0
ファイル: ocean_compute.py プロジェクト: eruizgar91/squid-py
    def create_compute_service_descriptor(self, attributes):
        """

        :param attributes:
        """
        brizo = BrizoProvider.get_brizo()
        config = ConfigProvider.get_config()
        compute_endpoint = brizo.get_compute_endpoint(config)
        template_id = self._keeper.get_agreement_template_id(
            ServiceTypes.CLOUD_COMPUTE)
        return ServiceDescriptor.compute_service_descriptor(
            attributes=attributes,
            service_endpoint=compute_endpoint,
            template_id=template_id)
コード例 #18
0
def get_access_service_descriptor_different_provider(address, metadata):
    access_service_attributes = {
        "main": {
            "name": "dataAssetAccessServiceAgreement",
            "creator": address,
            "cost": metadata[MetadataMain.KEY]["cost"],
            "timeout": 3600,
            "datePublished": metadata[MetadataMain.KEY]["dateCreated"],
        }
    }

    return ServiceDescriptor.access_service_descriptor(
        access_service_attributes,
        f"http://some_different_provider{BaseURLs.ASSETS_URL}/download",
    )
コード例 #19
0
def get_access_service_descriptor(keeper, account, metadata):
    template_name = keeper.template_manager.SERVICE_TO_TEMPLATE_NAME[
        ServiceTypes.ASSET_ACCESS]
    access_service_attributes = {
        "main": {
            "name": "dataAssetAccessServiceAgreement",
            "creator": account.address,
            "price": metadata[MetadataMain.KEY]['price'],
            "timeout": 3600,
            "datePublished": metadata[MetadataMain.KEY]['dateCreated']
        }
    }

    return ServiceDescriptor.access_service_descriptor(
        access_service_attributes,
        f'http://localhost:8030{BaseURLs.ASSETS_URL}/consume',
        keeper.template_manager.create_template_id(template_name))
コード例 #20
0
def get_compute_service_descriptor(keeper, account, price, metadata):
    template_name = keeper.template_manager.SERVICE_TO_TEMPLATE_NAME[
        ServiceTypes.CLOUD_COMPUTE]
    compute_service_attributes = {
        "main": {
            "name": "dataAssetComputeServiceAgreement",
            "creator": account.address,
            "price": price,
            "timeout": 3600,
            "datePublished": metadata[MetadataMain.KEY]['dateCreated']
        }
    }

    return ServiceDescriptor.compute_service_descriptor(
        compute_service_attributes,
        f'http://localhost:8030{BaseURLs.ASSETS_URL}/compute',
        keeper.template_manager.create_template_id(template_name))
コード例 #21
0
def get_compute_service_descriptor_no_rawalgo(address, price, metadata):
    compute_service_attributes = {
        "main": {
            "name": "dataAssetComputeServiceAgreement",
            "creator": address,
            "cost": price,
            "privacy": {
                "allowRawAlgorithm": False,
                "trustedAlgorithms": [],
                "allowNetworkAccess": True
            },
            "timeout": 3600,
            "datePublished": metadata[MetadataMain.KEY]['dateCreated']
        }
    }

    return ServiceDescriptor.compute_service_descriptor(
        compute_service_attributes,
        f'http://localhost:8030{BaseURLs.ASSETS_URL}/compute')
コード例 #22
0
def test_create_asset_with_address(publisher_ocean_instance):
    """Tests that an asset can be created with specific DT address."""
    ocn = publisher_ocean_instance
    alice = get_publisher_wallet()

    sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json")
    asset = DDO(json_filename=sample_ddo_path)
    asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    my_secret_store = "http://myownsecretstore.com"
    auth_service = ServiceDescriptor.authorization_service_descriptor(
        my_secret_store)

    token = ocn.create_data_token("DataToken1",
                                  "DT1",
                                  from_wallet=alice,
                                  blob="foo_blob")

    assert ocn.assets.create(asset.metadata,
                             alice, [auth_service],
                             data_token_address=token.address)
コード例 #23
0
def get_compute_service_descriptor_specific_algo_dids(address, price, metadata,
                                                      algos):
    compute_service_attributes = {
        "main": {
            "name": "dataAssetComputeServiceAgreement",
            "creator": address,
            "cost": price,
            "privacy": {
                "allowRawAlgorithm": False,
                "allowAllPublishedAlgorithms": False,
                "publisherTrustedAlgorithms": [],
                "allowNetworkAccess": True,
            },
            "timeout": 3600,
            "datePublished": metadata[MetadataMain.KEY]["dateCreated"],
        }
    }

    for algo in algos:
        service = algo.get_service(ServiceTypes.METADATA)
        compute_service_attributes["main"]["privacy"][
            "publisherTrustedAlgorithms"].append({
                "did":
                algo.did,
                "filesChecksum":
                hashlib.sha256(
                    (service.attributes["encryptedFiles"] +
                     json.dumps(service.main["files"], separators=(",", ":"))
                     ).encode("utf-8")).hexdigest(),
                "containerSectionChecksum":
                hashlib.sha256((json.dumps(
                    service.main["algorithm"]["container"],
                    separators=(",", ":"),
                )).encode("utf-8")).hexdigest(),
            })

    return ServiceDescriptor.compute_service_descriptor(
        compute_service_attributes,
        f"http://localhost:8030{BaseURLs.ASSETS_URL}/compute",
    )
コード例 #24
0
def get_compute_service_descriptor_specific_algo_dids(keeper, account, price,
                                                      metadata):
    template_name = keeper.template_manager.SERVICE_TO_TEMPLATE_NAME[
        ServiceTypes.CLOUD_COMPUTE]
    compute_service_attributes = {
        "main": {
            "name": "dataAssetComputeServiceAgreement",
            "creator": account.address,
            "price": price,
            "privacy": {
                "allowRawAlgorithm": True,
                "trustedAlgorithms": ['did:op:123', 'did:op:1234'],
                "allowNetworkAccess": True
            },
            "timeout": 3600,
            "datePublished": metadata[MetadataMain.KEY]['dateCreated']
        }
    }

    return ServiceDescriptor.compute_service_descriptor(
        compute_service_attributes,
        f'http://localhost:8030{BaseURLs.ASSETS_URL}/compute',
        keeper.template_manager.create_template_id(template_name))
コード例 #25
0
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}'
コード例 #26
0
ファイル: ocean_services.py プロジェクト: opscientia/ocean.py
 def create_compute_service(attributes, provider_uri=None):
     service_endpoint = provider_uri or DataServiceProvider.get_url(
         ConfigProvider.get_config())
     return ServiceDescriptor.compute_service_descriptor(
         attributes, service_endpoint)
コード例 #27
0
date_created = "2020-12-01T10:55:11Z"
service_attributes = {
        "main": {
            "name": "dataAssetAccessServiceAgreement",
            "creator": wallet.address,
            "timeout": 3600 * 24,
            "datePublished": date_created,
            "cost": 1.0, # <don't change, this is obsolete>
        }
    }

#service_endpoint = DataServiceProvider.get_url(ocean.config)
service_endpoint = DataServiceProvider.get_url(config)

download_service = ServiceDescriptor.access_service_descriptor(service_attributes, service_endpoint)


metadata =  {
    "main": {
        "type": "dataset", "name": "S1Seven", "author": "Hannes", 
        "license": "CC0: Public Domain", "dateCreated": date_created, 
        "files": [
            { "index": 0, "contentType": "application/zip", "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/training.zip"},
            { "index": 1, "contentType": "text/text", "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/monkey_labels.txt"},
            { "index": 2, "contentType": "application/zip", "url": "https://s3.amazonaws.com/datacommons-seeding-us-east/10_Monkey_Species_Small/assets/validation.zip"}]}
}



#ocean.assets.create will encrypt URLs using Provider's encrypt service endpoint, and update asset before putting on-chain.
コード例 #28
0
def get_registered_ddo(client, wallet, metadata, service_descriptor):
    aqua = Aquarius('http://localhost:5000')
    ddo_service_endpoint = aqua.get_service_endpoint()

    metadata_store_url = json.dumps({'t': 1, 'url': ddo_service_endpoint})
    # Create new data token contract
    addresses = get_contracts_addresses(get_address_file())
    dt_address = addresses.get(DTFactory.CONTRACT_NAME)
    if dt_address:
        factory_contract = DTFactory(dt_address)
    else:
        factory_contract = new_factory_contract()

    ddo_contract_address = addresses.get(MetadataContract.CONTRACT_NAME)
    metadata_contract = MetadataContract(ddo_contract_address)

    tx_id = factory_contract.createToken(metadata_store_url, 'DataToken1',
                                         'DT1', to_base_18(1000000), wallet)
    dt_contract = DataToken(factory_contract.get_token_address(tx_id))
    if not dt_contract:
        raise AssertionError('Creation of data token contract failed.')

    ddo = Asset()
    ddo.data_token_address = dt_contract.address

    metadata_service_desc = ServiceDescriptor.metadata_service_descriptor(
        metadata, ddo_service_endpoint)
    service_descriptors = list([
        ServiceDescriptor.authorization_service_descriptor(
            'http://localhost:12001')
    ])
    service_descriptors.append(service_descriptor)
    service_type = service_descriptor[0]

    service_descriptors = [metadata_service_desc] + service_descriptors

    services = ServiceFactory.build_services(service_descriptors)
    checksums = dict()
    for service in services:
        checksums[str(service.index)] = checksum(service.main)

    # Adding proof to the ddo.
    ddo.add_proof(checksums, wallet)

    did = ddo.assign_did(f'did:op:{remove_0x_prefix(ddo.data_token_address)}')
    ddo_service_endpoint.replace('{did}', did)
    services[0].set_service_endpoint(ddo_service_endpoint)

    stype_to_service = {s.type: s for s in services}
    _service = stype_to_service[service_type]

    for service in services:
        ddo.add_service(service)

    # ddo.proof['signatureValue'] = ocean_lib.sign_hash(
    #     did_to_id_bytes(did), account)

    ddo.add_public_key(did, wallet.address)

    ddo.add_authentication(did, PUBLIC_KEY_TYPE_RSA)

    # if not plecos.is_valid_dict_local(ddo.metadata):
    #     print(f'invalid metadata: {plecos.validate_dict_local(ddo.metadata)}')
    #     assert False, f'invalid metadata: {plecos.validate_dict_local(ddo.metadata)}'

    files_list_str = json.dumps(metadata['main']['files'])
    encrypted_files = encrypt_document(client, did, files_list_str, wallet)
    # encrypted_files = do_encrypt(files_list_str, provider_wallet)

    # only assign if the encryption worked
    if encrypted_files:
        index = 0
        for file in metadata['main']['files']:
            file['index'] = index
            index = index + 1
            del file['url']
        metadata['encryptedFiles'] = encrypted_files

    web3 = Web3Provider.get_web3()
    block = web3.eth.blockNumber
    try:
        data = lzma.compress(web3.toBytes(text=ddo.as_text()))
        tx_id = metadata_contract.create(ddo.asset_id, bytes([1]), data,
                                         wallet)
        if not metadata_contract.verify_tx(tx_id):
            raise AssertionError(
                f'create DDO on-chain failed, transaction status is 0. Transaction hash is {tx_id}'
            )
    except Exception as e:
        print(f'error publishing ddo {ddo.did} in Aquarius: {e}')
        raise

    log = metadata_contract.get_event_log(
        metadata_contract.EVENT_METADATA_CREATED, block, ddo.asset_id, 30)
    assert log, f'no ddo created event.'

    ddo = wait_for_ddo(aqua, ddo.did)
    assert ddo, f'resolve did {ddo.did} failed.'

    return ddo
コード例 #29
0
def test_order(publisher_ocean_instance, consumer_ocean_instance):
    consumer_account = consumer_ocean_instance.main_account
    publisher_account = publisher_ocean_instance.main_account
    keeper = publisher_ocean_instance.keeper
    ocean = consumer_ocean_instance
    BrizoProvider.set_brizo_class(Brizo)

    SecretStoreProvider.set_secret_store_class(SecretStore)
    sample_ddo_path = get_resource_path('ddo', 'ddo_with_compute_service.json')
    old_ddo = DDO(json_filename=sample_ddo_path)
    metadata = old_ddo.metadata
    metadata['main']['files'][0]['checksum'] = str(uuid.uuid4())
    template_name = keeper.template_manager.SERVICE_TO_TEMPLATE_NAME[ServiceTypes.CLOUD_COMPUTE]
    service = old_ddo.get_service(ServiceTypes.CLOUD_COMPUTE)
    brizo = BrizoProvider.get_brizo()
    compute_service = ServiceDescriptor.compute_service_descriptor(
        service.attributes,
        brizo.get_compute_endpoint(ocean.config),
        keeper.template_manager.create_template_id(template_name)
    )
    compute_ddo = publisher_ocean_instance.assets.create(
        metadata,
        publisher_account,
        providers=[],
        service_descriptors=[compute_service],
    )
    did = compute_ddo.did

    _compute_ddo = publisher_ocean_instance.assets.resolve(compute_ddo.did)
    algorithm_ddo_path = get_resource_path('ddo', 'ddo_sample_algorithm.json')
    algo_main = DDO(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)

    try:
        ocean.accounts.request_tokens(publisher_account, 50*keeper.dispenser.get_scale())
    except Exception as err:
        print(f'Requesting tokens failed: {err}')

    agreement_id = ocean.compute.order(
        compute_ddo.did,
        consumer_account,
        None,
        None
    )

    assert agreement_id, f'got agreementId {agreement_id}'

    def fulfill_compute_execution(_, ocn_instance, agr_id, _did, cons_address, account):
        ocn_instance.agreements.conditions.grant_compute(
            agr_id, _did, cons_address, account)

    event = keeper.lock_reward_condition.subscribe_condition_fulfilled(
        agreement_id,
        15,
        fulfill_compute_execution,
        (publisher_ocean_instance, agreement_id, compute_ddo.did,
         consumer_account.address, publisher_account),
        from_block=0,
        wait=True
    )
    assert event, f'lock condition event is not found.'

    print(f'processed agreement and got lockReward fulfilled: '
          f'{agreement_id}, {compute_ddo.did}, ')
    event = keeper.compute_execution_condition.subscribe_condition_fulfilled(
        agreement_id,
        20,
        None,
        (agreement_id, compute_ddo.did, consumer_account, None, algorithm_meta),
        from_block=0,
        wait=True
    )
    assert event, f'compute execution condition event is not found.'

    job_id = consumer_ocean_instance.compute.start(agreement_id, consumer_account, algorithm_meta=algorithm_meta)
    assert job_id, f'expected a job id, got {job_id}'

    status = consumer_ocean_instance.compute.status(agreement_id, job_id, consumer_account)
    print(f'got job status: {status}')
    assert status and status['ok'], f'something not right about the compute job, got status: {status}'

    status = consumer_ocean_instance.compute.stop(agreement_id, job_id, consumer_account)
    print(f'got job status after requesting stop: {status}')
    assert status, f'something not right about the compute job, got status: {status}'
コード例 #30
0
def get_registered_ddo(account, providers=None):
    keeper = keeper_instance()
    aqua = Aquarius('http://localhost:5000')

    for did in aqua.list_assets():
        aqua.retire_asset_ddo(did)

    metadata = get_sample_ddo()['service'][0]['attributes']
    metadata['main']['files'][0]['checksum'] = str(uuid.uuid4())
    ddo = DDO()
    ddo_service_endpoint = aqua.get_service_endpoint()

    metadata_service_desc = ServiceDescriptor.metadata_service_descriptor(
        metadata, ddo_service_endpoint)

    access_service_attributes = {
        "main": {
            "name": "dataAssetAccessServiceAgreement",
            "creator": account.address,
            "price": metadata[MetadataMain.KEY]['price'],
            "timeout": 3600,
            "datePublished": metadata[MetadataMain.KEY]['dateCreated']
        }
    }

    service_descriptors = [
        ServiceDescriptor.authorization_service_descriptor(
            'http://localhost:12001')
    ]
    service_descriptors += [
        ServiceDescriptor.access_service_descriptor(
            access_service_attributes, 'http://localhost:8030',
            keeper.escrow_access_secretstore_template.address)
    ]

    service_descriptors = [metadata_service_desc] + service_descriptors

    services = ServiceFactory.build_services(service_descriptors)
    checksums = dict()
    for service in services:
        checksums[str(service.index)] = checksum(service.main)

    # Adding proof to the ddo.
    ddo.add_proof(checksums, account)

    did = ddo.assign_did(DID.did(ddo.proof['checksum']))

    stype_to_service = {s.type: s for s in services}
    access_service = stype_to_service[ServiceTypes.ASSET_ACCESS]

    name_to_address = {
        cname: cinst.address
        for cname, cinst in keeper.contract_name_to_instance.items()
    }
    access_service.init_conditions_values(
        did, contract_name_to_address=name_to_address)
    ddo.add_service(access_service)
    for service in services:
        ddo.add_service(service)

    ddo.proof['signatureValue'] = keeper.sign_hash(did_to_id_bytes(did),
                                                   account)

    ddo.add_public_key(did, account.address)

    ddo.add_authentication(did, PUBLIC_KEY_TYPE_RSA)

    encrypted_files = do_secret_store_encrypt(
        remove_0x_prefix(ddo.asset_id), json.dumps(metadata['main']['files']),
        account, get_config())
    _files = metadata['main']['files']
    # only assign if the encryption worked
    if encrypted_files:
        index = 0
        for file in metadata['main']['files']:
            file['index'] = index
            index = index + 1
            del file['url']
        metadata['encryptedFiles'] = encrypted_files

    keeper_instance().did_registry.register(
        ddo.asset_id,
        checksum=web3().toBytes(hexstr=ddo.asset_id),
        url=ddo_service_endpoint,
        account=account,
        providers=providers)
    aqua.publish_asset_ddo(ddo)
    return ddo