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

Check Expr length when converting to abi.Address #432

Merged
merged 2 commits into from
Jul 1, 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
13 changes: 12 additions & 1 deletion pyteal/ast/abi/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,8 +99,15 @@ 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),
Comment on lines +103 to +104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahangsu for correctness & efficiency it's necessary to only put value into the AST a single time -- if it appears multiple times and that expression has side effects, those side effects will happen multiple times.

That's why Uint and Bool store the value into the scratch slot, then load the slot to check whether it's valid or not. The same approach can be taken here, e.g. Seq(self.stored_value.store(value), Assert(Len(self.stored_value.load()) == Int(AddressLength.Bytes.value)))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot about the trick in Uint, but that is correct, I am changing the code-gen for Expr case.

oh snap, I just got it merged... probably hard push to feature/abi?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to change history, the follow up PR you made is a good approach

)
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(
Expand Down
6 changes: 6 additions & 0 deletions pyteal/ast/abi/address_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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),
]
Expand Down