Пример #1
0
def test_artifacts(keeper_url, network_id, network_name):
    Web3Provider._web3 = Web3(CustomHTTPProvider(keeper_url))
    ContractHandler.artifacts_path = nevermined_contracts.get_artifacts_path()
    keeper = Keeper()
    assert keeper.get_network_id() in network_id
    assert keeper.get_network_name(keeper.get_network_id()) in network_name
    assert keeper.did_registry is not None
Пример #2
0
def fulfill_escrow_reward_condition(event, agreement_id, service_agreement,
                                    price, consumer_address, publisher_account,
                                    condition_ids, escrow_condition_id):
    """

    :param event: AttributeDict with the event data.
    :param agreement_id: id of the agreement, hex str
    :param service_agreement: ServiceAgreement instance
    :param price: Asset price, int
    :param consumer_address: ethereum account address of consumer, hex str
    :param publisher_account: Account instance of the publisher
    :param condition_ids: is a list of bytes32 content-addressed Condition IDs, bytes32
    :param escrow_condition_id: hex str the id of escrow reward condition at this `agreement_id`
    :return:
    """
    if not event:
        logger.warning(f'`fulfill_escrow_reward_condition` got empty event: '
                       f'event listener timed out.')
        return

    keeper = Keeper.get_instance()
    if keeper.condition_manager.get_condition_state(escrow_condition_id) > 1:
        logger.debug(
            f'escrow reward condition already fulfilled/aborted: '
            f'agreementId={agreement_id}, escrow reward conditionId={escrow_condition_id}'
        )
        return

    logger.debug(
        f"release reward (agreement {agreement_id}) after event {event}.")
    access_id, lock_id = condition_ids[:2]
    logger.debug(f'fulfill_escrow_reward_condition: '
                 f'agreementId={agreement_id}'
                 f'price={price}, {type(price)}'
                 f'consumer={consumer_address},'
                 f'publisher={publisher_account.address},'
                 f'conditionIds={condition_ids}')
    assert price == service_agreement.get_price(), 'price mismatch.'
    assert isinstance(
        price, int), f'price expected to be int type, got type "{type(price)}"'
    time.sleep(5)
    keeper = Keeper.get_instance()
    did_owner = keeper.agreement_manager.get_agreement_did_owner(agreement_id)
    args = (agreement_id, price,
            Web3Provider.get_web3().toChecksumAddress(did_owner),
            consumer_address, lock_id, access_id, publisher_account)
    process_fulfill_condition(args, keeper.escrow_reward_condition,
                              escrow_condition_id, logger, keeper, 10)
Пример #3
0
def test_compute_logs(publisher_instance_no_init, consumer_instance_no_init,
                      metadata, algorithm_ddo, workflow_ddo):
    consumer = publisher_instance_no_init.main_account
    publisher = consumer_instance_no_init.main_account

    # publish compute
    ddo_computing = publisher_instance_no_init.assets.create_compute(
        metadata, publisher)

    # publish algorithm
    metadata = algorithm_ddo['service'][0]
    ddo_algorithm = consumer_instance_no_init.assets.create(
        metadata['attributes'], consumer)

    metadata = workflow_ddo['service'][0]
    metadata['attributes']['main']['workflow']['stages'][0]['input'][0][
        'id'] = ddo_computing.did
    metadata['attributes']['main']['workflow']['stages'][0]['transformation'][
        'id'] = ddo_algorithm.did
    workflow_ddo = consumer_instance_no_init.assets.create(
        metadata['attributes'], publisher)
    assert workflow_ddo

    # order compute asset
    service = ddo_computing.get_service(
        service_type=ServiceTypes.CLOUD_COMPUTE)
    sa = ServiceAgreement.from_service_dict(service.as_dictionary())
    agreement_id = consumer_instance_no_init.assets.order(
        ddo_computing.did, sa.index, consumer, consumer)

    keeper = Keeper.get_instance()
    event = keeper.lock_payment_condition.subscribe_condition_fulfilled(
        agreement_id, 60, None, (), wait=True)
    assert event is not None, "Reward condition is not found"

    # execute workflow
    execution_id = consumer_instance_no_init.assets.execute(
        agreement_id, ddo_computing.did, sa.index, consumer, workflow_ddo.did)

    # get logs
    logs = consumer_instance_no_init.assets.compute_logs(
        agreement_id, execution_id, consumer)
    assert logs

    publisher_instance_no_init.assets.retire(ddo_computing.did)
    publisher_instance_no_init.assets.retire(ddo_algorithm.did)
    publisher_instance_no_init.assets.retire(workflow_ddo.did)
Пример #4
0
def fulfill_exec_compute_condition(event, agreement_id, did, service_agreement,
                                   consumer_address, publisher_account, exec_compute_condition_id):
    """
    Fulfill the exec compute condition.

    :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_address: ethereum account address of consumer, hex str
    :param publisher_account: Account instance of the publisher
    :param exec_compute_condition_id: hex str the id of the exec compute condition for this
        `agreement_id`
    """
    if not event:
        logger.debug(f'`fulfill_exec_compute_condition` got empty event: '
                     f'event listener timed out.')
        return

    keeper = Keeper.get_instance()
    if keeper.condition_manager.get_condition_state(exec_compute_condition_id) > 1:
        logger.debug(
            f'exec compute condition already fulfilled/aborted: '
            f'agreementId={agreement_id}, exec compute conditionId={exec_compute_condition_id}'
        )
        return

    logger.debug(f"grant access (agreement {agreement_id}) after event {event}.")
    name_to_parameter = {param.name: param for param in
                         service_agreement.condition_by_name['execCompute'].parameters}
    document_id = add_0x_prefix(name_to_parameter['_documentId'].value)
    asset_id = add_0x_prefix(did_to_id(did))
    assert document_id == asset_id, f'document_id {document_id} <=> asset_id {asset_id} mismatch.'

    args = (
        agreement_id,
        document_id,
        consumer_address,
        publisher_account
    )
    process_fulfill_condition(args, keeper.compute_execution_condition, exec_compute_condition_id,
                              logger, keeper, 10)
Пример #5
0
def setup_nft_sales_agreements_environment():
    consumer_acc = get_consumer_account()
    publisher_acc = get_publisher_account()
    keeper = Keeper.get_instance()

    ddo = get_ddo_nft_sample()

    did_seed = generate_prefixed_id()
    asset_id = keeper.did_registry.hash_did(did_seed, publisher_acc.address)
    ddo._did = DID.did(asset_id)

    keeper.did_registry.register_mintable_did(
        did_seed,
        checksum=Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id),
        url='http://172.17.0.1:5000',
        cap=10,
        royalties=10,
        account=publisher_acc,
        providers=None)

    keeper.did_registry.mint(ddo.asset_id, 10, account=publisher_acc)

    service_agreement = ServiceAgreement.from_ddo(ServiceTypes.NFT_SALES, ddo)
    agreement_id = ServiceAgreement.create_new_agreement_id()
    price = service_agreement.get_price()
    (access_cond_id, lock_cond_id,
     escrow_cond_id) = service_agreement.generate_agreement_condition_ids(
         agreement_id, asset_id, consumer_acc.address, keeper)

    nft_access_service_agreement = ServiceAgreement.from_ddo(
        ServiceTypes.NFT_ACCESS, ddo)
    nft_access_agreement_id = ServiceAgreement.create_new_agreement_id()

    (nft_access_cond_id, nft_holder_cond_id
     ) = nft_access_service_agreement.generate_agreement_condition_ids(
         nft_access_agreement_id, asset_id, consumer_acc.address, keeper)

    return (keeper, ddo, publisher_acc, consumer_acc, agreement_id,
            nft_access_agreement_id, asset_id, price, service_agreement,
            nft_access_service_agreement, (lock_cond_id, access_cond_id,
                                           escrow_cond_id),
            (nft_access_cond_id, nft_holder_cond_id))
Пример #6
0
def run_events_monitor():
    setup_logging()
    config = get_config()
    keeper_url = config.keeper_url
    storage_path = config.get('resources',
                              'storage.path',
                              fallback='./provider-events-monitor.db')

    web3 = Web3Provider.get_web3(keeper_url)
    ContractHandler.artifacts_path = get_keeper_path(config)
    keeper = Keeper.get_instance()
    init_account_envvars()

    account = get_account(0)
    if account is None:
        raise AssertionError(
            f'Provider events monitor cannot run without a valid '
            f'ethereum account. Account address was not found in the environment'
            f'variable `PROVIDER_ADDRESS`. Please set the following environment '
            f'variables and try again: `PROVIDER_ADDRESS`, [`PROVIDER_PASSWORD`, '
            f'and `PROVIDER_KEYFILE` or `PROVIDER_ENCRYPTED_KEY`] or `PROVIDER_KEY`.'
        )

    if not account.key_file and not (account.password and account.key_file):
        raise AssertionError(
            f'Provider events monitor cannot run without a valid '
            f'ethereum account with either a `PROVIDER_PASSWORD` '
            f'and `PROVIDER_KEYFILE`/`PROVIDER_ENCRYPTED_KEY` '
            f'or private key `PROVIDER_KEY`. Current account has password {account.password}, '
            f'keyfile {account.key_file}, encrypted-key {account._encrypted_key} '
            f'and private-key {account._private_key}.')

    monitor = ProviderEventsMonitor(keeper, web3, storage_path, account)
    monitor.start_agreement_events_monitor()
    while True:
        time.sleep(5)
Пример #7
0
def setup_did_sales_agreements_environment():
    consumer_acc = get_consumer_account()
    publisher_acc = get_publisher_account()
    keeper = Keeper.get_instance()

    ddo = get_ddo_did_sales_sample()

    did_seed = generate_prefixed_id()
    asset_id = keeper.did_registry.hash_did(did_seed, publisher_acc.address)
    ddo._did = DID.did(asset_id)

    keeper.did_registry.register(
        did_seed,
        checksum=Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id),
        url='metadata:5000',
        account=publisher_acc,
        providers=None)

    service_agreement = ServiceAgreement.from_ddo(ServiceTypes.DID_SALES, ddo)
    agreement_id = ServiceAgreement.create_new_agreement_id()
    price = service_agreement.get_price()
    (access_cond_id, lock_cond_id,
     escrow_cond_id) = service_agreement.generate_agreement_condition_ids(
         agreement_id, asset_id, consumer_acc.address, keeper)

    return (
        keeper,
        ddo,
        publisher_acc,
        consumer_acc,
        agreement_id,
        asset_id,
        price,
        service_agreement,
        (lock_cond_id, access_cond_id, escrow_cond_id),
    )
Пример #8
0
def setup_all():
    Web3Provider.get_web3('http://localhost:8545')
    ContractHandler.artifacts_path = os.path.expanduser(
        '~/.nevermined/nevermined-contracts/artifacts')
    Keeper.get_instance()
Пример #9
0
def setup_basic_environment():
    consumer_acc = get_consumer_account()
    publisher_acc = get_publisher_account()
    keeper = Keeper.get_instance()
    return (keeper, publisher_acc, consumer_acc)