Skip to content

Commit

Permalink
#5 Return dict with Yul CFGs
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcere committed Oct 3, 2024
1 parent efbd9d1 commit 2d68027
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/execution/sol_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,31 +234,36 @@ def _compile_json_input(self, json_file: str) -> Tuple[Dict, str]:
output_dict = json.loads(output)
return output_dict, error

def _process_json_output(self, output_dict: Dict, error: str) -> bool:
def _process_json_output(self, output_dict: Dict, error: str, deployed_contract: Optional[str]) -> Tuple[bool, Dict[str, Yul_CFG_T]]:
"""
Process the output generated from the json input
Process the output generated from the json input and returns a dict for each yul file
"""
# Check no field errors appear when parsing as a json and one of the messages is indeed an error
# (see https://docs.soliditylang.org/en/v0.8.17/using-the-compiler.html)
if "errors" in output_dict and any(error_msg["severity"] == "error" for error_msg in output_dict["errors"]):
logging.error(f"Errors: {output_dict['errors']}")
return False
return False, dict()
else:
if error != "":
logging.warning(error)

json_dict = dict()
# Produce a json file for each contract
for filename in output_dict["contracts"]:
current_file = output_dict["contracts"][filename]
for contract_name in current_file:
yul_cfg_current = current_file[contract_name]["yulCFGJson"]

if yul_cfg_current is not None and self._final_file is not None:
if yul_cfg_current is not None:
json_dict[contract_name] = yul_cfg_current
# Only store the contract that matches the specification
if self._final_file is not None and deployed_contract is not None \
and contract_name == deployed_contract:

# Store the output following the asm format
with open(self._final_file, 'w') as f:
json.dump(yul_cfg_current, f, indent=4)
return True
# Store the output following the asm format
with open(self._final_file, 'w') as f:
json.dump(yul_cfg_current, f, indent=4)
return True, json_dict

def compile_json_input(self, json_input: Dict, deployed_contract: Optional[str] = None) -> Optional[Dict[str, Yul_CFG_T]]:
# Change the settings from the json input
Expand All @@ -276,12 +281,12 @@ def compile_json_input(self, json_input: Dict, deployed_contract: Optional[str]

os.remove(tmp_file)
# Then we process the output and generate the corresponding file
correct_compilation = self._process_json_output(output_dict, error)
correct_compilation, yul_cfg_dict = self._process_json_output(output_dict, error, deployed_contract)

if not correct_compilation:
return None

return output_dict
return yul_cfg_dict

# Methods for compiling using the command line
def _compile_sol_command(self, sol_file: str):
Expand Down

0 comments on commit 2d68027

Please sign in to comment.