Skip to content

Commit

Permalink
test: add unit tests for internal abi type construction (vyperlang#3662)
Browse files Browse the repository at this point in the history
specifically test some internal sanity checks/validation

---------

Co-authored-by: Charles Cooper <cooper.charles.m@gmail.com>
  • Loading branch information
iFrostizz and charles-cooper authored Nov 7, 2023
1 parent a87fb8c commit 806dd90
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
26 changes: 26 additions & 0 deletions tests/abi_types/test_invalid_abi_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from vyper.abi_types import (
ABI_Bytes,
ABI_BytesM,
ABI_DynamicArray,
ABI_FixedMxN,
ABI_GIntM,
ABI_String,
)
from vyper.exceptions import InvalidABIType

cases_invalid_types = [
(ABI_GIntM, ((0, False), (7, False), (300, True), (300, False))),
(ABI_FixedMxN, ((0, 0, False), (8, 0, False), (256, 81, True), (300, 80, False))),
(ABI_BytesM, ((0,), (33,), (-10,))),
(ABI_Bytes, ((-1,), (-69,))),
(ABI_DynamicArray, ((ABI_GIntM(256, False), -1), (ABI_String(256), -10))),
]


@pytest.mark.parametrize("typ,params_variants", cases_invalid_types)
def test_invalid_abi_types(assert_compile_failed, typ, params_variants):
# double parametrization cannot work because the 2nd dimension is variable
for params in params_variants:
assert_compile_failed(lambda: typ(*params), InvalidABIType)
16 changes: 8 additions & 8 deletions vyper/abi_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from vyper.exceptions import CompilerPanic
from vyper.exceptions import InvalidABIType
from vyper.utils import ceil32


Expand Down Expand Up @@ -69,7 +69,7 @@ def __repr__(self):
class ABI_GIntM(ABIType):
def __init__(self, m_bits, signed):
if not (0 < m_bits <= 256 and 0 == m_bits % 8):
raise CompilerPanic("Invalid M provided for GIntM")
raise InvalidABIType("Invalid M provided for GIntM")

self.m_bits = m_bits
self.signed = signed
Expand Down Expand Up @@ -117,9 +117,9 @@ def selector_name(self):
class ABI_FixedMxN(ABIType):
def __init__(self, m_bits, n_places, signed):
if not (0 < m_bits <= 256 and 0 == m_bits % 8):
raise CompilerPanic("Invalid M for FixedMxN")
raise InvalidABIType("Invalid M for FixedMxN")
if not (0 < n_places and n_places <= 80):
raise CompilerPanic("Invalid N for FixedMxN")
raise InvalidABIType("Invalid N for FixedMxN")

self.m_bits = m_bits
self.n_places = n_places
Expand All @@ -142,7 +142,7 @@ def is_complex_type(self):
class ABI_BytesM(ABIType):
def __init__(self, m_bytes):
if not 0 < m_bytes <= 32:
raise CompilerPanic("Invalid M for BytesM")
raise InvalidABIType("Invalid M for BytesM")

self.m_bytes = m_bytes

Expand Down Expand Up @@ -173,7 +173,7 @@ def selector_name(self):
class ABI_StaticArray(ABIType):
def __init__(self, subtyp, m_elems):
if not m_elems >= 0:
raise CompilerPanic("Invalid M")
raise InvalidABIType("Invalid M")

self.subtyp = subtyp
self.m_elems = m_elems
Expand All @@ -200,7 +200,7 @@ def is_complex_type(self):
class ABI_Bytes(ABIType):
def __init__(self, bytes_bound):
if not bytes_bound >= 0:
raise CompilerPanic("Negative bytes_bound provided to ABI_Bytes")
raise InvalidABIType("Negative bytes_bound provided to ABI_Bytes")

self.bytes_bound = bytes_bound

Expand Down Expand Up @@ -234,7 +234,7 @@ def selector_name(self):
class ABI_DynamicArray(ABIType):
def __init__(self, subtyp, elems_bound):
if not elems_bound >= 0:
raise CompilerPanic("Negative bound provided to DynamicArray")
raise InvalidABIType("Negative bound provided to DynamicArray")

self.subtyp = subtyp
self.elems_bound = elems_bound
Expand Down
4 changes: 4 additions & 0 deletions vyper/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,7 @@ class UnfoldableNode(VyperInternalException):

class TypeCheckFailure(VyperInternalException):
"""An issue was not caught during type checking that should have been."""


class InvalidABIType(VyperInternalException):
"""An internal routine constructed an invalid ABI type"""

0 comments on commit 806dd90

Please sign in to comment.