Skip to content

Commit

Permalink
Merge branch 'master' into fix/venom-extcodesize-effect
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper authored Nov 25, 2024
2 parents 8c2ef5b + 8f433f8 commit 8952e61
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 18 deletions.
17 changes: 17 additions & 0 deletions tests/functional/codegen/features/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,23 @@ def foo():
assert log.topics == [event_id, topic1, topic2, topic3]


valid_list = [
# test constant folding inside raw_log
"""
topic: constant(bytes32) = 0x1212121212121210212801291212121212121210121212121212121212121212
@external
def foo():
raw_log([[topic]][0], b'')
"""
]


@pytest.mark.parametrize("code", valid_list)
def test_raw_log_pass(code):
assert compile_code(code) is not None


fail_list = [
(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def foo():
def foo():
a: bytes32 = keccak256("ѓtest")
""",
# test constant folding inside of `convert()`
"""
BAR: constant(uint16) = 256
@external
def foo():
a: uint8 = convert(BAR, uint8)
""",
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def foo():
"""
a: constant(address) = 0x3cd751e6b0078be393132286c442345e5dc49699
""",
# test constant folding inside `convert()`
"""
BAR: constant(Bytes[5]) = b"vyper"
@external
def foo():
a: Bytes[4] = convert(BAR, Bytes[4])
""",
]


Expand Down
16 changes: 16 additions & 0 deletions tests/functional/syntax/test_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ def foo(inp: Bytes[10]) -> Bytes[4]:
def foo() -> Bytes[10]:
return slice(b"badmintonzzz", 1, 10)
""",
# test constant folding for `slice()` `length` argument
"""
@external
def foo():
x: Bytes[32] = slice(msg.data, 0, 31 + 1)
""",
"""
@external
def foo(a: address):
x: Bytes[32] = slice(a.code, 0, 31 + 1)
""",
"""
@external
def foo(inp: Bytes[5], start: uint256) -> Bytes[3]:
return slice(inp, 0, 1 + 1)
""",
]


Expand Down
2 changes: 1 addition & 1 deletion vyper/builtins/_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ def to_flag(expr, arg, out_typ):
def convert(expr, context):
assert len(expr.args) == 2, "bad typecheck: convert"

arg_ast = expr.args[0]
arg_ast = expr.args[0].reduced()
arg = Expr(arg_ast, context).ir_node
original_arg = arg

Expand Down
7 changes: 4 additions & 3 deletions vyper/builtins/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def fetch_call_return(self, node):

arg = node.args[0]
start_expr = node.args[1]
length_expr = node.args[2]
length_expr = node.args[2].reduced()

# CMC 2022-03-22 NOTE slight code duplication with semantics/analysis/local
is_adhoc_slice = arg.get("attr") == "code" or (
Expand Down Expand Up @@ -1257,7 +1257,8 @@ def fetch_call_return(self, node):
def infer_arg_types(self, node, expected_return_typ=None):
self._validate_arg_types(node)

if not isinstance(node.args[0], vy_ast.List) or len(node.args[0].elements) > 4:
arg = node.args[0].reduced()
if not isinstance(arg, vy_ast.List) or len(arg.elements) > 4:
raise InvalidType("Expecting a list of 0-4 topics as first argument", node.args[0])

# return a concrete type for `data`
Expand All @@ -1269,7 +1270,7 @@ def infer_arg_types(self, node, expected_return_typ=None):
def build_IR(self, expr, args, kwargs, context):
context.check_is_not_constant(f"use {self._id}", expr)

topics_length = len(expr.args[0].elements)
topics_length = len(expr.args[0].reduced().elements)
topics = args[0].args
topics = [unwrap_location(topic) for topic in topics]

Expand Down
12 changes: 8 additions & 4 deletions vyper/cli/compile_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import zipfile
from pathlib import PurePath

from vyper.compiler import compile_from_file_input
from vyper.compiler import outputs_from_compiler_data
from vyper.compiler.input_bundle import FileInput, ZipInputBundle
from vyper.compiler.phases import CompilerData
from vyper.compiler.settings import Settings, merge_settings
from vyper.exceptions import BadArchive

Expand All @@ -19,6 +20,11 @@ class NotZipInput(Exception):


def compile_from_zip(file_name, output_formats, settings, no_bytecode_metadata):
compiler_data = compiler_data_from_zip(file_name, settings, no_bytecode_metadata)
return outputs_from_compiler_data(compiler_data, output_formats)


def compiler_data_from_zip(file_name, settings, no_bytecode_metadata):
with open(file_name, "rb") as f:
bcontents = f.read()

Expand Down Expand Up @@ -59,11 +65,9 @@ def compile_from_zip(file_name, output_formats, settings, no_bytecode_metadata):
settings, archive_settings, lhs_source="command line", rhs_source="archive settings"
)

# TODO: validate integrity sum (probably in CompilerData)
return compile_from_file_input(
return CompilerData(
file,
input_bundle=input_bundle,
output_formats=output_formats,
integrity_sum=integrity,
settings=settings,
no_bytecode_metadata=no_bytecode_metadata,
Expand Down
20 changes: 12 additions & 8 deletions vyper/compiler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ def compile_from_file_input(
"""
settings = settings or get_global_settings() or Settings()

if output_formats is None:
output_formats = ("bytecode",)

# make IR output the same between runs
# TODO: move this to CompilerData.__init__()
codegen.reset_names()

compiler_data = CompilerData(
file_input,
input_bundle,
Expand All @@ -116,6 +109,17 @@ def compile_from_file_input(
no_bytecode_metadata=no_bytecode_metadata,
)

return outputs_from_compiler_data(compiler_data, output_formats, exc_handler)


def outputs_from_compiler_data(
compiler_data: CompilerData,
output_formats: Optional[OutputFormats] = None,
exc_handler: Optional[Callable] = None,
):
if output_formats is None:
output_formats = ("bytecode",)

ret = {}
with anchor_settings(compiler_data.settings):
for output_format in output_formats:
Expand All @@ -126,7 +130,7 @@ def compile_from_file_input(
ret[output_format] = formatter(compiler_data)
except Exception as exc:
if exc_handler is not None:
exc_handler(str(file_input.path), exc)
exc_handler(str(compiler_data.file_input.path), exc)
else:
raise exc

Expand Down
4 changes: 4 additions & 0 deletions vyper/compiler/phases.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pathlib import Path, PurePath
from typing import Any, Optional

import vyper.codegen.core as codegen
from vyper import ast as vy_ast
from vyper.ast import natspec
from vyper.codegen import module
Expand Down Expand Up @@ -304,6 +305,9 @@ def generate_ir_nodes(global_ctx: ModuleT, settings: Settings) -> tuple[IRnode,
IR to generate deployment bytecode
IR to generate runtime bytecode
"""
# make IR output the same between runs
codegen.reset_names()

with anchor_settings(settings):
ir_nodes, ir_runtime = module.generate_ir_for_module(global_ctx)
if settings.optimize != OptimizationLevel.NONE:
Expand Down
4 changes: 2 additions & 2 deletions vyper/semantics/analysis/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _validate_address_code(node: vy_ast.Attribute, value_type: VyperType) -> Non
parent = node.get_ancestor()
if isinstance(parent, vy_ast.Call):
ok_func = isinstance(parent.func, vy_ast.Name) and parent.func.id == "slice"
ok_args = len(parent.args) == 3 and isinstance(parent.args[2], vy_ast.Int)
ok_args = len(parent.args) == 3 and isinstance(parent.args[2].reduced(), vy_ast.Int)
if ok_func and ok_args:
return

Expand All @@ -154,7 +154,7 @@ def _validate_msg_data_attribute(node: vy_ast.Attribute) -> None:
"msg.data is only allowed inside of the slice, len or raw_call functions", node
)
if parent.get("func.id") == "slice":
ok_args = len(parent.args) == 3 and isinstance(parent.args[2], vy_ast.Int)
ok_args = len(parent.args) == 3 and isinstance(parent.args[2].reduced(), vy_ast.Int)
if not ok_args:
raise StructureException(
"slice(msg.data) must use a compile-time constant for length argument", parent
Expand Down

0 comments on commit 8952e61

Please sign in to comment.