Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Provider multichain #574

Merged
merged 42 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
608b81c
Intermediary commit -- download works.
calina-c Nov 15, 2022
302b8ce
Black run.
calina-c Nov 15, 2022
a693fbc
More get_web3 corrections.
calina-c Nov 16, 2022
f68d0af
More intermediary work.
calina-c Nov 16, 2022
362519e
Fixes last regular initialize test.
calina-c Nov 16, 2022
6c15aa0
Adds actual dict for pk sample.
calina-c Nov 16, 2022
96a8428
Show networks in info endpoint.
calina-c Nov 16, 2022
73bd08b
Fix tests for onchain assets.
calina-c Nov 16, 2022
a36b2cf
Use any_chain in auth token signing.
calina-c Nov 16, 2022
d89ffd1
Fix proof tests.
calina-c Nov 16, 2022
5acd642
Add chainId parameter to encryption.
calina-c Nov 17, 2022
d4c19d3
Allow for PROVIDER_FEE_TOKEN to be defined as a dictionary.
calina-c Nov 17, 2022
361ff6f
Adds UNIVERSAL_PRIVATE_KEY and fixes RBAC.
calina-c Nov 18, 2022
cd09f1f
Adapt is_same_provider function.
calina-c Nov 18, 2022
7227ce4
Fix initialize tests.
calina-c Nov 18, 2022
ad5a924
Fix initialize tests.
calina-c Nov 18, 2022
b9c2996
Intermediary commit.
calina-c Nov 18, 2022
c61a290
Make startCompute multichain, fix algo validation tests.
calina-c Nov 21, 2022
a05a44d
Reinstate chain_id when sending the workflow.
calina-c Nov 21, 2022
90bdac0
Add some tests.
calina-c Nov 22, 2022
c832251
Adds tests for get_provider_addresses.
calina-c Nov 22, 2022
9e64219
Adds tests for get_provider_private_key.
calina-c Nov 22, 2022
6a177c7
Correct test values to sync with key settings.
calina-c Nov 22, 2022
d4734ad
Adds flat vs indexed on get_c2d_environments.
calina-c Nov 22, 2022
0e3745e
Fix failing smartcontract call test.
calina-c Nov 22, 2022
d4160dc
Fix missing c2d env.
calina-c Nov 22, 2022
3773efd
Remove Config object. (#576)
calina-c Nov 29, 2022
1fc928f
Fix some failing tests.
calina-c Nov 29, 2022
29310ce
Fix some failing tests.
calina-c Nov 29, 2022
cba2e1e
Remove get_web3 from status call.
calina-c Nov 29, 2022
34a078d
Cosmetisations to smartcontract type.
calina-c Nov 29, 2022
f4a340f
allow unlisted state
alexcos20 Dec 11, 2022
257c364
add type to fileinfo
alexcos20 Dec 15, 2022
f8801ba
Merge branch 'main' into multichain
calina-c Feb 13, 2023
cee4bb9
Merge branch 'main' into multichain
calina-c Mar 10, 2023
8a18573
Merge branch 'main' into multichain
calina-c Mar 10, 2023
40734a2
Merge branch 'main' into multichain
calina-c Mar 14, 2023
2303e19
Fix CIs.
calina-c Mar 14, 2023
80f0d6f
Fix a leftover conflict.
calina-c Mar 15, 2023
3cc27ed
Revert "Fix CIs."
calina-c Mar 15, 2023
e8ac847
Temp debug.
calina-c Mar 16, 2023
3cd7c9a
Temporarily disable old signature check.
calina-c Mar 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 19.3b0
rev: 'refs/tags/22.6.0:refs/tags/22.6.0'
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ Add the corresponding environment variables in your `.env` file. Here is an exam
FLASK_APP=ocean_provider/run.py
PROVIDER_CONFIG_FILE=config.ini
PROVIDER_ADDRESS=your ethereum address goes here
PROVIDER_PRIVATE_KEY=the private key
PROVIDER_FEE_TOKEN = address of ERC20 token used to get fees
PROVIDER_PRIVATE_KEY= the private key or string containing a dict of chain_id to private key pairs
PROVIDER_FEE_TOKEN = the address of ERC20 token used to get fees, or string containing a dict of chain_id to token address pairs
```

You might also want to set `FLASK_ENV=development`. Then run ```flask run --port=8030```
Expand Down
16 changes: 10 additions & 6 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

import pytest
from eth_account import Account

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

app = app
Expand Down Expand Up @@ -43,7 +47,7 @@ def consumer_address(consumer_wallet):

@pytest.fixture
def ganache_wallet():
web3 = get_web3()
web3 = get_web3(8996)
if (
web3.eth.accounts
and web3.eth.accounts[0].lower()
Expand All @@ -58,7 +62,7 @@ def ganache_wallet():

@pytest.fixture
def provider_wallet():
pk = os.environ.get("PROVIDER_PRIVATE_KEY")
pk = get_provider_private_key(8996)
return Account.from_key(pk)


Expand All @@ -69,7 +73,7 @@ def provider_address(provider_wallet):

@pytest.fixture(autouse=True)
def setup_all(provider_address, consumer_address, ganache_wallet):
web3 = get_web3()
web3 = get_web3(8996)
if ganache_wallet:
if (
web3.fromWei(
Expand All @@ -92,7 +96,7 @@ def setup_all(provider_address, consumer_address, ganache_wallet):

@pytest.fixture
def web3():
return get_web3()
return get_web3(8996)


@pytest.fixture
Expand Down
4 changes: 2 additions & 2 deletions ocean_provider/file_types/file_types_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
from typing import Any, Tuple

from enforce_typing import enforce_types

from ocean_provider.file_types.file_types import (
ArweaveFile,
GraphqlQuery,
IpfsFile,
UrlFile,
GraphqlQuery,
)
from ocean_provider.file_types.types.smartcontract import SmartContractCall

Expand Down Expand Up @@ -55,6 +54,7 @@ def validate_and_create(file_obj) -> Tuple[bool, Any]:
elif file_obj["type"] == "smartcontract":
instance = SmartContractCall(
address=file_obj.get("address"),
chain_id=file_obj.get("chainId"),
abi=file_obj.get("abi"),
userdata=file_obj.get("userdata"),
)
Expand Down
7 changes: 4 additions & 3 deletions ocean_provider/file_types/types/smartcontract.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

from enforce_typing import enforce_types
from flask import Response
from web3.main import Web3

from ocean_provider.file_types.definitions import FilesType
from ocean_provider.utils.basics import get_provider_wallet, get_web3
from web3.main import Web3

logger = logging.getLogger(__name__)

Expand All @@ -19,10 +18,12 @@ class SmartContractCall(FilesType):
def __init__(
self,
address: Optional[str] = None,
chain_id: Optional[int] = None,
abi: Optional[dict] = None,
userdata=None,
) -> None:
self.address = address
self.chain_id = chain_id
self.type = "smartcontract"
self.abi = abi
self.userdata = None
Expand Down Expand Up @@ -63,7 +64,7 @@ def get_filename(self) -> str:
return uuid4().hex

def fetch_smartcontract_call(self):
web3 = get_web3()
web3 = get_web3(self.chain_id)
contract = web3.eth.contract(
address=web3.toChecksumAddress(self.address), abi=[self.abi]
)
Expand Down
10 changes: 5 additions & 5 deletions ocean_provider/routes/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
# Copyright 2021 Ocean Protocol Foundation
# SPDX-License-Identifier: Apache-2.0
#
import jwt
import logging
import os

from flask import request, jsonify
import jwt
from flask import jsonify, request
from flask_sieve import validate

from ocean_provider.utils.util import get_request_data
from ocean_provider.user_nonce import (
force_expire_token,
force_restore_token,
is_token_valid,
)
from ocean_provider.utils.basics import get_provider_private_key
from ocean_provider.utils.util import get_request_data
from ocean_provider.validation.provider_requests import (
CreateTokenRequest,
DeleteTokenRequest,
Expand Down Expand Up @@ -64,7 +64,7 @@ def create_auth_token():
address = data.get("address")
expiration = int(data.get("expiration"))

pk = os.environ.get("PROVIDER_PRIVATE_KEY")
pk = get_provider_private_key(any_chain=True)
token = jwt.encode({"exp": expiration, "address": address}, pk, algorithm="HS256")
token = token.decode("utf-8") if isinstance(token, bytes) else token

Expand Down
17 changes: 8 additions & 9 deletions ocean_provider/routes/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,24 @@
validate_timestamp,
)
from ocean_provider.utils.compute import (
get_compute_endpoint,
get_compute_result_endpoint,
process_compute_request,
sign_for_compute,
get_compute_result_endpoint,
get_compute_endpoint,
)
from ocean_provider.utils.compute_environments import (
get_c2d_environments,
check_environment_exists,
get_c2d_environments,
)
from ocean_provider.utils.error_responses import error_response
from ocean_provider.utils.provider_fees import (
get_provider_fees_or_remote,
comb_for_valid_transfer_and_fees,
get_provider_fees_or_remote,
)
from ocean_provider.utils.util import (
get_request_data,
)
from ocean_provider.utils.util import get_request_data
from ocean_provider.validation.algo import (
WorkflowValidator,
InputItemValidator,
WorkflowValidator,
get_algo_checksums,
)
from ocean_provider.validation.images import validate_container
Expand All @@ -55,7 +53,6 @@

from . import services

provider_wallet = get_provider_wallet()
requests_session = get_requests_session()

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -154,13 +151,15 @@ def initializeCompute():
return error_response("DID is not a valid algorithm", 400, logger)

algo_service = algo_ddo.get_service_by_id(algorithm.get("serviceId"))
provider_wallet = get_provider_wallet(algo_ddo.chain_id)
algo_files_checksum, algo_container_checksum = get_algo_checksums(
algo_service, provider_wallet, algo_ddo
)

for i, dataset in enumerate(datasets):
dataset["algorithm"] = algorithm
dataset["consumerAddress"] = consumer_address
provider_wallet = get_provider_wallet(dataset.chain_id)
input_item_validator = InputItemValidator(
web3,
consumer_address,
Expand Down
30 changes: 12 additions & 18 deletions ocean_provider/routes/consume.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,19 @@
from flask_sieve import validate
from ocean_provider.file_types.file_types_factory import FilesTypeFactory
from ocean_provider.requests_session import get_requests_session
from ocean_provider.routes import services
from ocean_provider.user_nonce import get_nonce, update_nonce
from ocean_provider.utils.asset import (
get_asset_from_metadatastore,
check_asset_consumable,
get_asset_from_metadatastore,
)
from ocean_provider.utils.basics import (
get_provider_wallet,
get_web3,
get_metadata_url,
)
from ocean_provider.utils.basics import get_metadata_url, get_provider_wallet, get_web3
from ocean_provider.utils.datatoken import validate_order
from ocean_provider.utils.error_responses import error_response
from ocean_provider.utils.proof import send_proof
from ocean_provider.utils.provider_fees import get_provider_fees, get_c2d_environments
from ocean_provider.utils.provider_fees import get_c2d_environments, get_provider_fees
from ocean_provider.utils.services import ServiceType
from ocean_provider.utils.util import (
get_request_data,
get_service_files_list,
)
from ocean_provider.utils.util import get_request_data, get_service_files_list
from ocean_provider.validation.provider_requests import (
DownloadRequest,
FileInfoRequest,
Expand All @@ -36,9 +30,6 @@
)
from web3.main import Web3

from ocean_provider.routes import services

provider_wallet = get_provider_wallet()
requests_session = get_requests_session()

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -106,6 +97,7 @@ def fileinfo():
400,
logger,
)
provider_wallet = get_provider_wallet(asset.chain_id)
files_list = get_service_files_list(service, provider_wallet, asset)
if not files_list:
return error_response("Unable to get dataset files", 400, logger)
Expand Down Expand Up @@ -199,7 +191,7 @@ def initialize():
if "transferTxId" in data:
try:
_tx, _order_log, _, _ = validate_order(
get_web3(),
get_web3(asset.chain_id),
consumer_address,
data["transferTxId"],
asset,
Expand All @@ -215,6 +207,7 @@ def initialize():
file_index = int(data.get("fileIndex", "-1"))
# we check if the file is valid only if we have fileIndex
if file_index > -1:
provider_wallet = get_provider_wallet(asset.chain_id)
url_object = get_service_files_list(service, provider_wallet, asset)[file_index]
url_object["userdata"] = data.get("userdata")
valid, message = FilesTypeFactory.validate_and_create(url_object)
Expand All @@ -235,7 +228,7 @@ def initialize():
# of tokens required for this service
# The consumer must sign and execute this transaction in order to be
# able to consume the service
provider_fee = get_provider_fees(did, service, consumer_address, 0)
provider_fee = get_provider_fees(asset, service, consumer_address, 0)
if provider_fee:
provider_fee["providerFeeAmount"] = str(provider_fee["providerFeeAmount"])
approve_params = {
Expand Down Expand Up @@ -327,7 +320,7 @@ def download():

try:
_tx, _order_log, _, _ = validate_order(
get_web3(), consumer_address, tx_id, asset, service
get_web3(asset.chain_id), consumer_address, tx_id, asset, service
)
except Exception as e:
return error_response(
Expand All @@ -337,6 +330,7 @@ def download():
)

file_index = int(data.get("fileIndex"))
provider_wallet = get_provider_wallet(asset.chain_id)
files_list = get_service_files_list(service, provider_wallet, asset)
if file_index > len(files_list):
return error_response(f"No such fileIndex {file_index}", 400, logger)
Expand Down Expand Up @@ -375,7 +369,7 @@ def download():
consumer_data = f'{did}{data.get("nonce")}'

send_proof(
web3=get_web3(),
asset.chain_id,
order_tx_id=_tx.hash,
provider_data=provider_proof_data,
consumer_data=consumer_data,
Expand Down
15 changes: 6 additions & 9 deletions ocean_provider/routes/decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@
from flask_sieve import validate
from ocean_provider.requests_session import get_requests_session
from ocean_provider.user_nonce import update_nonce
from ocean_provider.utils.basics import (
get_config,
get_provider_wallet,
get_web3,
)
from ocean_provider.utils.basics import get_config, get_provider_wallet, get_web3
from ocean_provider.utils.data_nft import (
MetadataState,
get_metadata,
Expand All @@ -31,7 +27,6 @@

from . import services

provider_wallet = get_provider_wallet()
requests_session = get_requests_session()

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -124,8 +119,8 @@ def _decrypt(
update_nonce(decrypter_address, nonce)

# Check if given chain_id matches Provider's chain_id
web3 = get_web3()
if web3.chain_id != chain_id:
web3 = get_web3(chain_id)
if not web3:
return error_response(f"Unsupported chain ID {chain_id}", 400, logger)

# Check if decrypter is authorized
Expand Down Expand Up @@ -181,7 +176,9 @@ def _decrypt(
# bit 2: check if DDO is ecies encrypted
if flags[0] & 2:
try:
working_document = do_decrypt(working_document, get_provider_wallet())
working_document = do_decrypt(
working_document, get_provider_wallet(chain_id)
)
logger.info("Successfully decrypted document.")
except Exception:
return error_response("Failed to decrypt.", 400, logger)
Expand Down
Loading