-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14565 from ethereum/viaIR-codegen-import-bug-test
Add test for via-ir codegen import bug
- Loading branch information
Showing
5 changed files
with
172 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import os | ||
import subprocess | ||
from pathlib import Path | ||
from shutil import rmtree | ||
from tempfile import mkdtemp | ||
from textwrap import dedent | ||
from typing import List | ||
from typing import Optional | ||
|
||
from bytecodecompare.prepare_report import FileReport | ||
from bytecodecompare.prepare_report import parse_cli_output | ||
|
||
|
||
DEFAULT_PREAMBLE = dedent(""" | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity >=0.0; | ||
""") | ||
|
||
|
||
def inside_temporary_dir(prefix): | ||
""" | ||
Creates a temporary directory, enters the directory and executes the function inside it. | ||
Restores the previous working directory after executing the function. | ||
""" | ||
def tmp_dir_decorator(fn): | ||
previous_dir = os.getcwd() | ||
def f(*args, **kwargs): | ||
try: | ||
tmp_dir = mkdtemp(prefix=prefix) | ||
os.chdir(tmp_dir) | ||
result = fn(*args, **kwargs) | ||
rmtree(tmp_dir) | ||
return result | ||
finally: | ||
os.chdir(previous_dir) | ||
return f | ||
return tmp_dir_decorator | ||
|
||
|
||
def solc_bin_report(solc_binary: str, input_files: List[Path], via_ir: bool) -> FileReport: | ||
""" | ||
Runs the solidity compiler binary | ||
""" | ||
|
||
output = subprocess.check_output( | ||
[solc_binary, '--bin'] + | ||
input_files + | ||
(['--via-ir'] if via_ir else []), | ||
encoding='utf8', | ||
) | ||
return parse_cli_output('', output) | ||
|
||
|
||
def save_bytecode(bytecode_path: Path, reports: FileReport, contract: Optional[str] = None): | ||
with open(bytecode_path, 'w', encoding='utf8') as f: | ||
for report in reports.contract_reports: | ||
if contract is None or report.contract_name == contract: | ||
bytecode = report.bytecode if report.bytecode is not None else '<NO BYTECODE>' | ||
f.write(f'{report.contract_name}: {bytecode}\n') | ||
|
||
|
||
def add_preamble(source_path: Path, preamble: str = DEFAULT_PREAMBLE): | ||
for source in source_path.glob('**/*.sol'): | ||
with open(source, 'r+', encoding='utf8') as f: | ||
content = f.read() | ||
f.seek(0, 0) | ||
f.write(preamble + content) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
test/cmdlineTests/~bytecode_equivalence_independent_of_import_discovery_order/inputs.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
==== Source: A.sol ==== | ||
import "@/D.sol"; | ||
import "B.sol"; | ||
|
||
contract A is B { | ||
function a() public pure { | ||
e(); | ||
} | ||
} | ||
|
||
==== Source: B.sol ==== | ||
import "C.sol"; | ||
|
||
abstract contract B is C {} | ||
|
||
==== Source: C.sol ==== | ||
abstract contract C { | ||
function c() public pure returns (uint) { | ||
return 0; | ||
} | ||
} | ||
|
||
==== Source: @/D.sol ==== | ||
import "@/E.sol"; | ||
|
||
==== Source: @/E.sol ==== | ||
function e() pure returns (bytes memory returndata) { | ||
return ""; | ||
} |
43 changes: 43 additions & 0 deletions
43
test/cmdlineTests/~bytecode_equivalence_independent_of_import_discovery_order/test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import sys | ||
from pathlib import Path | ||
from textwrap import dedent | ||
|
||
# pylint: disable=wrong-import-position | ||
PROJECT_ROOT = Path(__file__).parents[3] | ||
sys.path.insert(0, str(PROJECT_ROOT / 'scripts')) | ||
|
||
from common.cmdline_helpers import add_preamble | ||
from common.cmdline_helpers import inside_temporary_dir | ||
from common.cmdline_helpers import save_bytecode | ||
from common.cmdline_helpers import solc_bin_report | ||
from common.git_helpers import git_diff | ||
from splitSources import split_sources | ||
|
||
|
||
@inside_temporary_dir(Path(__file__).parent.name) | ||
def test_bytecode_equivalence(): | ||
source_file_path = Path(__file__).parent / 'inputs.sol' | ||
split_sources(source_file_path, suppress_output=True) | ||
add_preamble(Path.cwd()) | ||
|
||
solc_binary = os.environ.get('SOLC') | ||
if solc_binary is None: | ||
raise RuntimeError(dedent("""\ | ||
`solc` compiler not found. | ||
Please ensure you set the SOLC environment variable | ||
with the correct path to the compiler's binary. | ||
""")) | ||
|
||
# Whether a file is passed to the compiler explicitly or only discovered when traversing imports | ||
# may affect the order in which files are processed and result in different AST IDs. | ||
# This, however, must not result in different bytecode being generated. | ||
save_bytecode(Path('A.bin'), solc_bin_report(solc_binary, [Path('A.sol')], via_ir=True)) | ||
save_bytecode(Path('AB.bin'), solc_bin_report(solc_binary, [Path('A.sol'), Path('B.sol')], via_ir=True)) | ||
return git_diff(Path('A.bin'), Path('AB.bin')) | ||
|
||
|
||
if __name__ == '__main__': | ||
sys.exit(test_bytecode_equivalence()) |