Skip to content

Commit

Permalink
use validUntil in c2d payload (#466)
Browse files Browse the repository at this point in the history
* use validUntil in c2d payload
* use Ocean as default for PROVIDER_FEE_TOKEN
* use BLACK_HOLE address
* fix get_provider_fee_token
* Fix algo validation tests.
* improve actions flow
* add docker auth
* search and use correct env
* more tests
* Add free c2d to conftest.py.

Co-authored-by: Calina Cenan <[email protected]>
  • Loading branch information
alexcos20 and calina-c authored Jun 3, 2022
1 parent 32891be commit a7466a8
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 62 deletions.
19 changes: 14 additions & 5 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,31 @@ jobs:
repository: "oceanprotocol/barge"
path: 'barge'
ref: v4
- name: Login to Docker Hub
if: ${{ env.DOCKERHUB_PASSWORD && env.DOCKERHUB_USERNAME }}
run: |
echo "Login to Docker Hub";echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_PASSWORD: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Run Barge
working-directory: ${{ github.workspace }}/barge
env:
CONTRACTS_VERSION: v1.0.0-alpha.32
run: |
bash -x start_ocean.sh --no-dashboard 2>&1 --with-rbac --with-provider2 --with-c2d > start_ocean.log &
for i in $(seq 1 150); do
sleep 5
[ -f "$HOME/.ocean/ocean-contracts/artifacts/ready" -a -f "$HOME/.ocean/ocean/c2d/ready" ] && break
done
ls -la "$HOME/.ocean/ocean-contracts/artifacts/"
- name: Install dependencies
working-directory: ${{ github.workspace }}
run: |
python -m pip install --upgrade pip
pip install -r requirements_dev.txt
- name: Wait for contracts deployment and C2D cluster to be ready
working-directory: ${{ github.workspace }}/barge
run: |
for i in $(seq 1 250); do
sleep 10
[ -f "$HOME/.ocean/ocean-contracts/artifacts/ready" -a -f "$HOME/.ocean/ocean-c2d/ready" ] && break
done
- name: Test with pytest
run: |
coverage run --source ocean_provider -m pytest
Expand Down
8 changes: 8 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ocean_provider.run import app
from ocean_provider.utils.basics import get_config, get_web3, send_ether
from ocean_provider.utils.provider_fees import get_c2d_environments

app = app

Expand Down Expand Up @@ -92,3 +93,10 @@ def setup_all(provider_address, consumer_address, ganache_wallet):
@pytest.fixture
def web3():
return get_web3()


@pytest.fixture
def free_c2d_env():
environments = get_c2d_environments()

return next(env for env in environments if float(env["priceMin"]) == float(0))
5 changes: 1 addition & 4 deletions ocean_provider/routes/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,6 @@ def computeStart():
logger.debug("Sending: %s", workflow)

compute_env = data.get("environment")
seconds = (
datetime.fromtimestamp(validator.valid_until) - datetime.utcnow()
).seconds

nonce, provider_signature = sign_for_compute(provider_wallet, consumer_address)
web3 = get_web3()
Expand All @@ -451,7 +448,7 @@ def computeStart():
"owner": consumer_address,
"providerAddress": provider_wallet.address,
"environment": compute_env,
"maxDuration": seconds,
"validUntil": validator.valid_until,
"nonce": nonce,
"chainId": web3.chain_id,
}
Expand Down
13 changes: 13 additions & 0 deletions ocean_provider/utils/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
# SPDX-License-Identifier: Apache-2.0
#
import json
import os
from pathlib import Path
from typing import Any, Dict, Union
from eth_typing.evm import HexAddress
from ocean_provider.utils.basics import get_config

BLACK_HOLE_ADDRESS = "0x0000000000000000000000000000000000000000"


def get_address_json(address_path: Union[str, Path]) -> Dict[str, Any]:
Expand All @@ -27,3 +31,12 @@ def get_contract_address(
for chain_addresses in address_json.values()
if chain_addresses["chainId"] == chain_id
)


def get_provider_fee_token(chain_id):
fee_token = os.environ.get("PROVIDER_FEE_TOKEN", get_ocean_address(chain_id))
return fee_token if fee_token else BLACK_HOLE_ADDRESS


def get_ocean_address(chain_id):
return get_contract_address(get_config().address_file, "Ocean", chain_id)
5 changes: 2 additions & 3 deletions ocean_provider/utils/compute_environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from urllib.parse import urljoin

from ocean_provider.requests_session import get_requests_session
from ocean_provider.utils.address import get_provider_fee_token
from ocean_provider.utils.basics import get_config, get_web3


Expand All @@ -27,9 +28,7 @@ def get_c2d_environments() -> List:
# loop envs and add provider token from config
envs = response.json()
for env in envs:
env["feeToken"] = os.getenv(
"PROVIDER_FEE_TOKEN", "0x0000000000000000000000000000000000000000"
)
env["feeToken"] = get_provider_fee_token(web3.chain_id)

return envs

Expand Down
7 changes: 3 additions & 4 deletions ocean_provider/utils/provider_fees.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from eth_keys.backends import NativeECCBackend
from ocean_provider.requests_session import get_requests_session
from ocean_provider.utils.asset import get_asset_from_metadatastore
from ocean_provider.utils.address import get_provider_fee_token
from ocean_provider.utils.basics import (
get_provider_wallet,
get_metadata_url,
Expand Down Expand Up @@ -39,9 +40,7 @@ def get_provider_fees(
web3 = get_web3()
provider_wallet = get_provider_wallet()
provider_fee_address = provider_wallet.address
provider_fee_token = os.environ.get(
"PROVIDER_FEE_TOKEN", "0x0000000000000000000000000000000000000000"
)
provider_fee_token = get_provider_fee_token(web3.chain_id)

if compute_env and not force_zero:
provider_fee_amount = get_provider_fee_amount(
Expand Down Expand Up @@ -174,7 +173,7 @@ def get_provider_fee_amount(valid_until, compute_env, web3, provider_fee_token):
if provider_fee_token == "0x0000000000000000000000000000000000000000":
return 0

provider_fee_amount = float(seconds * env["priceMin"] / 60)
provider_fee_amount = float(seconds * float(env["priceMin"]) / 60)

dt = get_datatoken_contract(web3, provider_fee_token)
decimals = dt.caller.decimals()
Expand Down
8 changes: 4 additions & 4 deletions ocean_provider/validation/algo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
get_asset_from_metadatastore,
check_asset_consumable,
)
from ocean_provider.utils.address import get_provider_fee_token
from ocean_provider.utils.basics import get_config, get_metadata_url
from ocean_provider.utils.datatoken import (
record_consume_request,
Expand Down Expand Up @@ -112,11 +113,10 @@ def validate_input(self, index=0):
valid_until_list.append(self.algo_valid_until)
provider_fee_amounts.append(self.algo_fee_amount)

self.valid_until = min(valid_until_list)
self.valid_until = max(valid_until_list)

provider_fee_token = get_provider_fee_token(self.web3.chain_id)

provider_fee_token = os.environ.get(
"PROVIDER_FEE_TOKEN", "0x0000000000000000000000000000000000000000"
)
required_provider_fee = get_provider_fee_amount(
self.valid_until,
self.data.get("environment"),
Expand Down
9 changes: 9 additions & 0 deletions ocean_provider/validation/test/test_algo_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pytest
from ocean_provider.utils.asset import Asset
from ocean_provider.utils.basics import get_web3
from ocean_provider.utils.services import Service, ServiceType
from ocean_provider.validation.algo import WorkflowValidator
from tests.ddo.ddo_sample1_compute import alg_ddo_dict, ddo_dict
Expand All @@ -31,6 +32,7 @@
)
def test_passes_algo_ddo(provider_wallet, consumer_address, web3):
"""Tests happy flow of validator with algo ddo."""
web3 = get_web3()
ddo = Asset(ddo_dict)
alg_ddo = Asset(alg_ddo_dict)
sa_compute = get_first_service_by_type(alg_ddo, ServiceType.ACCESS)
Expand All @@ -43,6 +45,7 @@ def test_passes_algo_ddo(provider_wallet, consumer_address, web3):
"serviceId": sa_compute.id,
"transferTxId": "alg_tx_id",
},
"environment": "ocean-compute",
}

def side_effect(*args, **kwargs):
Expand Down Expand Up @@ -72,6 +75,7 @@ def side_effect(*args, **kwargs):
)
def test_passes_raw(provider_wallet, consumer_address, web3):
"""Tests happy flow of validator with raw algo."""
web3 = get_web3()
ddo = Asset(ddo_dict)
sa = get_first_service_by_type(ddo, ServiceType.COMPUTE)
data = {
Expand All @@ -85,6 +89,7 @@ def test_passes_raw(provider_wallet, consumer_address, web3):
"container": {"entrypoint": "node $ALGO", "image": "node", "tag": "10"},
},
},
"environment": "ocean-compute",
}

with patch(
Expand Down Expand Up @@ -229,6 +234,7 @@ def test_fails_meta_issues(provider_wallet, consumer_address, web3):
return_value=[{"url": "dummy"}],
)
def test_additional_datasets(provider_wallet, consumer_address, web3):
web3 = get_web3()
ddo = Asset(ddo_dict)
alg_ddo = Asset(alg_ddo_dict)
sa_compute = get_first_service_by_type(alg_ddo, ServiceType.ACCESS)
Expand All @@ -242,6 +248,7 @@ def test_additional_datasets(provider_wallet, consumer_address, web3):
"transferTxId": "alg_tx_id",
},
"additionalDatasets": "",
"environment": "ocean-compute",
}

def side_effect(*args, **kwargs):
Expand Down Expand Up @@ -813,6 +820,7 @@ def test_success_multiple_services_types(provider_wallet, consumer_address, web3
"additionalDatasets": [
{"documentId": ddo.did, "transferTxId": "ddo.did", "serviceId": "access_1"}
],
"environment": "ocean-compute",
}

def side_effect(*args, **kwargs):
Expand Down Expand Up @@ -900,6 +908,7 @@ def another_side_effect(*args, **kwargs):
)
def test_fee_amount_not_paid(provider_wallet, consumer_address, web3):
"""Tests happy flow of validator with algo ddo."""
web3 = get_web3()
ddo = Asset(ddo_dict)
alg_ddo = Asset(alg_ddo_dict)
sa_compute = get_first_service_by_type(alg_ddo, ServiceType.ACCESS)
Expand Down
7 changes: 3 additions & 4 deletions tests/helpers/compute_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def build_and_send_ddo_with_compute_service(
do_send=True,
short_valid_until=True,
timeout=3600,
c2d_environment="ocean-compute",
):
web3 = get_web3()
algo_metadata = build_metadata_dict_type_algorithm()
Expand Down Expand Up @@ -82,8 +83,6 @@ def build_and_send_ddo_with_compute_service(
datatoken = service.datatoken_address
mint_100_datatokens(web3, datatoken, consumer_wallet.address, publisher_wallet)

environments = get_c2d_environments()

if not do_send:
return (dataset_ddo_w_compute_service, alg_ddo)

Expand All @@ -97,7 +96,7 @@ def build_and_send_ddo_with_compute_service(
service,
consumer_wallet.address,
get_future_valid_until(short=short_valid_until),
environments[0]["id"],
c2d_environment,
),
consumer_wallet,
)
Expand All @@ -114,7 +113,7 @@ def build_and_send_ddo_with_compute_service(
alg_service,
consumer_wallet.address,
get_future_valid_until(short=short_valid_until),
environments[0]["id"],
c2d_environment,
force_zero=True,
),
consumer_wallet,
Expand Down
Loading

0 comments on commit a7466a8

Please sign in to comment.