Skip to content

Commit

Permalink
wip: add tstorage tests for selected builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberthirst committed Mar 23, 2024
1 parent f8d4b97 commit 27a9824
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 62 deletions.
33 changes: 24 additions & 9 deletions tests/functional/builtins/codegen/test_abi_decode.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from decimal import Decimal

import pytest
from tests.utils import wrap_typ_with_storage_loc

from eth.codecs import abi

from vyper.exceptions import ArgumentException, StructureException
from vyper.evm.opcodes import version_check

TEST_ADDR = "0x" + b"".join(chr(i).encode("utf-8") for i in range(20)).hex()

Expand Down Expand Up @@ -220,9 +223,13 @@ def abi_decode(x: Bytes[{len}]) -> DynArray[DynArray[DynArray[uint256, 3], 3], 3
assert c.abi_decode(encoded) == args


def test_side_effects_evaluation(get_contract):
contract_1 = """
counter: uint256
@pytest.mark.parametrize('location', ["storage", "transient"])
def test_side_effects_evaluation(get_contract, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

contract_1 = f"""
counter: {wrap_typ_with_storage_loc("uint256", location)}
@deploy
def __init__():
Expand Down Expand Up @@ -253,9 +260,13 @@ def foo(addr: address) -> (uint256, String[5]):
assert tuple(c2.foo(c.address)) == (1, "hello")


def test_abi_decode_private_dynarray(get_contract):
code = """
bytez: DynArray[uint256, 3]
@pytest.mark.parametrize('location', ["storage", "transient"])
def test_abi_decode_private_dynarray(get_contract, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

code = f"""
bytez: {wrap_typ_with_storage_loc("DynArray[uint256, 3]", location)}
@internal
def _foo(bs: Bytes[160]):
Expand All @@ -273,9 +284,13 @@ def foo(bs: Bytes[160]) -> (uint256, DynArray[uint256, 3]):
assert c.foo(encoded) == [2**256 - 1, bs]


def test_abi_decode_private_nested_dynarray(get_contract):
code = """
bytez: DynArray[DynArray[DynArray[uint256, 3], 3], 3]
@pytest.mark.parametrize('location', ["storage", "transient"])
def test_abi_decode_private_nested_dynarray(get_contract, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

code = f"""
bytez: {wrap_typ_with_storage_loc("DynArray[DynArray[DynArray[uint256, 3], 3], 3]", location)}
@internal
def _foo(bs: Bytes[1696]):
Expand Down
43 changes: 31 additions & 12 deletions tests/functional/builtins/codegen/test_abi_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import pytest
from eth.codecs import abi

from tests.utils import wrap_typ_with_storage_loc

from vyper.evm.opcodes import version_check

# @pytest.mark.parametrize("string", ["a", "abc", "abcde", "potato"])
def test_abi_encode(get_contract):
Expand Down Expand Up @@ -259,9 +262,13 @@ def abi_encode(
)


def test_side_effects_evaluation(get_contract):
contract_1 = """
counter: uint256
@pytest.mark.parametrize('location', ["storage", "transient"])
def test_side_effects_evaluation(get_contract, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

contract_1 = f"""
counter: {wrap_typ_with_storage_loc("uint256", location)}
@deploy
def __init__():
Expand Down Expand Up @@ -295,9 +302,13 @@ def foo(addr: address) -> Bytes[164]:


# test _abi_encode in private functions to check buffer overruns
def test_abi_encode_private(get_contract):
code = """
bytez: Bytes[96]
@pytest.mark.parametrize("location", ["storage", "transient"])
def test_abi_encode_private(get_contract, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

code = f"""
bytez: {wrap_typ_with_storage_loc("Bytes[96]", location)}
@internal
def _foo(bs: Bytes[32]):
self.bytez = _abi_encode(bs)
Expand All @@ -313,9 +324,13 @@ def foo(bs: Bytes[32]) -> (uint256, Bytes[96]):
assert c.foo(bs) == [2**256 - 1, abi.encode("(bytes)", (bs,))]


def test_abi_encode_private_dynarray(get_contract):
code = """
bytez: Bytes[160]
@pytest.mark.parametrize("location", ["storage", "transient"])
def test_abi_encode_private_dynarray(get_contract, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

code = f"""
bytez: {wrap_typ_with_storage_loc("Bytes[160]", location)}
@internal
def _foo(bs: DynArray[uint256, 3]):
self.bytez = _abi_encode(bs)
Expand All @@ -330,9 +345,13 @@ def foo(bs: DynArray[uint256, 3]) -> (uint256, Bytes[160]):
assert c.foo(bs) == [2**256 - 1, abi.encode("(uint256[])", (bs,))]


def test_abi_encode_private_nested_dynarray(get_contract):
code = """
bytez: Bytes[1696]
@pytest.mark.parametrize("location", ["storage", "transient"])
def test_abi_encode_private_nested_dynarray(get_contract, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

code = f"""
bytez: {wrap_typ_with_storage_loc("Bytes[1696]", location)}
@internal
def _foo(bs: DynArray[DynArray[DynArray[uint256, 3], 3], 3]):
self.bytez = _abi_encode(bs)
Expand Down
16 changes: 13 additions & 3 deletions tests/functional/builtins/codegen/test_addmod.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import pytest

from tests.utils import wrap_typ_with_storage_loc

from vyper.evm.opcodes import version_check

def test_uint256_addmod(tx_failed, get_contract_with_gas_estimation):
uint256_code = """
@external
Expand Down Expand Up @@ -58,9 +64,13 @@ def c() -> uint256:
assert c.foo() == 2


def test_uint256_addmod_evaluation_order(get_contract_with_gas_estimation):
code = """
a: uint256
@pytest.mark.parametrize('location', ["storage", "transient"])
def test_uint256_addmod_evaluation_order(get_contract_with_gas_estimation, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

code = f"""
a: {wrap_typ_with_storage_loc("uint256", location)}
@external
def foo1() -> uint256:
Expand Down
31 changes: 19 additions & 12 deletions tests/functional/builtins/codegen/test_ceil.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import math
from decimal import Decimal

import pytest
from tests.utils import wrap_typ_with_storage_loc

def test_ceil(get_contract_with_gas_estimation):
code = """
x: decimal
from vyper.evm.opcodes import version_check

@deploy
def __init__():
self.x = 504.0000000001

@pytest.mark.parametrize('location', ["storage", "transient"])
def test_ceil(get_contract_with_gas_estimation, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

code = f"""
x: {wrap_typ_with_storage_loc("decimal", location)}
@external
def x_ceil() -> int256:
self.x = 504.0000000001
return ceil(self.x)
@external
Expand Down Expand Up @@ -49,16 +55,17 @@ def fou() -> int256:


# ceil(x) should yield the smallest integer greater than or equal to x
def test_ceil_negative(get_contract_with_gas_estimation):
code = """
x: decimal
@pytest.mark.parametrize("location", ["storage", "transient"])
def test_ceil_negative(get_contract_with_gas_estimation, get_contract, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

@deploy
def __init__():
self.x = -504.0000000001
code = f"""
x: {wrap_typ_with_storage_loc("decimal", location)}
@external
def x_ceil() -> int256:
self.x = -504.0000000001
return ceil(self.x)
@external
Expand Down
52 changes: 32 additions & 20 deletions tests/functional/builtins/codegen/test_concat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import pytest

from tests.utils import wrap_typ_with_storage_loc

from vyper.evm.opcodes import version_check


def test_concat(get_contract_with_gas_estimation):
test_concat = """
@external
Expand All @@ -21,7 +28,6 @@ def foo3(input1: Bytes[50], input2: Bytes[50], input3: Bytes[50]) -> Bytes[1000]
c.foo3(b"horses" * 4, b"mice" * 7, b"crows" * 10)
== b"horses" * 4 + b"mice" * 7 + b"crows" * 10
) # noqa: E501
print("Passed simple concat test")


def test_concat2(get_contract_with_gas_estimation):
Expand All @@ -34,12 +40,15 @@ def foo(inp: Bytes[50]) -> Bytes[1000]:

c = get_contract_with_gas_estimation(test_concat2)
assert c.foo(b"horse" * 9 + b"vyper") == (b"horse" * 9 + b"vyper") * 10
print("Passed second concat test")


def test_crazy_concat_code(get_contract_with_gas_estimation):
crazy_concat_code = """
y: Bytes[10]
@pytest.mark.parametrize('location', ["storage", "transient"])
def test_crazy_concat_code(get_contract_with_gas_estimation, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

crazy_concat_code = f"""
y: {wrap_typ_with_storage_loc("Bytes[10]", location)}
@external
def krazykonkat(z: Bytes[10]) -> Bytes[25]:
Expand All @@ -52,8 +61,6 @@ def krazykonkat(z: Bytes[10]) -> Bytes[25]:

assert c.krazykonkat(b"moose") == b"cow horse moose"

print("Passed third concat test")


def test_concat_buffer(get_contract):
# GHSA-2q8v-3gqq-4f8p
Expand Down Expand Up @@ -92,15 +99,19 @@ def foo() -> int256:
assert c.foo() == -1


def test_concat_buffer3(get_contract):
@pytest.mark.parametrize('location', ["storage", "transient"])
def test_concat_buffer3(get_contract, location):
# GHSA-2q8v-3gqq-4f8p
code = """
s: String[1]
s2: String[33]
s3: String[34]
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

@deploy
def __init__():
code = f"""
s: {wrap_typ_with_storage_loc("String[1]", location)}
s2: {wrap_typ_with_storage_loc("String[33]", location)}
s3: {wrap_typ_with_storage_loc("String[34]", location)}
@internal
def init_test():
self.s = "a"
self.s2 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # 33*'a'
Expand All @@ -111,6 +122,7 @@ def bar() -> uint256:
@external
def foo() -> int256:
self.init_test()
i: int256 = -1
b: uint256 = self.bar()
return i
Expand Down Expand Up @@ -140,12 +152,14 @@ def fivetimes(inp: bytes32) -> Bytes[160]:
assert c.sandwich(b"\x57" * 97, b"\x57" * 32) == b"\x57" * 161
assert c.fivetimes(b"mongoose" * 4) == b"mongoose" * 20

print("Passed concat bytes32 test")

@pytest.mark.parametrize('location', ["storage", "transient"])
def test_konkat_code(get_contract_with_gas_estimation, location):
if location == "transient" and not version_check(begin="cancun"):
pytest.skip("Skipping test as storage_location is 'transient' and EVM version is pre-Cancun")

def test_konkat_code(get_contract_with_gas_estimation):
konkat_code = """
ecks: bytes32
konkat_code = f"""
ecks: {wrap_typ_with_storage_loc("bytes32", location)}
@external
def foo(x: bytes32, y: bytes32) -> Bytes[64]:
Expand All @@ -167,8 +181,6 @@ def hoo(x: bytes32, y: bytes32) -> Bytes[64]:
assert c.goo(b"\x35" * 32, b"\x00" * 32) == b"\x35" * 32 + b"\x00" * 32
assert c.hoo(b"\x35" * 32, b"\x00" * 32) == b"\x35" * 32 + b"\x00" * 32

print("Passed second concat tests")


def test_small_output(get_contract_with_gas_estimation):
code = """
Expand Down
Loading

0 comments on commit 27a9824

Please sign in to comment.