Skip to content

Commit

Permalink
fix: issue with default optimization(s) (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Jul 8, 2024
1 parent 090e669 commit 2a9bb55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
17 changes: 13 additions & 4 deletions ape_vyper/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
24 changes: 15 additions & 9 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down

0 comments on commit 2a9bb55

Please sign in to comment.