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

Adds a dictionary of event args/data to Event type for easier access to variables #60

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
48641f5
add key-value pair of event variable name to data emitted to all boa.…
kibagateaux Jan 23, 2023
9c1f958
move all Event type instantiation to decode_logs
kibagateaux Jan 23, 2023
ecbb5be
generate Event.args_map on request and cache instead of instaniating …
kibagateaux Jan 23, 2023
481b7f5
refactor for less code change
kibagateaux Jan 24, 2023
b25388e
use pre-commit hook
kibagateaux Jan 24, 2023
5ee6d62
fix formatting issues
kibagateaux Jan 24, 2023
ee50b01
return typed event data in args_map
kibagateaux Jan 25, 2023
7f6f671
update with pr comments and run pre-commit
kibagateaux Jan 30, 2023
813e0e6
add initial event tests
kibagateaux Feb 6, 2023
d36cbda
simplify tests
kibagateaux Feb 7, 2023
0c65895
remove chacing from event.ordered_args and update tests
kibagateaux Feb 7, 2023
0a35685
Merge branch 'master' of https://github.com/vyperlang/titanoboa
kibagateaux Jun 26, 2023
3385a34
improve state machine test
charles-cooper Jun 30, 2023
a9a710f
move some test directories around
charles-cooper Jun 30, 2023
3697e04
remove dead member variable
charles-cooper Jun 30, 2023
96cdb93
fix lint
charles-cooper Jun 30, 2023
43bfedc
add key-value pair of event variable name to data emitted to all boa.…
kibagateaux Jan 23, 2023
f4f0c90
move all Event type instantiation to decode_logs
kibagateaux Jan 23, 2023
2169b86
add initial event tests
kibagateaux Feb 6, 2023
4b960e7
simplify tests
kibagateaux Feb 7, 2023
dca7546
remove chacing from event.ordered_args and update tests
kibagateaux Feb 7, 2023
c027156
simplify and clean up event tests
kibagateaux Jul 4, 2023
5a3dc46
move event test out of strategies
kibagateaux Jul 4, 2023
1a2fc06
bump vyper version and update event tests for compatability
kibagateaux Jul 4, 2023
7dea3c3
update event tests to use actual contract event emissions
kibagateaux Jul 5, 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
2 changes: 1 addition & 1 deletion boa/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ def _hook_trace_computation(self, computation, contract=None):
# loop over pc so that it is available when coverage hooks into it
pass
for child in computation.children:
if child.msg.code_address != b'':
if child.msg.code_address != b"":
child_contract = self.lookup_contract(child.msg.code_address)
self._hook_trace_computation(child, child_contract)

Expand Down
1 change: 0 additions & 1 deletion boa/vyper/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,6 @@ def decode_log(self, e):
)

tuple_typ = TupleT(arg_typs)

args = abi.decode(tuple_typ.abi_type.selector_name(), data)

return Event(log_id, self.address, event_t, decoded_topics, args)
Expand Down
14 changes: 11 additions & 3 deletions boa/vyper/event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from typing import Any, List
from functools import cached_property
from typing import Any, Dict, List, Tuple


@dataclass
Expand All @@ -11,6 +12,14 @@ class Event:
args: List[Any] # list of decoded args

def __repr__(self):
args = ", ".join(f"{k}={v}" for k, v in self.ordered_args())
return f"{self.event_type.name}({args})"

@cached_property
def args_map(self) -> Dict[str, str | int]:
return dict(self.ordered_args())

def ordered_args(self) -> List[Tuple[str, str | int]]:
t_i = 0
a_i = 0
b = []
Expand All @@ -26,8 +35,7 @@ def __repr__(self):
b.append((k, self.args[a_i]))
a_i += 1

args = ", ".join(f"{k}={v}" for k, v in b)
return f"{self.event_type.name}({args})"
return b


class RawEvent:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hypothesis.strategies as st
import pytest
from hypothesis._settings import HealthCheck
from hypothesis.stateful import (
Expand All @@ -17,27 +18,30 @@ def boa_contract():
a: public(uint256)

@external
def add_to_a(_a: uint256):
self.a += _a
def add_to_a(d: uint256):
self.a += d
"""
return boa.loads(source_code)


NUM_STEPS = 100


class StateMachine(RuleBasedStateMachine):
contract = None

@initialize()
def setup(self):
self.contract.add_to_a(10)
self.a = 0

# empty rule just so hypothesis does not complain
@rule()
def void(self):
pass
@rule(d=st.integers(min_value=0, max_value=(2**256) // NUM_STEPS))
def change_a(self, d):
self.contract.add_to_a(d)
self.a += d

@invariant()
def foo(self):
assert self.contract.a() == 10
assert self.contract.a() == self.a

# ensure overriding teardown doesn't break things
def teardown(self):
Expand All @@ -47,7 +51,7 @@ def teardown(self):
def test_state_machine_isolation(boa_contract):
StateMachine.contract = boa_contract
StateMachine.settings = {
"max_examples": 5,
"stateful_step_count": NUM_STEPS,
"suppress_health_check": HealthCheck.all(),
}
run_state_machine_as_test(StateMachine)
44 changes: 44 additions & 0 deletions tests/unitary/test_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from boa.interpret import load

me = "0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"
token = load("examples/ERC20.vy", "Test", "TEST", 18, 0)
token.mint(me, 100)
event = token.get_logs()[0]


def test_topics_saved_to_event():
assert len(event.topics) == 2
assert event.topics[0] == "0x0000000000000000000000000000000000000000"
assert event.topics[1] == me


def test_args_saved_to_event():
assert len(event.args) == 1
assert event.args[0] == 100


def test_args_ordered_to_event_param_sequence():
args = event.ordered_args()
assert args[0][0] == "sender"
assert args[1][0] == "receiver"
assert args[2][0] == "value"


def test_args_map_match_event_params():
args = event.args_map
assert args["sender"] == "0x0000000000000000000000000000000000000000"
assert args["receiver"] == me
assert args["value"] == 100


def test_args_map_values_match_ordered_args():
assert event.args_map == {
"sender": "0x0000000000000000000000000000000000000000",
"receiver": me,
"value": 100,
}
assert event.ordered_args() == [
("sender", "0x0000000000000000000000000000000000000000"),
("receiver", me),
("value", 100),
]
File renamed without changes.