Beispiel #1
0
def execute_computation(event, agreement_id, did, service_agreement, consumer_account,
                        consume_callback, workflow_did):
    """
    Consumption of an asset after get the event call.

    :param event: AttributeDict with the event data.
    :param agreement_id: id of the agreement, hex str
    :param did: DID, str
    :param service_agreement: ServiceAgreement instance
    :param consumer_account: Account instance of the consumer
    :param consume_callback:
    :param secret_store_url: str URL of secret store node for retrieving decryption keys
    :param parity_url: str URL of parity client to use for secret store encrypt/decrypt
    :param downloads_path: str path to save downloaded files
    """
    logger.debug(f"consuming asset (agreementId {agreement_id}) after event {event}.")
    if consume_callback:
        brizo = BrizoProvider.get_brizo()
        consume_callback(
            agreement_id,
            DIDResolver(Keeper.get_instance().did_registry).resolve(did),
            DIDResolver(Keeper.get_instance().did_registry).resolve(workflow_did),
            consumer_account,
            brizo
        )
Beispiel #2
0
def test_did_resolver_library(publisher_account, aquarius):
    did_registry = keeper().did_registry
    checksum_test = Web3.sha3(text='checksum')
    value_test = aquarius.root_url

    did_resolver = DIDResolver(keeper().did_registry)

    sample_ddo_path = get_resource_path('ddo', 'ddo_sample1.json')
    assert sample_ddo_path.exists(), "{} does not exist!".format(
        sample_ddo_path)
    asset1 = DDO(json_filename=sample_ddo_path)
    asset1._did = DID.did({"0": "0x1098098"})
    did_registry.register(asset1.asset_id,
                          checksum_test,
                          url=value_test,
                          account=publisher_account)
    aquarius.publish_asset_ddo(asset1)

    did_resolved = did_resolver.resolve(asset1.did)
    assert did_resolved
    assert did_resolved.did == asset1.did

    with pytest.raises(ValueError):
        did_resolver.resolve(asset1.asset_id)

    aquarius.retire_asset_ddo(asset1.did)
Beispiel #3
0
def test_get_resolve_url(aquarius, publisher_account):
    register_account = publisher_account
    did_registry = keeper().did_registry
    did = DID.did({"0": "0x1"})
    asset_id = did_to_id(did)
    value_test = aquarius.root_url
    did_resolver = DIDResolver(keeper().did_registry)
    did_registry.register(asset_id,
                          b'test',
                          url=value_test,
                          account=register_account)
    url = did_resolver.get_resolve_url(Web3.toBytes(hexstr=asset_id))
    assert url == value_test
Beispiel #4
0
def build_stage_algorithm_dict(algorithm_did, algorithm_meta,
                               provider_account):
    if algorithm_did is not None:
        # use the DID
        algo_asset = DIDResolver(
            keeper_instance().did_registry).resolve(algorithm_did)
        algo_id = algorithm_did
        raw_code = ''
        algo_url = get_asset_url_at_index(0, algo_asset, provider_account)
        container = algo_asset.metadata['main']['algorithm']['container']
    else:
        algo_id = ''
        algo_url = algorithm_meta.get('url')
        raw_code = algorithm_meta.get('rawcode')
        container = algorithm_meta.get('container')

    return dict({
        'id': algo_id,
        'url': algo_url,
        'rawcode': raw_code,
        'container': container
    })
def _check_job_id(client, job_id, agreement_id, wait_time=20):
    endpoint = BaseURLs.ASSETS_URL + '/compute'
    cons_acc = get_consumer_account()

    keeper = keeper_instance()
    msg = f'{cons_acc.address}{job_id}{agreement_id}'
    agreement_id_hash = add_ethereum_prefix_and_hash_msg(msg)
    signature = keeper.sign_hash(agreement_id_hash, cons_acc)
    payload = dict({
        'signature': signature,
        'serviceAgreementId': agreement_id,
        'consumerAddress': cons_acc.address,
        'jobId': job_id,
    })

    job_info = get_compute_job_info(client, 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()
    did = None
    # get did of results
    for _ in range(wait_time * 4):
        job_info = get_compute_job_info(client, endpoint, payload)
        did = job_info['did']
        if did:
            break
        time.sleep(0.25)

    assert did, f'Compute job has no results, job info {job_info}.'
    # check results ddo
    ddo = DIDResolver(keeper.did_registry).resolve(did)
    assert ddo, f'Failed to resolve ddo for did {did}'
    consumer_permission = keeper.did_registry.get_permission(
        did, cons_acc.address)
    assert consumer_permission is True, \
        f'Consumer address {cons_acc.address} has no permissions on the results ' \
        f'did {did}. This is required, the consumer must be able to access the results'
Beispiel #6
0
def test_get_resolve_multiple_urls(publisher_account):
    register_account = publisher_account
    did_registry = keeper().did_registry
    did = DID.did({"0": "0x1"})
    did2 = DID.did({"0": "0x2"})
    did3 = DID.did({"0": "0x3"})
    did4 = DID.did({"0": "0x4"})
    did5 = DID.did({"0": "0x5"})
    did6 = DID.did({"0": "0x6"})
    did7 = DID.did({"0": "0x7"})
    did8 = DID.did({"0": "0x8"})
    did9 = DID.did({"0": "0x9"})
    did10 = DID.did({"0": "0x10"})
    value_test = 'http://localhost:5000'
    value_test2 = 'http://localhost:5001'
    value_test3 = 'http://localhost:5002'
    value_test4 = 'http://localhost:5003'
    value_test5 = 'http://localhost:5004'
    value_test6 = 'http://localhost:5005'
    value_test7 = 'http://localhost:5006'
    value_test8 = 'http://localhost:5007'
    value_test9 = 'http://localhost:5008'
    value_test10 = 'http://localhost:5009'
    did_id = did_to_id(did)
    did_id2 = did_to_id(did2)
    did_id3 = did_to_id(did3)
    did_id4 = did_to_id(did4)
    did_id5 = did_to_id(did5)
    did_id6 = did_to_id(did6)
    did_id7 = did_to_id(did7)
    did_id8 = did_to_id(did8)
    did_id9 = did_to_id(did9)
    did_id10 = did_to_id(did10)
    did_resolver = DIDResolver(keeper().did_registry)
    did_registry.register(did_id,
                          b'test',
                          url=value_test,
                          account=register_account)
    did_registry.register(did_id2,
                          b'test',
                          url=value_test2,
                          account=register_account)
    did_registry.register(did_id3,
                          b'test',
                          url=value_test3,
                          account=register_account)
    did_registry.register(did_id4,
                          b'test',
                          url=value_test4,
                          account=register_account)
    did_registry.register(did_id5,
                          b'test',
                          url=value_test5,
                          account=register_account)
    did_registry.register(did_id6,
                          b'test',
                          url=value_test6,
                          account=register_account)
    did_registry.register(did_id7,
                          b'test',
                          url=value_test7,
                          account=register_account)
    did_registry.register(did_id8,
                          b'test',
                          url=value_test8,
                          account=register_account)
    did_registry.register(did_id9,
                          b'test',
                          url=value_test9,
                          account=register_account)
    did_registry.register(did_id10,
                          b'test',
                          url=value_test10,
                          account=register_account)
    url = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id))
    url2 = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id2))
    url3 = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id3))
    url4 = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id4))
    url5 = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id5))
    url6 = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id6))
    url7 = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id7))
    url8 = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id8))
    url9 = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id9))
    url10 = did_resolver.get_resolve_url(Web3.toBytes(hexstr=did_id10))
    assert url == value_test
    assert url2 == value_test2
    assert url3 == value_test3
    assert url4 == value_test4
    assert url5 == value_test5
    assert url6 == value_test6
    assert url7 == value_test7
    assert url8 == value_test8
    assert url9 == value_test9
    assert url10 == value_test10
Beispiel #7
0
def test_did_not_found():
    did_resolver = DIDResolver(keeper().did_registry)
    did_id = secrets.token_hex(32)
    did_id_bytes = Web3.toBytes(hexstr=did_id)
    with pytest.raises(OceanDIDNotFound):
        did_resolver.resolve(did_id_bytes)
Beispiel #8
0
def test_get_did_not_valid():
    did_resolver = DIDResolver(keeper().did_registry)
    with pytest.raises(TypeError):
        did_resolver.get_resolve_url('not valid')
Beispiel #9
0
    def __init__(self, config=None):
        """
        Initialize Ocean class.
           >> # Make a new Ocean instance
           >> ocean = Ocean({...})

        This class provides the main top-level functions in ocean protocol:
         * Publish assets metadata and associated services
            * Each asset is assigned a unique DID and a DID Document (DDO)
            * The DDO contains the asset's services including the metadata
            * The DID is registered on-chain with a URL of the metadata store
              to retrieve the DDO from

            >> ddo = ocean.assets.create(metadata, publisher_account)

         * Discover/Search assets via the current configured metadata store (Aquarius)
            >> assets_list = ocean.assets.search('search text')

         * Purchase asset services by choosing a service agreement from the
           asset's DDO. Purchase goes through the service agreements interface
           and starts by signing a service agreement then sending the signature
           to the publisher's Brizo server via the `purchaseEndpoint` in the service
           definition:

           >> service_def_id = ddo.get_service(ServiceTypes.ASSET_ACCESS).service_definition_id
           >> service_agreement_id = ocean.assets.order(did, service_def_id, consumer_account)

        An instance of Ocean is parameterized by a `Config` instance.

        :param config: Config instance
        """
        # Configuration information for the market is stored in the Config class
        # config = Config(filename=config_file, options_dict=config_dict)
        if not config:
            config = ConfigProvider.get_config()

        self._config = config
        self._web3 = Web3Provider.get_web3(self._config.keeper_url)
        ContractHandler.set_artifacts_path(self._config.keeper_path)
        contracts = [
            'DIDRegistry', 'Dispenser', 'TemplateStoreManager', 'OceanToken',
            'ConditionStoreManager', 'EscrowAccessSecretStoreTemplate',
            'AgreementStoreManager', 'AgreementStoreManager',
            'AccessSecretStoreCondition', 'LockRewardCondition',
            'HashLockCondition', 'SignCondition', 'EscrowReward'
        ]
        self._keeper = Keeper.get_instance(contracts)
        self._did_resolver = DIDResolver(self._keeper.did_registry)

        # Initialize the public sub-modules
        self.tokens = OceanTokens(self._keeper)
        self.accounts = OceanAccounts(self._keeper, self._config, self.tokens)
        self.secret_store = OceanSecretStore(self._config)
        self.templates = OceanTemplates(self._keeper, config)
        self.agreements = self._make_ocean_agreements()
        self.assets = OceanAssets(self._keeper, self._did_resolver,
                                  self.agreements, AssetConsumer,
                                  AssetExecutor, self._config)
        self.services = OceanServices()
        self.ocean_providers = OceanProviders(self._keeper, self._did_resolver,
                                              self._config)
        self.auth = OceanAuth(self._keeper, self._config.storage_path)

        logger.debug('Squid Ocean instance initialized: ')
        logger.debug(
            f'\tOther accounts: {sorted([a.address for a in self.accounts.list()])}'
        )
        logger.debug(f'\tDIDRegistry @ {self._keeper.did_registry.address}')
Beispiel #10
0
    def process_condition_events(self,
                                 agreement_id,
                                 conditions,
                                 did,
                                 consumer_address,
                                 block_number,
                                 new_agreement=True,
                                 template_id=None):

        ddo = DIDResolver(self._keeper.did_registry).resolve(did)

        cond_order = self._get_conditions_order(template_id)
        agreement_type = self._get_agreement_type(template_id)
        service_agreement = ddo.get_service(agreement_type)
        if not service_agreement:
            logger.warning(
                f'Failed to find service agreement of type {agreement_type} and '
                f'templateId {template_id}. \nKnown template ids are:'
                f'{self._keeper.escrow_access_secretstore_template.address} and '
                f'{self._keeper.escrow_compute_execution_template.address}.'
                f'Processing service agreement {agreement_id} failed.')

        condition_def_dict = service_agreement.condition_by_name
        price = service_agreement.get_price()
        if new_agreement:
            start_time = int(datetime.now().timestamp())
            self.db.record_service_agreement(
                agreement_id, ddo.did, service_agreement.index, price,
                ddo.metadata.get('encryptedFiles'), consumer_address,
                start_time, block_number, agreement_type,
                service_agreement.condition_by_name.keys())

        condition_ids = service_agreement.generate_agreement_condition_ids(
            agreement_id=agreement_id,
            asset_id=ddo.asset_id,
            consumer_address=consumer_address,
            publisher_address=ddo.publisher,
            keeper=self._keeper)
        cond_to_id = {
            cond_order[i]: _id
            for i, _id in enumerate(condition_ids)
        }
        for cond in conditions:

            if cond == 'lockReward':
                if agreement_type == ServiceTypes.ASSET_ACCESS:
                    condition = lockRewardCondition.fulfillAccessSecretStoreCondition
                else:
                    condition = lockRewardExecutionCondition.fulfillExecComputeCondition
                self._keeper.lock_reward_condition.subscribe_condition_fulfilled(
                    agreement_id,
                    max(condition_def_dict['lockReward'].timeout,
                        self.EVENT_WAIT_TIMEOUT),
                    condition,
                    (agreement_id, ddo.did, service_agreement,
                     consumer_address, self._account, condition_ids[0]),
                    from_block=block_number)

            elif cond == 'accessSecretStore':
                self._keeper.access_secret_store_condition.subscribe_condition_fulfilled(
                    agreement_id,
                    max(condition_def_dict['accessSecretStore'].timeout,
                        self.EVENT_WAIT_TIMEOUT),
                    accessSecretStore.fulfillEscrowRewardCondition,
                    (agreement_id, service_agreement, price, consumer_address,
                     self._account, condition_ids, condition_ids[2]),
                    from_block=block_number)
            elif cond == 'execCompute':
                self._keeper.compute_execution_condition.subscribe_condition_fulfilled(
                    agreement_id,
                    max(condition_def_dict['execCompute'].timeout,
                        self.EVENT_WAIT_TIMEOUT),
                    accessSecretStore.fulfillEscrowRewardCondition,
                    (agreement_id, service_agreement, price, consumer_address,
                     self._account, condition_ids, condition_ids[2]),
                    from_block=block_number)
            elif cond == 'escrowReward':
                self._keeper.escrow_reward_condition.subscribe_condition_fulfilled(
                    agreement_id,
                    max(condition_def_dict['escrowReward'].timeout,
                        self.EVENT_WAIT_TIMEOUT),
                    self._last_condition_fulfilled, (agreement_id, cond_to_id),
                    from_block=block_number)