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

Consistently use start_index and end_index #436

Merged
merged 2 commits into from
Jul 11, 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
17 changes: 10 additions & 7 deletions pyteal/ast/abi/address_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,33 @@ def test_Address_decode():
address = bytes([0] * abi.AddressLength.Bytes)
encoded = pt.Bytes(address)

for startIndex in (None, pt.Int(0)):
for endIndex in (None, pt.Int(1)):
for start_index in (None, pt.Int(0)):
for end_index in (None, pt.Int(1)):
for length in (None, pt.Int(2)):
value = abi.Address()

if endIndex is not None and length is not None:
if end_index is not None and length is not None:
with pytest.raises(pt.TealInputError):
value.decode(
encoded,
start_index=startIndex,
end_index=endIndex,
start_index=start_index,
end_index=end_index,
length=length,
)
continue

expr = value.decode(
encoded, start_index=startIndex, end_index=endIndex, length=length
encoded, start_index=start_index, end_index=end_index, length=length
)
assert expr.type_of() == pt.TealType.none
assert expr.has_return() is False

expectedExpr = value.stored_value.store(
substringForDecoding(
encoded, startIndex=startIndex, endIndex=endIndex, length=length
encoded,
start_index=start_index,
end_index=end_index,
length=length,
)
)
expected, _ = expectedExpr.__teal__(options)
Expand Down
2 changes: 1 addition & 1 deletion pyteal/ast/abi/array_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def decode(
the scratch variable.
"""
extracted = substringForDecoding(
encoded, startIndex=start_index, endIndex=end_index, length=length
encoded, start_index=start_index, end_index=end_index, length=length
)
return self.stored_value.store(extracted)

Expand Down
17 changes: 10 additions & 7 deletions pyteal/ast/abi/array_dynamic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,33 @@ def test_DynamicArrayTypeSpec_byte_length_static():
def test_DynamicArray_decode():
encoded = pt.Bytes("encoded")
dynamicArrayType = abi.DynamicArrayTypeSpec(abi.Uint64TypeSpec())
for startIndex in (None, pt.Int(1)):
for endIndex in (None, pt.Int(2)):
for start_index in (None, pt.Int(1)):
for end_index in (None, pt.Int(2)):
for length in (None, pt.Int(3)):
value = dynamicArrayType.new_instance()

if endIndex is not None and length is not None:
if end_index is not None and length is not None:
with pytest.raises(pt.TealInputError):
value.decode(
encoded,
start_index=startIndex,
end_index=endIndex,
start_index=start_index,
end_index=end_index,
length=length,
)
continue

expr = value.decode(
encoded, start_index=startIndex, end_index=endIndex, length=length
encoded, start_index=start_index, end_index=end_index, length=length
)
assert expr.type_of() == pt.TealType.none
assert not expr.has_return()

expectedExpr = value.stored_value.store(
substringForDecoding(
encoded, startIndex=startIndex, endIndex=endIndex, length=length
encoded,
start_index=start_index,
end_index=end_index,
length=length,
)
)
expected, _ = expectedExpr.__teal__(options)
Expand Down
17 changes: 10 additions & 7 deletions pyteal/ast/abi/array_static_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,35 @@ def test_StaticArrayTypeSpec_byte_length_static():

def test_StaticArray_decode():
encoded = pt.Bytes("encoded")
for startIndex in (None, pt.Int(1)):
for endIndex in (None, pt.Int(2)):
for start_index in (None, pt.Int(1)):
for end_index in (None, pt.Int(2)):
for length in (None, pt.Int(3)):
value = abi.StaticArray(
abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 10)
)

if endIndex is not None and length is not None:
if end_index is not None and length is not None:
with pytest.raises(pt.TealInputError):
value.decode(
encoded,
start_index=startIndex,
end_index=endIndex,
start_index=start_index,
end_index=end_index,
length=length,
)
continue

expr = value.decode(
encoded, start_index=startIndex, end_index=endIndex, length=length
encoded, start_index=start_index, end_index=end_index, length=length
)
assert expr.type_of() == pt.TealType.none
assert not expr.has_return()

expectedExpr = value.stored_value.store(
substringForDecoding(
encoded, startIndex=startIndex, endIndex=endIndex, length=length
encoded,
start_index=start_index,
end_index=end_index,
length=length,
)
)
expected, _ = expectedExpr.__teal__(options)
Expand Down
12 changes: 6 additions & 6 deletions pyteal/ast/abi/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,29 @@ def boolAwareStaticByteLength(types: Sequence[TypeSpec]) -> int:


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


def consecutiveBoolTypeSpecNum(types: Sequence[TypeSpec], startIndex: int) -> int:
def consecutiveBoolTypeSpecNum(types: Sequence[TypeSpec], start_index: int) -> int:
if len(types) != 0 and not isinstance(types[0], TypeSpec):
raise TypeError("Sequence of types expected")
return consecutiveThingNum(types, startIndex, lambda t: t == BoolTypeSpec())
return consecutiveThingNum(types, start_index, lambda t: t == BoolTypeSpec())


def consecutiveBoolInstanceNum(values: Sequence[BaseType], startIndex: int) -> int:
def consecutiveBoolInstanceNum(values: Sequence[BaseType], start_index: int) -> int:
if len(values) != 0 and not isinstance(values[0], BaseType):
raise TypeError(
"Sequence of types expected, but got {}".format(type(values[0]))
)
return consecutiveThingNum(
values, startIndex, lambda t: t.type_spec() == BoolTypeSpec()
values, start_index, lambda t: t.type_spec() == BoolTypeSpec()
)


Expand Down
8 changes: 4 additions & 4 deletions pyteal/ast/abi/bool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,19 @@ def test_Bool_get():
def test_Bool_decode():
value = abi.Bool()
encoded = pt.Bytes("encoded")
for startIndex in (None, pt.Int(1)):
for endIndex in (None, pt.Int(2)):
for start_index in (None, pt.Int(1)):
for end_index in (None, pt.Int(2)):
for length in (None, pt.Int(3)):
expr = value.decode(
encoded, start_index=startIndex, end_index=endIndex, length=length
encoded, start_index=start_index, end_index=end_index, length=length
)
assert expr.type_of() == pt.TealType.none
assert not expr.has_return()

expected = pt.TealSimpleBlock(
[
pt.TealOp(None, pt.Op.byte, '"encoded"'),
pt.TealOp(None, pt.Op.int, 0 if startIndex is None else 1),
pt.TealOp(None, pt.Op.int, 0 if start_index is None else 1),
pt.TealOp(None, pt.Op.int, 8),
pt.TealOp(None, pt.Op.mul),
pt.TealOp(None, pt.Op.getbit),
Expand Down
11 changes: 6 additions & 5 deletions pyteal/ast/abi/reference_type_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,22 @@ def test_ReferenceType_encode():
def test_ReferenceType_decode():
encoded = pt.Bytes("encoded")
for value in (abi.Account(), abi.Asset(), abi.Application()):
for startIndex in (None, pt.Int(1)):
for endIndex in (None, pt.Int(2)):
for start_index in (None, pt.Int(1)):
for end_index in (None, pt.Int(2)):
for length in (None, pt.Int(3)):
expr = value.decode(
encoded,
start_index=startIndex,
end_index=endIndex,
start_index=start_index,
end_index=end_index,
length=length,
)
assert expr.type_of() == pt.TealType.none
assert expr.has_return() is False

expected_decoding = value.stored_value.store(
pt.GetByte(
encoded, startIndex if startIndex is not None else pt.Int(0)
encoded,
start_index if start_index is not None else pt.Int(0),
)
)
expected, _ = expected_decoding.__teal__(options)
Expand Down
17 changes: 10 additions & 7 deletions pyteal/ast/abi/string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,33 @@ def test_String_encode():
def test_DynamicArray_decode():
encoded = pt.Bytes("encoded")
stringType = abi.StringTypeSpec()
for startIndex in (None, pt.Int(1)):
for endIndex in (None, pt.Int(2)):
for start_index in (None, pt.Int(1)):
for end_index in (None, pt.Int(2)):
for length in (None, pt.Int(3)):
value = stringType.new_instance()

if endIndex is not None and length is not None:
if end_index is not None and length is not None:
with pytest.raises(pt.TealInputError):
value.decode(
encoded,
start_index=startIndex,
end_index=endIndex,
start_index=start_index,
end_index=end_index,
length=length,
)
continue

expr = value.decode(
encoded, start_index=startIndex, end_index=endIndex, length=length
encoded, start_index=start_index, end_index=end_index, length=length
)
assert expr.type_of() == pt.TealType.none
assert expr.has_return() is False

expectedExpr = value.stored_value.store(
substringForDecoding(
encoded, startIndex=startIndex, endIndex=endIndex, length=length
encoded,
start_index=start_index,
end_index=end_index,
length=length,
)
)
expected, _ = expectedExpr.__teal__(options)
Expand Down
26 changes: 13 additions & 13 deletions pyteal/ast/abi/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,34 +174,34 @@ def indexTuple(

nextDynamicValueOffset += typeAfter.byte_length_static()

startIndex = ExtractUint16(encoded, Int(offset))
start_index = ExtractUint16(encoded, Int(offset))
if not hasNextDynamicValue:
# This is the final dynamic value, so decode the substring from startIndex to the end of
# This is the final dynamic value, so decode the substring from start_index to the end of
# encoded
return output.decode(encoded, start_index=startIndex)
return output.decode(encoded, start_index=start_index)

# There is a dynamic value after this one, and endIndex is where its tail starts, so decode
# the substring from startIndex to endIndex
endIndex = ExtractUint16(encoded, Int(nextDynamicValueOffset))
return output.decode(encoded, start_index=startIndex, end_index=endIndex)
# There is a dynamic value after this one, and end_index is where its tail starts, so decode
# the substring from start_index to end_index
end_index = ExtractUint16(encoded, Int(nextDynamicValueOffset))
return output.decode(encoded, start_index=start_index, end_index=end_index)

startIndex = Int(offset)
start_index = Int(offset)
length = Int(valueType.byte_length_static())

if index + 1 == len(valueTypes):
if offset == 0:
# This is the first and only value in the tuple, so decode all of encoded
return output.decode(encoded)
# This is the last value in the tuple, so decode the substring from startIndex to the end of
# This is the last value in the tuple, so decode the substring from start_index to the end of
# encoded
return output.decode(encoded, start_index=startIndex)
return output.decode(encoded, start_index=start_index)

if offset == 0:
# This is the first value in the tuple, so decode the substring from 0 with length length
return output.decode(encoded, length=length)

# This is not the first or last value, so decode the substring from startIndex with length length
return output.decode(encoded, start_index=startIndex, length=length)
# This is not the first or last value, so decode the substring from start_index with length length
return output.decode(encoded, start_index=start_index, length=length)


class TupleTypeSpec(TypeSpec):
Expand Down Expand Up @@ -289,7 +289,7 @@ def decode(
length: Expr = None,
) -> Expr:
extracted = substringForDecoding(
encoded, startIndex=start_index, endIndex=end_index, length=length
encoded, start_index=start_index, end_index=end_index, length=length
)
return self.stored_value.store(extracted)

Expand Down
17 changes: 10 additions & 7 deletions pyteal/ast/abi/tuple_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,28 +600,31 @@ def test_TupleTypeSpec_byte_length_static():
def test_Tuple_decode():
encoded = pt.Bytes("encoded")
tupleValue = abi.Tuple(abi.TupleTypeSpec(abi.Uint64TypeSpec()))
for startIndex in (None, pt.Int(1)):
for endIndex in (None, pt.Int(2)):
for start_index in (None, pt.Int(1)):
for end_index in (None, pt.Int(2)):
for length in (None, pt.Int(3)):
if endIndex is not None and length is not None:
if end_index is not None and length is not None:
with pytest.raises(pt.TealInputError):
tupleValue.decode(
encoded,
start_index=startIndex,
end_index=endIndex,
start_index=start_index,
end_index=end_index,
length=length,
)
continue

expr = tupleValue.decode(
encoded, start_index=startIndex, end_index=endIndex, length=length
encoded, start_index=start_index, end_index=end_index, length=length
)
assert expr.type_of() == pt.TealType.none
assert not expr.has_return()

expectedExpr = tupleValue.stored_value.store(
substringForDecoding(
encoded, startIndex=startIndex, endIndex=endIndex, length=length
encoded,
start_index=start_index,
end_index=end_index,
length=length,
)
)
expected, _ = expectedExpr.__teal__(options)
Expand Down
22 changes: 11 additions & 11 deletions pyteal/ast/abi/type.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,18 @@ def decode(
The arguments to this function are means to be as flexible as possible for the caller.
Multiple types of substrings can be specified based on the arguments, as listed below:

* Entire string: if startIndex, endIndex, and length are all None, the entire encoded string
* Entire string: if start_index, end_index, and length are all None, the entire encoded string
is decoded.
* Prefix: if startIndex is None and one of endIndex or length is provided, a prefix of the
encoded string is decoded. The range is 0 through endIndex or length (they are equivalent).
* Suffix: if startIndex is provided and endIndex and length are None, a suffix of the encoded
string is decoded. The range is startIndex through the end of the string.
* Substring specified with endIndex: if startIndex and endIndex are provided and length is
None, a substring of the encoded string is decoded. The range is startIndex through
endIndex.
* Substring specified with length: if startIndex and length are provided and endIndex is
None, a substring of the encoded string is decoded. The range is startIndex through
startIndex+length.
* Prefix: if start_index is None and one of end_index or length is provided, a prefix of the
encoded string is decoded. The range is 0 through end_index or length (they are equivalent).
* Suffix: if start_index is provided and end_index and length are None, a suffix of the encoded
string is decoded. The range is start_index through the end of the string.
* Substring specified with end_index: if start_index and end_index are provided and length is
None, a substring of the encoded string is decoded. The range is start_index through
end_index.
* Substring specified with length: if start_index and length are provided and end_index is
None, a substring of the encoded string is decoded. The range is start_index through
start_index+length.

Args:
encoded: An expression containing the bytes to decode. Must evaluate to TealType.bytes.
Expand Down
Loading