Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[lang]: export interfaces #3919

Merged
merged 17 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions tests/functional/codegen/modules/test_exports.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import pytest

from vyper.compiler import compile_code
from vyper.utils import method_id


def test_simple_export(make_input_bundle, get_contract):
lib1 = """
@external
Expand Down Expand Up @@ -147,3 +153,169 @@ def foo() -> uint256:
c = get_contract(main, input_bundle=input_bundle)

assert c.foo() == 5


@pytest.fixture
def simple_library(make_input_bundle):
ifoo = """
@external
def foo() -> uint256:
...

@external
def bar() -> uint256:
...
"""
ibar = """
@external
def bar() -> uint256:
...

@external
def qux() -> uint256:
...
"""
lib1 = """
import ifoo
import ibar

implements: ifoo
implements: ibar

@external
def foo() -> uint256:
return 1

@external
def bar() -> uint256:
return 2

@external
def qux() -> uint256:
return 3
"""
return make_input_bundle({"lib1.vy": lib1, "ifoo.vyi": ifoo, "ibar.vyi": ibar})


@pytest.fixture
def send_failing_tx_to_signature(w3, tx_failed):
def _send_transaction(c, method_sig):
data = method_id(method_sig)
with tx_failed():
w3.eth.send_transaction({"to": c.address, "data": data})

return _send_transaction


def test_exports_interface_simple(get_contract, simple_library):
main = """
import lib1

exports: lib1.__interface__
"""
c = get_contract(main, input_bundle=simple_library)
assert c.foo() == 1
assert c.bar() == 2
assert c.qux() == 3


def test_exports_interface2(get_contract, send_failing_tx_to_signature, simple_library):
main = """
import lib1

exports: lib1.ifoo
"""
out = compile_code(
main, output_formats=["abi"], contract_path="main.vy", input_bundle=simple_library
)
fnames = [item["name"] for item in out["abi"]]
assert fnames == ["foo", "bar"]

c = get_contract(main, input_bundle=simple_library)
assert c.foo() == 1
assert c.bar() == 2
assert not hasattr(c, "qux")
send_failing_tx_to_signature(c, "qux()")


def test_exported_fun_part_of_interface(get_contract, make_input_bundle):
main = """
import lib2

exports: lib2.__interface__
"""
lib1 = """
@external
def bar() -> uint256:
return 1
"""
lib2 = """
import lib1

@external
def foo() -> uint256:
return 2

exports: lib1.bar
"""
input_bundle = make_input_bundle({"lib1.vy": lib1, "lib2.vy": lib2})
c = get_contract(main, input_bundle=input_bundle)
assert c.bar() == 1
assert c.foo() == 2


def test_imported_module_not_part_of_interface(
send_failing_tx_to_signature, get_contract, make_input_bundle
):
main = """
import lib2

exports: lib2.__interface__
"""
lib1 = """
@external
def bar() -> uint256:
return 1
"""
lib2 = """
import lib1

@external
def foo() -> uint256:
return 2
"""
input_bundle = make_input_bundle({"lib1.vy": lib1, "lib2.vy": lib2})
c = get_contract(main, input_bundle=input_bundle)
assert c.foo() == 2
send_failing_tx_to_signature(c, "bar()")


def test_export_unimplemented_function(
send_failing_tx_to_signature, get_contract, make_input_bundle
):
ifoo = """
@external
def foo() -> uint256:
...
"""
lib1 = """
import ifoo
implements: ifoo

@external
def foo() -> uint256:
return 1

@external
def bar() -> uint256:
return 2
"""
main = """
import lib1

exports: lib1.ifoo
"""
input_bundle = make_input_bundle({"lib1.vy": lib1, "ifoo.vyi": ifoo})
c = get_contract(main, input_bundle=input_bundle)
assert c.foo() == 1
send_failing_tx_to_signature(c, "bar()")
137 changes: 136 additions & 1 deletion tests/functional/syntax/modules/test_exports.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import pytest

from vyper.compiler import compile_code
from vyper.exceptions import ImmutableViolation, NamespaceCollision, StructureException
from vyper.exceptions import (
ImmutableViolation,
InterfaceViolation,
NamespaceCollision,
StructureException,
)

from .helpers import NONREENTRANT_NOTE

Expand Down Expand Up @@ -309,3 +314,133 @@ def bar():
assert e.value.prev_decl.col_offset == 9
assert e.value.prev_decl.node_source_code == "lib1.foo"
assert e.value.prev_decl.module_node.path == "main.vy"


def test_interface_export_collision(make_input_bundle):
main = """
import lib1

exports: lib1.__interface__
exports: lib1.bar
"""
lib1 = """
@external
def bar() -> uint256:
return 1
"""
input_bundle = make_input_bundle({"lib1.vy": lib1})
with pytest.raises(StructureException) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "already exported!"


def test_no_export_missing_function(make_input_bundle):
ifoo = """
@external
def do_xyz():
...
"""
lib1 = """
import ifoo

@external
@view
def bar() -> uint256:
return 1
"""
main = """
import lib1

exports: lib1.ifoo
"""
input_bundle = make_input_bundle({"lib1.vy": lib1, "ifoo.vyi": ifoo})
with pytest.raises(InterfaceViolation) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "requested `lib1.ifoo` but `lib1` does not implement `lib1.ifoo`!"


def test_no_export_unimplemented_interface(make_input_bundle):
ifoo = """
@external
def do_xyz():
...
"""
lib1 = """
import ifoo

# technically implements ifoo, but missing `implements: ifoo`

@external
def do_xyz():
pass
"""
main = """
import lib1

exports: lib1.ifoo
"""
input_bundle = make_input_bundle({"lib1.vy": lib1, "ifoo.vyi": ifoo})
with pytest.raises(InterfaceViolation) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "requested `lib1.ifoo` but `lib1` does not implement `lib1.ifoo`!"


def test_export_selector_conflict(make_input_bundle):
ifoo = """
@external
def gsf():
...
"""
lib1 = """
import ifoo

@external
def gsf():
pass

@external
@view
def tgeo() -> uint256:
return 1
"""
main = """
import lib1

exports: (lib1.ifoo, lib1.tgeo)
"""
input_bundle = make_input_bundle({"lib1.vy": lib1, "ifoo.vyi": ifoo})
with pytest.raises(StructureException) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "Methods produce colliding method ID `0x67e43e43`: gsf(), tgeo()"


def test_export_different_return_type(make_input_bundle):
ifoo = """
@external
def foo() -> uint256:
...
"""
lib1 = """
import ifoo

foo: public(int256)

@deploy
def __init__():
self.foo = -1
"""
main = """
import lib1

initializes: lib1

exports: lib1.ifoo

@deploy
def __init__():
lib1.__init__()
"""
input_bundle = make_input_bundle({"lib1.vy": lib1, "ifoo.vyi": ifoo})
with pytest.raises(InterfaceViolation) as e:
compile_code(main, input_bundle=input_bundle)
assert e.value._message == "requested `lib1.ifoo` but `lib1` does not implement `lib1.ifoo`!"
Loading
Loading