def prepare_contracts(web3, config):
    addresses = get_contracts_addresses(get_network_name(), config)
    if not addresses:
        raise AssertionError(f'Cannot find contracts addresses for network {get_network_name()}')

    addresses = {name: web3.toChecksumAddress(a) for name, a in addresses.items()}
    return addresses
Example #2
0
def test_get_ocean_token_address(config):
    addresses = util.get_contracts_addresses(config.address_file, "ganache")
    assert addresses
    assert isinstance(addresses, dict)
    assert "Ocean" in addresses

    address = get_ocean_token_address(config.address_file, "ganache")
    assert address[:2] == "0x", "It is not a token address."
    assert address == addresses["Ocean"]
Example #3
0
def test_get_dtfactory_address(config):
    addresses = util.get_contracts_addresses(config.address_file, "ganache")
    assert addresses
    assert isinstance(addresses, dict)
    assert "DTFactory" in addresses

    address = get_dtfactory_address(config.address_file, "ganache")
    assert address[:2] == "0x", "It is not a token address."
    assert address == addresses["DTFactory"]
Example #4
0
def test_get_contracts_addresses(config):
    addresses = util.get_contracts_addresses(config.address_file, "ganache")
    assert addresses
    assert isinstance(addresses, dict)
    assert ("DTFactory" and "BFactory" and "FixedRateExchange" and "Metadata"
            and "Ocean" in addresses)
    assert len(addresses) == 6
    for value in addresses.values():
        assert value.startswith("0x"), "It is not a token address."
Example #5
0
def test_get_contracts_addresses():
    config = Config(os.getenv(ENV_CONFIG_FILE))
    ConfigProvider.set_config(config)
    addresses = util.get_contracts_addresses("ganache", config)
    assert addresses
    assert isinstance(addresses, dict)
    assert ("DTFactory" and "BFactory" and "FixedRateExchange" and "Metadata"
            and "Ocean" in addresses)
    assert len(addresses) == 5
    for _, value in addresses.items():
        assert value.startswith("0x")
Example #6
0
def test_update_trusted_algorithms():
    setup = Setup()

    config = ConfigProvider.get_config()
    ddo_address = get_contracts_addresses(
        "ganache", config)[MetadataContract.CONTRACT_NAME]
    ddo_registry = MetadataContract(ddo_address)

    # Setup algorithm meta to run raw algorithm
    algorithm_ddo = get_registered_algorithm_ddo(
        setup.publisher_ocean_instance, setup.publisher_wallet)
    # verify the ddo is available in Aquarius
    _ = setup.publisher_ocean_instance.assets.resolve(algorithm_ddo.did)

    # Dataset with compute service
    compute_ddo = get_registered_ddo_with_compute_service(
        setup.publisher_ocean_instance,
        setup.publisher_wallet,
        trusted_algorithms=[algorithm_ddo.did],
    )
    # verify the ddo is available in Aquarius
    _ = setup.publisher_ocean_instance.assets.resolve(compute_ddo.did)
    trusted_algo_list = create_publisher_trusted_algorithms(
        [algorithm_ddo.did],
        setup.publisher_ocean_instance.config.aquarius_url)
    compute_ddo.update_compute_privacy(trusted_algorithms=trusted_algo_list,
                                       allow_all=False,
                                       allow_raw_algorithm=False)

    tx_id = setup.publisher_ocean_instance.assets.update(
        compute_ddo, setup.publisher_wallet)

    tx_receipt = ddo_registry.get_tx_receipt(tx_id)
    logs = ddo_registry.event_MetadataUpdated.processReceipt(tx_receipt)
    assert logs[0].args.dataToken == compute_ddo.data_token_address

    wait_for_update(
        setup.publisher_ocean_instance,
        compute_ddo.did,
        "privacy",
        {"publisherTrustedAlgorithms": [algorithm_ddo.did]},
    )

    compute_ddo_updated = setup.publisher_ocean_instance.assets.resolve(
        compute_ddo.did)

    run_compute_test(
        setup.consumer_ocean_instance,
        setup.publisher_wallet,
        setup.consumer_wallet,
        [compute_ddo_updated],
        algo_ddo=algorithm_ddo,
    )
def test_update_trusted_algorithms(config, web3, algorithm_ddo,
                                   asset_with_trusted):
    setup = Setup()

    ddo_address = get_contracts_addresses(
        config.address_file, "ganache")[MetadataContract.CONTRACT_NAME]
    ddo_registry = MetadataContract(web3, ddo_address)

    trusted_algo_list = create_publisher_trusted_algorithms(
        [algorithm_ddo.did],
        setup.publisher_ocean_instance.config.metadata_cache_uri)
    asset_with_trusted.update_compute_privacy(
        trusted_algorithms=trusted_algo_list,
        trusted_algo_publishers=[],
        allow_all=False,
        allow_raw_algorithm=False,
    )

    tx_id = setup.publisher_ocean_instance.assets.update(
        asset_with_trusted, setup.publisher_wallet)

    tx_receipt = ddo_registry.get_tx_receipt(web3, tx_id)
    logs = ddo_registry.event_MetadataUpdated.processReceipt(tx_receipt,
                                                             errors=DISCARD)
    assert logs[0].args.dataToken == asset_with_trusted.data_token_address

    wait_for_update(
        setup.publisher_ocean_instance,
        asset_with_trusted.did,
        "privacy",
        {"publisherTrustedAlgorithms": [algorithm_ddo.did]},
    )

    compute_ddo_updated = setup.publisher_ocean_instance.assets.resolve(
        asset_with_trusted.did)

    run_compute_test(
        setup.consumer_ocean_instance,
        setup.publisher_wallet,
        setup.consumer_wallet,
        [compute_ddo_updated],
        algo_ddo=algorithm_ddo,
    )
Example #8
0
def test_metadata_contract(publisher_ocean_instance, config):
    ocn = publisher_ocean_instance
    alice = get_publisher_wallet()
    block = ocn.web3.eth.block_number

    ddo_address = get_contracts_addresses(
        config.address_file, "ganache")[MetadataContract.CONTRACT_NAME]
    ddo_registry = MetadataContract(ocn.web3, ddo_address)

    # Tested the event properties.
    assert (ddo_registry.event_MetadataCreated.abi["name"] ==
            MetadataContract.EVENT_METADATA_CREATED)
    assert (ddo_registry.event_MetadataUpdated.abi["name"] ==
            MetadataContract.EVENT_METADATA_UPDATED)

    # Tested get_event_log for create event.
    original_ddo = create_asset(ocn, alice)
    assert original_ddo, "Create asset failed."

    asset_id = original_ddo.asset_id
    creation_log = ddo_registry.get_event_log(
        ddo_registry.EVENT_METADATA_CREATED, block, asset_id, 30)
    assert creation_log, "No ddo created event."
    assert creation_log.args.createdBy == alice.address
    assert (creation_log.args.createdBy == alice.address
            ), "The event is not created by the publisher."
    assert (creation_log.event == ddo_registry.EVENT_METADATA_CREATED
            ), "Different event types."

    # Tested get_event_log for update event.
    ddo = wait_for_ddo(ocn, original_ddo.did)
    _ = ocn.assets.update(ddo, alice)
    updating_log = ddo_registry.get_event_log(
        ddo_registry.EVENT_METADATA_UPDATED, block, asset_id, 30)
    assert updating_log, "No ddo updated event."
    assert (updating_log.args.updatedBy == alice.address
            ), "The event is not updated by the publisher."
    assert (updating_log.event == ddo_registry.EVENT_METADATA_UPDATED
            ), "Different event types."
Example #9
0
    def __init__(self, config=None, data_provider=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

            >> asset = ocean.assets.create(metadata, publisher_wallet)

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

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

        :param config: Config instance
        :param data_provider: DataServiceProvider instance
        """
        # Configuration information for the market is stored in the Config class
        # config = Config(filename=config_file, options_dict=config_dict)
        if not config:
            try:
                config = ConfigProvider.get_config()
            except AssertionError:
                config = Config(os.getenv(ENV_CONFIG_FILE))
                ConfigProvider.set_config(config)
        if isinstance(config, dict):
            # fallback to metadataStoreUri
            cache_key = ("metadataCacheUri" if ("metadataCacheUri" in config)
                         else "metadataStoreUri")
            aqua_url = config.get(
                cache_key, config.get("aquarius.url", "http://localhost:5000"))
            config_dict = {
                "eth-network": {
                    "network": config.get("network", "")
                },
                "resources": {
                    "aquarius.url":
                    aqua_url,
                    "provider.url":
                    config.get("providerUri", "http://localhost:8030"),
                },
            }
            config = Config(options_dict=config_dict)
        ConfigProvider.set_config(config)
        self._config = config
        ContractHandler.set_artifacts_path(self._config.artifacts_path)
        Web3Provider.init_web3(
            provider=get_web3_connection_provider(self._config.network_url))

        self._web3 = Web3Provider.get_web3()

        if not data_provider:
            data_provider = DataServiceProvider

        network = Web3Helper.get_network_name()
        addresses = get_contracts_addresses(network, self._config)
        self.assets = OceanAssets(
            self._config, data_provider,
            addresses.get(MetadataContract.CONTRACT_NAME))
        self.services = OceanServices()
        self.auth = OceanAuth(self._config.storage_path)
        self.compute = OceanCompute(self.auth, self._config, data_provider)

        ocean_address = get_ocean_token_address(network)
        self.pool = OceanPool(ocean_address, get_bfactory_address(network))
        self.exchange = OceanExchange(
            ocean_address,
            FixedRateExchange.configured_address(
                network or Web3Helper.get_network_name(),
                ConfigProvider.get_config().address_file,
            ),
            self.config,
        )

        logger.debug("Ocean instance initialized: ")
def _get_exchange_address(config):
    """Helper function to retrieve a known exchange address."""
    return get_contracts_addresses(config.address_file,
                                   _NETWORK)[FixedRateExchange.CONTRACT_NAME]
Example #11
0
def _get_exchange_address():
    """Helper function to retrieve a known exchange address."""
    return get_contracts_addresses(
        _NETWORK, ConfigProvider.get_config())[FixedRateExchange.CONTRACT_NAME]
Example #12
0
def test_ddo_on_chain():
    config = ConfigProvider.get_config()
    ddo_address = get_contracts_addresses(
        "ganache", config)[MetadataContract.CONTRACT_NAME]
    dtfactory_address = get_contracts_addresses(
        "ganache", config)[DTFactory.CONTRACT_NAME]
    ddo_registry = MetadataContract(ddo_address)
    wallet = get_publisher_wallet()
    web3 = Web3Provider.get_web3()

    dtfactory = DTFactory(dtfactory_address)
    tx_id = dtfactory.createToken("", "dt1", "dt1", 1000, wallet)
    dt = DataToken(dtfactory.get_token_address(tx_id))

    # test create ddo
    asset = get_ddo_sample(dt.address)
    old_name = asset.metadata["main"]["name"]
    txid = ddo_registry.create(
        asset.asset_id, b"", lzma.compress(web3.toBytes(text=asset.as_text())),
        wallet)
    assert ddo_registry.verify_tx(txid), f"create ddo failed: txid={txid}"
    logs = ddo_registry.event_MetadataCreated.processReceipt(
        ddo_registry.get_tx_receipt(txid))
    assert logs, f"no logs found for create ddo tx {txid}"
    log = logs[0]
    assert add_0x_prefix(log.args.dataToken) == asset.asset_id
    # read back the asset ddo from the event log
    ddo_text = web3.toText(lzma.decompress(log.args.data))
    assert ddo_text == asset.as_text(), "ddo text does not match original."

    _asset = Asset(json_text=ddo_text)
    assert _asset.did == asset.did, "did does not match."
    name = _asset.metadata["main"]["name"]
    assert name == old_name, f"name does not match: {name} != {old_name}"

    # test_update ddo
    asset.metadata["main"]["name"] = "updated name for test"
    txid = ddo_registry.update(
        asset.asset_id, b"", lzma.compress(web3.toBytes(text=asset.as_text())),
        wallet)
    assert ddo_registry.verify_tx(txid), f"update ddo failed: txid={txid}"
    logs = ddo_registry.event_MetadataUpdated.processReceipt(
        ddo_registry.get_tx_receipt(txid))
    assert logs, f"no logs found for update ddo tx {txid}"
    log = logs[0]
    assert add_0x_prefix(log.args.dataToken) == asset.asset_id
    # read back the asset ddo from the event log
    ddo_text = web3.toText(lzma.decompress(log.args.data))
    assert ddo_text == asset.as_text(), "ddo text does not match original."
    _asset = Asset(json_text=ddo_text)
    assert (_asset.metadata["main"]["name"] == "updated name for test"
            ), "name does not seem to be updated."
    assert DataToken(asset.asset_id).contract_concise.isMinter(wallet.address)

    # test update fails from wallet other than the original publisher
    bob = get_consumer_wallet()
    try:
        txid = ddo_registry.update(
            asset.asset_id, b"",
            lzma.compress(web3.toBytes(text=asset.as_text())), bob)
        assert ddo_registry.verify_tx(
            txid) is False, f"update ddo failed: txid={txid}"
        logs = ddo_registry.event_MetadataUpdated.processReceipt(
            ddo_registry.get_tx_receipt(txid))
        assert (
            not logs
        ), f"should be no logs for MetadataUpdated, but seems there are some logs: tx {txid}, logs {logs}"
    except ValueError:
        print("as expected, only owner can update a published ddo.")

    # test ddoOwner
    assert DataToken(asset.asset_id).contract_concise.isMinter(
        wallet.address
    ), (f"ddo owner does not match the expected publisher address {wallet.address}, "
        f"owner is {DataToken(asset.asset_id).contract_concise.minter(wallet.address)}"
        )
Example #13
0
def _get_exchange_address():
    return get_contracts_addresses(
        _NETWORK, ConfigProvider.get_config())[FixedRateExchange.CONTRACT_NAME]
Example #14
0
    def __init__(
        self, config: Union[Dict, Config], data_provider: Optional[Type] = None
    ) -> None:
        """Initialize Ocean class.

        Usage: Make a new Ocean instance

        `ocean = Ocean({...})`

        This class provides the main top-level functions in ocean protocol:
        1. 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

            `asset = ocean.assets.create(metadata, publisher_wallet)`

        2. Discover/Search assets via the current configured metadata store (Aquarius)

            - Usage:
            `assets_list = ocean.assets.search('search text')`

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

        :param config: `Config` instance
        :param data_provider: `DataServiceProvider` instance
        """
        if isinstance(config, dict):
            # fallback to metadataStoreUri
            cache_key = (
                "metadataCacheUri"
                if ("metadataCacheUri" in config)
                else "metadataStoreUri"
            )
            metadata_cache_uri = config.get(
                cache_key, config.get("metadata_cache_uri", "http://localhost:5000")
            )
            config_dict = {
                "eth-network": {"network": config.get("network", "")},
                "resources": {
                    "metadata_cache_uri": metadata_cache_uri,
                    "provider.url": config.get("providerUri", "http://localhost:8030"),
                },
            }
            config = Config(options_dict=config_dict)
        self.config = config
        self.web3 = get_web3(self.config.network_url)

        if not data_provider:
            data_provider = DataServiceProvider

        network = get_network_name(web3=self.web3)
        addresses = get_contracts_addresses(self.config.address_file, network)
        self.assets = OceanAssets(
            self.config,
            self.web3,
            data_provider,
            addresses.get(MetadataContract.CONTRACT_NAME),
        )
        self.compute = OceanCompute(self.config, data_provider)

        ocean_address = get_ocean_token_address(self.config.address_file, network)
        self.pool = OceanPool(
            self.web3,
            ocean_address,
            get_bfactory_address(self.config.address_file, network),
            get_dtfactory_address(self.config.address_file, network),
        )
        self.exchange = OceanExchange(
            self.web3,
            ocean_address,
            FixedRateExchange.configured_address(network, self.config.address_file),
            self.config,
        )

        logger.debug("Ocean instance initialized: ")