def test_add_trusted_algorithm_no_compute_service(publisher_ocean_instance, metadata): """Tests if the DDO has or not a compute service.""" publisher = get_publisher_wallet() algorithm_ddo = get_registered_algorithm_ddo(publisher_ocean_instance, publisher) wait_for_ddo(publisher_ocean_instance, algorithm_ddo.did) assert algorithm_ddo is not None publisher = get_publisher_wallet() metadata_copy = metadata.copy() ddo = publisher_ocean_instance.assets.create(metadata_copy, publisher) wait_for_ddo(publisher_ocean_instance, ddo.did) assert ddo is not None create_publisher_trusted_algorithms( [algorithm_ddo.did], publisher_ocean_instance.config.metadata_cache_uri) with pytest.raises(AssertionError): add_publisher_trusted_algorithm( ddo.did, algorithm_ddo.did, publisher_ocean_instance.config.metadata_cache_uri, )
def test_pay_for_service_insufficient_balance(publisher_ocean_instance): """Tests if balance is lower than the purchased amount.""" ocn = publisher_ocean_instance alice = get_publisher_wallet() sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json") asset = V3Asset(json_filename=sample_ddo_path) asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4()) token = ocn.create_data_token("DataToken1", "DT1", from_wallet=alice, blob="foo_blob") with pytest.raises(InsufficientBalance): ocn.assets.pay_for_service( ocn.web3, 10000000000000, token.address, asset.did, 0, ZERO_ADDRESS, alice, alice.address, )
def test_store_token(): ocn_auth = OceanAuth(":memory:") wallet = get_publisher_wallet() token = ocn_auth.store(wallet) assert ocn_auth.check(token) == wallet.address, "invalid token, check failed." # verify it is saved assert ocn_auth.restore(wallet) == token, "Restoring token failed."
def setup_all(): config = ExampleConfig.get_config() ConfigProvider.set_config(config) Web3Provider.init_web3( provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path) network = Web3Helper.get_network_name() wallet = get_ganache_wallet() if network in ["ganache", "development"] and wallet: print( f"sender: {wallet.key}, {wallet.address}, {wallet.password}, {wallet.keysStr()}" ) print( f"sender balance: {Web3Helper.from_wei(Web3Helper.get_ether_balance(wallet.address))}" ) assert Web3Helper.from_wei(Web3Helper.get_ether_balance( wallet.address)) > 10 from ocean_lib.models.data_token import DataToken OCEAN_token = DataToken(get_ocean_token_address(network)) amt_distribute = 1000 amt_distribute_base = to_base_18(float(amt_distribute)) for w in (get_publisher_wallet(), get_consumer_wallet()): if Web3Helper.from_wei(Web3Helper.get_ether_balance( w.address)) < 2: Web3Helper.send_ether(wallet, w.address, 4) if OCEAN_token.token_balance(w.address) < 100: OCEAN_token.transfer(w.address, amt_distribute_base, from_wallet=wallet)
def test_creating_ddo_from_scratch(): """Tests creating a DDO from scratch.""" # create an empty ddo ddo = DDO() assert ddo.did is None assert ddo.asset_id is None assert ddo.created is not None did = DID.did({"0": "0x99999999999999999"}) ddo.assign_did(did) assert ddo.did == did ddo.add_service(TEST_SERVICE_TYPE, TEST_SERVICE_URL) pub_acc = get_publisher_wallet() # add a proof to the first public_key/authentication ddo.add_proof("checksum", pub_acc) ddo_text_proof = ddo.as_text() assert ddo_text_proof assert not ddo.public_keys ddo.add_public_key(did, pub_acc.address) assert len(ddo.public_keys) == 1 assert ddo.get_public_key(0) == ddo.public_keys[0] with pytest.raises(IndexError): ddo.get_public_key(1) assert ddo.get_public_key(did) == ddo.public_keys[0] assert ddo.get_public_key("0x32233") is None assert not ddo.authentications ddo.add_authentication(did, "") assert len(ddo.authentications) == 1
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"
def test_ocean_assets_search(publisher_ocean_instance, metadata): """Tests that a created asset can be searched successfully.""" identifier = str(uuid.uuid1()).replace("-", "") metadata_copy = metadata.copy() metadata_copy["main"]["name"] = identifier assert len(publisher_ocean_instance.assets.search(identifier)) == 0 publisher = get_publisher_wallet() ddo = publisher_ocean_instance.assets.create(metadata_copy, publisher) wait_for_ddo(publisher_ocean_instance, ddo.did) time.sleep(1) # apparently changes are not instantaneous assert len(publisher_ocean_instance.assets.search(identifier)) == 1 assert (len( publisher_ocean_instance.assets.query({ "query_string": { "query": identifier, "fields": ["service.attributes.main.name"], } })) == 1) assert (len( publisher_ocean_instance.assets.query({ "query_string": { "query": "Gorilla", "fields": ["service.attributes.main.name"], } })) == 0)
def test_AquariusError(publisher_ocean_instance, metadata): metadata_copy = metadata.copy() publisher = get_publisher_wallet() ocn = publisher_ocean_instance alice_wallet = get_publisher_wallet() dt = ocn.create_data_token("DataToken1", "DT1", alice_wallet, blob=ocn.config.metadata_cache_uri) with patch( "ocean_lib.common.aquarius.aquarius.Aquarius.ddo_exists") as mock: mock.return_value = True with pytest.raises(AquariusError): publisher_ocean_instance.assets.create( metadata_copy, publisher, data_token_address=dt.address)
def test_ocean_assets_algorithm(publisher_ocean_instance): publisher = get_publisher_wallet() metadata = get_algorithm_ddo()["service"][0] metadata["attributes"]["main"]["files"][0]["checksum"] = str(uuid.uuid4()) ddo = publisher_ocean_instance.assets.create(metadata["attributes"], publisher) assert ddo _ddo = wait_for_ddo(publisher_ocean_instance, ddo.did) assert _ddo, f"assets.resolve failed for did {ddo.did}"
def test_ocean_assets_compute(publisher_ocean_instance): publisher = get_publisher_wallet() metadata = get_computing_metadata() metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4()) ddo = publisher_ocean_instance.assets.create(metadata, publisher) assert ddo _ddo = wait_for_ddo(publisher_ocean_instance, ddo.did) assert _ddo, f"assets.resolve failed for did {ddo.did}"
def test_create_bad_metadata(publisher_ocean_instance): """Tests that we can't create the asset with plecos failure.""" publisher = get_publisher_wallet() metadata = get_sample_algorithm_ddo()["service"][0] metadata["attributes"]["main"]["files"][0]["EXTRA ATTRIB!"] = 0 with pytest.raises(ValueError): publisher_ocean_instance.assets.create(metadata["attributes"], publisher)
def test_ocean_assets_algorithm(publisher_ocean_instance): publisher = get_publisher_wallet() metadata = get_algorithm_ddo()['service'][0] metadata['attributes']['main']['files'][0]['checksum'] = str(uuid.uuid4()) ddo = publisher_ocean_instance.assets.create(metadata['attributes'], publisher) assert ddo _ddo = wait_for_ddo(publisher_ocean_instance, ddo.did) assert _ddo, f'assets.resolve failed for did {ddo.did}'
def test_ocean_assets_compute(publisher_ocean_instance): """Tests the creation of an asset with a compute service.""" publisher = get_publisher_wallet() metadata = get_computing_metadata() metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4()) ddo = publisher_ocean_instance.assets.create(metadata, publisher) assert ddo, "DDO None. The ddo is not cached after the creation." _ddo = wait_for_ddo(publisher_ocean_instance, ddo.did) assert _ddo, f"assets.resolve failed for did {ddo.did}"
def test_ddo_dict(): """Tests DDO creation from dictionary.""" sample_ddo_path = get_resource_path("ddo", "ddo_sample_algorithm.json") assert sample_ddo_path.exists(), f"{sample_ddo_path} does not exist!" ddo1 = V3Asset(json_filename=sample_ddo_path) assert ddo1.did == "did:op:8d1b4d73e7af4634958f071ab8dfe7ab0df14019" ddo1.add_proof({"checksum": "test"}, get_publisher_wallet()) ddo_dict = ddo1.as_dictionary() assert ddo_dict["publicKey"][0]["id"] == ddo1.did assert ddo_dict["publicKey"][0]["owner"] == get_publisher_wallet().address assert ddo_dict["publicKey"][0]["type"] == "EthereumECDSAKey" assert ddo_dict["authentication"][0] == { "type": "RsaSignatureAuthentication2018", "publicKey": ddo1.did, }
def test_check_token(web3_instance): ocn_auth = OceanAuth(":memory:") wallet = get_publisher_wallet() token = ocn_auth.get(wallet) address = ocn_auth.check(token) assert address != "0x0", "Verifying token failed." sig = token.split("-")[0] assert ocn_auth.check(sig) == "0x0"
def test_check_token(web3_instance): ocn_auth = OceanAuth(':memory:') wallet = get_publisher_wallet() token = ocn_auth.get(wallet) address = ocn_auth.check(token) assert address != '0x0', 'Verifying token failed.' sig = token.split('-')[0] assert ocn_auth.check(sig) == '0x0'
def test_ocean_exchange(publisher_ocean_instance): """Tests various flows of DataToken exchanges.""" ocn = publisher_ocean_instance alice_wallet = get_publisher_wallet() bob_wallet = get_consumer_wallet() dt = ocn.create_data_token("DataToken1", "DT1", alice_wallet, blob="http://example.com") dt.mint_tokens(bob_wallet.address, 100.0, alice_wallet) ox = OceanExchange(ocn.OCEAN_address, _get_exchange_address(), ocn.config) rate = 0.9 x_id = ox.create(dt.address, rate, bob_wallet) dt.approve_tokens(ox._exchange_address, 20, bob_wallet) # create with invalid token address with pytest.raises(ValueError): ox.create(ox.ocean_address, 0.9, bob_wallet) # TODO: Enable this ValueError handling when the ERC20 check is added in FixedRateExchange.create solidity function # with pytest.raises(ValueError): # ox.create(ox._exchange_address, 0.9, bob_wallet) # create with negative rate, should fail with pytest.raises(AssertionError): _ = ox.create(dt.address, rate * -1.0, bob_wallet) # create using 0 rate with pytest.raises(AssertionError): _ = ox.create(dt.address, 0.0, bob_wallet) ############## # get_quote base_token_amount = ox.get_quote(2.0, exchange_id=x_id) assert ( base_token_amount == 2.0 * rate ), f"unexpected quote of base token {base_token_amount}, should be {2.0*rate}." ############# # test buying datatokens # Alice is buying from exchange owned by bob assert (ox.buy_at_fixed_rate( 2.0, alice_wallet, max_OCEAN_amount=base_token_amount, data_token=dt.address, exchange_owner=bob_wallet.address, ) is True), "buy datatokens failed" assert (ox.buy_at_fixed_rate(2.0, alice_wallet, max_OCEAN_amount=base_token_amount, exchange_id=x_id) is True), "buy datatokens failed" print("-------- all good --------")
def test_ocean_assets_create_fails_fileinfo(publisher_ocean_instance): """Tests that a file with invalid URL can not be published.""" publisher = get_publisher_wallet() metadata = get_sample_algorithm_ddo()["service"][0] metadata["attributes"]["main"]["files"][0]["checksum"] = str(uuid.uuid4()) metadata_copy = metadata.copy() metadata_copy["attributes"]["main"]["files"][0][ "url"] = "http://127.0.0.1/not_valid" with pytest.raises(ValueError): publisher_ocean_instance.assets.create(metadata_copy["attributes"], publisher)
def test_restore_token(): ocn_auth = OceanAuth(':memory:') wallet = get_publisher_wallet() assert ocn_auth.restore( wallet) is None, 'Expecting None when restoring non-existing token.' token = ocn_auth.store(wallet) assert ocn_auth.check( token) == wallet.address, 'invalid token, check failed.' # verify it is saved assert ocn_auth.restore(wallet) == token, 'Restoring token failed.'
def test_get_token(): ocn_auth = OceanAuth(':memory:') wallet = get_publisher_wallet() token = ocn_auth.get(wallet) assert isinstance(token, str), 'Invalid auth token type.' assert token.startswith('0x'), 'Invalid auth token.' parts = token.split('-') assert len(parts) == 2, 'Invalid token, timestamp separator is not found.' address = ocn_auth.check(token) assert address != '0x0', 'Verifying token failed.'
def test_get_token(): ocn_auth = OceanAuth(":memory:") wallet = get_publisher_wallet() token = ocn_auth.get(wallet) assert isinstance(token, str), "Invalid auth token type." assert token.startswith("0x"), "Invalid auth token." parts = token.split("-") assert len(parts) == 2, "Invalid token, timestamp separator is not found." address = ocn_auth.check(token) assert address != "0x0", "Verifying token failed."
def test_restore_token(): ocn_auth = OceanAuth(":memory:") wallet = get_publisher_wallet() assert ( ocn_auth.restore(wallet) is None ), "Expecting None when restoring non-existing token." token = ocn_auth.store(wallet) assert ocn_auth.check(token) == wallet.address, "invalid token, check failed." # verify it is saved assert ocn_auth.restore(wallet) == token, "Restoring token failed."
def __init__(self, ocean_instance=None, wallet=None): if not ocean_instance: from tests.resources.helper_functions import get_publisher_ocean_instance ocean_instance = get_publisher_ocean_instance( use_provider_mock=True) self.ocean_instance = ocean_instance self.wallet = wallet if not wallet: from tests.resources.helper_functions import get_publisher_wallet self.wallet = get_publisher_wallet()
def test_download_fails(publisher_ocean_instance): """Tests failures of assets download function.""" publisher = get_publisher_wallet() with patch("ocean_lib.ocean.ocean_assets.OceanAssets.resolve") as mock: mock.return_value = get_sample_ddo() with pytest.raises(AssertionError): publisher_ocean_instance.assets.download("0x1", 1, publisher, "", "", -4) with pytest.raises(TypeError): publisher_ocean_instance.assets.download("0x1", "", publisher, "", "", "string_index")
def test_ocean_assets_algorithm(publisher_ocean_instance): """Tests the creation of an algorithm DDO.""" publisher = get_publisher_wallet() metadata = get_sample_algorithm_ddo_dict()["service"][0] metadata["attributes"]["main"]["files"][0]["checksum"] = str(uuid.uuid4()) ddo = publisher_ocean_instance.assets.create(metadata["attributes"], publisher) assert ddo, "DDO None. The ddo is not cached after the creation." _ddo = wait_for_ddo(publisher_ocean_instance, ddo.did) assert _ddo, f"assets.resolve failed for did {ddo.did}" assert _ddo.is_consumable() == ConsumableCodes.OK
def ocean_assets_download_destination_file_helper(tmpdir): """Tests downloading to an existing directory.""" publisher = get_publisher_wallet() data_provider = DataServiceProvider ddo = get_sample_ddo() sa = ddo.get_service(ServiceTypes.ASSET_ACCESS) written_path = download_asset_files( sa.index, ddo, publisher, tmpdir, "0x1", "test_order_tx_id", data_provider ) assert os.path.exists(written_path)
def test_create_asset_without_dt_address(publisher_ocean_instance): """Tests creation of the asset which has not the data token address.""" ocn = publisher_ocean_instance alice = get_publisher_wallet() sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json") asset = V3Asset(json_filename=sample_ddo_path) asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4()) assert ocn.assets.create( asset.metadata, alice, [], data_token_address=None ), "Asset creation failed with the specified datatoken address."
def test_create_asset_with_owner_address(publisher_ocean_instance): """Tests that an asset can be created with owner address.""" ocn = publisher_ocean_instance alice = get_publisher_wallet() sample_ddo_path = get_resource_path("ddo", "ddo_sa_sample.json") asset = V3Asset(json_filename=sample_ddo_path) asset.metadata["main"]["files"][0]["checksum"] = str(uuid.uuid4()) assert ocn.assets.create( asset.metadata, alice, [], owner_address=alice.address ), "Asset creation failed with the specified owner address."
def test_ContractNotFound(publisher_ocean_instance, metadata): publisher = get_publisher_wallet() metadata_copy = metadata.copy() # used a random address from Etherscan token_address = "0xB3b8239719403E38de3bdF19B9AC147B48c72BF2" with patch( "ocean_lib.models.dtfactory.DTFactory.verify_data_token") as mock: mock.return_value = False with pytest.raises(ContractNotFound): publisher_ocean_instance.assets.create( metadata_copy, publisher, data_token_address=token_address)
def test_block_confirmations(): alice_wallet = get_publisher_wallet() bob_address = get_consumer_wallet().address # Send transaction first, start dummy tx thread second # else risk race condition: out of order nonce web3 = alice_wallet.web3 tx = { "from": alice_wallet.address, "to": bob_address, "value": 1, "chainId": web3.eth.chain_id, "gasPrice": get_gas_price(web3), } tx["gas"] = web3.eth.estimate_gas(tx) raw_tx = alice_wallet.sign_tx(tx) tx_hash = web3.eth.send_raw_transaction(raw_tx) # Start thread that sends dummy transactions dummy_tx_thread = StoppableThread(target=send_dummy_transactions, args=(alice_wallet, bob_address)) dummy_tx_thread.start() poll_interval = BLOCK_NUMBER_POLL_INTERVAL[1337] wait_for_transaction_receipt_and_block_confirmations( web3, tx_hash, block_confirmations=0, block_number_poll_interval=poll_interval) wait_for_transaction_receipt_and_block_confirmations( web3, tx_hash, block_confirmations=1, block_number_poll_interval=poll_interval) wait_for_transaction_receipt_and_block_confirmations( web3, tx_hash, block_confirmations=6, block_number_poll_interval=poll_interval) wait_for_transaction_receipt_and_block_confirmations( web3, tx_hash, block_confirmations=27, block_number_poll_interval=poll_interval) dummy_tx_thread.stop() dummy_tx_thread.join()