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

Make ABI types fully specified #222

Merged
merged 23 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
292 changes: 176 additions & 116 deletions pyteal/ast/abi/array.py

Large diffs are not rendered by default.

335 changes: 168 additions & 167 deletions pyteal/ast/abi/array_test.py

Large diffs are not rendered by default.

65 changes: 42 additions & 23 deletions pyteal/ast/abi/bool.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union, cast, Sequence
from typing import TypeVar, Union, cast, Sequence, Callable, Type as PyType

from ...types import TealType
from ..expr import Expr
Expand All @@ -13,22 +13,27 @@


class Bool(Type):
def __init__(self) -> None:
super().__init__(TealType.uint64)
@classmethod
def has_same_type_as(cls, other: Union[PyType[Type], Type]) -> bool:
return other is Bool or type(other) is Bool

def new_instance(self) -> "Bool":
return Bool()

def has_same_type_as(self, other: Type) -> bool:
return type(other) is Bool

def is_dynamic(self) -> bool:
@classmethod
def is_dynamic(cls) -> bool:
return False

def byte_length_static(self) -> int:
@classmethod
def byte_length_static(cls) -> int:
# Not completely accurate since up to 8 consecutive bools will fit into a single byte
return 1

@classmethod
def storage_type(cls) -> TealType:
return TealType.uint64

@classmethod
def __str__(cls) -> str:
return "bool"

def get(self) -> Expr:
return self.stored_value.load()

Expand Down Expand Up @@ -68,33 +73,47 @@ def decodeBit(self, encoded, bitIndex: Expr) -> Expr:
def encode(self) -> Expr:
return SetBit(Bytes(b"\x00"), Int(0), self.get())

def __str__(self) -> str:
return "bool"


def boolAwareStaticByteLength(types: Sequence[Type]) -> int:
def boolAwareStaticByteLength(types: Sequence[PyType[Type]]) -> int:
length = 0
ignoreNext = 0
for i, t in enumerate(types):
if ignoreNext > 0:
ignoreNext -= 1
continue
if type(t) is Bool:
numBools = consecutiveBoolNum(types, i)
if t is Bool:
numBools = consecutiveBoolTypeNum(types, i)
ignoreNext = numBools - 1
length += boolSequenceLength(numBools)
continue
length += t.byte_length_static()
return length


def consecutiveBoolNum(types: Sequence[Type], startIndex: int) -> int:
numConsecutiveBools = 0
for t in types[startIndex:]:
if type(t) is not Bool:
T = TypeVar("T")


def consecutiveThingNum(
things: Sequence[T], startIndex: int, condition: Callable[[T], bool]
) -> int:
numConsecutiveThings = 0
for t in things[startIndex:]:
if not condition(t):
break
numConsecutiveBools += 1
return numConsecutiveBools
numConsecutiveThings += 1
return numConsecutiveThings


def consecutiveBoolTypeNum(types: Sequence[PyType[Type]], startIndex: int) -> int:
if len(types) != 0 and not issubclass(types[0], Type):
raise TypeError("Sequence of types expected")
return consecutiveThingNum(types, startIndex, lambda t: t is Bool)


def consecutiveBoolNum(values: Sequence[Type], startIndex: int) -> int:
if len(values) != 0 and not isinstance(values[0], Type):
raise TypeError("Sequence of types expected")
return consecutiveThingNum(values, startIndex, lambda t: type(t) is Bool)


def boolSequenceLength(num_bools: int) -> int:
Expand Down
127 changes: 63 additions & 64 deletions pyteal/ast/abi/bool_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from typing import NamedTuple, List
from typing import NamedTuple, List, Type
import pytest

from ... import *
from .bool import (
boolAwareStaticByteLength,
consecutiveBoolNum,
consecutiveBoolTypeNum,
boolSequenceLength,
encodeBoolSequence,
)
Expand All @@ -21,39 +22,34 @@ def test_Bool_str():


def test_Bool_is_dynamic():
boolType = abi.Bool()
assert not boolType.is_dynamic()
assert not abi.Bool.is_dynamic()


def test_Bool_has_same_type_as():
boolType = abi.Bool()
assert boolType.has_same_type_as(abi.Bool())
assert abi.Bool.has_same_type_as(abi.Bool)
assert abi.Bool.has_same_type_as(abi.Bool())

for otherType in (
abi.Byte(),
abi.Uint64(),
abi.StaticArray(boolType, 1),
abi.DynamicArray(boolType),
abi.Byte,
abi.Uint64,
abi.StaticArray[abi.Bool, 1],
abi.DynamicArray[abi.Bool],
):
assert not boolType.has_same_type_as(otherType)


def test_Bool_new_instance():
boolType = abi.Bool()
assert type(boolType.new_instance()) is abi.Bool
assert not abi.Bool.has_same_type_as(otherType)
assert not abi.Bool.has_same_type_as(otherType())


def test_Bool_set_static():
boolType = abi.Bool()
for value in (True, False):
expr = boolType.set(value)
value = abi.Bool()
for value_to_set in (True, False):
expr = value.set(value_to_set)
assert expr.type_of() == TealType.none
assert not expr.has_return()

expected = TealSimpleBlock(
[
TealOp(None, Op.int, 1 if value else 0),
TealOp(None, Op.store, boolType.stored_value.slot),
TealOp(None, Op.int, 1 if value_to_set else 0),
TealOp(None, Op.store, value.stored_value.slot),
]
)

Expand All @@ -66,8 +62,8 @@ def test_Bool_set_static():


def test_Bool_set_expr():
boolType = abi.Bool()
expr = boolType.set(Int(0).Or(Int(1)))
value = abi.Bool()
expr = value.set(Int(0).Or(Int(1)))
assert expr.type_of() == TealType.none
assert not expr.has_return()

Expand All @@ -76,8 +72,8 @@ def test_Bool_set_expr():
TealOp(None, Op.int, 0),
TealOp(None, Op.int, 1),
TealOp(None, Op.logic_or),
TealOp(None, Op.store, boolType.stored_value.slot),
TealOp(None, Op.load, boolType.stored_value.slot),
TealOp(None, Op.store, value.stored_value.slot),
TealOp(None, Op.load, value.stored_value.slot),
TealOp(None, Op.int, 2),
TealOp(None, Op.lt),
TealOp(None, Op.assert_),
Expand All @@ -94,15 +90,15 @@ def test_Bool_set_expr():

def test_Bool_set_copy():
other = abi.Bool()
boolType = abi.Bool()
expr = boolType.set(other)
value = abi.Bool()
expr = value.set(other)
assert expr.type_of() == TealType.none
assert not expr.has_return()

expected = TealSimpleBlock(
[
TealOp(None, Op.load, other.stored_value.slot),
TealOp(None, Op.store, boolType.stored_value.slot),
TealOp(None, Op.store, value.stored_value.slot),
]
)

Expand All @@ -115,25 +111,25 @@ def test_Bool_set_copy():


def test_Bool_get():
boolType = abi.Bool()
expr = boolType.get()
value = abi.Bool()
expr = value.get()
assert expr.type_of() == TealType.uint64
assert not expr.has_return()

expected = TealSimpleBlock([TealOp(expr, Op.load, boolType.stored_value.slot)])
expected = TealSimpleBlock([TealOp(expr, Op.load, value.stored_value.slot)])

actual, _ = expr.__teal__(options)

assert actual == expected


def test_Bool_decode():
boolType = abi.Bool()
value = abi.Bool()
encoded = Bytes("encoded")
for startIndex in (None, Int(1)):
for endIndex in (None, Int(2)):
for length in (None, Int(3)):
expr = boolType.decode(
expr = value.decode(
encoded, startIndex=startIndex, endIndex=endIndex, length=length
)
assert expr.type_of() == TealType.none
Expand All @@ -146,7 +142,7 @@ def test_Bool_decode():
TealOp(None, Op.int, 8),
TealOp(None, Op.mul),
TealOp(None, Op.getbit),
TealOp(None, Op.store, boolType.stored_value.slot),
TealOp(None, Op.store, value.stored_value.slot),
]
)

Expand All @@ -159,10 +155,10 @@ def test_Bool_decode():


def test_Bool_decodeBit():
boolType = abi.Bool()
value = abi.Bool()
bitIndex = Int(17)
encoded = Bytes("encoded")
expr = boolType.decodeBit(encoded, bitIndex)
expr = value.decodeBit(encoded, bitIndex)
assert expr.type_of() == TealType.none
assert not expr.has_return()

Expand All @@ -171,7 +167,7 @@ def test_Bool_decodeBit():
TealOp(None, Op.byte, '"encoded"'),
TealOp(None, Op.int, 17),
TealOp(None, Op.getbit),
TealOp(None, Op.store, boolType.stored_value.slot),
TealOp(None, Op.store, value.stored_value.slot),
]
)

Expand All @@ -184,16 +180,16 @@ def test_Bool_decodeBit():


def test_Bool_encode():
boolType = abi.Bool()
expr = boolType.encode()
value = abi.Bool()
expr = value.encode()
assert expr.type_of() == TealType.bytes
assert not expr.has_return()

expected = TealSimpleBlock(
[
TealOp(None, Op.byte, "0x00"),
TealOp(None, Op.int, 0),
TealOp(None, Op.load, boolType.stored_value.slot),
TealOp(None, Op.load, value.stored_value.slot),
TealOp(None, Op.setbit),
]
)
Expand All @@ -208,25 +204,25 @@ def test_Bool_encode():

def test_boolAwareStaticByteLength():
class ByteLengthTest(NamedTuple):
types: List[abi.Type]
types: List[Type[abi.Type]]
expectedLength: int

tests: List[ByteLengthTest] = [
ByteLengthTest(types=[], expectedLength=0),
ByteLengthTest(types=[abi.Uint64()], expectedLength=8),
ByteLengthTest(types=[abi.Bool()], expectedLength=1),
ByteLengthTest(types=[abi.Bool()] * 8, expectedLength=1),
ByteLengthTest(types=[abi.Bool()] * 9, expectedLength=2),
ByteLengthTest(types=[abi.Bool()] * 16, expectedLength=2),
ByteLengthTest(types=[abi.Bool()] * 17, expectedLength=3),
ByteLengthTest(types=[abi.Bool()] * 100, expectedLength=13),
ByteLengthTest(types=[abi.Bool(), abi.Byte(), abi.Bool()], expectedLength=3),
ByteLengthTest(types=[abi.Uint64], expectedLength=8),
ByteLengthTest(types=[abi.Bool], expectedLength=1),
ByteLengthTest(types=[abi.Bool] * 8, expectedLength=1),
ByteLengthTest(types=[abi.Bool] * 9, expectedLength=2),
ByteLengthTest(types=[abi.Bool] * 16, expectedLength=2),
ByteLengthTest(types=[abi.Bool] * 17, expectedLength=3),
ByteLengthTest(types=[abi.Bool] * 100, expectedLength=13),
ByteLengthTest(types=[abi.Bool, abi.Byte, abi.Bool], expectedLength=3),
ByteLengthTest(
types=[abi.Bool(), abi.Bool(), abi.Byte(), abi.Bool(), abi.Bool()],
types=[abi.Bool, abi.Bool, abi.Byte, abi.Bool, abi.Bool],
expectedLength=3,
),
ByteLengthTest(
types=[abi.Bool()] * 16 + [abi.Byte(), abi.Bool(), abi.Bool()],
types=[abi.Bool] * 16 + [abi.Byte, abi.Bool, abi.Bool],
expectedLength=4,
),
]
Expand All @@ -238,38 +234,41 @@ class ByteLengthTest(NamedTuple):

def test_consecutiveBoolNum():
class ConsecutiveTest(NamedTuple):
types: List[abi.Type]
types: List[Type[abi.Type]]
start: int
expected: int

tests: List[ConsecutiveTest] = [
ConsecutiveTest(types=[], start=0, expected=0),
ConsecutiveTest(types=[abi.Uint16()], start=0, expected=0),
ConsecutiveTest(types=[abi.Bool()], start=0, expected=1),
ConsecutiveTest(types=[abi.Bool()], start=1, expected=0),
ConsecutiveTest(types=[abi.Bool(), abi.Bool()], start=0, expected=2),
ConsecutiveTest(types=[abi.Bool(), abi.Bool()], start=1, expected=1),
ConsecutiveTest(types=[abi.Bool(), abi.Bool()], start=2, expected=0),
ConsecutiveTest(types=[abi.Bool() for _ in range(10)], start=0, expected=10),
ConsecutiveTest(types=[abi.Uint16], start=0, expected=0),
ConsecutiveTest(types=[abi.Bool], start=0, expected=1),
ConsecutiveTest(types=[abi.Bool], start=1, expected=0),
ConsecutiveTest(types=[abi.Bool, abi.Bool], start=0, expected=2),
ConsecutiveTest(types=[abi.Bool, abi.Bool], start=1, expected=1),
ConsecutiveTest(types=[abi.Bool, abi.Bool], start=2, expected=0),
ConsecutiveTest(types=[abi.Bool for _ in range(10)], start=0, expected=10),
ConsecutiveTest(
types=[abi.Bool(), abi.Bool(), abi.Byte(), abi.Bool()], start=0, expected=2
types=[abi.Bool, abi.Bool, abi.Byte, abi.Bool], start=0, expected=2
),
ConsecutiveTest(
types=[abi.Bool(), abi.Bool(), abi.Byte(), abi.Bool()], start=2, expected=0
types=[abi.Bool, abi.Bool, abi.Byte, abi.Bool], start=2, expected=0
),
ConsecutiveTest(
types=[abi.Bool(), abi.Bool(), abi.Byte(), abi.Bool()], start=3, expected=1
types=[abi.Bool, abi.Bool, abi.Byte, abi.Bool], start=3, expected=1
),
ConsecutiveTest(
types=[abi.Byte(), abi.Bool(), abi.Bool(), abi.Byte()], start=0, expected=0
types=[abi.Byte, abi.Bool, abi.Bool, abi.Byte], start=0, expected=0
),
ConsecutiveTest(
types=[abi.Byte(), abi.Bool(), abi.Bool(), abi.Byte()], start=1, expected=2
types=[abi.Byte, abi.Bool, abi.Bool, abi.Byte], start=1, expected=2
),
]

for i, test in enumerate(tests):
actual = consecutiveBoolNum(test.types, test.start)
actual = consecutiveBoolTypeNum(test.types, test.start)
assert actual == test.expected, "Test at index {} failed".format(i)

actual = consecutiveBoolNum([t() for t in test.types], test.start)
assert actual == test.expected, "Test at index {} failed".format(i)


Expand Down
Loading