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: implements check for indexed event arguments #3570

Merged
Merged
Show file tree
Hide file tree
Changes from 6 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
76 changes: 76 additions & 0 deletions tests/parser/syntax/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,82 @@ def f(a: uint256): # visibility is nonpayable instead of view
""",
InterfaceViolation,
),
(
# `receiver` of `Transfer` event should be indexed
"""
from vyper.interfaces import ERC20

implements: ERC20

event Transfer:
sender: indexed(address)
receiver: address
value: uint256

event Approval:
owner: indexed(address)
spender: indexed(address)
value: uint256

name: public(String[32])
symbol: public(String[32])
decimals: public(uint8)
balanceOf: public(HashMap[address, uint256])
allowance: public(HashMap[address, HashMap[address, uint256]])
totalSupply: public(uint256)

@external
def transfer(_to : address, _value : uint256) -> bool:
return True

@external
def transferFrom(_from : address, _to : address, _value : uint256) -> bool:
return True

@external
def approve(_spender : address, _value : uint256) -> bool:
return True
""",
InterfaceViolation,
),
(
# `value` of `Transfer` event should not be indexed
"""
from vyper.interfaces import ERC20

implements: ERC20

event Transfer:
sender: indexed(address)
receiver: indexed(address)
value: indexed(uint256)

event Approval:
owner: indexed(address)
spender: indexed(address)
value: uint256

name: public(String[32])
symbol: public(String[32])
decimals: public(uint8)
balanceOf: public(HashMap[address, uint256])
allowance: public(HashMap[address, HashMap[address, uint256]])
totalSupply: public(uint256)

@external
def transfer(_to : address, _value : uint256) -> bool:
return True

@external
def transferFrom(_from : address, _to : address, _value : uint256) -> bool:
return True

@external
def approve(_spender : address, _value : uint256) -> bool:
return True
""",
InterfaceViolation,
),
]


Expand Down
21 changes: 15 additions & 6 deletions vyper/semantics/types/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def __init__(self, name: str, arguments: dict, indexed: list) -> None:
super().__init__(members=arguments)
self.name = name
self.indexed = indexed
assert len(self.indexed) == len(self.arguments)
self.event_id = int(keccak256(self.signature.encode()).hex(), 16)

# backward compatible
Expand All @@ -172,8 +173,13 @@ def arguments(self):
return self.members

def __repr__(self):
arg_types = ",".join(repr(a) for a in self.arguments.values())
return f"event {self.name}({arg_types})"
args = []
for is_indexed, (_, argtype) in zip(self.indexed, self.arguments.items()):
argtype_str = repr(argtype)
if is_indexed:
argtype_str = f"indexed({argtype_str})"
args.append(f"{argtype_str}")
return f"event {self.name}({','.join(args)})"

# TODO rename to abi_signature
@property
Expand Down Expand Up @@ -337,12 +343,15 @@ def _is_function_implemented(fn_name, fn_type):

# check for missing events
for name, event in self.events.items():
if name not in namespace:
unimplemented.append(name)
if not isinstance(namespace[name], EventT):
unimplemented.append(f"{name} is not an event!")
if (
name not in namespace
or not isinstance(namespace[name], EventT)
or namespace[name].event_id != event.event_id
namespace[name].event_id != event.event_id
or namespace[name].indexed != event.indexed
):
unimplemented.append(name)
unimplemented.append(f"{name} is not implemented! (should be {event})")

if len(unimplemented) > 0:
# TODO: improve the error message for cases where the
Expand Down