From 2a9bb55a90fe2ba682aaa99b46583b7f53cfad38 Mon Sep 17 00:00:00 2001 From: antazoey Date: Mon, 8 Jul 2024 16:00:59 -0500 Subject: [PATCH] fix: issue with default optimization(s) (#124) --- ape_vyper/compiler.py | 17 +++++++++++++---- tests/test_compiler.py | 24 +++++++++++++++--------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/ape_vyper/compiler.py b/ape_vyper/compiler.py index b6814a0..ef35283 100644 --- a/ape_vyper/compiler.py +++ b/ape_vyper/compiler.py @@ -247,12 +247,15 @@ def get_evmversion_pragma(source: Union[str, Path]) -> Optional[str]: def get_optimization_pragma_map( - contract_filepaths: Iterable[Path], base_path: Path + contract_filepaths: Iterable[Path], + base_path: Path, + default: Optimization, ) -> dict[str, Optimization]: pragma_map: dict[str, Optimization] = {} for path in contract_filepaths: - pragma = get_optimization_pragma(path) or "codesize" + res = get_optimization_pragma(path) + pragma = default if res is None else res source_id = str(get_relative_path(path.absolute(), base_path.absolute())) pragma_map[source_id] = pragma @@ -1194,15 +1197,21 @@ def _get_compiler_settings_from_version_map( if not source_paths: continue + default_optimization: Optimization = True if version < Version("0.3.10") else "gas" output_selection: dict[str, set[str]] = {} - optimizations_map = get_optimization_pragma_map(source_paths, pm.path) + optimizations_map = get_optimization_pragma_map( + source_paths, pm.path, default_optimization + ) evm_version_map = get_evm_version_pragma_map(source_paths, pm.path) default_evm_version = data.get( "evm_version", data.get("evmVersion") ) or EVM_VERSION_DEFAULT.get(version.base_version) for source_path in source_paths: source_id = str(get_relative_path(source_path.absolute(), pm.path)) - optimization = optimizations_map.get(source_id, "codesize") + + if not (optimization := optimizations_map.get(source_id)): + optimization = True if version < Version("0.3.10") else "gas" + evm_version = evm_version_map.get(source_id, default_evm_version) settings_key = f"{optimization}%{evm_version}".lower() if settings_key not in output_selection: diff --git a/tests/test_compiler.py b/tests/test_compiler.py index 395bf00..745589a 100644 --- a/tests/test_compiler.py +++ b/tests/test_compiler.py @@ -177,8 +177,14 @@ def run_test(manifest): all_latest_03 = [ c for c in manifest.compilers if str(c.version) == str(VERSION_FROM_PRAGMA) ] - evm_latest = [c for c in all_latest_03 if c.settings.get("evmVersion") == "paris"][0] - codesize_latest = [ + evm_opt = [c for c in all_latest_03 if c.settings.get("evmVersion") == "paris"][0] + gas_opt = [c for c in all_latest_03 if c.settings["optimize"] == "gas"][0] + true_opt = [ + c + for c in manifest.compilers + if c.settings["optimize"] is True and "non_payable_default" in c.contractTypes + ][0] + codesize_opt = [ c for c in all_latest_03 if c.settings["optimize"] == "codesize" and c.settings.get("evmVersion") != "paris" @@ -187,20 +193,20 @@ def run_test(manifest): c for c in manifest.compilers if str(c.version) == str(OLDER_VERSION_FROM_PRAGMA) ][0] - for compiler in (vyper_028, codesize_latest): + for compiler in (vyper_028, codesize_opt): assert compiler.name == "vyper" assert vyper_028.settings["evmVersion"] == "berlin" - assert codesize_latest.settings["evmVersion"] == "shanghai" + assert codesize_opt.settings["evmVersion"] == "shanghai" # There is only one contract with evm-version pragma. - assert evm_latest.contractTypes == ["evm_pragma"] - assert evm_latest.settings.get("evmVersion") == "paris" + assert evm_opt.contractTypes == ["evm_pragma"] + assert evm_opt.settings.get("evmVersion") == "paris" - assert len(codesize_latest.contractTypes) >= 9 - assert len(vyper_028.contractTypes) >= 1 - assert "contract_0310" in codesize_latest.contractTypes + assert "optimize_codesize" in codesize_opt.contractTypes assert "older_version" in vyper_028.contractTypes + assert len(gas_opt.contractTypes) >= 1 + assert "non_payable_default" in true_opt.contractTypes project.update_manifest(compilers=[]) project.load_contracts(use_cache=False)