Skip to content

Commit

Permalink
test: improve merge reports test (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Nov 1, 2022
1 parent d475291 commit ae4e67f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 22.6.0
rev: 22.10.0
hooks:
- id: black
name: black

- repo: https://gitlab.com/pycqa/flake8
rev: 4.0.1
rev: 5.0.4
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.971
rev: v0.982
hooks:
- id: mypy
additional_dependencies: [types-PyYAML, types-requests]
Expand Down
8 changes: 5 additions & 3 deletions evm_trace/gas.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import copy
from typing import Any, Dict, List
from typing import Dict, List, TypeVar

from evm_trace.base import CallTreeNode

GasReport = Dict[Any, Dict[Any, List[int]]]
ContractID = TypeVar("ContractID")
MethodID = TypeVar("MethodID")
GasReport = Dict[ContractID, Dict[MethodID, List[int]]]


def get_gas_report(calltree: CallTreeNode) -> GasReport:
Expand All @@ -14,7 +16,7 @@ def get_gas_report(calltree: CallTreeNode) -> GasReport:
calltree (:class:`~evm_trace.base.CallTreeNode`): call tree used for gas report.
Returns:
:class:`~evm_trace.gas.Report`: Gas report structure from a call tree.
:class:`~evm_trace.gas.GasReport`: Gas report structure from a call tree.
"""
report = {
calltree.address: {calltree.calldata[:4]: [calltree.gas_cost] if calltree.gas_cost else []}
Expand Down
2 changes: 0 additions & 2 deletions evm_trace/parity.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ def get_calltree_from_parity_trace(
likely loaded from the response data from the ``trace_transaction`` RPC response.
root (:class:`~evm_trace.parity.ParityTrace`): The root parity trace node. Optional, uses
the first item by default.
display_cls (Type[DisplayableCallTreeNode]]: A custom class to use for representing
the call tree. Defaults to :class:`~evm_trace.display.DisplayableCallTreeNode`.
**root_kwargs: Additional kwargs to append to the root node. Useful for adding gas for
reverted calls.
Expand Down
8 changes: 4 additions & 4 deletions evm_trace/vmtrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from typing import Any, Dict, Iterator, List, Optional, Type, Union

from eth.vm.memory import Memory # type: ignore
from eth.vm.stack import Stack # type: ignore
from eth.vm.memory import Memory
from eth.vm.stack import Stack
from eth_utils import to_checksum_address
from hexbytes import HexBytes
from msgspec import Struct # type: ignore
from msgspec.json import Decoder # type: ignore
from msgspec import Struct
from msgspec.json import Decoder

# opcodes grouped by the number of items they pop from the stack
# fmt: off
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"eth-hash[pysha3]", # For eth-utils address checksumming
],
"lint": [
"black>=22.6.0", # auto-formatter and linter
"mypy>=0.971", # Static type analyzer
"flake8>=4.0.1", # Style linter
"black>=22.10.0", # auto-formatter and linter
"mypy>=0.982", # Static type analyzer
"flake8>=5.0.4", # Style linter
"isort>=5.10.1", # Import sorting linter
],
"release": [ # `release` GitHub Action job uses this
Expand Down
34 changes: 22 additions & 12 deletions tests/test_gas_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,42 @@
from evm_trace.gas import GasReport, get_gas_report, merge_reports

# Simplified version of gas reports only for testing purposes
CONTRACT_A = HexBytes("0x0000000000000000000000000000000000000001")
CONTRACT_B = HexBytes("0x0000000000000000000000000000000000000002")
CONTRACT_C = HexBytes("0x0000000000000000000000000000000000000003")
METHOD_A = HexBytes("0x181d60da")
METHOD_B = HexBytes("0xc7cee1b7")
METHOD_C = HexBytes("0xd76d0659")

reports: List[GasReport] = [
{
HexBytes("1"): {HexBytes("10"): [1]},
HexBytes("2"): {HexBytes("20"): [2]},
CONTRACT_A: {METHOD_A: [100, 101, 100, 102]},
CONTRACT_B: {METHOD_B: [200, 202, 202, 200, 200]},
},
{
HexBytes("1"): {HexBytes("10"): [1]},
HexBytes("2"): {HexBytes("21"): [2]},
HexBytes("3"): {HexBytes("30"): [3]},
CONTRACT_A: {METHOD_A: [105, 106]},
CONTRACT_B: {METHOD_A: [200, 201]},
CONTRACT_C: {METHOD_C: [300]},
},
]


def test_builds_gas_report(call_tree_data):
def test_get_gas_report(call_tree_data):
tree = CallTreeNode(**call_tree_data)
gas_report = get_gas_report(tree)

for call in tree.calls:
assert call.address in gas_report
def assert_all(t):
assert t.address in gas_report
for c in t.calls:
assert_all(c)

assert_all(tree)


def test_merged_reports():
merged = merge_reports(*reports)

assert merged == {
HexBytes("0x01"): {HexBytes("0x10"): [1, 1]},
HexBytes("0x02"): {HexBytes("0x20"): [2], HexBytes("0x21"): [2]},
HexBytes("0x03"): {HexBytes("0x30"): [3]},
CONTRACT_A: {METHOD_A: [100, 101, 100, 102, 105, 106]},
CONTRACT_B: {METHOD_A: [200, 201], METHOD_B: [200, 202, 202, 200, 200]},
CONTRACT_C: {METHOD_C: [300]},
}

0 comments on commit ae4e67f

Please sign in to comment.