From 7d6eb2a9ae72d50a28ca6c7de878646f34434cbf Mon Sep 17 00:00:00 2001 From: Hang Su Date: Fri, 1 Jul 2022 11:28:36 -0400 Subject: [PATCH 1/4] minor fix --- pyteal/ast/abi/address.py | 9 ++++++++- pyteal/ast/abi/address_test.py | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pyteal/ast/abi/address.py b/pyteal/ast/abi/address.py index 1e8da1f0f..47c559225 100644 --- a/pyteal/ast/abi/address.py +++ b/pyteal/ast/abi/address.py @@ -4,7 +4,11 @@ from pyteal.errors import TealInputError +from pyteal.ast.assert_ import Assert from pyteal.ast.bytes import Bytes +from pyteal.ast.int import Int +from pyteal.ast.seq import Seq +from pyteal.ast.unaryexpr import Len from pyteal.ast.addr import Addr from pyteal.ast.abi.type import ComputedValue, BaseType from pyteal.ast.abi.array_static import StaticArray, StaticArrayTypeSpec @@ -95,7 +99,10 @@ def set( f"Got bytes with length {len(value)}, expected {AddressLength.Bytes}" ) case Expr(): - return self.stored_value.store(value) + return Seq( + Assert(Len(value) == Int(AddressLength.Bytes.value)), + self.stored_value.store(value), + ) case CollectionSequence(): return super().set(cast(Sequence[Byte], value)) diff --git a/pyteal/ast/abi/address_test.py b/pyteal/ast/abi/address_test.py index eb5ed4681..1b9f793f9 100644 --- a/pyteal/ast/abi/address_test.py +++ b/pyteal/ast/abi/address_test.py @@ -2,6 +2,7 @@ import pyteal as pt from pyteal import abi +from pyteal.ast.abi.address import AddressLength from pyteal.ast.abi.type_test import ContainerType from pyteal.ast.abi.util import substringForDecoding @@ -199,6 +200,11 @@ def test_Address_set_expr(): vts, _ = value_to_set.__teal__(options) expected = pt.TealSimpleBlock( [ + vts.ops[0], + pt.TealOp(None, pt.Op.len), + pt.TealOp(None, pt.Op.int, AddressLength.Bytes.value), + pt.TealOp(None, pt.Op.eq), + pt.TealOp(None, pt.Op.assert_), vts.ops[0], pt.TealOp(None, pt.Op.store, value.stored_value.slot), ] From 851932f96c5d34f003cc3e6b1f664b6f12f72c50 Mon Sep 17 00:00:00 2001 From: Hang Su Date: Fri, 1 Jul 2022 11:58:09 -0400 Subject: [PATCH 2/4] minor --- pyteal/ast/abi/address.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyteal/ast/abi/address.py b/pyteal/ast/abi/address.py index 47c559225..35a567a41 100644 --- a/pyteal/ast/abi/address.py +++ b/pyteal/ast/abi/address.py @@ -104,6 +104,10 @@ def set( self.stored_value.store(value), ) case CollectionSequence(): + if len(value) != AddressLength.Bytes: + raise TealInputError( + f"Got bytes with length {len(value)}, expected {AddressLength.Bytes}" + ) return super().set(cast(Sequence[Byte], value)) raise TealInputError( From 5b63af1458dadfb18b34d5bad033b14d8a794e7d Mon Sep 17 00:00:00 2001 From: Hang Su Date: Fri, 1 Jul 2022 12:41:46 -0400 Subject: [PATCH 3/4] per Jason comment --- pyteal/ast/abi/address.py | 2 +- pyteal/ast/abi/address_test.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pyteal/ast/abi/address.py b/pyteal/ast/abi/address.py index 35a567a41..ebf854806 100644 --- a/pyteal/ast/abi/address.py +++ b/pyteal/ast/abi/address.py @@ -100,8 +100,8 @@ def set( ) case Expr(): return Seq( - Assert(Len(value) == Int(AddressLength.Bytes.value)), self.stored_value.store(value), + Assert(Len(self.stored_value.load()) == Int(AddressLength.Bytes.value)), ) case CollectionSequence(): if len(value) != AddressLength.Bytes: diff --git a/pyteal/ast/abi/address_test.py b/pyteal/ast/abi/address_test.py index 1b9f793f9..823623f51 100644 --- a/pyteal/ast/abi/address_test.py +++ b/pyteal/ast/abi/address_test.py @@ -201,12 +201,12 @@ def test_Address_set_expr(): expected = pt.TealSimpleBlock( [ vts.ops[0], + pt.TealOp(None, pt.Op.store, value.stored_value.slot), + pt.TealOp(None, pt.Op.load, value.stored_value.slot), pt.TealOp(None, pt.Op.len), pt.TealOp(None, pt.Op.int, AddressLength.Bytes.value), pt.TealOp(None, pt.Op.eq), pt.TealOp(None, pt.Op.assert_), - vts.ops[0], - pt.TealOp(None, pt.Op.store, value.stored_value.slot), ] ) From 719acef5e161b2d38d14ec1d50d7060d6c4f6304 Mon Sep 17 00:00:00 2001 From: Hang Su Date: Fri, 1 Jul 2022 12:50:37 -0400 Subject: [PATCH 4/4] minor, formatting --- pyteal/ast/abi/address.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyteal/ast/abi/address.py b/pyteal/ast/abi/address.py index ebf854806..145b0f336 100644 --- a/pyteal/ast/abi/address.py +++ b/pyteal/ast/abi/address.py @@ -101,7 +101,9 @@ def set( case Expr(): return Seq( self.stored_value.store(value), - Assert(Len(self.stored_value.load()) == Int(AddressLength.Bytes.value)), + Assert( + Len(self.stored_value.load()) == Int(AddressLength.Bytes.value) + ), ) case CollectionSequence(): if len(value) != AddressLength.Bytes: