-
Notifications
You must be signed in to change notification settings - Fork 330
Count opcodes #1353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Count opcodes #1353
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,46 @@ | ||
""" | ||
EVM trace implementation that counts how many times each opcode is executed. | ||
""" | ||
from collections import defaultdict | ||
|
||
from ethereum.trace import EvmTracer, OpStart, TraceEvent | ||
|
||
from .protocols import Evm | ||
|
||
|
||
class CountTracer(EvmTracer): | ||
""" | ||
EVM trace implementation that counts how many times each opcode is | ||
executed. | ||
""" | ||
|
||
transaction_environment: object | None | ||
Carsons-Eels marked this conversation as resolved.
Show resolved
Hide resolved
|
||
active_traces: defaultdict[str, int] | ||
|
||
def __init__(self) -> None: | ||
self.transaction_environment = None | ||
self.active_traces = defaultdict(lambda: 0) | ||
|
||
def __call__(self, evm: object, event: TraceEvent) -> None: | ||
""" | ||
Create a trace of the event. | ||
""" | ||
if not isinstance(event, OpStart): | ||
return | ||
|
||
assert isinstance(evm, Evm) | ||
|
||
if self.transaction_environment is not evm.message.tx_env: | ||
self.active_traces = defaultdict(lambda: 0) | ||
self.transaction_environment = evm.message.tx_env | ||
|
||
self.active_traces[event.op.name] += 1 | ||
|
||
def results(self) -> dict[str, int]: | ||
""" | ||
Return and clear the current opcode counts. | ||
""" | ||
results = self.active_traces | ||
self.active_traces = defaultdict(lambda: 0) | ||
self.transaction_environment = None | ||
return results |
This file contains hidden or 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,38 @@ | ||
""" | ||
EVM trace implementation that fans out to many concrete trace implementations. | ||
""" | ||
from typing import Final | ||
|
||
from typing_extensions import override | ||
|
||
from ethereum.trace import EvmTracer, TraceEvent | ||
|
||
|
||
class GroupTracer(EvmTracer): | ||
""" | ||
EVM trace implementation that fans out to many concrete trace | ||
implementations. | ||
""" | ||
|
||
tracers: Final[set[EvmTracer]] | ||
|
||
def __init__(self) -> None: | ||
self.tracers = set() | ||
|
||
def add(self, tracer: EvmTracer) -> None: | ||
""" | ||
Insert a new tracer. | ||
""" | ||
self.tracers.add(tracer) | ||
|
||
@override | ||
def __call__( | ||
self, | ||
evm: object, | ||
event: TraceEvent, | ||
) -> None: | ||
""" | ||
Record a trace event. | ||
""" | ||
for tracer in self.tracers: | ||
tracer(evm, event) |
This file contains hidden or 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,49 @@ | ||
import json | ||
from io import StringIO | ||
from pathlib import Path | ||
from typing import Callable | ||
|
||
import pytest | ||
|
||
from ethereum_spec_tools.evm_tools import create_parser | ||
from ethereum_spec_tools.evm_tools.t8n import T8N | ||
|
||
parser = create_parser() | ||
|
||
|
||
@pytest.mark.evm_tools | ||
def test_count_opcodes(root_relative: Callable[[str | Path], Path]) -> None: | ||
base_path = root_relative( | ||
"fixtures/evm_tools_testdata/t8n/fixtures/testdata/2" | ||
) | ||
|
||
options = parser.parse_args( | ||
[ | ||
"t8n", | ||
f"--input.env={base_path / 'env.json'}", | ||
f"--input.alloc={base_path / 'alloc.json'}", | ||
f"--input.txs={base_path / 'txs.json'}", | ||
"--output.result=stdout", | ||
"--output.body=stdout", | ||
"--output.alloc=stdout", | ||
"--opcode.count=stdout", | ||
"--state-test", | ||
] | ||
) | ||
|
||
in_file = StringIO() | ||
out_file = StringIO() | ||
|
||
t8n_tool = T8N(options, out_file=out_file, in_file=in_file) | ||
exit_code = t8n_tool.run() | ||
assert 0 == exit_code | ||
|
||
results = json.loads(out_file.getvalue()) | ||
|
||
assert results["opcodeCount"] == { | ||
"PUSH1": 5, | ||
"MSTORE8": 1, | ||
"CREATE": 1, | ||
"ADD": 1, | ||
"SELFDESTRUCT": 1, | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.