From ae4e67f20991c7c905846537db7e3b0169a3fd14 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Mon, 31 Oct 2022 19:13:13 -0500 Subject: [PATCH] test: improve merge reports test (#21) --- .pre-commit-config.yaml | 6 +++--- evm_trace/gas.py | 8 +++++--- evm_trace/parity.py | 2 -- evm_trace/vmtrace.py | 8 ++++---- setup.py | 6 +++--- tests/test_gas_report.py | 34 ++++++++++++++++++++++------------ 6 files changed, 37 insertions(+), 27 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b40096a..24efb07 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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] diff --git a/evm_trace/gas.py b/evm_trace/gas.py index b3c8687..d5b4996 100644 --- a/evm_trace/gas.py +++ b/evm_trace/gas.py @@ -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: @@ -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 []} diff --git a/evm_trace/parity.py b/evm_trace/parity.py index fa999ce..7861f39 100644 --- a/evm_trace/parity.py +++ b/evm_trace/parity.py @@ -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. diff --git a/evm_trace/vmtrace.py b/evm_trace/vmtrace.py index 080c69d..244bffa 100644 --- a/evm_trace/vmtrace.py +++ b/evm_trace/vmtrace.py @@ -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 diff --git a/setup.py b/setup.py index 1f7638e..011757d 100644 --- a/setup.py +++ b/setup.py @@ -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 diff --git a/tests/test_gas_report.py b/tests/test_gas_report.py index 4301288..c9134f6 100644 --- a/tests/test_gas_report.py +++ b/tests/test_gas_report.py @@ -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]}, }