Skip to content

Commit

Permalink
fix: handle anonymous and strange event data in parse_rich_tree() m…
Browse files Browse the repository at this point in the history
…ethod (#2243)
  • Loading branch information
antazoey authored Aug 23, 2024
1 parent ff080f2 commit a250304
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/ape_ethereum/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,10 +654,15 @@ def _debug_trace_call(self):
def parse_rich_tree(call: dict, verbose: bool = False) -> Tree:
tree = _create_tree(call, verbose=verbose)
for event in call.get("events", []):
if "calldata" not in event and "name" not in event:
# Not sure; or not worth showing.
logger.debug(f"Unknown event data: '{event}'.")
continue

event_tree = _create_event_tree(event)
tree.add(event_tree)

for sub_call in call["calls"]:
for sub_call in call.get("calls", []):
sub_tree = parse_rich_tree(sub_call, verbose=verbose)
tree.add(sub_tree)

Expand All @@ -671,6 +676,8 @@ def _events_to_trees(events: list[dict]) -> list[Tree]:
calldata = evt.get("calldata")

if not name or not calldata:
# Not sure; or not worth showing.
logger.debug(f"Unknown event data: '{evt}'.")
continue

tuple_key = (
Expand All @@ -682,9 +689,15 @@ def _events_to_trees(events: list[dict]) -> list[Tree]:
result = []
for evt_tup, events in event_counter.items():
count = len(events)
evt = events[0]
if "name" not in evt and "calldata" not in evt:
# Not sure; or not worth showing.
logger.debug(f"Unknown event data: '{evt}'.")
continue

# NOTE: Using similar style to gas-cost on purpose.
suffix = f"[[{TraceStyles.GAS_COST}]x{count}[/]]" if count > 1 else ""
evt_tree = _create_event_tree(events[0], suffix=suffix)
evt_tree = _create_event_tree(evt, suffix=suffix)
result.append(evt_tree)

return result
Expand Down Expand Up @@ -757,8 +770,9 @@ def _call_to_str(call: dict, stylize: bool = False, verbose: bool = False) -> st
def _event_to_str(event: dict, stylize: bool = False, suffix: str = "") -> str:
# NOTE: Some of the styles are matching others parts of the trace,
# even though the 'name' is a bit misleading.
name = f"[{TraceStyles.METHODS}]{event['name']}[/]" if stylize else event["name"]
arguments_str = _get_inputs_str(event.get("calldata"), stylize=stylize)
event_name = event.get("name", "ANONYMOUS_EVENT")
name = f"[{TraceStyles.METHODS}]{event_name}[/]" if stylize else event_name
arguments_str = _get_inputs_str(event.get("calldata", "0x"), stylize=stylize)
prefix = f"[{TraceStyles.CONTRACTS}]log[/]" if stylize else "log"
return f"{prefix} {name}{arguments_str}{suffix}"

Expand Down
13 changes: 13 additions & 0 deletions tests/functional/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ def test_parse_rich_tree(vyper_contract_instance):
assert actual == expected


def test_parse_rich_tree_handles_anon_events():
data = {"events": [{"calldata": "0x123"}]}
tree = parse_rich_tree(data)
actual = tree.children[0].label.label # type: ignore
assert actual == "[#ff8c00]log[/] [bright_green]ANONYMOUS_EVENT[/](0x123)"


def test_parse_rich_tree_handle_bad_event_data():
data = {"events": [{"foo": "I don't know what this is."}]}
tree = parse_rich_tree(data)
assert len(tree.children) == 0


def test_get_gas_report(gas_tracker, owner, vyper_contract_instance):
tx = vyper_contract_instance.setNumber(924, sender=owner)
trace = tx.trace
Expand Down

0 comments on commit a250304

Please sign in to comment.