From 42945727838ca6dd1ee11180a4805f1b459ee374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20L=C3=B3pez?= Date: Mon, 13 Nov 2023 10:20:57 -0300 Subject: [PATCH 1/4] platform: etherscan: add support for viaIR --- crytic_compile/platform/etherscan.py | 7 +++++++ crytic_compile/platform/solc_standard_json.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/crytic_compile/platform/etherscan.py b/crytic_compile/platform/etherscan.py index 2dd8e5d4..113aa7fc 100644 --- a/crytic_compile/platform/etherscan.py +++ b/crytic_compile/platform/etherscan.py @@ -372,6 +372,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None: working_dir: Optional[str] = None remappings: Optional[List[str]] = None + dict_source_code: Optional[Dict] = None try: # etherscan might return an object with two curly braces, {{ content }} dict_source_code = json.loads(source_code[1:-1]) @@ -390,6 +391,11 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None: _handle_single_file(source_code, addr, prefix, contract_name, export_dir) ] + # viaIR is not exposed on the top level JSON offered by etherscan, so we need to inspect the settings + via_ir_enabled: Optional[bool] = None + if isinstance(dict_source_code, dict): + via_ir_enabled = dict_source_code.get("settings", {}).get("viaIR", None) + compilation_unit = CompilationUnit(crytic_compile, contract_name) compilation_unit.compiler_version = CompilerVersion( @@ -413,6 +419,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None: working_dir=working_dir, remappings=remappings, evm_version=evm_version, + via_ir=via_ir_enabled, ) def clean(self, **_kwargs: str) -> None: diff --git a/crytic_compile/platform/solc_standard_json.py b/crytic_compile/platform/solc_standard_json.py index beadf442..f81686c6 100644 --- a/crytic_compile/platform/solc_standard_json.py +++ b/crytic_compile/platform/solc_standard_json.py @@ -31,6 +31,7 @@ def standalone_compile( working_dir: Optional[str] = None, remappings: Optional[List[str]] = None, evm_version: Optional[str] = None, + via_ir: Optional[bool] = None, ) -> None: """ Boilerplate function to run the the standardjson. compilation_unit.compiler_version must be set before calling this function @@ -48,6 +49,7 @@ def standalone_compile( working_dir (Optional[str]): working directory remappings (Optional[List[str]]): list of solc remaps to use evm_version (Optional[str]): EVM version to target. None for default + via_ir (Optional[bool]): whether to enable the viaIR compilation flag. None for unset Returns: @@ -70,6 +72,9 @@ def standalone_compile( if evm_version is not None: add_evm_version(standard_json_dict, evm_version) + if via_ir is not None: + add_via_ir(standard_json_dict, via_ir) + add_optimization( standard_json_dict, compilation_unit.compiler_version.optimized, @@ -278,6 +283,20 @@ def add_evm_version(json_dict: Dict, version: str) -> None: json_dict["settings"]["evmVersion"] = version +def add_via_ir(json_dict: Dict, enabled: bool) -> None: + """ + Enable or disable the "viaIR" compilation flag. + + Args: + json_dict (Dict): solc standard json input + enabled (bool): whether viaIR is enabled + + Returns: + + """ + json_dict["settings"]["viaIR"] = enabled + + def parse_standard_json_output( targets_json: Dict, compilation_unit: CompilationUnit, solc_working_dir: Optional[str] = None ) -> None: From c06df0bf7bcee4c0c2808fedadae32a6169d27d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20L=C3=B3pez?= Date: Mon, 13 Nov 2023 10:25:42 -0300 Subject: [PATCH 2/4] ci: etherscan: add viaIR test case --- scripts/ci_test_etherscan.sh | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/scripts/ci_test_etherscan.sh b/scripts/ci_test_etherscan.sh index 11918b93..7adcef2c 100755 --- a/scripts/ci_test_etherscan.sh +++ b/scripts/ci_test_etherscan.sh @@ -86,3 +86,16 @@ then exit 255 fi echo "::endgroup::" + +delay_etherscan + +# via-ir test for crytic/crytic-compile#517 +echo "::group::Etherscan #7" +crytic-compile 0x9AB6b21cDF116f611110b048987E58894786C244 --compile-remove-metadata --etherscan-apikey "$GITHUB_ETHERSCAN" + +if [ $? -ne 0 ] +then + echo "Etherscan #7 test failed" + exit 255 +fi +echo "::endgroup::" From e51ba575e56dab975a3b222d08df920b2e18c313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20L=C3=B3pez?= Date: Mon, 13 Nov 2023 11:29:06 -0300 Subject: [PATCH 3/4] Fix mypy warning --- crytic_compile/platform/etherscan.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crytic_compile/platform/etherscan.py b/crytic_compile/platform/etherscan.py index 113aa7fc..a8f54b43 100644 --- a/crytic_compile/platform/etherscan.py +++ b/crytic_compile/platform/etherscan.py @@ -376,6 +376,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None: try: # etherscan might return an object with two curly braces, {{ content }} dict_source_code = json.loads(source_code[1:-1]) + assert isinstance(dict_source_code, dict) filenames, working_dir, remappings = _handle_multiple_files( dict_source_code, addr, prefix, contract_name, export_dir ) @@ -383,6 +384,7 @@ def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None: try: # or etherscan might return an object with single curly braces, { content } dict_source_code = json.loads(source_code) + assert isinstance(dict_source_code, dict) filenames, working_dir, remappings = _handle_multiple_files( dict_source_code, addr, prefix, contract_name, export_dir ) From 457fd52782cffc88715c9d02fe4144ba8968986b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20L=C3=B3pez?= Date: Mon, 13 Nov 2023 11:41:54 -0300 Subject: [PATCH 4/4] Silence pylint warning --- crytic_compile/platform/solc_standard_json.py | 1 + 1 file changed, 1 insertion(+) diff --git a/crytic_compile/platform/solc_standard_json.py b/crytic_compile/platform/solc_standard_json.py index f81686c6..3bc4a94c 100644 --- a/crytic_compile/platform/solc_standard_json.py +++ b/crytic_compile/platform/solc_standard_json.py @@ -25,6 +25,7 @@ LOGGER = logging.getLogger("CryticCompile") +# pylint: disable=too-many-arguments def standalone_compile( filenames: List[str], compilation_unit: CompilationUnit,