Skip to content

Commit 24554fc

Browse files
authored
Merge pull request #12 from marioevz/eips-support-debugging
Transition Tool EIPs Support, Print Traces, Helper Methods
2 parents d010033 + b838d73 commit 24554fc

File tree

15 files changed

+477
-118
lines changed

15 files changed

+477
-118
lines changed

src/ethereum_test_filling_tool/main.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ def parse_arguments() -> argparse.Namespace:
6868
help="limit to filling only tests with matching name",
6969
)
7070

71+
parser.add_argument(
72+
"--traces",
73+
action="store_true",
74+
help="collect traces of the execution information from the "
75+
+ "transition tool",
76+
)
77+
7178
return parser.parse_args()
7279

7380
options: argparse.Namespace
@@ -115,7 +122,9 @@ def fill(self) -> None:
115122

116123
os.makedirs(self.options.output, exist_ok=True)
117124

118-
t8n = EvmTransitionTool(binary=self.options.evm_bin)
125+
t8n = EvmTransitionTool(
126+
binary=self.options.evm_bin, trace=self.options.traces
127+
)
119128
b11r = EvmBlockBuilder(binary=self.options.evm_bin)
120129

121130
for filler in fillers:

src/ethereum_test_tools/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .common import (
88
Account,
99
Block,
10+
CodeGasMeasure,
1011
Environment,
1112
JSONEncoder,
1213
TestAddress,
@@ -23,6 +24,7 @@
2324
"Block",
2425
"BlockchainTest",
2526
"Code",
27+
"CodeGasMeasure",
2628
"Environment",
2729
"JSONEncoder",
2830
"StateTest",

src/ethereum_test_tools/code/code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Union
77

88

9-
class Code(str):
9+
class Code(object):
1010
"""
1111
Generic code object.
1212
"""

src/ethereum_test_tools/common/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
TestAddress,
99
TestPrivateKey,
1010
)
11-
from .helpers import to_address, to_hash
11+
from .helpers import CodeGasMeasure, to_address, to_hash
1212
from .types import (
1313
Account,
1414
Block,
@@ -29,6 +29,7 @@
2929
"AddrAA",
3030
"AddrBB",
3131
"Block",
32+
"CodeGasMeasure",
3233
"EmptyTrieRoot",
3334
"Environment",
3435
"Fixture",

src/ethereum_test_tools/common/helpers.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""
2-
Helper functions for generating Ethereum tests.
2+
Helper functions/classes used to generate Ethereum tests.
33
"""
44

5+
from dataclasses import dataclass
6+
7+
from ..code import Code, code_to_bytes
8+
59

610
def to_address(input: int | str) -> str:
711
"""
@@ -25,3 +29,75 @@ def to_hash(input: int | str) -> str:
2529
if type(input) is int:
2630
return "0x" + input.to_bytes(32, "big").hex()
2731
raise Exception("invalid type to convert to hash")
32+
33+
34+
@dataclass(kw_only=True)
35+
class CodeGasMeasure(Code):
36+
"""
37+
Helper class used to generate bytecode that measures gas usage of a
38+
bytecode, taking into account and subtracting any extra overhead gas costs
39+
required to execute.
40+
By default, the result gas calculation is saved to storage key 0.
41+
"""
42+
43+
code: bytes | str | Code
44+
"""
45+
Bytecode to be executed to measure the gas usage.
46+
"""
47+
overhead_cost: int = 0
48+
"""
49+
Extra gas cost to be subtracted from extra operations.
50+
"""
51+
extra_stack_items: int = 0
52+
"""
53+
Extra stack items that remain at the end of the execution.
54+
To be considered when subtracting the value of the previous GAS operation,
55+
and to be popped at the end of the execution.
56+
"""
57+
sstore_key: int = 0
58+
"""
59+
Storage key to save the gas used.
60+
"""
61+
62+
def assemble(self) -> bytes:
63+
"""
64+
Assemble the bytecode that measures gas usage.
65+
"""
66+
res = bytes()
67+
res += bytes(
68+
[
69+
0x5A, # GAS
70+
]
71+
)
72+
res += code_to_bytes(self.code) # Execute code to measure its gas cost
73+
res += bytes(
74+
[
75+
0x5A, # GAS
76+
]
77+
)
78+
# We need to swap and pop for each extra stack item that remained from
79+
# the execution of the code
80+
res += (
81+
bytes(
82+
[
83+
0x90, # SWAP1
84+
0x50, # POP
85+
]
86+
)
87+
* self.extra_stack_items
88+
)
89+
res += bytes(
90+
[
91+
0x90, # SWAP1
92+
0x03, # SUB
93+
0x60, # PUSH1
94+
self.overhead_cost + 2, # Overhead cost + GAS opcode price
95+
0x90, # SWAP1
96+
0x03, # SUB
97+
0x60, # PUSH1
98+
self.sstore_key, # -> SSTORE key
99+
0x55, # SSTORE
100+
0x00, # STOP
101+
]
102+
)
103+
return res

0 commit comments

Comments
 (0)