generated from ApeWorX/project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from NotPeopling2day/feat/gas-report
- Loading branch information
Showing
10 changed files
with
160 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import copy | ||
from typing import Any, Dict, List | ||
|
||
from evm_trace.base import CallTreeNode | ||
|
||
GasReport = Dict[Any, Dict[Any, List[int]]] | ||
|
||
|
||
def get_gas_report(calltree: CallTreeNode) -> GasReport: | ||
""" | ||
Extracts a gas report object from a :class:`~evm_trace.base.CallTreeNode`. | ||
Args: | ||
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. | ||
""" | ||
report = { | ||
calltree.address: {calltree.calldata[:4]: [calltree.gas_cost] if calltree.gas_cost else []} | ||
} | ||
return merge_reports(report, *map(get_gas_report, calltree.calls)) | ||
|
||
|
||
def merge_reports(*reports: GasReport) -> GasReport: | ||
""" | ||
Merge method for merging a list of gas reports and combining a list of gas costs. | ||
""" | ||
reports_ls = list(reports) | ||
if len(reports_ls) < 1: | ||
raise ValueError("Must be 2 or more reports to merge") | ||
elif len(reports_ls) == 1: | ||
return reports_ls[0] | ||
|
||
merged_report: GasReport = copy.deepcopy(reports_ls.pop(0)) | ||
|
||
if len(reports_ls) < 1: | ||
return merged_report | ||
|
||
for report in reports_ls: | ||
for outer_key, inner_dict in report.items(): | ||
if outer_key not in merged_report: | ||
merged_report[outer_key] = inner_dict | ||
continue | ||
|
||
for inner_key, inner_list in report[outer_key].items(): | ||
if inner_key in merged_report[outer_key]: | ||
merged_report[outer_key][inner_key].extend(inner_list) | ||
else: | ||
merged_report[outer_key][inner_key] = inner_list | ||
|
||
return merged_report |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import List | ||
|
||
from ethpm_types import HexBytes | ||
|
||
from evm_trace import CallTreeNode | ||
from evm_trace.gas import GasReport, get_gas_report, merge_reports | ||
|
||
# Simplified version of gas reports only for testing purposes | ||
reports: List[GasReport] = [ | ||
{ | ||
HexBytes("1"): {HexBytes("10"): [1]}, | ||
HexBytes("2"): {HexBytes("20"): [2]}, | ||
}, | ||
{ | ||
HexBytes("1"): {HexBytes("10"): [1]}, | ||
HexBytes("2"): {HexBytes("21"): [2]}, | ||
HexBytes("3"): {HexBytes("30"): [3]}, | ||
}, | ||
] | ||
|
||
|
||
def test_builds_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 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]}, | ||
} |