Skip to content

Commit ca235f4

Browse files
committed
kevm-pyk/{solc_to_k, __main__}: resolve sourcemap at load time, also build contract ids
1 parent f0ea5c8 commit ca235f4

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

kevm-pyk/src/kevm_pyk/__main__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,15 @@ def exec_foundry_kompile(
154154
json_paths = _contract_json_paths(foundry_out)
155155
contracts = [_contract_from_json(json_path) for json_path in json_paths]
156156

157+
contract_id_map: Dict[int, str] = {}
157158
for c in contracts:
158159
srcmap_file = srcmap_dir / f'{c.name}.json'
159-
with open(srcmap_file, 'w') as smf:
160-
smf.write(json.dumps(c.srcmap))
161-
_LOGGER.info(f'Wrote source map: {srcmap_file}')
160+
srcmap_file.write_text(json.dumps(c.srcmap))
161+
_LOGGER.info(f'Wrote source map: {srcmap_file}')
162+
contract_id_map[c.contract_id] = str(c.contract_path)
163+
contract_id_map_path = srcmap_dir / 'contract_id_map.json'
164+
contract_id_map_path.write_text(json.dumps(contract_id_map))
165+
_LOGGER.info(f'Wrote contract id map: {contract_id_map_path}')
162166

163167
foundry = Foundry(definition_dir, profile=profile)
164168
empty_config = foundry.definition.empty_config(Foundry.Sorts.FOUNDRY_CELL)

kevm-pyk/src/kevm_pyk/solc_to_k.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ def application(self) -> KInner:
109109
methods: Tuple[Method, ...]
110110
test_methods: Tuple[Method, ...]
111111
fields: FrozenDict
112-
srcmap: Optional[Dict[int, str]]
112+
srcmap: Optional[Dict[int, Tuple[int, int, int, str, int]]]
113+
contract_id: int
114+
contract_path: Path
113115

114116
def __init__(self, contract_name: str, contract_json: Dict, foundry: bool = False) -> None:
115117
def _get_method_abi(_mname: str) -> Dict:
@@ -152,6 +154,8 @@ def _get_method_abi(_mname: str) -> Dict:
152154
self.fields = FrozenDict(_fields)
153155

154156
self.srcmap = None
157+
self.contract_id = contract_json['id']
158+
self.contract_path = contract_json['ast']['absolutePath']
155159
if len(self.bytecode) > 0:
156160
instr_to_pc = {}
157161
pc = 0
@@ -166,13 +170,29 @@ def _get_method_abi(_mname: str) -> Dict:
166170
pc += 1
167171
instr += 1
168172

169-
instr_srcmap = (
173+
instrs_srcmap = (
170174
contract_json['evm']['deployedBytecode']['sourceMap']
171175
if not foundry
172176
else contract_json['deployedBytecode']['sourceMap']
173177
).split(';')
174178

175-
self.srcmap = {instr_to_pc[instr]: src for instr, src in enumerate(instr_srcmap)}
179+
s, l, f, j, m = (0, 0, 0, '', 0)
180+
_srcmap = {}
181+
for i, instr_srcmap in enumerate(instrs_srcmap):
182+
fields = instr_srcmap.split(':')
183+
if len(fields) > 0 and fields[0] != '':
184+
s = int(fields[0])
185+
if len(fields) > 1 and fields[1] != '':
186+
l = int(fields[1])
187+
if len(fields) > 2 and fields[2] != '':
188+
f = int(fields[2])
189+
if len(fields) > 3 and fields[3] != '':
190+
j = fields[3]
191+
if len(fields) > 4 and fields[4] != '':
192+
m = int(fields[4])
193+
_srcmap[i] = (s, l, f, j, m)
194+
195+
self.srcmap = _srcmap
176196

177197
@staticmethod
178198
def contract_to_module_name(c: str, spec: bool = True) -> str:

0 commit comments

Comments
 (0)