Esempio n. 1
0
def test_compute_not_an_algo(client):
    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    (
        _,
        did,
        tx_id,
        sa,
        data_token,
        _,
        alg_data_token,
        _,
        alg_tx_id,
    ) = build_and_send_ddo_with_compute_service(
        client, asset_type="allow_all_published")

    nonce = get_nonce(client, cons_wallet.address)
    # prepare consumer signature on did
    msg = f"{cons_wallet.address}{did}{str(nonce)}"
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = Web3Helper.sign_hash(_hash, cons_wallet)

    # Start the compute job
    payload = dict({
        "signature":
        signature,
        "documentId":
        did,
        "serviceId":
        sa.index,
        "serviceType":
        sa.type,
        "consumerAddress":
        cons_wallet.address,
        "transferTxId":
        tx_id,
        "dataToken":
        data_token,
        "output":
        build_stage_output_dict(dict(), sa.service_endpoint,
                                cons_wallet.address, pub_wallet),
        "algorithmDid":
        did,  # intentionally, should not be an algo did
        "algorithmDataToken":
        alg_data_token,
        "algorithmTransferTxId":
        alg_tx_id,
    })

    # Start compute with valid signature
    payload["signature"] = signature
    compute_endpoint = BaseURLs.ASSETS_URL + "/compute"
    response = client.post(compute_endpoint,
                           data=json.dumps(payload),
                           content_type="application/json")
    assert response.status == "400 BAD REQUEST"
    error = response.get_json()["error"]
    assert "is not a valid algorithm" in error
Esempio n. 2
0
def test_compute_norawalgo_allowed(client):
    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    # publish a dataset asset
    dataset_ddo_w_compute_service = get_dataset_ddo_with_compute_service_no_rawalgo(client, pub_wallet)
    did = dataset_ddo_w_compute_service.did
    ddo = dataset_ddo_w_compute_service
    data_token = dataset_ddo_w_compute_service.data_token_address
    dt_contract = DataToken(data_token)
    mint_tokens_and_wait(dt_contract, cons_wallet, pub_wallet)

    # CHECKPOINT 1
    algorithm_meta = {
        "rawcode": "console.log('Hello world'!)",
        "format": 'docker-image',
        "version": '0.1',
        "container": {
            "entrypoint": 'node $ALGO',
            "image": 'node',
            "tag": '10'
        }
    }
    # prepare parameter values for the compute endpoint
    # signature, documentId, consumerAddress, and algorithmDid or algorithmMeta

    sa = ServiceAgreement.from_ddo(ServiceTypes.CLOUD_COMPUTE, dataset_ddo_w_compute_service)
    tx_id = send_order(client, ddo, dt_contract, sa, cons_wallet)
    nonce = get_nonce(client, cons_wallet.address)

    # prepare consumer signature on did
    msg = f'{cons_wallet.address}{did}{nonce}'
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = Web3Helper.sign_hash(_hash, cons_wallet)

    # Start the compute job
    payload = dict({
        'signature': signature,
        'documentId': did,
        'serviceId': sa.index,
        'serviceType': sa.type,
        'consumerAddress': cons_wallet.address,
        'transferTxId': tx_id,
        'dataToken': data_token,
        'output': build_stage_output_dict(dict(), dataset_ddo_w_compute_service, cons_wallet.address, pub_wallet),
        'algorithmDid': '',
        'algorithmMeta': algorithm_meta,
        'algorithmDataToken': ''
    })

    compute_endpoint = BaseURLs.ASSETS_URL + '/compute'
    response = client.post(
        compute_endpoint,
        data=json.dumps(payload),
        content_type='application/json'
    )
    assert response.status == '400 BAD REQUEST', f'start compute job failed: {response.status} , { response.data}'
Esempio n. 3
0
def test_download_service(client):
    aqua = Aquarius("http://localhost:5000")
    try:
        for did in aqua.list_assets():
            aqua.retire_asset_ddo(did)
    except (ValueError, Exception):
        pass

    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    ddo = get_dataset_ddo_with_access_service(client, pub_wallet)
    dt_address = ddo.as_dictionary()["dataToken"]
    dt_token = DataToken(dt_address)
    mint_tokens_and_wait(dt_token, cons_wallet, pub_wallet)

    sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)
    tx_id = send_order(client, ddo, dt_token, sa, cons_wallet)
    index = 0
    download_endpoint = BaseURLs.ASSETS_URL + "/download"
    # Consume using url index and auth token
    # (let the provider do the decryption)
    payload = dict({
        "documentId": ddo.did,
        "serviceId": sa.index,
        "serviceType": sa.type,
        "dataToken": dt_address,
        "consumerAddress": cons_wallet.address,
    })
    payload["signature"] = generate_auth_token(cons_wallet)
    payload["transferTxId"] = tx_id
    payload["fileIndex"] = index
    request_url = (download_endpoint + "?" +
                   "&".join([f"{k}={v}" for k, v in payload.items()]))
    response = client.get(request_url)
    assert response.status_code == 200, f"{response.data}"

    # Consume using url index and signature (withOUT nonce), should fail
    _hash = add_ethereum_prefix_and_hash_msg(ddo.did)
    payload["signature"] = Web3Helper.sign_hash(_hash, cons_wallet)
    request_url = (download_endpoint + "?" +
                   "&".join([f"{k}={v}" for k, v in payload.items()]))
    print(
        ">>>> Expecting InvalidSignatureError from the download endpoint <<<<"
    )  # noqa
    response = client.get(request_url)
    assert response.status_code == 400, f"{response.data}"

    # Consume using url index and signature (with nonce)
    nonce = get_nonce(client, cons_wallet.address)
    _hash = add_ethereum_prefix_and_hash_msg(f"{ddo.did}{nonce}")
    payload["signature"] = Web3Helper.sign_hash(_hash, cons_wallet)
    request_url = (download_endpoint + "?" +
                   "&".join([f"{k}={v}" for k, v in payload.items()]))
    response = client.get(request_url)
    assert response.status_code == 200, f"{response.data}"
def test_initialize_on_ipfs_url(client):
    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    ddo = get_dataset_with_ipfs_url_ddo(client, pub_wallet)
    data_token = ddo.data_token_address
    dt_contract = DataToken(data_token)
    sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)

    send_order(client, ddo, dt_contract, sa, cons_wallet)
Esempio n. 5
0
def test_compute_diff_provider(client):
    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    (
        _,
        did,
        tx_id,
        sa,
        data_token,
        alg_ddo,
        alg_data_token,
        _,
        alg_tx_id,
    ) = build_and_send_ddo_with_compute_service(client, alg_diff=True)

    nonce = get_nonce(client, cons_wallet.address)
    # prepare consumer signature on did
    msg = f"{cons_wallet.address}{did}{str(nonce)}"
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = Web3Helper.sign_hash(_hash, cons_wallet)

    # Start the compute job
    payload = dict({
        "signature":
        signature,
        "documentId":
        did,
        "serviceId":
        sa.index,
        "serviceType":
        sa.type,
        "consumerAddress":
        cons_wallet.address,
        "transferTxId":
        tx_id,
        "dataToken":
        data_token,
        "output":
        build_stage_output_dict(dict(), sa.service_endpoint,
                                cons_wallet.address, pub_wallet),
        "algorithmDid":
        alg_ddo.did,
        "algorithmDataToken":
        alg_data_token,
        "algorithmTransferTxId":
        alg_tx_id,
    })

    compute_endpoint = BaseURLs.ASSETS_URL + "/compute"
    response = client.post(compute_endpoint,
                           data=json.dumps(payload),
                           content_type="application/json")

    assert response.status == "200 OK", f"start compute job failed: {response.data}"
Esempio n. 6
0
def test_download_service(client):
    aqua = Aquarius('http://localhost:5000')
    try:
        for did in aqua.list_assets():
            aqua.retire_asset_ddo(did)
    except (ValueError, Exception):
        pass

    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    ddo = get_dataset_ddo_with_access_service(client, pub_wallet)
    dt_address = ddo.as_dictionary()['dataToken']
    dt_token = DataToken(dt_address)
    mint_tokens_and_wait(dt_token, cons_wallet, pub_wallet)

    sa = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, ddo)
    tx_id = send_order(client, ddo, dt_token, sa, cons_wallet)
    index = 0
    download_endpoint = BaseURLs.ASSETS_URL + '/download'
    # Consume using url index and auth token (let the provider do the decryption)
    payload = dict({
        'documentId': ddo.did,
        'serviceId': sa.index,
        'serviceType': sa.type,
        'dataToken': dt_address,
        'consumerAddress': cons_wallet.address
    })
    payload['signature'] = generate_auth_token(cons_wallet)
    payload['transferTxId'] = tx_id
    payload['fileIndex'] = index
    request_url = download_endpoint + '?' + '&'.join(
        [f'{k}={v}' for k, v in payload.items()])
    response = client.get(request_url)
    assert response.status_code == 200, f'{response.data}'

    # Consume using url index and signature (withOUT nonce), should fail
    _hash = add_ethereum_prefix_and_hash_msg(ddo.did)
    payload['signature'] = Web3Helper.sign_hash(_hash, cons_wallet)
    request_url = download_endpoint + '?' + '&'.join(
        [f'{k}={v}' for k, v in payload.items()])
    print(
        '>>>> Expecting InvalidSignatureError from the download endpoint <<<<')
    response = client.get(request_url)
    assert response.status_code == 401, f'{response.data}'

    # Consume using url index and signature (with nonce)
    nonce = get_nonce(client, cons_wallet.address)
    _hash = add_ethereum_prefix_and_hash_msg(f'{ddo.did}{nonce}')
    payload['signature'] = Web3Helper.sign_hash(_hash, cons_wallet)
    request_url = download_endpoint + '?' + '&'.join(
        [f'{k}={v}' for k, v in payload.items()])
    response = client.get(request_url)
    assert response.status_code == 200, f'{response.data}'
Esempio n. 7
0
def test_compute_specific_algo_dids(client):
    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    # publish a dataset asset
    dataset_ddo_w_compute_service = get_dataset_ddo_with_compute_service_specific_algo_dids(client, pub_wallet)
    did = dataset_ddo_w_compute_service.did
    ddo = dataset_ddo_w_compute_service
    data_token = dataset_ddo_w_compute_service.as_dictionary()['dataToken']
    dt_contract = DataToken(data_token)
    mint_tokens_and_wait(dt_contract, cons_wallet, pub_wallet)

    # publish an algorithm asset (asset with metadata of type `algorithm`)
    alg_ddo = get_algorithm_ddo(client, cons_wallet)
    alg_data_token = alg_ddo.as_dictionary()['dataToken']
    alg_dt_contract = DataToken(alg_data_token)
    mint_tokens_and_wait(alg_dt_contract, pub_wallet, cons_wallet)
    # CHECKPOINT 1

    sa = ServiceAgreement.from_ddo(ServiceTypes.CLOUD_COMPUTE, dataset_ddo_w_compute_service)
    tx_id = send_order(client, ddo, dt_contract, sa, cons_wallet)
    nonce = get_nonce(client, cons_wallet.address)

    # prepare consumer signature on did
    msg = f'{cons_wallet.address}{did}{nonce}'
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = Web3Helper.sign_hash(_hash, cons_wallet)

    # Start the compute job
    payload = dict({
        'signature': signature,
        'documentId': did,
        'serviceId': sa.index,
        'serviceType': sa.type,
        'consumerAddress': cons_wallet.address,
        'transferTxId': tx_id,
        'dataToken': data_token,
        'output': build_stage_output_dict(dict(), dataset_ddo_w_compute_service, cons_wallet.address, pub_wallet),
        'algorithmDid': alg_ddo.did,
        'algorithmMeta': {},
        'algorithmDataToken': alg_data_token
    })

    compute_endpoint = BaseURLs.ASSETS_URL + '/compute'
    response = client.post(
        compute_endpoint,
        data=json.dumps(payload),
        content_type='application/json'
    )
    assert response.status == '400 BAD REQUEST', f'start compute job failed: {response.status} , { response.data}'
Esempio n. 8
0
def setup_all():
    setup_network()
    web3 = Web3Provider.get_web3()
    if web3.eth.accounts and web3.eth.accounts[0].lower(
    ) == '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260'.lower():
        wallet = Wallet(
            web3,
            private_key=
            '0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58'
        )

        provider = get_provider_wallet()
        if web3.fromWei(Web3Helper.get_ether_balance(provider.address),
                        'ether') < 10:
            Web3Helper.send_ether(wallet, provider.address, 25)

        consumer = get_consumer_wallet()
        if web3.fromWei(Web3Helper.get_ether_balance(consumer.address),
                        'ether') < 10:
            Web3Helper.send_ether(wallet, consumer.address, 25)
Esempio n. 9
0
def setup_all():
    setup_network()
    web3 = Web3Provider.get_web3()
    if (
        web3.eth.accounts
        and web3.eth.accounts[0].lower()
        == "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260".lower()
    ):
        wallet = Wallet(
            web3,
            private_key="0xfd5c1ccea015b6d663618850824154a3b3fb2882c46cefb05b9a93fea8c3d215",
        )

        provider = get_provider_wallet()
        if web3.fromWei(Web3Helper.get_ether_balance(provider.address), "ether") < 10:
            Web3Helper.send_ether(wallet, provider.address, 25)

        consumer = get_consumer_wallet()
        if web3.fromWei(Web3Helper.get_ether_balance(consumer.address), "ether") < 10:
            Web3Helper.send_ether(wallet, consumer.address, 25)
Esempio n. 10
0
def test_compute_norawalgo_allowed(client):
    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    # publish a dataset asset
    dataset_ddo_w_compute_service = comp_ds_no_rawalgo(client, pub_wallet)
    did = dataset_ddo_w_compute_service.did
    ddo = dataset_ddo_w_compute_service

    data_token = dataset_ddo_w_compute_service.data_token_address
    dt_contract = DataToken(data_token)
    mint_tokens_and_wait(dt_contract, cons_wallet, pub_wallet)

    # CHECKPOINT 1
    algorithm_meta = {
        "rawcode": "console.log('Hello world'!)",
        "format": "docker-image",
        "version": "0.1",
        "container": {
            "entrypoint": "node $ALGO",
            "image": "node",
            "tag": "10"
        },
    }
    # prepare parameter values for the compute endpoint
    # signature, documentId, consumerAddress, and algorithmDid or algorithmMeta

    sa = ServiceAgreement.from_ddo(ServiceTypes.CLOUD_COMPUTE,
                                   dataset_ddo_w_compute_service)
    tx_id = send_order(client, ddo, dt_contract, sa, cons_wallet)
    nonce = get_nonce(client, cons_wallet.address)

    # prepare consumer signature on did
    msg = f"{cons_wallet.address}{did}{nonce}"
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = Web3Helper.sign_hash(_hash, cons_wallet)

    # Start the compute job
    payload = dict({
        "signature":
        signature,
        "documentId":
        did,
        "serviceId":
        sa.index,
        "serviceType":
        sa.type,
        "consumerAddress":
        cons_wallet.address,
        "transferTxId":
        tx_id,
        "dataToken":
        data_token,
        "output":
        build_stage_output_dict(dict(), sa.service_endpoint,
                                cons_wallet.address, pub_wallet),
        "algorithmMeta":
        algorithm_meta,
        "algorithmDataToken":
        "",
    })

    compute_endpoint = BaseURLs.ASSETS_URL + "/compute"
    response = client.post(compute_endpoint,
                           data=json.dumps(payload),
                           content_type="application/json")
    assert (response.status == "400 BAD REQUEST"
            ), f"start compute job failed: {response.status} , {response.data}"
Esempio n. 11
0
def test_compute(client):
    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    (
        _,
        did,
        tx_id,
        sa,
        data_token,
        alg_ddo,
        alg_data_token,
        _,
        alg_tx_id,
    ) = build_and_send_ddo_with_compute_service(client)

    nonce = get_nonce(client, cons_wallet.address)
    # prepare consumer signature on did
    msg = f"{cons_wallet.address}{did}{str(nonce)}"
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = Web3Helper.sign_hash(_hash, cons_wallet)

    # Start the compute job
    payload = dict({
        "signature":
        signature,
        "documentId":
        did,
        "serviceId":
        sa.index,
        "serviceType":
        sa.type,
        "consumerAddress":
        cons_wallet.address,
        "transferTxId":
        tx_id,
        "dataToken":
        data_token,
        "output":
        build_stage_output_dict(dict(), sa.service_endpoint,
                                cons_wallet.address, pub_wallet),
        "algorithmDid":
        alg_ddo.did,
        "algorithmDataToken":
        alg_data_token,
        "algorithmTransferTxId":
        alg_tx_id,
    })

    # Start compute using invalid signature (withOUT nonce), should fail
    msg = f"{cons_wallet.address}{did}"
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    payload["signature"] = Web3Helper.sign_hash(_hash, cons_wallet)
    compute_endpoint = BaseURLs.ASSETS_URL + "/compute"
    response = client.post(compute_endpoint,
                           data=json.dumps(payload),
                           content_type="application/json")

    assert response.status_code == 400, f"{response.data}"

    # Start compute with valid signature
    payload["signature"] = signature
    response = client.post(compute_endpoint,
                           data=json.dumps(payload),
                           content_type="application/json")
    assert response.status == "200 OK", f"start compute job failed: {response.data}"
    job_info = response.json[0]
    print(f"got response from starting compute job: {job_info}")
    job_id = job_info.get("jobId", "")

    nonce = get_nonce(client, cons_wallet.address)
    msg = f"{cons_wallet.address}{job_id}{did}{nonce}"
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = Web3Helper.sign_hash(_hash, cons_wallet)

    payload = dict({
        "signature": signature,
        "documentId": did,
        "consumerAddress": cons_wallet.address,
        "jobId": job_id,
    })

    job_info = get_compute_job_info(client, compute_endpoint, payload)
    assert job_info, f"Failed to get job info for jobId {job_id}"
    print(f"got info for compute job {job_id}: {job_info}")
    assert job_info["statusText"] in get_possible_compute_job_status_text()

    # get compute job status without signature should return limited status info
    payload.pop("signature")
    job_info = get_compute_job_info(client, compute_endpoint, payload)
    assert job_info, f"Failed to get job status without signature: payload={payload}"
    assert "owner" not in job_info, "owner should not be in this status response"
    assert (
        "resultsUrl"
        not in job_info), "resultsUrl should not be in this status response"
    assert ("algorithmLogUrl" not in job_info
            ), "algorithmLogUrl should not be in this status response"
    assert (
        "resultsDid"
        not in job_info), "resultsDid should not be in this status response"

    payload["signature"] = ""
    job_info = get_compute_job_info(client, compute_endpoint, payload)
    assert job_info, f"Failed to get job status without signature: payload={payload}"
    assert "owner" not in job_info, "owner should not be in this status response"
    assert (
        "resultsUrl"
        not in job_info), "resultsUrl should not be in this status response"
    assert ("algorithmLogUrl" not in job_info
            ), "algorithmLogUrl should not be in this status response"
    assert (
        "resultsDid"
        not in job_info), "resultsDid should not be in this status response"
Esempio n. 12
0
def test_compute(client):

    pub_wallet = get_publisher_wallet()
    cons_wallet = get_consumer_wallet()

    # publish a dataset asset
    dataset_ddo_w_compute_service = get_dataset_ddo_with_compute_service(client, pub_wallet)
    did = dataset_ddo_w_compute_service.did
    ddo = dataset_ddo_w_compute_service
    data_token = dataset_ddo_w_compute_service.data_token_address
    dt_contract = DataToken(data_token)
    mint_tokens_and_wait(dt_contract, cons_wallet, pub_wallet)

    # publish an algorithm asset (asset with metadata of type `algorithm`)
    alg_ddo = get_algorithm_ddo(client, cons_wallet)
    alg_data_token = alg_ddo.as_dictionary()['dataToken']
    alg_dt_contract = DataToken(alg_data_token)
    mint_tokens_and_wait(alg_dt_contract, cons_wallet, cons_wallet)

    sa = ServiceAgreement.from_ddo(ServiceTypes.CLOUD_COMPUTE, dataset_ddo_w_compute_service)
    tx_id = send_order(client, ddo, dt_contract, sa, cons_wallet)

    alg_service = ServiceAgreement.from_ddo(ServiceTypes.ASSET_ACCESS, alg_ddo)
    alg_tx_id = send_order(client, alg_ddo, alg_dt_contract, alg_service, cons_wallet)

    nonce = get_nonce(client, cons_wallet.address)
    # prepare consumer signature on did
    msg = f'{cons_wallet.address}{did}{str(nonce)}'
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = Web3Helper.sign_hash(_hash, cons_wallet)

    # Start the compute job
    payload = dict({
        'signature': signature,
        'documentId': did,
        'serviceId': sa.index,
        'serviceType': sa.type,
        'consumerAddress': cons_wallet.address,
        'transferTxId': tx_id,
        'dataToken': data_token,
        'output': build_stage_output_dict(dict(), dataset_ddo_w_compute_service, cons_wallet.address, pub_wallet),
        'algorithmDid': alg_ddo.did,
        'algorithmMeta': {},
        'algorithmDataToken': alg_data_token,
        'algorithmTransferTxId': alg_tx_id
    })

    # Start compute using invalid signature (withOUT nonce), should fail
    msg = f'{cons_wallet.address}{did}'
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    payload['signature'] = Web3Helper.sign_hash(_hash, cons_wallet)
    compute_endpoint = BaseURLs.ASSETS_URL + '/compute'
    response = client.post(
        compute_endpoint,
        data=json.dumps(payload),
        content_type='application/json'
    )

    assert response.status_code == 401, f'{response.data}'

    # Start compute with valid signature
    payload['signature'] = signature
    response = client.post(
        compute_endpoint,
        data=json.dumps(payload),
        content_type='application/json'
    )
    assert response.status == '200 OK', f'start compute job failed: {response.data}'
    job_info = response.json[0]
    print(f'got response from starting compute job: {job_info}')
    job_id = job_info.get('jobId', '')

    nonce = get_nonce(client, cons_wallet.address)
    msg = f'{cons_wallet.address}{job_id}{did}{nonce}'
    _hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = Web3Helper.sign_hash(_hash, cons_wallet)

    payload = dict({
        'signature': signature,
        'documentId': did,
        'consumerAddress': cons_wallet.address,
        'jobId': job_id,
    })

    job_info = get_compute_job_info(client, compute_endpoint, payload)
    assert job_info, f'Failed to get job info for jobId {job_id}'
    print(f'got info for compute job {job_id}: {job_info}')
    assert job_info['statusText'] in get_possible_compute_job_status_text()

    # get compute job status without signature should return limited status info
    payload.pop('signature')
    job_info = get_compute_job_info(client, compute_endpoint, payload)
    assert job_info, f'Failed to get job status without signature: payload={payload}'
    assert 'owner' not in job_info, 'owner should not be in this status response'
    assert 'resultsUrl' not in job_info, 'resultsUrl should not be in this status response'
    assert 'algorithmLogUrl' not in job_info, 'algorithmLogUrl should not be in this status response'
    assert 'resultsDid' not in job_info, 'resultsDid should not be in this status response'

    payload['signature'] = ''
    job_info = get_compute_job_info(client, compute_endpoint, payload)
    assert job_info, f'Failed to get job status without signature: payload={payload}'
    assert 'owner' not in job_info, 'owner should not be in this status response'
    assert 'resultsUrl' not in job_info, 'resultsUrl should not be in this status response'
    assert 'algorithmLogUrl' not in job_info, 'algorithmLogUrl should not be in this status response'
    assert 'resultsDid' not in job_info, 'resultsDid should not be in this status response'
Esempio n. 13
0
def test_fails(client):
    provider_wallet = get_provider_wallet()
    consumer_address = get_consumer_wallet().address
    pub_wallet = get_publisher_wallet()

    (
        dataset,
        did,
        tx_id,
        sa,
        _,
        alg_ddo,
        alg_data_token,
        _,
        alg_tx_id,
    ) = build_and_send_ddo_with_compute_service(client)

    # output key is invalid
    data = {
        "documentId": did,
        "transferTxId": tx_id,
        "serviceId": sa.index,
        "output": "this can not be decoded",
        "algorithmDid": alg_ddo.did,
        "algorithmDataToken": alg_data_token,
        "algorithmTransferTxId": alg_tx_id,
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert validator.error == "Output is invalid or can not be decoded."

    # algorithmDid is not actually an algorithm
    data = {
        "documentId":
        did,
        "transferTxId":
        tx_id,
        "serviceId":
        sa.index,
        "output":
        build_stage_output_dict(dict(), sa.service_endpoint, consumer_address,
                                pub_wallet),
        "algorithmDid":
        did,
        "algorithmDataToken":
        alg_data_token,
        "algorithmTransferTxId":
        alg_tx_id,
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert validator.error == f"DID {did} is not a valid algorithm"

    valid_output = build_stage_output_dict(dict(), sa.service_endpoint,
                                           consumer_address, pub_wallet)

    # algorithmMeta doesn't contain 'url' or 'rawcode'
    data = {
        "documentId": did,
        "transferTxId": tx_id,
        "serviceId": sa.index,
        "output": valid_output,
        "algorithmMeta": {},
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert (
        validator.error ==
        "algorithmMeta must define one of `url` or `rawcode` or `remote`, but all seem missing."
    )

    # algorithmMeta container is empty
    data = {
        "documentId": did,
        "transferTxId": tx_id,
        "serviceId": sa.index,
        "output": valid_output,
        "algorithmMeta": {
            "rawcode": "console.log('Hello world'!)",
            "format": "docker-image",
            "version": "0.1",
            "container": {},
        },
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert (
        validator.error ==
        "algorithm `container` must specify values for all of entrypoint, image and tag."
    )

    # algorithmMeta container is missing image
    data = {
        "documentId": did,
        "transferTxId": tx_id,
        "serviceId": sa.index,
        "output": valid_output,
        "algorithmMeta": {
            "rawcode": "console.log('Hello world'!)",
            "format": "docker-image",
            "version": "0.1",
            "container": {
                "entrypoint": "node $ALGO",
                "tag": "10"
            },
        },
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert (
        validator.error ==
        "algorithm `container` must specify values for all of entrypoint, image and tag."
    )

    # Additional Input validations ###
    data = {
        "documentId": did,
        "transferTxId": tx_id,
        "serviceId": sa.index,
        "output": valid_output,
        "algorithmDid": alg_ddo.did,
        "algorithmDataToken": alg_data_token,
        "algorithmTransferTxId": alg_tx_id,
        "additionalInputs": "",
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is True

    # additional input is invalid
    data = {
        "documentId": did,
        "transferTxId": tx_id,
        "serviceId": sa.index,
        "output": valid_output,
        "algorithmDid": alg_ddo.did,
        "algorithmDataToken": alg_data_token,
        "algorithmTransferTxId": alg_tx_id,
        "additionalInputs": "i can not be decoded in json!",
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert validator.error == "Additional input is invalid or can not be decoded."

    # Missing did in additional input
    data = {
        "documentId": did,
        "transferTxId": tx_id,
        "serviceId": sa.index,
        "output": valid_output,
        "algorithmDid": alg_ddo.did,
        "algorithmDataToken": alg_data_token,
        "algorithmTransferTxId": alg_tx_id,
        "additionalInputs": [{
            "transferTxId": tx_id,
            "serviceId": sa.index
        }],
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert validator.error == "Error in input at index 1: No documentId in input item."

    # Did is not valid
    data = {
        "documentId":
        did,
        "transferTxId":
        tx_id,
        "serviceId":
        sa.index,
        "output":
        valid_output,
        "algorithmDid":
        alg_ddo.did,
        "algorithmDataToken":
        alg_data_token,
        "algorithmTransferTxId":
        alg_tx_id,
        "additionalInputs": [{
            "documentId": "i am not a did",
            "transferTxId": tx_id,
            "serviceId": sa.index,
        }],
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert (
        validator.error ==
        "Error in input at index 1: Asset for did i am not a did not found.")

    # Service is not compute, nor access
    other_service = [
        s for s in dataset.services if s.type not in
        [ServiceTypes.CLOUD_COMPUTE, ServiceTypes.ASSET_ACCESS]
    ][0]
    data = {
        "documentId":
        did,
        "transferTxId":
        tx_id,
        "serviceId":
        sa.index,
        "output":
        valid_output,
        "algorithmDid":
        alg_ddo.did,
        "algorithmDataToken":
        alg_data_token,
        "algorithmTransferTxId":
        alg_tx_id,
        "additionalInputs": [{
            "documentId": did,
            "transferTxId": tx_id,
            "serviceId": other_service.index
        }],
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert (
        validator.error ==
        "Error in input at index 1: Services in input can only be access or compute."
    )

    # Additional input has other trusted algs
    (
        _,
        trust_did,
        trust_tx_id,
        trust_sa,
        _,
        _,
        _,
        _,
        _,
    ) = build_and_send_ddo_with_compute_service(
        client, asset_type="specific_algo_dids")

    data = {
        "documentId":
        did,
        "transferTxId":
        tx_id,
        "serviceId":
        sa.index,
        "output":
        valid_output,
        "algorithmDid":
        alg_ddo.did,
        "algorithmDataToken":
        alg_data_token,
        "algorithmTransferTxId":
        alg_tx_id,
        "additionalInputs": [{
            "documentId": trust_did,
            "transferTxId": trust_tx_id,
            "serviceId": trust_sa.index,
        }],
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is False
    assert (
        validator.error ==
        f"Error in input at index 1: this algorithm did {alg_ddo.did} is not trusted."
    )
Esempio n. 14
0
def test_passes(client):
    provider_wallet = get_provider_wallet()
    consumer_address = get_consumer_wallet().address
    pub_wallet = get_publisher_wallet()

    (
        _,
        did,
        tx_id,
        sa,
        _,
        alg_ddo,
        alg_data_token,
        _,
        alg_tx_id,
    ) = build_and_send_ddo_with_compute_service(client)

    data = {
        "documentId":
        did,
        "serviceId":
        sa.index,
        "transferTxId":
        tx_id,
        "output":
        build_stage_output_dict(dict(), sa.service_endpoint, consumer_address,
                                pub_wallet),
        "algorithmDid":
        alg_ddo.did,
        "algorithmDataToken":
        alg_data_token,
        "algorithmTransferTxId":
        alg_tx_id,
    }

    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is True

    data = {
        "documentId":
        did,
        "serviceId":
        sa.index,
        "transferTxId":
        tx_id,
        "output":
        build_stage_output_dict(dict(), sa.service_endpoint, consumer_address,
                                pub_wallet),
        "algorithmMeta": {
            "rawcode": "console.log('Hello world'!)",
            "format": "docker-image",
            "version": "0.1",
            "container": {
                "entrypoint": "node $ALGO",
                "image": "node",
                "tag": "10"
            },
        },
    }
    validator = WorkflowValidator(consumer_address, provider_wallet, data)
    assert validator.validate() is True