Skip to content

Commit

Permalink
Merge pull request #1209 from oceanprotocol/fix/create_algo
Browse files Browse the repository at this point in the history
Towards #1108 :In C2D README, show how to submit an algorithm for use by C2D *including docker container
  • Loading branch information
jfdelgad authored Dec 14, 2022
2 parents c617cd0 + 793d16b commit 28c724a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 57 deletions.
32 changes: 1 addition & 31 deletions READMEs/c2d-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,40 +107,10 @@ In the same Python console:
ALGO_url = "https://raw.githubusercontent.com/oceanprotocol/c2d-examples/main/branin_and_gpr/gpr.py"

name = "grp"
(ALGO_data_nft, ALGO_datatoken, ALGO_ddo) = ocean.assets.create_url_asset(name, ALGO_url, alice_wallet, wait_for_aqua=True)
(ALGO_data_nft, ALGO_datatoken, ALGO_ddo) = ocean.assets.create_algo_asset(name, ALGO_url, alice_wallet, wait_for_aqua=True)

print(f"ALGO_data_nft address = '{ALGO_data_nft.address}'")
print(f"ALGO_datatoken address = '{ALGO_datatoken.address}'")

# Specify metadata and services, using the Branin test dataset
ALGO_date_created = "2021-12-28T10:55:11Z"
ALGO_metadata = {
"created": ALGO_date_created,
"updated": ALGO_date_created,
"description": "gpr",
"name": "gpr",
"type": "algorithm",
"author": "Trent",
"license": "CC0: PublicDomain",
"algorithm": {
"language": "python",
"format": "docker-image",
"version": "0.1",
"container": {
"entrypoint": "python $ALGO",
"image": "oceanprotocol/algo_dockers",
"tag": "python-branin",
"checksum": "sha256:8221d20c1c16491d7d56b9657ea09082c0ee4a8ab1a6621fa720da58b09580e4",
},
}
}

# update ALGO_Asset metadata
ALGO_ddo.metadata.update(ALGO_metadata)
ALGO_ddo = ocean.assets.update(
asset=ALGO_ddo, publisher_wallet=alice_wallet, provider_uri=config["PROVIDER_URL"])


print(f"ALGO_ddo did = '{ALGO_ddo.did}'")
```

Expand Down
74 changes: 48 additions & 26 deletions ocean_lib/ocean/ocean_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,46 @@ def _assert_ddo_metadata(metadata: dict):

assert "name" in metadata, "Must have name in metadata."

@enforce_types
def create_algo_asset(
self,
name: str,
url: str,
publisher_wallet,
image: str = "oceanprotocol/algo_dockers",
tag: str = "python-branin",
checksum: str = "sha256:8221d20c1c16491d7d56b9657ea09082c0ee4a8ab1a6621fa720da58b09580e4",
wait_for_aqua: bool = True,
) -> tuple:
"""Create asset of type "algorithm", having UrlFiles, with good defaults"""

if image == "oceanprotocol/algo_dockers" or tag == "python-branin":
assert image == "oceanprotocol/algo_dockers" and tag == "python-branin"

metadata = self._default_metadata(name, publisher_wallet, "algorithm")
metadata["algorithm"] = {
"language": "python",
"format": "docker-image",
"version": "0.1",
"container": {
"entrypoint": "python $ALGO",
"image": image,
"tag": tag,
"checksum": checksum,
},
}

files = [UrlFile(url)]
return self._create_1dt(metadata, files, publisher_wallet, wait_for_aqua)

@enforce_types
def create_url_asset(
self, name: str, url: str, publisher_wallet, wait_for_aqua: bool = True
) -> tuple:
"""Create an asset of type "UrlFile", with good defaults"""
"""Create asset of type "data", having UrlFiles, with good defaults"""
metadata = self._default_metadata(name, publisher_wallet)
files = [UrlFile(url)]
return self.create_with_default_metadata(
name, files, publisher_wallet, wait_for_aqua
)
return self._create_1dt(metadata, files, publisher_wallet, wait_for_aqua)

@enforce_types
def create_graphql_asset(
Expand All @@ -165,11 +196,10 @@ def create_graphql_asset(
publisher_wallet,
wait_for_aqua: bool = True,
) -> tuple:
"""Create an asset of type "GraphqlQuery", with good defaults"""
"""Create asset of type "data", having GraphqlQuery files, w good defaults"""
metadata = self._default_metadata(name, publisher_wallet)
files = [GraphqlQuery(url, query)]
return self.create_with_default_metadata(
name, files, publisher_wallet, wait_for_aqua
)
return self._create_1dt(metadata, files, publisher_wallet, wait_for_aqua)

@enforce_types
def create_onchain_asset(
Expand All @@ -180,39 +210,31 @@ def create_onchain_asset(
publisher_wallet,
wait_for_aqua: bool = True,
) -> tuple:
"""Create an asset of type "SmartContractCall", with good defaults"""
"""Create asset of type "data", having SmartContractCall files, w defaults"""
chain_id = self._chain_id
onchain_data = SmartContractCall(contract_address, chain_id, contract_abi)
files = [onchain_data]
return self.create_with_default_metadata(
name, files, publisher_wallet, wait_for_aqua
)
metadata = self._default_metadata(name, publisher_wallet)
return self._create_1dt(metadata, files, publisher_wallet, wait_for_aqua)

@enforce_types
def create_with_default_metadata(
self,
name: str,
files: list,
publisher_wallet,
wait_for_aqua: bool = True,
) -> tuple:
"""Thin wrapper for create(). Creates 1 datatoken, with good defaults.
If wait_for_aqua, then attempt to update aquarius within time constraints.
Returns (data_nft, datatoken, ddo)
"""
def _default_metadata(self, name: str, publisher_wallet, type="dataset") -> dict:
date_created = datetime.now().isoformat()
metadata = {
"created": date_created,
"updated": date_created,
"description": name,
"name": name,
"type": "dataset",
"type": type,
"author": publisher_wallet.address[:7],
"license": "CC0: PublicDomain",
}
return metadata

@enforce_types
def _create_1dt(self, metadata, files, publisher_wallet, wait_for_aqua):
"""Call create(), focusing on just one datatoken"""
name = metadata["name"]
(data_nft, datatokens, ddo) = self.create(
metadata,
publisher_wallet,
Expand Down
14 changes: 14 additions & 0 deletions ocean_lib/ocean/test/test_ocean_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,17 @@ def test_asset_creation_errors(publisher_ocean, publisher_wallet, config):
deployed_datatokens=[datatoken],
encrypt_flag=True,
)


@pytest.mark.integration
def test_create_algo_asset(publisher_ocean, publisher_wallet):
ocean = publisher_ocean

name = "Branin dataset"
url = "https://raw.githubusercontent.com/oceanprotocol/c2d-examples/main/branin_and_gpr/gpr.py"
(data_nft, datatoken, ddo) = ocean.assets.create_algo_asset(
name, url, publisher_wallet
)

assert ddo.nft["name"] == name # thorough testing is below, on create() directly
assert len(ddo.datatokens) == 1

0 comments on commit 28c724a

Please sign in to comment.