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

Fix Incorrect event validation #632

Merged
merged 7 commits into from
Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 20 additions & 12 deletions ocean_provider/utils/datatoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,29 @@ def verify_order_tx(

# check provider fees
datatoken_contract = get_datatoken_contract(web3, datatoken_address)
provider_fee_order_log = None
provider_fee_event_logs = datatoken_contract.events.ProviderFee().processReceipt(
tx_receipt, errors=DISCARD
)
# search in all provider_fee events until we have a match. if not, we don't have a valid event
# also, make sure that somebody is not spoofing provider fee event from another datatoken
for provider_fees_logs in provider_fee_event_logs:
try:
provider_data = json.loads(provider_fees_logs.args.providerData)
if (
provider_data["dt"].lower() == datatoken_address.lower()
alexcos20 marked this conversation as resolved.
Show resolved Hide resolved
and provider_data["id"].lower() == service.id.lower()
):
provider_fee_order_log = provider_fees_logs

except:
calina-c marked this conversation as resolved.
Show resolved Hide resolved
# silent pass, means json formatting errors
pass

provider_fee_order_log = (
provider_fee_event_logs[0] if provider_fee_event_logs else None
)
if not provider_fee_order_log:
raise AssertionError(
f"Cannot find the event for the provider fee in tx id {tx_id}."
)
if len(provider_fee_event_logs) > 1:
raise AssertionError(
f"Multiple order events in the same transaction !!! {provider_fee_order_log}"
)

provider_initialize_timestamp = 0
if extra_data:
Expand Down Expand Up @@ -176,16 +184,16 @@ def verify_order_tx(
except Exception as e:
logger.error(e)
logger.debug(f"Got events log when searching for OrderStarted : {event_logs}")
order_log = event_logs[0] if event_logs else None
order_log = None
alexcos20 marked this conversation as resolved.
Show resolved Hide resolved
# search in all startOrder events until we have a match. if not, we don't have a valid event
for log in event_logs:
if log.args.serviceIndex == service.index:
order_log = log

if not order_log:
raise AssertionError(
f"Cannot find the event for the order transaction with tx id {tx_id}."
)
if len(event_logs) > 1:
raise AssertionError(
f"Multiple order events in the same transaction !!! {event_logs}"
)

if order_log.args.serviceIndex != service.index:
raise AssertionError(
Expand Down
7 changes: 6 additions & 1 deletion ocean_provider/utils/provider_fees.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ def get_provider_fees(
provider_fee_amount = 0

provider_data = json.dumps(
{"environment": compute_env, "timestamp": datetime.utcnow().timestamp()},
{
"environment": compute_env,
"timestamp": datetime.utcnow().timestamp(),
"dt": service.datatoken_address,
"id": service.id,
},
separators=(",", ":"),
)
message_hash = Web3.solidityKeccak(
Expand Down
89 changes: 89 additions & 0 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
# SPDX-License-Identifier: Apache-2.0
#
import copy
import logging
import time
from datetime import datetime
from unittest.mock import patch

import pytest
from ocean_provider.constants import BaseURLs
from ocean_provider.utils.accounts import sign_message
from ocean_provider.utils.data_nft_factory import get_data_nft_factory_address
from ocean_provider.utils.provider_fees import get_provider_fees
from ocean_provider.utils.services import ServiceType
from tests.helpers.constants import ARWEAVE_TRANSACTION_ID
from tests.test_auth import create_token
from tests.test_helpers import (
approve_multiple_tokens,
get_dataset_ddo_with_multiple_files,
get_first_service_by_type,
get_registered_asset,
get_service_by_index,
mint_100_datatokens,
mint_multiple_tokens,
start_order,
start_multiple_order,
try_download,
)

logger = logging.getLogger(__name__)


@pytest.mark.integration
@pytest.mark.parametrize(
Expand Down Expand Up @@ -96,6 +105,86 @@ def test_download_service(
assert response.status_code == 400, f"{response.data}"


@pytest.mark.integration
def test_download_multiple_orders(client, publisher_wallet, consumer_wallet, web3):
nft_factory_address = get_data_nft_factory_address(web3)
# create an asset with one services using standard template
asset1 = get_registered_asset(publisher_wallet, erc20_enterprise=False)
service1 = get_first_service_by_type(asset1, ServiceType.ACCESS)

# create an asset with two services using enterprise
asset2 = get_registered_asset(
publisher_wallet, erc20_enterprise=True, no_of_services=2
)
service2 = get_first_service_by_type(asset2, ServiceType.ACCESS)
alexcos20 marked this conversation as resolved.
Show resolved Hide resolved
service3 = get_service_by_index(asset2, 1)

mint_multiple_tokens(
web3,
[
service1.datatoken_address,
service2.datatoken_address,
service3.datatoken_address,
],
consumer_wallet.address,
publisher_wallet,
)
approve_multiple_tokens(
web3,
[
service1.datatoken_address,
service2.datatoken_address,
service3.datatoken_address,
],
nft_factory_address,
1,
consumer_wallet,
)
tx_id, _ = start_multiple_order(
web3,
[
(
service1.datatoken_address,
consumer_wallet.address,
service1.index,
get_provider_fees(asset1, service1, consumer_wallet.address, 0),
(
"0x0000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000",
0,
),
),
(
service2.datatoken_address,
consumer_wallet.address,
service2.index,
get_provider_fees(asset2, service2, consumer_wallet.address, 0),
(
"0x0000000000000000000000000000000000000000",
alexcos20 marked this conversation as resolved.
Show resolved Hide resolved
"0x0000000000000000000000000000000000000000",
0,
),
),
(
service3.datatoken_address,
consumer_wallet.address,
service3.index,
get_provider_fees(asset2, service3, consumer_wallet.address, 0),
(
"0x0000000000000000000000000000000000000000",
"0x0000000000000000000000000000000000000000",
0,
),
),
],
consumer_wallet,
)
# check to see if we can get all 3 files
try_download(client, asset1, service1, consumer_wallet, tx_id, False)
try_download(client, asset2, service2, consumer_wallet, tx_id, False)
try_download(client, asset2, service3, consumer_wallet, tx_id, False)


@pytest.mark.integration
@pytest.mark.parametrize("timeout", [0, 1, 3600])
def test_download_timeout(client, publisher_wallet, consumer_wallet, web3, timeout):
Expand Down
Loading