Skip to content

Commit

Permalink
Merge branch 'master' into fix/as_wei_value
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper authored Dec 30, 2024
2 parents 7799025 + 194d60a commit bf29be8
Show file tree
Hide file tree
Showing 21 changed files with 351 additions and 35 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
# docs: documentation
# test: test suite
# lang: language changes
# stdlib: changes to the stdlib
# ux: language changes (UX)
# tool: integration
# ir: (old) IR/codegen changes
Expand All @@ -43,6 +44,7 @@ jobs:
docs
test
lang
stdlib
ux
tool
ir
Expand Down
105 changes: 105 additions & 0 deletions tests/functional/codegen/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,3 +876,108 @@ def bar() -> uint256:
input_bundle = make_input_bundle({"lib1.vy": lib1})
c = get_contract(main, input_bundle=input_bundle)
assert c.bar() == 1


def test_interface_with_flags():
code = """
struct MyStruct:
a: address
flag Foo:
BOO
MOO
POO
event Transfer:
sender: indexed(address)
@external
def bar():
pass
flag BAR:
BIZ
BAZ
BOO
@external
@view
def foo(s: MyStruct) -> MyStruct:
return s
"""

out = compile_code(code, contract_path="code.vy", output_formats=["interface"])["interface"]

assert "# Flags" in out
assert "flag Foo:" in out
assert "flag BAR" in out
assert "BOO" in out
assert "MOO" in out

compile_code(out, contract_path="code.vyi", output_formats=["interface"])


vyi_filenames = [
"test__test.vyi",
"test__t.vyi",
"t__test.vyi",
"t__t.vyi",
"t_t.vyi",
"test_test.vyi",
"t_test.vyi",
"test_t.vyi",
"_test_t__t_tt_.vyi",
"foo_bar_baz.vyi",
]


@pytest.mark.parametrize("vyi_filename", vyi_filenames)
def test_external_interface_names(vyi_filename):
code = """
@external
def foo():
...
"""

compile_code(code, contract_path=vyi_filename, output_formats=["external_interface"])


def test_external_interface_with_flag():
code = """
flag Foo:
Blah
@external
def foo() -> Foo:
...
"""

out = compile_code(code, contract_path="test__test.vyi", output_formats=["external_interface"])[
"external_interface"
]
assert "-> Foo:" in out


def test_external_interface_compiles_again():
code = """
@external
def foo() -> uint256:
...
@external
def bar(a:int32) -> uint256:
...
"""

out = compile_code(code, contract_path="test.vyi", output_formats=["external_interface"])[
"external_interface"
]
compile_code(out, contract_path="test.vyi", output_formats=["external_interface"])


@pytest.mark.xfail
def test_weird_interface_name():
# based on comment https://github.com/vyperlang/vyper/pull/4290#discussion_r1884137428
# we replace "_" for "" which results in an interface without name
out = compile_code("", contract_path="_.vyi", output_formats=["external_interface"])[
"external_interface"
]
assert "interface _:" in out
16 changes: 14 additions & 2 deletions tests/functional/venom/test_venom_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from tests.venom_utils import assert_ctx_eq, parse_venom
from vyper.compiler import compile_code
from vyper.compiler.phases import generate_bytecode
from vyper.compiler.settings import OptimizationLevel
from vyper.venom import generate_assembly_experimental, run_passes_on
from vyper.venom.context import IRContext

Expand All @@ -20,14 +21,19 @@ def get_example_vy_filenames():


@pytest.mark.parametrize("vy_filename", get_example_vy_filenames())
def test_round_trip_examples(vy_filename, optimize, compiler_settings):
def test_round_trip_examples(vy_filename, debug, optimize, compiler_settings, request):
"""
Check all examples round trip
"""
path = f"examples/{vy_filename}"
with open(path) as f:
vyper_source = f.read()

if debug and optimize == OptimizationLevel.CODESIZE:
# FIXME: some round-trips fail when debug is enabled due to labels
# not getting pinned
request.node.add_marker(pytest.mark.xfail(strict=False))

_round_trip_helper(vyper_source, optimize, compiler_settings)


Expand All @@ -45,11 +51,17 @@ def _loop() -> uint256:


@pytest.mark.parametrize("vyper_source", vyper_sources)
def test_round_trip_sources(vyper_source, optimize, compiler_settings):
def test_round_trip_sources(vyper_source, debug, optimize, compiler_settings, request):
"""
Test vyper_sources round trip
"""
vyper_source = textwrap.dedent(vyper_source)

if debug and optimize == OptimizationLevel.CODESIZE:
# FIXME: some round-trips fail when debug is enabled due to labels
# not getting pinned
request.node.add_marker(pytest.mark.xfail(strict=False))

_round_trip_helper(vyper_source, optimize, compiler_settings)


Expand Down
2 changes: 2 additions & 0 deletions tests/unit/ast/test_ast_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ def foo():
"node_id": 0,
"path": "main.vy",
"source_id": 1,
"is_interface": False,
"type": {
"name": "main.vy",
"type_decl_node": {"node_id": 0, "source_id": 1},
Expand Down Expand Up @@ -1175,6 +1176,7 @@ def foo():
"node_id": 0,
"path": "lib1.vy",
"source_id": 0,
"is_interface": False,
"type": {
"name": "lib1.vy",
"type_decl_node": {"node_id": 0, "source_id": 0},
Expand Down
128 changes: 128 additions & 0 deletions tests/unit/cli/vyper_compile/test_compile_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from vyper.cli.vyper_compile import compile_files
from vyper.cli.vyper_json import compile_json
from vyper.compiler import INTERFACE_OUTPUT_FORMATS, OUTPUT_FORMATS
from vyper.compiler.input_bundle import FilesystemInputBundle
from vyper.compiler.output_bundle import OutputBundle
from vyper.compiler.phases import CompilerData
Expand Down Expand Up @@ -360,6 +361,105 @@ def test_archive_b64_output(input_files):
assert out[contract_file] == out2[archive_path]


def test_archive_compile_options(input_files):
tmpdir, _, _, contract_file = input_files
search_paths = [".", tmpdir]

options = ["abi_python", "json", "ast", "annotated_ast", "ir_json"]

for option in options:
out = compile_files([contract_file], ["archive_b64", option], paths=search_paths)

archive_b64 = out[contract_file].pop("archive_b64")

archive_path = Path("foo.zip.b64")
with archive_path.open("w") as f:
f.write(archive_b64)

# compare compiling the two input bundles
out2 = compile_files([archive_path], [option])

if option in ["ast", "annotated_ast"]:
# would have to normalize paths and imports, so just verify it compiles
continue

assert out[contract_file] == out2[archive_path]


format_options = [
"bytecode",
"bytecode_runtime",
"blueprint_bytecode",
"abi",
"abi_python",
"source_map",
"source_map_runtime",
"method_identifiers",
"userdoc",
"devdoc",
"metadata",
"combined_json",
"layout",
"ast",
"annotated_ast",
"interface",
"external_interface",
"opcodes",
"opcodes_runtime",
"ir",
"ir_json",
"ir_runtime",
"asm",
"integrity",
"archive",
"solc_json",
]


def test_compile_vyz_with_options(input_files):
tmpdir, _, _, contract_file = input_files
search_paths = [".", tmpdir]

for option in format_options:
out_archive = compile_files([contract_file], ["archive"], paths=search_paths)

archive = out_archive[contract_file].pop("archive")

archive_path = Path("foo.zip.out.vyz")
with archive_path.open("wb") as f:
f.write(archive)

# compare compiling the two input bundles
out = compile_files([contract_file], [option], paths=search_paths)
out2 = compile_files([archive_path], [option])

if option in ["ast", "annotated_ast", "metadata"]:
# would have to normalize paths and imports, so just verify it compiles
continue

if option in ["ir_runtime", "ir", "archive"]:
# ir+ir_runtime is different due to being different compiler runs
# archive is different due to different metadata (timestamps)
continue

assert out[contract_file] == out2[archive_path]


def test_archive_compile_simultaneous_options(input_files):
tmpdir, _, _, contract_file = input_files
search_paths = [".", tmpdir]

for option in format_options:
with pytest.raises(ValueError) as e:
_ = compile_files([contract_file], ["archive", option], paths=search_paths)

err_opt = "archive"
if option in ("combined_json", "solc_json"):
err_opt = option

assert f"If using {err_opt} it must be the only output format requested" in str(e.value)


def test_solc_json_output(input_files):
tmpdir, _, _, contract_file = input_files
search_paths = [".", tmpdir]
Expand Down Expand Up @@ -425,3 +525,31 @@ def test_archive_search_path(tmp_path_factory, make_file, chdir_tmp_path):

used_dir = search_paths[-1].stem # either dir1 or dir2
assert output_bundle.used_search_paths == [".", "0/" + used_dir]


def test_compile_interface_file(make_file):
interface = """
@view
@external
def foo() -> String[1]:
...
@view
@external
def bar() -> String[1]:
...
@external
def baz() -> uint8:
...
"""
file = make_file("interface.vyi", interface)
compile_files([file], INTERFACE_OUTPUT_FORMATS)

# check unallowed output formats
for f in OUTPUT_FORMATS:
if f in INTERFACE_OUTPUT_FORMATS:
continue
with pytest.raises(ValueError):
compile_files([file], [f])
3 changes: 2 additions & 1 deletion tests/unit/cli/vyper_json/test_compile_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def oopsie(a: uint256) -> bool:


@pytest.fixture(scope="function")
def input_json(optimize, evm_version, experimental_codegen):
def input_json(optimize, evm_version, experimental_codegen, debug):
return {
"language": "Vyper",
"sources": {
Expand All @@ -87,6 +87,7 @@ def input_json(optimize, evm_version, experimental_codegen):
"optimize": optimize.name.lower(),
"evmVersion": evm_version,
"experimentalCodegen": experimental_codegen,
"debug": debug,
},
}

Expand Down
Loading

0 comments on commit bf29be8

Please sign in to comment.