Exemplo n.º 1
0
def build_compute_descriptor(ocean, publisher):
    # build compute service metadata
    cluster_attributes = ocean.compute.build_cluster_attributes(
        cluster_type='Kubernetes', url='/cluster/url')
    supported_containers = [
        ocean.compute.build_container_attributes(image='tensorflow/tensorflow',
                                                 tag='latest',
                                                 entrypoint='python $ALGO')
    ]
    servers = [
        ocean.compute.build_server_attributes(server_id='1',
                                              server_type='xlsize',
                                              cpu=16,
                                              gpu=0,
                                              memory='16gb',
                                              disk='1tb',
                                              max_run_time=3600)
    ]
    provider_attributes = ocean.compute.build_service_provider_attributes(
        provider_type='Azure',
        description='Compute power 1',
        cluster=cluster_attributes,
        containers=supported_containers,
        servers=servers)
    compute_attributes = ocean.compute.create_compute_service_attributes(
        timeout=3600,
        creator=publisher,
        date_published=get_timestamp(),
        provider_attributes=provider_attributes)

    return ocean.compute.create_compute_service_descriptor(compute_attributes)
Exemplo n.º 2
0
def test_create_compute_service(publisher_ocean_instance):
    ocn_compute = publisher_ocean_instance.compute

    cluster = ocn_compute.build_cluster_attributes('kubernetes',
                                                   '/cluster/url')
    container = ocn_compute.build_container_attributes(
        "tensorflow/tensorflow", "latest",
        "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc"
    )
    server = ocn_compute.build_server_attributes("1", "xlsize", "16", "0",
                                                 "128gb", "160gb", 86400)
    provider_attributes = ocn_compute.build_service_provider_attributes(
        "Azure", "some description of the compute server instance", cluster,
        [container], [server])
    attributes = ocn_compute.create_compute_service_attributes(
        "10", 3600 * 24, publisher_ocean_instance.main_account.address,
        get_timestamp(), provider_attributes)
    service = publisher_ocean_instance.services.create_compute_service(
        attributes, 'http://brizo.com:8030/api/v1/services/compute')
    assert isinstance(service, tuple) and len(service) == 2
    assert service[0] == ServiceTypes.CLOUD_COMPUTE
    assert isinstance(service[1], dict)
    assert service[1]['templateId'] == SquidKeeper.get_instance(
    ).get_agreement_template_id(ServiceTypes.CLOUD_COMPUTE)
    assert service[1]['attributes'] == attributes
    assert service[1][
        'serviceEndpoint'] == 'http://brizo.com:8030/api/v1/services/compute'
Exemplo n.º 3
0
def build_compute_descriptor(ocean, publisher):
    # build compute service metadata
    cluster_attributes = ocean.compute.build_cluster_attributes(
        cluster_type="Kubernetes", url="/cluster/url")
    supported_containers = [
        ocean.compute.build_container_attributes(image="tensorflow/tensorflow",
                                                 tag="latest",
                                                 entrypoint="python $ALGO")
    ]
    servers = [
        ocean.compute.build_server_attributes(
            server_id="1",
            server_type="xlsize",
            cpu=16,
            gpu=0,
            memory="16gb",
            disk="1tb",
            max_run_time=3600,
        )
    ]
    provider_attributes = ocean.compute.build_service_provider_attributes(
        provider_type="Azure",
        description="Compute power 1",
        cluster=cluster_attributes,
        containers=supported_containers,
        servers=servers,
    )
    compute_attributes = ocean.compute.create_compute_service_attributes(
        timeout=3600,
        creator=publisher,
        date_published=get_timestamp(),
        provider_attributes=provider_attributes,
    )

    return ocean.compute.create_compute_service_descriptor(compute_attributes)
Exemplo n.º 4
0
def test_create_compute_service(publisher_ocean_instance):
    """Tests that a compute service is correctly created.

    Includes cluster, container and server creation."""
    ocn_compute = publisher_ocean_instance.compute

    cluster = ocn_compute.build_cluster_attributes("kubernetes", "/cluster/url")
    container = ocn_compute.build_container_attributes(
        "tensorflow/tensorflow",
        "latest",
        "sha256:cb57ecfa6ebbefd8ffc7f75c0f00e57a7fa739578a429b6f72a0df19315deadc",
    )
    server = ocn_compute.build_server_attributes(
        "1", "xlsize", "16", "0", "128gb", "160gb", 86400
    )
    provider_attributes = ocn_compute.build_service_provider_attributes(
        "Azure",
        "some description of the compute server instance",
        cluster,
        [container],
        [server],
    )
    attributes = ocn_compute.create_compute_service_attributes(
        3600 * 24, get_publisher_wallet().address, get_timestamp(), provider_attributes
    )
    service = publisher_ocean_instance.services.create_compute_service(
        attributes, "http://provider.com:8030"
    )
    assert isinstance(service, tuple) and len(service) == 2
    assert service[0] == ServiceTypes.CLOUD_COMPUTE
    assert isinstance(service[1], dict)
    assert service[1]["attributes"] == attributes
    assert service[1]["serviceEndpoint"] == "http://provider.com:8030"
Exemplo n.º 5
0
    def add_proof(self, checksums, publisher_account):
        """Add a proof to the DDO, based on the public_key id/index and signed with the private key
        add a static proof to the DDO, based on one of the public keys.

        :param checksums: dict with the checksum of the main attributes of each service, dict
        :param publisher_account: account of the publisher, account
        """
        self._proof = {
            'type': PROOF_TYPE,
            'created': get_timestamp(),
            'creator': publisher_account.address,
            'signatureValue': '',
            'checksum': checksums

        }
Exemplo n.º 6
0
    def __init__(self, did=None, json_text=None, json_filename=None, created=None, dictionary=None):
        """Clear the DDO data values."""
        self._did = did
        self._public_keys = []
        self._authentications = []
        self._services = []
        self._proof = None
        self._created = None
        self._other_values = {}

        if created:
            self._created = created
        else:
            self._created = get_timestamp()

        if not json_text and json_filename:
            with open(json_filename, 'r') as file_handle:
                json_text = file_handle.read()

        if json_text:
            self._read_dict(json.loads(json_text))
        elif dictionary:
            self._read_dict(dictionary)
Exemplo n.º 7
0
    def as_dictionary(self, is_proof=True):
        """
        Return the DDO as a JSON dict.

        :param if is_proof: if False then do not include the 'proof' element.
        :return: dict
        """
        if self._created is None:
            self._created = get_timestamp()

        data = {
            '@context': DID_DDO_CONTEXT_URL,
            'id': self._did,
            'created': self._created,
        }
        if self._public_keys:
            values = []
            for public_key in self._public_keys:
                values.append(public_key.as_dictionary())
            data['publicKey'] = values
        if self._authentications:
            values = []
            for authentication in self._authentications:
                values.append(authentication)
            data['authentication'] = values
        if self._services:
            values = []
            for service in self._services:
                values.append(service.as_dictionary())
            data['service'] = values
        if self._proof and is_proof:
            data['proof'] = self._proof

        if self._other_values:
            data.update(self._other_values)

        return data
Exemplo n.º 8
0
def buy_asset():
    """
    Requires all ocean services running.

    """
    setup_logging(default_level=logging.INFO)
    ConfigProvider.set_config(ExampleConfig.get_config())
    config = ConfigProvider.get_config()
    providers = {
        'duero': '0xfEF2d5e1670342b9EF22eeeDcb287EC526B48095',
        'nile': '0x4aaab179035dc57b35e2ce066919048686f82972'
    }
    # make ocean instance
    ocn = Ocean()
    acc = get_account(1)
    if not acc:
        acc = ([acc for acc in ocn.accounts.list() if acc.password] or ocn.accounts.list())[0]

    Diagnostics.verify_contracts()
    metadata = example_metadata.metadata.copy()
    metadata['main']['dateCreated'] = get_timestamp()
    keeper = Keeper.get_instance()
    # Register ddo
    did = ''
    if did:
        ddo = ocn.assets.resolve(did)
        logging.info(f'using ddo: {did}')
    else:

        ddo = ocn.assets.create(metadata, acc, providers=[], use_secret_store=True)
        assert ddo is not None, f'Registering asset on-chain failed.'
        did = ddo.did
        logging.info(f'registered ddo: {did}')
        # ocn here will be used only to publish the asset. Handling the asset by the publisher
        # will be performed by the Brizo server running locally
        test_net = os.environ.get('TEST_NET', '')
        if test_net.startswith('nile'):
            provider = keeper.did_registry.to_checksum_address(providers['nile'])
        elif test_net.startswith('duero'):
            provider = keeper.did_registry.to_checksum_address(providers['duero'])
        else:
            provider = '0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0'

        # Wait for did registry event
        event = keeper.did_registry.subscribe_to_event(
            keeper.did_registry.DID_REGISTRY_EVENT_NAME,
            30,
            event_filter={
                '_did': Web3Provider.get_web3().toBytes(hexstr=ddo.asset_id),
                '_owner': acc.address},
            wait=True
        )
        if not event:
            logging.warning(f'Failed to get the did registry event for asset with did {did}.')
        assert keeper.did_registry.get_block_number_updated(ddo.asset_id) > 0, \
            f'There is an issue in registering asset {did} on-chain.'

        keeper.did_registry.add_provider(ddo.asset_id, provider, acc)
        logging.info(f'is {provider} set as did provider: '
                     f'{keeper.did_registry.is_did_provider(ddo.asset_id, provider)}')

    _providers = keeper.did_registry.get_did_providers(ddo.asset_id)

    access_template_name = keeper.template_manager.SERVICE_TO_TEMPLATE_NAME['access']
    template_id = keeper.template_manager.create_template_id(access_template_name)
    approved = keeper.template_manager.is_template_approved(template_id)
    print(f'agreement template approved: {approved}')
    template_data = keeper.template_manager.get_template(template_id)
    print(f'access agreement template: {template_data}')
    cons_ocn = Ocean()
    consumer_account = get_account(0)

    # sign agreement using the registered asset did above
    service = ddo.get_service(service_type=ServiceTypes.ASSET_ACCESS)
    # This will send the order request to Brizo which in turn will execute the agreement on-chain
    cons_ocn.accounts.request_tokens(consumer_account, 10)
    sa = ServiceAgreement.from_json(service.as_dictionary())
    agreement_id = ''
    if not agreement_id:
        agreement_id = cons_ocn.assets.order(
            did, sa.index, consumer_account)

    logging.info('placed order: %s, %s', did, agreement_id)
    event = keeper.agreement_manager.subscribe_agreement_created(
        agreement_id, 60, None, (), wait=True
    )
    assert event, "Agreement event is not found, check the keeper node's logs"
    logging.info(f'Got agreement event, next: lock reward condition')

    event = keeper.lock_reward_condition.subscribe_condition_fulfilled(
        agreement_id, 60, None, (), wait=True
    )
    assert event, "Lock reward condition fulfilled event is not found, check the keeper node's logs"
    logging.info('Got lock reward event, next: wait for the access condition..')

    event = keeper.access_secret_store_condition.subscribe_condition_fulfilled(
        agreement_id, 15, None, (), wait=True
    )
    logging.info(f'Got access event {event}')
    i = 0
    while ocn.agreements.is_access_granted(
            agreement_id, did, consumer_account.address) is not True and i < 30:
        time.sleep(1)
        i += 1

    assert ocn.agreements.is_access_granted(agreement_id, did, consumer_account.address)

    ocn.assets.consume(
        agreement_id,
        did,
        sa.index,
        consumer_account,
        config.downloads_path)
    logging.info('Success buying asset.')

    event = keeper.escrow_reward_condition.subscribe_condition_fulfilled(
        agreement_id,
        30,
        None,
        (),
        wait=True
    )
    assert event, 'no event for EscrowReward.Fulfilled'
    logging.info(f'got EscrowReward.FULFILLED event: {event}')
    logging.info('Done buy asset.')
Exemplo n.º 9
0
def build_compute_descriptor(ocean, publisher):
    # build compute service metadata
    cluster_attributes = ocean.compute.build_cluster_attributes(cluster_type='Kubernetes', url='https://localhost:8050')
    supported_containers = [ocean.compute.build_container_attributes(image='huggingface/transformers-pytorch-gpu', tag='latest', entrypoint='python $ALGO')]
    servers = [ocean.compute.build_server_attributes(server_id='1', server_type='xlsize', cpu=8, gpu=1, memory='16gb', disk='200gb', max_run_time=3600)]
    provider_attributes = ocean.compute.build_service_provider_attributes(provider_type='AWS', description='GPU-V100 compute',
                                                                          cluster=cluster_attributes,
                                                                          containers=supported_containers,
                                                                          servers=servers)
    compute_attributes = ocean.compute.create_compute_service_attributes(timeout=3600, creator=publisher,
                                                                         date_published=get_timestamp(),
                                                                         provider_attributes=provider_attributes)
    return ocean.compute.create_compute_service_descriptor(compute_attributes)