コード例 #1
0
def test_ocean_assets_compute(publisher_ocean_instance):
    publisher = get_publisher_wallet()
    metadata = get_computing_metadata()
    metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    ddo = publisher_ocean_instance.assets.create(metadata, publisher)
    assert ddo
    _ddo = wait_for_ddo(publisher_ocean_instance, ddo.did)
    assert _ddo, f"assets.resolve failed for did {ddo.did}"
コード例 #2
0
def test_ocean_assets_algorithm(publisher_ocean_instance):
    publisher = get_publisher_wallet()
    metadata = get_algorithm_ddo()["service"][0]
    metadata["attributes"]["main"]["files"][0]["checksum"] = str(uuid.uuid4())
    ddo = publisher_ocean_instance.assets.create(metadata["attributes"], publisher)
    assert ddo
    _ddo = wait_for_ddo(publisher_ocean_instance, ddo.did)
    assert _ddo, f"assets.resolve failed for did {ddo.did}"
コード例 #3
0
def test_ocean_assets_algorithm(publisher_ocean_instance):
    publisher = get_publisher_wallet()
    metadata = get_algorithm_ddo()['service'][0]
    metadata['attributes']['main']['files'][0]['checksum'] = str(uuid.uuid4())
    ddo = publisher_ocean_instance.assets.create(metadata['attributes'],
                                                 publisher)
    assert ddo
    _ddo = wait_for_ddo(publisher_ocean_instance, ddo.did)
    assert _ddo, f'assets.resolve failed for did {ddo.did}'
コード例 #4
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}'
コード例 #5
0
def test_register_asset(publisher_ocean_instance):
    ocn = publisher_ocean_instance
    ddo_reg = ocn.assets.ddo_registry()
    block = ocn.web3.eth.blockNumber
    alice = get_publisher_wallet()
    bob = get_consumer_wallet()

    def _get_num_assets(_minter):
        dids = [add_0x_prefix(did_to_id(a)) for a in ocn.assets.owner_assets(_minter)]
        dids = [a for a in dids if len(a) == 42]
        return len([a for a in dids if DataToken(a).contract_concise.isMinter(_minter)])

    num_assets_owned = _get_num_assets(alice.address)

    original_ddo = create_asset(ocn, alice)
    assert original_ddo, "create asset failed."

    # try to resolve new asset
    did = original_ddo.did
    asset_id = original_ddo.asset_id
    log = ddo_reg.get_event_log(ddo_reg.EVENT_METADATA_CREATED, block, asset_id, 30)
    assert log, "no ddo created event."

    ddo = wait_for_ddo(ocn, did)
    ddo_dict = ddo.as_dictionary()
    original = original_ddo.as_dictionary()
    assert ddo_dict["publicKey"] == original["publicKey"]
    assert ddo_dict["authentication"] == original["authentication"]
    assert ddo_dict["service"]
    assert original["service"]
    metadata = ddo_dict["service"][0]["attributes"]
    if "datePublished" in metadata["main"]:
        metadata["main"].pop("datePublished")
    assert (
        ddo_dict["service"][0]["attributes"]["main"]["name"]
        == original["service"][0]["attributes"]["main"]["name"]
    )
    assert ddo_dict["service"][1] == original["service"][1]

    # Can't resolve unregistered asset
    unregistered_did = DID.did({"0": "0x00112233445566"})
    with pytest.raises(ValueError):
        ocn.assets.resolve(unregistered_did)

    # Raise error on bad did
    invalid_did = "did:op:0123456789"
    with pytest.raises(ValueError):
        ocn.assets.resolve(invalid_did)

    meta_data_assets = ocn.assets.search("")
    if meta_data_assets:
        print("Currently registered assets:")
        print(meta_data_assets)

    # Publish the metadata
    _ = ddo.metadata["main"]["name"]
    _name = "updated name"
    ddo.metadata["main"]["name"] = _name
    assert ddo.metadata["main"]["name"] == _name
    try:
        ocn.assets.update(ddo, bob)
        assert False, "this asset update should fail, but did not."
    except Exception:
        pass

    _ = ocn.assets.update(ddo, alice)
    log = ddo_reg.get_event_log(ddo_reg.EVENT_METADATA_UPDATED, block, asset_id, 30)
    assert log, "no ddo updated event"
    _asset = wait_for_update(ocn, ddo.did, "name", _name)
    assert _asset, "Cannot read asset after update."
    assert (
        _asset.metadata["main"]["name"] == _name
    ), "updated asset does not have the new updated name !!!"

    assert (
        ocn.assets.owner(ddo.did) == alice.address
    ), "asset owner does not seem correct."

    assert _get_num_assets(alice.address) == num_assets_owned + 1
コード例 #6
0
def test_ocean_assets_search(publisher_ocean_instance, metadata):
    publisher = get_publisher_wallet()
    ddo = publisher_ocean_instance.assets.create(metadata, publisher)
    wait_for_ddo(publisher_ocean_instance, ddo.did)
    assert len(publisher_ocean_instance.assets.search("Monkey")) > 0