oceanprotocol / ocean.py

🦑 Ocean Protocol's Python library to privately & securely publish, exchange, and consume data.
https://oceanprotocol.com
Apache License 2.0
172 stars 78 forks source link

ocean_assets.py::download_file() error, because allowedSwapper is nonzero #1161

Closed trentmc closed 1 year ago

trentmc commented 1 year ago

Summary

Steps to solve this issue, while keeping ocean.py code clean & maintainable:

Background & motivation

Traceback shared by Alex:

Hi, new here and getting just started using Ocean Protocol. Taking a stab at the Storage Network Data Challenge. I have set up the entire environment and sucessfully connected to a Polygon Mainnet Wallet containing sufficient MATIC. print(accounts.at(challenge_wallet.address).balance()) in python console shows me the correct MATIC amount so I tend to think everything is fine to start downloading stuff from the Ocean Market. However, when I try to do that for the data sets in the Challenge I get an error.  Any pointers what could be the issue?  Command I tried is: filecoin_price = ocean.assets.download_file("did:op:d2f9630759d2fad32d1765a5e9a380b77e674a9561d107a062bd47a66b70f4bc",challenge_wallet)
Resolve did...
Dispense access token...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ronald/ocean/challenges/StorageNetworkDataChallenge/venv/lib/python3.10/site-packages/enforce_typing/decorator.py", line 29, in wrapper
    return func(*args, **kwargs)
  File "/home/ronald/ocean/challenges/StorageNetworkDataChallenge/venv/lib/python3.10/site-packages/ocean_lib/ocean/ocean_assets.py", line 709, in download_file
    raise ValueError("Not allowed. allowedSwapper={allowedSwapper}")
ValueError: Not allowed. allowedSwapper={allowedSwapper}

@alexcos20 's Q: does ocean.py 2.0.0a4 knows how to handle different templates ?

Here's the code inside ocean_assets.py::download_file():

            # catch key failure modes
            st = dispenser.status(datatoken.address)
            active, allowedSwapper = st[0], st[6]
            if not active:
                raise ValueError("No active dispenser for datatoken")
            if allowedSwapper not in [ZERO_ADDRESS, wallet.address]:
                raise ValueError("Not allowed. allowedSwapper={allowedSwapper}")

Trent notes:

We need to do three things to fix this:

  1. tactical fix for the user, now
  2. definitive fix in market, so that when assets are published as "free" in Ocean Market, anyone can dispense() them
  3. workaround fix in ocean.py download_file(), enable download of assets that are {"free", "enterprise template", allowed_swapper != ZERO}. See "summary" at the top of this issue, and below

Details For (1): tactical fix for the user, now

Here's the fix for the user: whoever is the owner of that asset, needs to set allowedSwapper to 0x000...000, via a call to Dispenser.sol::setAllowedSwapper(). It could be via etherscan, ocean.js, or ocean.py. Here's the key line for ocean.py

Example in python

private_key = <set as needed>
config = <set as needed>
datatoken_address = <set as needed>

import os
from ocean_lib.web3_internal.constants import ZERO_ADDRESS
from ocean_lib.ocean.ocean import Ocean
from brownie.network import accounts

accounts.clear()
your_wallet = accounts.add(private_key)
ocean = Ocean(config)

# main work
ocean.dispenser.setAllowedSwapper(datatoken_address, ZERO_ADDRESS, {"from" : your_wallet})

Details for (2): definitive fix in market

Goal: so that when assets are published as "free" in Ocean Market, anyone can dispense() them

Like mentioned, we had this issue before - see market#1725 - opened on Oct 4, closed Nov 15.

But it seems like it's not fixed, given the behavior above.

Here's the PR that aimed to fix it.

To recap: the issue in Ocean Market is: when publishing a free asset, in Ocean Market, people think that assume that anyone can actually download it. Yet dispense() doesn't work -- not only in ocean.py, but also anywhere else that people would try, like etherscan or web3.js.

Did the PR actually fix it? Investigating, the answer is no. https://github.com/oceanprotocol/market/blob/0ccd375738d3f51d23a66f9e0576a0f3b412a6fd/app.config.js#L21

Towards solving, market#1725 has been re-opened.

Details for (3): definitive fix in ocean.py

Note: the "summary" at the top of this issue is for (3).

Goal: enable download of assets that are {"free", "enterprise template", allowed_swapper != ZERO}

Note that ocean_assets::download_file() did work for both templates, exactly as expected.

So how does it relate to the issue at hand? ocean_assets::download_file() already works for {"free", "enterprise template", allowed_swapper == ZERO} but because "enterprise template" weirdly set allowed_swapper != ZERO, even for "free", and that default is not changeable, we want to find a workaround, so ocean.py can the asset anyway. That is: download in the case of {"free", "enterprise template", allowed_swapper != ZERO}

A tool:

But we can use this quirk as a workaround:

trentmc commented 1 year ago

Marked as high priority (at least to do part 1), because it's blocking users of a current Data Challenge.

alexcos20 commented 1 year ago

I will fix it in ocean.py

calina-c commented 1 year ago

@trentmc after merging the last part of the interface simplification in #1220, we can resume this.