-
-
Notifications
You must be signed in to change notification settings - Fork 836
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
add compile-time evaluation of slice()
#3667
Open
iFrostizz
wants to merge
16
commits into
vyperlang:master
Choose a base branch
from
iFrostizz:franfran/slice-eval
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
71d3618
add slice evaluate() and tests
iFrostizz 42188f6
add comp/run time equivalence check
iFrostizz 3614409
lint
iFrostizz a68d783
add compile-time strict type check
iFrostizz a782cbe
add fuzz tests, harden Slice
iFrostizz b3ad481
revert unit tests
iFrostizz 162d6b3
add more robust fuzzing, better slice errors
iFrostizz 7c011ea
remove bytes conversion
iFrostizz ea35be9
fix slice bound for dynamic types
iFrostizz 1dd6af5
add dynamic bounds check
iFrostizz 16cac7d
rename variables and use a tuple
iFrostizz 5faddf8
better naming, use hypothesis bytes
iFrostizz 261fb62
lint
iFrostizz 4ea5d0e
use `zfill`
iFrostizz e585b77
reduce nesting, better variables
iFrostizz 5b959fe
lint
iFrostizz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,115 @@ | ||
import string | ||
|
||
import pytest | ||
from hypothesis import given, settings | ||
from hypothesis import strategies as st | ||
|
||
from vyper import ast as vy_ast | ||
from vyper.builtins import functions as vy_fn | ||
from vyper.exceptions import ArgumentException | ||
|
||
|
||
@pytest.mark.fuzzing | ||
@settings(max_examples=50) | ||
@given( | ||
bytes_in=st.binary(max_size=32), | ||
start=st.integers(min_value=0, max_value=31), | ||
length=st.integers(min_value=1, max_value=32), | ||
) | ||
def test_slice_bytes32(get_contract, bytes_in, start, length): | ||
as_hex = "0x" + str(bytes_in.hex()).zfill(64) | ||
length = min(32 - start, length) | ||
|
||
source = f""" | ||
@external | ||
def foo(bytes_in: bytes32) -> Bytes[{length}]: | ||
return slice(bytes_in, {start}, {length}) | ||
""" | ||
contract = get_contract(source) | ||
|
||
vyper_ast = vy_ast.parse_to_ast(f"slice({as_hex}, {start}, {length})") | ||
old_node = vyper_ast.body[0].value | ||
new_node = vy_fn.DISPATCH_TABLE["slice"].evaluate(old_node) | ||
|
||
start *= 2 | ||
length *= 2 | ||
assert ( | ||
contract.foo(as_hex) | ||
== new_node.value | ||
== bytes.fromhex(as_hex[2:][start : (start + length)]) | ||
) | ||
|
||
|
||
@pytest.mark.fuzzing | ||
@settings(max_examples=50) | ||
@given( | ||
bytes_in=st.binary(max_size=31), | ||
start=st.integers(min_value=0, max_value=31), | ||
length=st.integers(min_value=1, max_value=32), | ||
) | ||
def test_slice_bytesnot32(bytes_in, start, length): | ||
if not len(bytes_in): | ||
as_hex = "0x00" | ||
else: | ||
as_hex = "0x" + bytes_in.hex() | ||
length = min(32, 32 - start, length) | ||
|
||
vyper_ast = vy_ast.parse_to_ast(f"slice({as_hex}, {start}, {length})") | ||
old_node = vyper_ast.body[0].value | ||
with pytest.raises(ArgumentException): | ||
vy_fn.DISPATCH_TABLE["slice"].evaluate(old_node) | ||
|
||
|
||
@pytest.mark.fuzzing | ||
@settings(max_examples=50) | ||
@given( | ||
bytes_in=st.binary(min_size=1, max_size=100), | ||
start=st.integers(min_value=0, max_value=99), | ||
length=st.integers(min_value=1, max_value=100), | ||
) | ||
def test_slice_dynbytes(get_contract, bytes_in, start, length): | ||
start = start % len(bytes_in) | ||
length = min(len(bytes_in), len(bytes_in) - start, length) | ||
|
||
source = f""" | ||
@external | ||
def foo(bytes_in: Bytes[100]) -> Bytes[{length}]: | ||
return slice(bytes_in, {start}, {length}) | ||
""" | ||
contract = get_contract(source) | ||
|
||
vyper_ast = vy_ast.parse_to_ast(f"slice({bytes_in}, {start}, {length})") | ||
old_node = vyper_ast.body[0].value | ||
new_node = vy_fn.DISPATCH_TABLE["slice"].evaluate(old_node) | ||
|
||
assert contract.foo(bytes_in) == new_node.value == bytes_in[start : (start + length)] | ||
|
||
|
||
valid_char = [ | ||
char for char in string.printable if char not in (string.whitespace.replace(" ", "") + '"\\') | ||
] | ||
|
||
|
||
@pytest.mark.fuzzing | ||
@settings(max_examples=50) | ||
@given( | ||
string_in=st.text(alphabet=valid_char, min_size=1, max_size=100), | ||
start=st.integers(min_value=0, max_value=99), | ||
length=st.integers(min_value=1, max_value=100), | ||
) | ||
def test_slice_string(get_contract, string_in, start, length): | ||
start = start % len(string_in) | ||
length = min(len(string_in), len(string_in) - start, length) | ||
|
||
source = f""" | ||
@external | ||
def foo(string_in: String[100]) -> String[{length}]: | ||
return slice(string_in, {start}, {length}) | ||
""" | ||
contract = get_contract(source) | ||
|
||
vyper_ast = vy_ast.parse_to_ast(f'slice("{string_in}", {start}, {length})') | ||
old_node = vyper_ast.body[0].value | ||
new_node = vy_fn.DISPATCH_TABLE["slice"].evaluate(old_node) | ||
|
||
assert contract.foo(string_in) == new_node.value == string_in[start : (start + length)] |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@charles-cooper how do you feel about this line ? Here the
bytes_value
type isstr
while otherwise it'sbytes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks confusing