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]: add raw_return() builtin #4527

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion docs/built-in-functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,19 @@ Vyper has three built-ins for contract creation; all three contract creation bui
def foo(_topic: bytes32, _data: Bytes[100]):
raw_log([_topic], _data)

.. py:function:: raw_revert(data: Bytes) -> None
.. py:function:: raw_return(data: Bytes[...]) -> None

Provides low level access to the ``RETURN`` opcode, reverting execution with the specified data returned.

* ``data``: Data representing the error message causing the revert.

.. code-block:: vyper

@external
def foo(_data: Bytes[100]):
raw_return(_data)

.. py:function:: raw_revert(data: Bytes[...]) -> None

Provides low level access to the ``REVERT`` opcode, reverting execution with the specified data returned.

Expand Down
56 changes: 56 additions & 0 deletions tests/functional/builtins/codegen/test_raw_return.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from eth.codecs import abi


def test_raw_return(env, get_contract):
code = """
@external
def foo(data: Bytes[128]) -> DynArray[uint256, 2]:
raw_return(data)
"""

c = get_contract(code)

data = [1, 2]
abi_encoded = abi.encode("(uint256[])", (data,))
assert c.foo(abi_encoded) == data


def test_proxy_raw_return(env, get_contract):
impl1 = """
@external
def greet() -> String[32]:
return "Hello"
"""

impl2 = """
# test delegate calling with a different type, but byte-compatible in abi
@external
def greet() -> Bytes[32]:
return b"Goodbye"
"""

proxy = """
target: address

@external
def set_implementation(target: address):
self.target = target

@external
def greet() -> String[32]:
# forward msg.data to the implementation contract
data: Bytes[128] = raw_call(self.target, msg.data, is_delegate_call=True, max_outsize=128)
raw_return(data)
"""

impl_c1 = get_contract(impl1)
impl_c2 = get_contract(impl2)

proxy_c = get_contract(proxy)

proxy_c.set_implementation(impl_c1.address)
assert proxy_c.greet() == impl_c1.greet() == "Hello"

proxy_c.set_implementation(impl_c2.address)
assert impl_c2.greet() == b"Goodbye"
assert proxy_c.greet() == "Goodbye" # note: unsafe casted from bytes
23 changes: 19 additions & 4 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,8 +1225,7 @@ def build_IR(self, expr, args, kwargs, contact):
return IRnode.from_list(["blobhash", args[0]], typ=BYTES32_T)


class RawRevert(BuiltinFunctionT):
_id = "raw_revert"
class _RawReturnOrRevert(BuiltinFunctionT):
_inputs = [("data", BytesT.any())]
_return_type = None
_is_terminus = True
Expand All @@ -1239,12 +1238,27 @@ def infer_arg_types(self, node, expected_return_typ=None):
data_type = get_possible_types_from_node(node.args[0]).pop()
return [data_type]

@property
def OPCODE(self):
# must be implemented by subclass
raise NotImplementedError()

@process_inputs
def build_IR(self, expr, args, kwargs, context):
with ensure_in_memory(args[0], context).cache_when_complex("err_buf") as (b, buf):
with ensure_in_memory(args[0], context).cache_when_complex("buf") as (b, buf):
data = bytes_data_ptr(buf)
len_ = get_bytearray_length(buf)
return b.resolve(IRnode.from_list(["revert", data, len_]))
return b.resolve(IRnode.from_list([self.OPCODE, data, len_]))


class RawRevert(_RawReturnOrRevert):
_id = "raw_revert"
OPCODE = "revert"


class RawReturn(_RawReturnOrRevert):
_id = "raw_return"
OPCODE = "return"


class RawLog(BuiltinFunctionT):
Expand Down Expand Up @@ -2687,6 +2701,7 @@ def _try_fold(self, node):
"raw_call": RawCall(),
"raw_log": RawLog(),
"raw_revert": RawRevert(),
"raw_return": RawReturn(),
"create_minimal_proxy_to": CreateMinimalProxyTo(),
"create_forwarder_to": CreateForwarderTo(),
"create_copy_of": CreateCopyOf(),
Expand Down
Loading