Skip to content

Commit

Permalink
Merge pull request #17 from unparalleled-js/refactor/display-node-begone
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Sep 8, 2022
2 parents 6acb4c0 + 7f0328c commit b865209
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest] # eventually add `windows-latest`
python-version: [3.7, 3.8, 3.9, "3.10"]
python-version: [3.8, 3.9, "3.10"]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ repos:


default_language_version:
python: python3.8
python: python3.9
21 changes: 1 addition & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Ethereum Virtual Machine transaction tracing tool

## Dependencies

* [python3](https://www.python.org/downloads) version 3.7.2 or greater, python3-dev
* [python3](https://www.python.org/downloads) version 3.8 or greater, python3-dev

## Installation

Expand Down Expand Up @@ -79,25 +79,6 @@ from evm_trace import get_calltree_from_parity_trace
tree = get_calltree_from_parity_trace(trace_list)
```

### Call Tree Node Customization

You can also customize the output by making your own display class:

```python
from evm_trace.display import DisplayableCallTreeNode, get_calltree_from_trace


class CustomDisplay(DisplayableCallTreeNode):
def title(self) -> str:
call_type = self.call.call_type.value.lower().capitalize()
address = self.call.address.hex()
cost = self.call.gas_cost
return f"{call_type} call @ {address} gas_cost={cost}"


calltree = get_calltree_from_geth_trace(trace, display_cls=CustomDisplay)
```

### Gas Reports

If you are using a node that supports creating traces, you can get a gas report.
Expand Down
10 changes: 3 additions & 7 deletions evm_trace/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import math
from typing import Dict, Iterator, List, Optional, Type
from typing import Dict, Iterator, List, Optional

from eth_utils import to_int
from ethpm_types import BaseModel, HexBytes
from pydantic import Field

from evm_trace.display import DisplayableCallTreeNode
from evm_trace.display import get_tree_display
from evm_trace.enums import CallType


Expand All @@ -32,13 +32,9 @@ class CallTreeNode(BaseModel):
calls: List["CallTreeNode"] = []
selfdestruct: bool = False
failed: bool = False
display_cls: Type[DisplayableCallTreeNode] = DisplayableCallTreeNode

def get_display_nodes(self) -> Iterator[DisplayableCallTreeNode]:
return self.display_cls.make_tree(self)

def __str__(self) -> str:
return "\n".join([str(t) for t in self.get_display_nodes()])
return get_tree_display(self)

def __repr__(self) -> str:
return str(self)
Expand Down
26 changes: 15 additions & 11 deletions evm_trace/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
from evm_trace.base import CallTreeNode


class DisplayableCallTreeNode(object):
_FILE_MIDDLE_PREFIX = "├──"
_FILE_LAST_PREFIX = "└──"
_PARENT_PREFIX_MIDDLE = " "
_PARENT_PREFIX_LAST = "│ "
def get_tree_display(call: "CallTreeNode") -> str:
return "\n".join([str(t) for t in TreeRepresentation.make_tree(call)])


class TreeRepresentation(object):
FILE_MIDDLE_PREFIX = "├──"
FILE_LAST_PREFIX = "└──"
PARENT_PREFIX_MIDDLE = " "
PARENT_PREFIX_LAST = "│ "

def __init__(
self,
call: "CallTreeNode",
parent: Optional["DisplayableCallTreeNode"] = None,
parent: Optional["TreeRepresentation"] = None,
is_last: bool = False,
):
self.call = call
Expand Down Expand Up @@ -57,9 +61,9 @@ def title(self) -> str:
def make_tree(
cls,
root: "CallTreeNode",
parent: Optional["DisplayableCallTreeNode"] = None,
parent: Optional["TreeRepresentation"] = None,
is_last: bool = False,
) -> Iterator["DisplayableCallTreeNode"]:
) -> Iterator["TreeRepresentation"]:
displayable_root = cls(root, parent=parent, is_last=is_last)
yield displayable_root

Expand All @@ -77,12 +81,12 @@ def __str__(self) -> str:
if self.parent is None:
return self.title

_filename_prefix = self._FILE_LAST_PREFIX if self.is_last else self._FILE_MIDDLE_PREFIX
filename_prefix = self.FILE_LAST_PREFIX if self.is_last else self.FILE_MIDDLE_PREFIX

parts = [f"{_filename_prefix} {self.title}"]
parts = [f"{filename_prefix} {self.title}"]
parent = self.parent
while parent and parent.parent is not None:
parts.append(self._PARENT_PREFIX_MIDDLE if parent.is_last else self._PARENT_PREFIX_LAST)
parts.append(self.PARENT_PREFIX_MIDDLE if parent.is_last else self.PARENT_PREFIX_LAST)
parent = parent.parent

return "".join(reversed(parts))
5 changes: 1 addition & 4 deletions evm_trace/parity.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import Any, Dict, List, Optional, Type, Union
from typing import Any, Dict, List, Optional, Union

from pydantic import BaseModel, Field, validator

from evm_trace.base import CallTreeNode, CallType
from evm_trace.display import DisplayableCallTreeNode


class CallAction(BaseModel):
Expand Down Expand Up @@ -95,7 +94,6 @@ def __getitem__(self, item):
def get_calltree_from_parity_trace(
traces: ParityTraceList,
root: Optional[ParityTrace] = None,
display_cls: Type[DisplayableCallTreeNode] = DisplayableCallTreeNode,
**root_kwargs,
) -> CallTreeNode:
"""
Expand All @@ -120,7 +118,6 @@ def get_calltree_from_parity_trace(
node_kwargs: Dict[Any, Any] = dict(
call_type=root.call_type,
failed=failed,
display_cls=display_cls,
**root_kwargs,
)

Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"eth-utils>=2.0.0",
"ethpm-types>=0.3.7,<0.4.0",
],
python_requires=">=3.7.2,<4",
python_requires=">=3.8,<4",
extras_require=extras_require,
py_modules=["evm_trace"],
license="Apache-2.0",
Expand All @@ -76,7 +76,6 @@
"Operating System :: MacOS",
"Operating System :: POSIX",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
Expand Down

0 comments on commit b865209

Please sign in to comment.