Skip to content

Commit

Permalink
fix: general improvements [APE-647] (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Feb 20, 2023
1 parent c27e848 commit 44b6a13
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
pip install .[lint]
- name: Run Black
run: black --check .
run: black --check . --diff

- name: Run isort
run: isort --check-only .
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 22.10.0
rev: 23.1.0
hooks:
- id: black
name: black
Expand Down
4 changes: 4 additions & 0 deletions evm_trace/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
class CallType(Enum):
INTERNAL = "INTERNAL" # Non-opcode internal call
CREATE = "CREATE"
CREATE2 = "CREATE2"
CALL = "CALL"
DELEGATECALL = "DELEGATECALL"
STATICCALL = "STATICCALL"
CALLCODE = "CALLCODE"
SELFDESTRUCT = "SELFDESTRUCT"


CALL_OPCODES = (CallType.CALL, CallType.CALLCODE, CallType.DELEGATECALL, CallType.STATICCALL)
6 changes: 5 additions & 1 deletion evm_trace/gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ def merge_reports(*reports: GasReport) -> GasReport:
If given a single report, it only returns it.
"""
reports_ls = list(reports)
if len(reports_ls) == 1:
num_reports = len(reports_ls)
if num_reports == 0:
return {}

elif num_reports == 1:
return reports_ls[0]

merged_report: GasReport = copy.deepcopy(reports_ls.pop(0))
Expand Down
11 changes: 5 additions & 6 deletions evm_trace/geth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import Field, validator

from evm_trace.base import CallTreeNode
from evm_trace.enums import CallType
from evm_trace.enums import CALL_OPCODES, CallType


class TraceFrame(BaseModel):
Expand Down Expand Up @@ -142,19 +142,18 @@ def _create_node_from_call(

node = CallTreeNode(**node_kwargs)
for frame in trace:
if frame.op in ("CALL", "DELEGATECALL", "STATICCALL"):

if frame.op in [x.value for x in CALL_OPCODES]:
# NOTE: Because of the different meanings in structLog style gas values,
# gas is not set for nodes created this way.
child_node_kwargs = {"address": frame.stack[-2][-20:], "depth": frame.depth}

if frame.op == "CALL":
if frame.op == CallType.CALL.value:
child_node_kwargs["call_type"] = CallType.CALL
child_node_kwargs["value"] = int(frame.stack[-3].hex(), 16)
child_node_kwargs["calldata"] = _extract_memory(
offset=frame.stack[-4], size=frame.stack[-5], memory=frame.memory
)
elif frame.op == "DELEGATECALL":
elif frame.op == CallType.DELEGATECALL.value:
child_node_kwargs["call_type"] = CallType.DELEGATECALL
child_node_kwargs["calldata"] = _extract_memory(
offset=frame.stack[-3], size=frame.stack[-4], memory=frame.memory
Expand All @@ -172,7 +171,7 @@ def _create_node_from_call(

# TODO: Handle internal nodes using JUMP and JUMPI

elif frame.op == "SELFDESTRUCT":
elif frame.op == CallType.SELFDESTRUCT.value:
# TODO: Handle the internal value transfer
node.selfdestruct = True
break
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"eth-hash[pysha3]", # For eth-utils address checksumming
],
"lint": [
"black>=22.10.0", # auto-formatter and linter
"black>=23.1.0", # auto-formatter and linter
"mypy>=0.991", # Static type analyzer
"types-setuptools", # Needed due to mypy typeshed
"flake8>=5.0.4", # Style linter
Expand Down
4 changes: 4 additions & 0 deletions tests/test_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ def test_merge_single_report():
actual = merge_reports(reports[0])
expected = reports[0]
assert actual == expected


def test_merge_no_reports():
assert merge_reports() == {}

0 comments on commit 44b6a13

Please sign in to comment.