Skip to content

Commit 98eac94

Browse files
authored
Filter GST discarded keys in the Python harness (#2679)
* filter discarded keys in the Python harness * remove unused import * update kevm-pyk run command * use frozenset instead of list
1 parent 496cb34 commit 98eac94

File tree

5 files changed

+68
-18
lines changed

5 files changed

+68
-18
lines changed

kevm-pyk/src/kevm_pyk/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
from . import VERSION, config
3636
from .cli import _create_argument_parser, generate_options, get_argument_type_setter, get_option_string_destination
37-
from .gst_to_kore import SORT_ETHEREUM_SIMULATION, gst_to_kore, kore_pgm_to_kore
37+
from .gst_to_kore import SORT_ETHEREUM_SIMULATION, filter_gst_keys, gst_to_kore, kore_pgm_to_kore
3838
from .kevm import KEVM, KEVMSemantics, kevm_node_printer
3939
from .kompile import KompileTarget, kevm_kompile
4040
from .utils import (
@@ -591,7 +591,8 @@ def exec_run(options: RunOptions) -> None:
591591

592592
try:
593593
json_read = json.loads(options.input_file.read_text())
594-
kore_pattern = gst_to_kore(json_read, options.schedule, options.mode, options.chainid, options.usegas)
594+
gst_data = filter_gst_keys(json_read)
595+
kore_pattern = gst_to_kore(gst_data, options.schedule, options.mode, options.chainid, options.usegas)
595596
except json.JSONDecodeError:
596597
pgm_token = KToken(options.input_file.read_text(), KSort('EthereumSimulation'))
597598
kast_pgm = kevm.parse_token(pgm_token)

kevm-pyk/src/kevm_pyk/gst_to_kore.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,67 @@
2828
SORT_MODE: Final = SortApp('SortMode')
2929
SORT_ETHEREUM_SIMULATION: Final = SortApp('SortEthereumSimulation')
3030

31+
_GST_DISCARD_KEYS: Final = frozenset(
32+
[
33+
'//',
34+
'_info',
35+
'callcreates',
36+
'sealEngine',
37+
'transactionSequence',
38+
'chainname',
39+
'expectException',
40+
'lastblockhash',
41+
]
42+
)
43+
_GST_LOAD_KEYS: Final = frozenset(
44+
[
45+
'env',
46+
'pre',
47+
'rlp',
48+
'network',
49+
'genesisRLP',
50+
]
51+
)
52+
_GST_EXEC_KEYS: Final = frozenset(
53+
[
54+
'exec',
55+
'blocks',
56+
]
57+
)
58+
_GST_POST_KEYS: Final = frozenset(
59+
[
60+
'post',
61+
'postState',
62+
'postStateHash',
63+
]
64+
)
65+
_GST_ALL_POST_KEYS: Final = _GST_POST_KEYS.union(['expect', 'export'])
66+
_GST_CHECK_KEYS: Final = _GST_ALL_POST_KEYS.union(
67+
[
68+
'logs',
69+
'out',
70+
'gas',
71+
'blockHeader',
72+
'transactions',
73+
'uncleHeaders',
74+
'genesisBlockHeader',
75+
'withdrawals',
76+
'blocknumber',
77+
]
78+
)
79+
80+
81+
def filter_gst_keys(gst_data: dict) -> dict:
82+
"""Filters the discarded keys out of a single GeneralStateTest.
83+
84+
:param gst_data: A single test from a GST file structured as {"test_name": {test_fields}, ... }.
85+
:returns: The gst_data object after filtering out _GST_DISCARD_KEYS.
86+
"""
87+
return {
88+
test_name: {k: v for k, v in test_data.items() if k not in _GST_DISCARD_KEYS}
89+
for test_name, test_data in gst_data.items()
90+
}
91+
3192

3293
def gst_to_kore(gst_data: Any, schedule: str, mode: str, chainid: int, usegas: bool) -> App:
3394
return kore_pgm_to_kore(json_to_kore(gst_data), SORT_JSON, schedule, mode, chainid, usegas)

kevm-pyk/src/kevm_pyk/interpreter.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from pyk.kore.parser import KoreParser
77
from pyk.utils import run_process_2
88

9-
from .gst_to_kore import gst_to_kore
9+
from .gst_to_kore import filter_gst_keys, gst_to_kore
1010

1111
if TYPE_CHECKING:
1212
from subprocess import CompletedProcess
@@ -26,7 +26,8 @@ def interpret(gst_data: Any, schedule: str, mode: str, chainid: int, usegas: boo
2626

2727

2828
def _interpret(gst_data: Any, schedule: str, mode: str, chainid: int, usegas: bool) -> CompletedProcess:
29+
gst_data_filtered = filter_gst_keys(gst_data)
2930
interpreter = kdist.get('evm-semantics.llvm') / 'interpreter'
30-
init_kore = gst_to_kore(gst_data, schedule, mode, chainid, usegas)
31+
init_kore = gst_to_kore(gst_data_filtered, schedule, mode, chainid, usegas)
3132
proc_res = run_process_2([str(interpreter), '/dev/stdin', '-1', '/dev/stdout'], input=init_kore.text, check=False)
3233
return proc_res

kevm-pyk/src/kevm_pyk/kproj/evm-semantics/driver.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Note that `TEST` is sorted here so that key `"network"` comes before key `"pre"`
254254
// -------------------------------------
255255
rule <k> run { .JSONs } => .K ... </k>
256256
rule <k> run { TESTID : { TEST:JSONs } , TESTS }
257-
=> run ( TESTID : { qsortJSONs(TEST) } )
257+
=> run ( TESTID : { TEST } )
258258
~> #if #hasPost?( { TEST } ) #then .K #else exception #fi
259259
~> clear
260260
~> run { TESTS }
@@ -298,7 +298,6 @@ Note that `TEST` is sorted here so that key `"network"` comes before key `"pre"`
298298
299299
rule <k> process TESTID : { KEY : VAL , REST } => load KEY : VAL ~> process TESTID : { REST } ... </k> requires KEY in #loadKeys
300300
rule <k> process TESTID : { KEY : VAL , REST } => process TESTID : { REST } ~> check TESTID : {KEY : VAL} ... </k> requires KEY in #checkKeys
301-
rule <k> process _TESTID : { KEY : _ , REST => REST } ... </k> requires KEY in #discardKeys
302301
rule <k> process _TESTID : { .JSONs } => #startBlock ~> startTx ... </k>
303302
304303
rule <k> run _TESTID : { "exec" : (EXEC:JSON) } => loadCallState EXEC ~> start ~> flush ... </k>
@@ -339,16 +338,6 @@ Note that `TEST` is sorted here so that key `"network"` comes before key `"pre"`
339338
rule <k> run TESTID : { KEY : (VAL:JSON) , REST } => run TESTID : { REST } ~> check TESTID : { KEY : VAL } ... </k> requires KEY in #checkKeys andBool notBool KEY in #allPostKeys
340339
```
341340

342-
- `#discardKeys` are all the JSON nodes in the tests which should just be ignored.
343-
344-
```k
345-
syntax Set ::= "#discardKeys" [function]
346-
// ----------------------------------------
347-
rule #discardKeys => ( SetItem("//") SetItem("_info") SetItem("callcreates") SetItem("sealEngine") SetItem("transactionSequence") SetItem("chainname") SetItem("expectException") SetItem("lastblockhash"))
348-
349-
rule <k> run TESTID : { KEY : _ , REST } => run TESTID : { REST } ... </k> requires KEY in #discardKeys
350-
```
351-
352341
- `driver.md` specific handling of state-utils commands
353342

354343
```k

kevm-pyk/src/tests/integration/test_conformance.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525

2626
_LOGGER: Final = logging.getLogger(__name__)
27-
_LOG_FORMAT: Final = '%(levelname)s %(asctime)s %(name)s - %(message)s'
28-
2927

3028
sys.setrecursionlimit(10**8)
3129

0 commit comments

Comments
 (0)