-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathgst_to_kore.py
148 lines (118 loc) · 3.95 KB
/
gst_to_kore.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from __future__ import annotations
import json
import logging
import sys
from argparse import ArgumentParser
from typing import TYPE_CHECKING
from pyk.cli.utils import file_path
from pyk.kore.prelude import BOOL, INT, SORT_JSON, SORT_K_ITEM, bool_dv, inj, int_dv, json_to_kore, top_cell_initializer
from pyk.kore.syntax import App, SortApp
from .cli import KEVMCLIArgs
if TYPE_CHECKING:
from argparse import Namespace
from pathlib import Path
from typing import Any, Final
from pyk.kore.syntax import Pattern
_LOGGER: Final = logging.getLogger(__name__)
_LOG_FORMAT: Final = '%(levelname)s %(asctime)s %(name)s - %(message)s'
SORT_SCHEDULE: Final = SortApp('SortSchedule')
SORT_MODE: Final = SortApp('SortMode')
SORT_ETHEREUM_SIMULATION: Final = SortApp('SortEthereumSimulation')
_GST_DISCARD_KEYS: Final = frozenset(
[
'//',
'_info',
'callcreates',
'sealEngine',
'transactionSequence',
'chainname',
'lastblockhash',
'hasBigInt',
'config',
]
)
_GST_LOAD_KEYS: Final = frozenset(
[
'env',
'pre',
'rlp',
'network',
'genesisRLP',
]
)
_GST_EXEC_KEYS: Final = frozenset(
[
'exec',
'blocks',
]
)
_GST_POST_KEYS: Final = frozenset(
[
'post',
'postState',
'postStateHash',
]
)
_GST_ALL_POST_KEYS: Final = _GST_POST_KEYS.union(['expect', 'export'])
_GST_CHECK_KEYS: Final = _GST_ALL_POST_KEYS.union(
[
'logs',
'out',
'gas',
'blockHeader',
'transactions',
'uncleHeaders',
'genesisBlockHeader',
'withdrawals',
'blocknumber',
]
)
def filter_gst_keys(gst_data: dict) -> dict:
"""
Filters the discarded keys out of a single GeneralStateTest,
recursively removing them from nested dicts/lists as well.
"""
def _remove_discard_keys(obj: Any) -> Any:
if type(obj) is dict:
return {k: _remove_discard_keys(v) for k, v in obj.items() if k not in _GST_DISCARD_KEYS}
elif type(obj) is list:
return [_remove_discard_keys(item) for item in obj]
else:
# If it's neither dict nor list, just return as-is
return obj
return _remove_discard_keys(gst_data)
def gst_to_kore(gst_data: Any, schedule: str, mode: str, chainid: int, usegas: bool) -> App:
return kore_pgm_to_kore(json_to_kore(gst_data), SORT_JSON, schedule, mode, chainid, usegas)
def kore_pgm_to_kore(pgm: Pattern, pattern_sort: SortApp, schedule: str, mode: str, chainid: int, usegas: bool) -> App:
config = {
'$PGM': inj(pattern_sort, SORT_K_ITEM, pgm),
'$SCHEDULE': inj(SORT_SCHEDULE, SORT_K_ITEM, _schedule_to_kore(schedule)),
'$MODE': inj(SORT_MODE, SORT_K_ITEM, _mode_to_kore(mode)),
'$CHAINID': inj(INT, SORT_K_ITEM, int_dv(chainid)),
'$USEGAS': inj(BOOL, SORT_K_ITEM, bool_dv(usegas)),
}
return top_cell_initializer(config)
def _schedule_to_kore(schedule: str) -> App:
return App(f"Lbl{schedule}'Unds'EVM")
def _mode_to_kore(mode: str) -> App:
return App(f'Lbl{mode}')
def main() -> None:
sys.setrecursionlimit(15000000)
args = _parse_args()
_exec_gst_to_kore(args.input_file, args.schedule, args.mode, args.chainid, args.usegas)
def _exec_gst_to_kore(input_file: Path, schedule: str, mode: str, chainid: int, usegas: bool) -> None:
gst_data = json.loads(input_file.read_text())
kore = gst_to_kore(gst_data, schedule, mode, chainid, usegas)
kore.write(sys.stdout)
sys.stdout.write('\n')
_LOGGER.info('Finished writing KORE')
def _parse_args() -> Namespace:
kevm_cli_args = KEVMCLIArgs()
parser = ArgumentParser(
description='Convert a GeneralStateTest to Kore for compsumption by KEVM',
parents=[kevm_cli_args.evm_chain_args],
)
parser.add_argument('input_file', type=file_path, help='path to GST')
return parser.parse_args()
if __name__ == '__main__':
main()