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

Cannot declare HashMap inside struct #2316

Closed
arjunaskykok opened this issue Feb 20, 2021 · 5 comments
Closed

Cannot declare HashMap inside struct #2316

arjunaskykok opened this issue Feb 20, 2021 · 5 comments

Comments

@arjunaskykok
Copy link

arjunaskykok commented Feb 20, 2021

Version Information

  • vyper Version (output of vyper --version): 0.2.10+commit.418c85b
  • OS: linux
  • Python Version (output of python --version): Python 3.8.5
  • Environment (output of pip freeze):
apipkg==1.5
asttokens==2.0.4
attrs==20.3.0
base58==2.0.1
bitarray==1.2.2
blake2b-py==0.1.3
cached-property==1.5.2
certifi==2020.12.5
chardet==4.0.0
coverage==5.3.1
cytoolz==0.11.0
eth-abi==2.1.1
eth-account==0.5.4
eth-bloom==1.0.3
eth-hash==0.2.0
eth-keyfile==0.5.1
eth-keys==0.3.3
eth-rlp==0.2.1
eth-tester==0.5.0b3
eth-typing==2.2.2
eth-utils==1.9.5
execnet==1.7.1
hexbytes==0.2.1
hypothesis==5.46.0
idna==2.10
iniconfig==1.1.1
ipfshttpclient==0.7.0a1
jsonschema==3.2.0
lark==0.11.1
lru-dict==1.1.6
multiaddr==0.0.9
mypy-extensions==0.4.3
netaddr==0.8.0
packaging==20.8
parsimonious==0.8.1
pluggy==0.13.1
protobuf==3.14.0
py==1.10.0
py-ecc==4.1.0
py-evm==0.3.0a20
pycryptodome==3.9.9
pyethash==0.1.27
pyparsing==2.4.7
pyrsistent==0.17.3
pytest==6.2.1
pytest-cov==2.10.1
pytest-forked==1.3.0
pytest-xdist==2.2.0
requests==2.25.1
rlp==2.0.1
semantic-version==2.8.5
six==1.15.0
sortedcontainers==2.3.0
toml==0.10.2
toolz==0.11.1
trie==2.0.0a5
typing-extensions==3.7.4.3
urllib3==1.26.2
varint==1.0.2
vyper==0.2.10
web3==5.13.1
websockets==8.1

What's your issue about?

$ cat /tmp/a.vy
struct a:
  map: HashMap[address, bool]
$ vyper /tmp/a.vy
Error compiling: /tmp/a.vy
vyper.exceptions.StructureException: HashMap can only be declared as a storage variable
  contract "/tmp/a.vy", line 2:7 
       1 struct a:
  ---> 2   map: HashMap[address, bool]
  --------------^
       3

I would like to have HashMap inside struct. Right now I am translating OpenZeppelin code to Vyper. For example I'm translating this code: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/AccessControl.sol

They got mapping inside struct.

    struct RoleData {
        mapping (address => bool) members;
        bytes32 adminRole;
    }
@iamdefinitelyahuman
Copy link
Contributor

This is a documented behavior. Including a mapping within a struct means the struct cannot be used in calldata or memory, and so creates an inconsistent behavior dependent upon the shape of the struct.

@fubuloubu
Copy link
Member

fubuloubu commented Feb 20, 2021

Seems like a better approach here would be a simple HashMap[address, bytes32] and use bitand-masking to compute inclusion/exclusion checks, with each bit serving as a role. and bitorA maximum of 256 roles would therefore be possible.

Better support for Enums would make this easier (similar to IntFlagEnum), you could then add an API for something like this:

enum Roles:
    admin  # bit 0
    ...  # bits 1 - N-1

user_role: HashMap[address, Roles]

def only_admin():
    # NOTE: The following performs `self.roles[msg.sender] & Roles.admin == Roles.admin`
    assert self.roles[msg.sender] is Roles.admin  # weird semantics...
    # OR
    assert Roles.admin in self.roles[msg.sender]  # weird to read, but makes sense if you think about it

Enums would make a good candidate for a VIP!

@arjunaskykok
Copy link
Author

So we are not going to support this? Feel free to close this issue.

Right now I compensate the lacking of HashMap inside struct with two arrays:

_roles: HashMap[bytes32, HashMap[address, bool]]
_admin_roles: HashMap[bytes32, bytes32]

@fubuloubu
Copy link
Member

So we are not going to support this? Feel free to close this issue.

IMO it's a bad idea to support that behavior, without letting them be used in similar contexts between storage and memory.

@charles-cooper
Copy link
Member

won't fix, it's a feature not a bug!

@charles-cooper charles-cooper closed this as not planned Won't fix, can't repro, duplicate, stale Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants