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

ABI Bytes from ABI Uint8 #194

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 20 additions & 4 deletions pyteal/ast/abi/uint.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ def __init__(
def new_instance(self) -> "Uint8":
return Uint8()

def set(self, value: Union[int, Expr, "Uint8"]) -> Expr:
def set(self, value: Union[int, Expr, "Uint8", "Byte"]) -> Expr:
checked = False
if type(value) is int:
if value >= 2 ** self.bit_size:
raise TealInputError("Value exceeds Uint8 maximum: {}".format(value))
raise TealInputError(
"Value exceeds {} maximum: {}".format(
self.__class__.__name__, value
)
)
value = Int(value)
checked = True

if type(value) is Uint8:
if type(value) is Uint8 or type(value) is Byte:
value = value.get()
checked = True

Expand Down Expand Up @@ -93,7 +97,19 @@ def encode(self) -> Expr:

Uint8.__module__ = "pyteal"

Byte = Uint8

class Byte(Uint8):
def __init__(self) -> None:
super().__init__()

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

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


Byte.__module__ = "pyteal"


class Uint16(Uint):
Expand Down
35 changes: 35 additions & 0 deletions pyteal/ast/abi/uint_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def test_Uint_bits():
def test_Uint_str():
for test in testData:
assert str(test.uintType) == "uint{}".format(test.expectedBits)
assert str(abi.Byte()) == "byte"


def test_Uint_is_dynamic():
Expand Down Expand Up @@ -251,3 +252,37 @@ def test_Uint_encode():

with TealComponent.Context.ignoreExprEquality():
assert actual == expected


def test_ByteUint8_set_error():
with pytest.raises(TealInputError) as uint8_err_msg:
abi.Uint8().set(256)
assert "Uint8" in uint8_err_msg.__str__()

with pytest.raises(TealInputError) as byte_err_msg:
abi.Byte().set(256)
assert "Byte" in byte_err_msg.__str__()


def test_ByteUint8_mutual_conversion():
for type_a, type_b in [(abi.Uint8, abi.Byte), (abi.Byte, abi.Uint8)]:
type_b_instance = type_b()
other = type_a()
expr = type_b_instance.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, type_b_instance.stored_value.slot),
]
)

actual, _ = expr.__teal__(options)
actual.addIncoming()
actual = TealBlock.NormalizeBlocks(actual)

with TealComponent.Context.ignoreExprEquality():
assert actual == expected