def test_StringTypeSpec_eq(): assert abi.StringTypeSpec() == abi.StringTypeSpec() for otherType in ( abi.ByteTypeSpec(), abi.StaticArrayTypeSpec(abi.ByteTypeSpec(), 1), abi.DynamicArrayTypeSpec(abi.Uint8TypeSpec()), abi.DynamicArrayTypeSpec(abi.ByteTypeSpec()), ): assert abi.StringTypeSpec() != otherType
def test_TupleTypeSpec_str(): assert str(abi.TupleTypeSpec()) == "()" assert str(abi.TupleTypeSpec(abi.TupleTypeSpec())) == "(())" assert str(abi.TupleTypeSpec(abi.TupleTypeSpec(), abi.TupleTypeSpec())) == "((),())" assert ( str( abi.TupleTypeSpec( abi.Uint64TypeSpec(), abi.Uint32TypeSpec(), abi.BoolTypeSpec() ) ) == "(uint64,uint32,bool)" ) assert ( str( abi.TupleTypeSpec( abi.BoolTypeSpec(), abi.Uint64TypeSpec(), abi.Uint32TypeSpec() ) ) == "(bool,uint64,uint32)" ) assert ( str( abi.TupleTypeSpec( abi.Uint16TypeSpec(), abi.DynamicArrayTypeSpec(abi.Uint8TypeSpec()) ) ) == "(uint16,uint8[])" )
def test_TupleTypeSpec_is_dynamic(): assert not abi.TupleTypeSpec().is_dynamic() assert not abi.TupleTypeSpec( abi.Uint64TypeSpec(), abi.Uint32TypeSpec(), abi.BoolTypeSpec() ).is_dynamic() assert abi.TupleTypeSpec( abi.Uint16TypeSpec(), abi.DynamicArrayTypeSpec(abi.Uint8TypeSpec()) ).is_dynamic()
def test_AddressTypeSpec_eq(): assert abi.AddressTypeSpec() == abi.AddressTypeSpec() for otherType in ( abi.ByteTypeSpec(), abi.StaticArrayTypeSpec(abi.ByteTypeSpec(), 32), abi.DynamicArrayTypeSpec(abi.ByteTypeSpec()), ): assert abi.AddressTypeSpec() != otherType
def test_BoolTypeSpec_eq(): assert abi.BoolTypeSpec() == abi.BoolTypeSpec() for otherType in ( abi.ByteTypeSpec, abi.Uint64TypeSpec, abi.StaticArrayTypeSpec(abi.BoolTypeSpec(), 1), abi.DynamicArrayTypeSpec(abi.BoolTypeSpec()), ): assert abi.BoolTypeSpec() != otherType
def test_ArrayElement_init(): dynamicArrayType = abi.DynamicArrayTypeSpec(abi.Uint64TypeSpec()) array = dynamicArrayType.new_instance() index = pt.Int(6) element = abi.ArrayElement(array, index) assert element.array is array assert element.index is index with pytest.raises(pt.TealTypeError): abi.ArrayElement(array, pt.Bytes("abc")) with pytest.raises(pt.TealTypeError): abi.ArrayElement(array, pt.Assert(index))
def test_TupleTypeSpec_byte_length_static(): assert abi.TupleTypeSpec().byte_length_static() == 0 assert abi.TupleTypeSpec(abi.TupleTypeSpec()).byte_length_static() == 0 assert ( abi.TupleTypeSpec(abi.TupleTypeSpec(), abi.TupleTypeSpec()).byte_length_static() == 0 ) assert ( abi.TupleTypeSpec( abi.Uint64TypeSpec(), abi.Uint32TypeSpec(), abi.BoolTypeSpec() ).byte_length_static() == 8 + 4 + 1 ) assert ( abi.TupleTypeSpec( abi.Uint64TypeSpec(), abi.Uint32TypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), ).byte_length_static() == 8 + 4 + 1 ) assert ( abi.TupleTypeSpec( abi.Uint64TypeSpec(), abi.Uint32TypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.BoolTypeSpec(), ).byte_length_static() == 8 + 4 + 2 ) with pytest.raises(ValueError): abi.TupleTypeSpec( abi.Uint16TypeSpec(), abi.DynamicArrayTypeSpec(abi.Uint8TypeSpec()) ).byte_length_static()
def test_UintTypeSpec_eq(): for i, test in enumerate(testData): assert test.uintType == test.uintType for j, otherTest in enumerate(testData): if i == j: continue assert test.uintType != otherTest.uintType for otherType in ( abi.BoolTypeSpec(), abi.StaticArrayTypeSpec(test.uintType, 1), abi.DynamicArrayTypeSpec(test.uintType), ): assert test.uintType != otherType assert abi.ByteTypeSpec() != abi.Uint8TypeSpec() assert abi.Uint8TypeSpec() != abi.ByteTypeSpec()
def test_ArrayElement_store_into(): for elementType in STATIC_TYPES + DYNAMIC_TYPES: staticArrayType = abi.StaticArrayTypeSpec(elementType, 100) staticArray = staticArrayType.new_instance() index = pt.Int(9) element = abi.ArrayElement(staticArray, index) output = elementType.new_instance() expr = element.store_into(output) encoded = staticArray.encode() stride = pt.Int(staticArray.type_spec()._stride()) expectedLength = staticArray.length() if elementType == abi.BoolTypeSpec(): expectedExpr = cast(abi.Bool, output).decode_bit(encoded, index) elif not elementType.is_dynamic(): expectedExpr = output.decode(encoded, start_index=stride * index, length=stride) else: expectedExpr = output.decode( encoded, start_index=pt.ExtractUint16(encoded, stride * index), end_index=pt.If(index + pt.Int(1) == expectedLength).Then( pt.Len(encoded)).Else( pt.ExtractUint16(encoded, stride * index + pt.Int(2))), ) expected, _ = expectedExpr.__teal__(options) expected.addIncoming() expected = pt.TealBlock.NormalizeBlocks(expected) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected with pytest.raises(pt.TealInputError): element.store_into(abi.Tuple(abi.TupleTypeSpec(elementType))) for elementType in STATIC_TYPES + DYNAMIC_TYPES: dynamicArrayType = abi.DynamicArrayTypeSpec(elementType) dynamicArray = dynamicArrayType.new_instance() index = pt.Int(9) element = abi.ArrayElement(dynamicArray, index) output = elementType.new_instance() expr = element.store_into(output) encoded = dynamicArray.encode() stride = pt.Int(dynamicArray.type_spec()._stride()) expectedLength = dynamicArray.length() if elementType == abi.BoolTypeSpec(): expectedExpr = cast(abi.Bool, output).decode_bit(encoded, index + pt.Int(16)) elif not elementType.is_dynamic(): expectedExpr = output.decode(encoded, start_index=stride * index + pt.Int(2), length=stride) else: expectedExpr = output.decode( encoded, start_index=pt.ExtractUint16( encoded, stride * index + pt.Int(2)) + pt.Int(2), end_index=pt.If(index + pt.Int(1) == expectedLength).Then( pt.Len(encoded)).Else( pt.ExtractUint16( encoded, stride * index + pt.Int(2) + pt.Int(2)) + pt.Int(2)), ) expected, _ = expectedExpr.__teal__(options) expected.addIncoming() expected = pt.TealBlock.NormalizeBlocks(expected) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): with pt.TealComponent.Context.ignoreScratchSlotEquality(): assert actual == expected assert pt.TealBlock.MatchScratchSlotReferences( pt.TealBlock.GetReferencedScratchSlots(actual), pt.TealBlock.GetReferencedScratchSlots(expected), ) with pytest.raises(pt.TealInputError): element.store_into(abi.Tuple(abi.TupleTypeSpec(elementType)))
abi.TupleTypeSpec(abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.Uint64TypeSpec()), abi.StaticArrayTypeSpec(abi.BoolTypeSpec(), 10), abi.StaticArrayTypeSpec(abi.Uint8TypeSpec(), 10), abi.StaticArrayTypeSpec(abi.Uint16TypeSpec(), 10), abi.StaticArrayTypeSpec(abi.Uint32TypeSpec(), 10), abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(), 10), abi.StaticArrayTypeSpec( abi.TupleTypeSpec(abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.Uint64TypeSpec()), 10, ), ] DYNAMIC_TYPES: List[abi.TypeSpec] = [ abi.DynamicArrayTypeSpec(abi.BoolTypeSpec()), abi.DynamicArrayTypeSpec(abi.Uint8TypeSpec()), abi.DynamicArrayTypeSpec(abi.Uint16TypeSpec()), abi.DynamicArrayTypeSpec(abi.Uint32TypeSpec()), abi.DynamicArrayTypeSpec(abi.Uint64TypeSpec()), abi.DynamicArrayTypeSpec(abi.TupleTypeSpec()), abi.DynamicArrayTypeSpec( abi.TupleTypeSpec(abi.BoolTypeSpec(), abi.BoolTypeSpec(), abi.Uint64TypeSpec())), abi.DynamicArrayTypeSpec(abi.StaticArrayTypeSpec(abi.BoolTypeSpec(), 10)), abi.DynamicArrayTypeSpec(abi.StaticArrayTypeSpec(abi.Uint8TypeSpec(), 10)), abi.DynamicArrayTypeSpec(abi.StaticArrayTypeSpec(abi.Uint16TypeSpec(), 10)), abi.DynamicArrayTypeSpec(abi.StaticArrayTypeSpec(abi.Uint32TypeSpec(), 10)), abi.DynamicArrayTypeSpec(abi.StaticArrayTypeSpec(abi.Uint64TypeSpec(),
def test_indexTuple(): class IndexTest(NamedTuple): types: List[abi.TypeSpec] typeIndex: int expected: Callable[[abi.BaseType], pt.Expr] # variables used to construct the tests uint64_t = abi.Uint64TypeSpec() byte_t = abi.ByteTypeSpec() bool_t = abi.BoolTypeSpec() tuple_t = abi.TupleTypeSpec(abi.BoolTypeSpec(), abi.BoolTypeSpec()) dynamic_array_t1 = abi.DynamicArrayTypeSpec(abi.Uint64TypeSpec()) dynamic_array_t2 = abi.DynamicArrayTypeSpec(abi.Uint16TypeSpec()) encoded = pt.Bytes("encoded") tests: List[IndexTest] = [ IndexTest( types=[uint64_t], typeIndex=0, expected=lambda output: output.decode(encoded), ), IndexTest( types=[uint64_t, uint64_t], typeIndex=0, expected=lambda output: output.decode(encoded, length=pt.Int(8)), ), IndexTest( types=[uint64_t, uint64_t], typeIndex=1, expected=lambda output: output.decode(encoded, start_index=pt.Int(8)), ), IndexTest( types=[uint64_t, byte_t, uint64_t], typeIndex=1, expected=lambda output: output.decode( encoded, start_index=pt.Int(8), length=pt.Int(1) ), ), IndexTest( types=[uint64_t, byte_t, uint64_t], typeIndex=2, expected=lambda output: output.decode( encoded, start_index=pt.Int(9), length=pt.Int(8) ), ), IndexTest( types=[bool_t], typeIndex=0, expected=lambda output: output.decode_bit(encoded, pt.Int(0)), ), IndexTest( types=[bool_t, bool_t], typeIndex=0, expected=lambda output: output.decode_bit(encoded, pt.Int(0)), ), IndexTest( types=[bool_t, bool_t], typeIndex=1, expected=lambda output: output.decode_bit(encoded, pt.Int(1)), ), IndexTest( types=[uint64_t, bool_t], typeIndex=1, expected=lambda output: output.decode_bit(encoded, pt.Int(8 * 8)), ), IndexTest( types=[uint64_t, bool_t, bool_t], typeIndex=1, expected=lambda output: output.decode_bit(encoded, pt.Int(8 * 8)), ), IndexTest( types=[uint64_t, bool_t, bool_t], typeIndex=2, expected=lambda output: output.decode_bit(encoded, pt.Int(8 * 8 + 1)), ), IndexTest( types=[bool_t, uint64_t], typeIndex=0, expected=lambda output: output.decode_bit(encoded, pt.Int(0)), ), IndexTest( types=[bool_t, uint64_t], typeIndex=1, expected=lambda output: output.decode(encoded, start_index=pt.Int(1)), ), IndexTest( types=[bool_t, bool_t, uint64_t], typeIndex=0, expected=lambda output: output.decode_bit(encoded, pt.Int(0)), ), IndexTest( types=[bool_t, bool_t, uint64_t], typeIndex=1, expected=lambda output: output.decode_bit(encoded, pt.Int(1)), ), IndexTest( types=[bool_t, bool_t, uint64_t], typeIndex=2, expected=lambda output: output.decode(encoded, start_index=pt.Int(1)), ), IndexTest( types=[tuple_t], typeIndex=0, expected=lambda output: output.decode(encoded) ), IndexTest( types=[byte_t, tuple_t], typeIndex=1, expected=lambda output: output.decode(encoded, start_index=pt.Int(1)), ), IndexTest( types=[tuple_t, byte_t], typeIndex=0, expected=lambda output: output.decode( encoded, start_index=pt.Int(0), length=pt.Int(tuple_t.byte_length_static()), ), ), IndexTest( types=[byte_t, tuple_t, byte_t], typeIndex=1, expected=lambda output: output.decode( encoded, start_index=pt.Int(1), length=pt.Int(tuple_t.byte_length_static()), ), ), IndexTest( types=[dynamic_array_t1], typeIndex=0, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(0)) ), ), IndexTest( types=[byte_t, dynamic_array_t1], typeIndex=1, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(1)) ), ), IndexTest( types=[dynamic_array_t1, byte_t], typeIndex=0, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(0)) ), ), IndexTest( types=[byte_t, dynamic_array_t1, byte_t], typeIndex=1, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(1)) ), ), IndexTest( types=[byte_t, dynamic_array_t1, byte_t, dynamic_array_t2], typeIndex=1, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(1)), end_index=pt.ExtractUint16(encoded, pt.Int(4)), ), ), IndexTest( types=[byte_t, dynamic_array_t1, byte_t, dynamic_array_t2], typeIndex=3, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(4)) ), ), IndexTest( types=[byte_t, dynamic_array_t1, tuple_t, dynamic_array_t2], typeIndex=1, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(1)), end_index=pt.ExtractUint16(encoded, pt.Int(4)), ), ), IndexTest( types=[byte_t, dynamic_array_t1, tuple_t, dynamic_array_t2], typeIndex=3, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(4)) ), ), IndexTest( types=[byte_t, dynamic_array_t2, bool_t, bool_t, dynamic_array_t2], typeIndex=1, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(1)), end_index=pt.ExtractUint16(encoded, pt.Int(4)), ), ), IndexTest( types=[byte_t, dynamic_array_t1, bool_t, bool_t, dynamic_array_t2], typeIndex=4, expected=lambda output: output.decode( encoded, start_index=pt.ExtractUint16(encoded, pt.Int(4)) ), ), ] for i, test in enumerate(tests): output = test.types[test.typeIndex].new_instance() expr = _index_tuple(test.types, encoded, test.typeIndex, output) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected, _ = test.expected(output).__teal__(options) expected.addIncoming() expected = pt.TealBlock.NormalizeBlocks(expected) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected, "Test at index {} failed".format(i) with pytest.raises(ValueError): _index_tuple(test.types, encoded, len(test.types), output) with pytest.raises(ValueError): _index_tuple(test.types, encoded, -1, output) otherType = abi.Uint64() if output.type_spec() == otherType.type_spec(): otherType = abi.Uint16() with pytest.raises(TypeError): _index_tuple(test.types, encoded, test.typeIndex, otherType)
def test_encodeTuple(): class EncodeTest(NamedTuple): types: List[abi.BaseType] expected: pt.Expr # variables used to construct the tests uint64_a = abi.Uint64() uint64_b = abi.Uint64() uint16_a = abi.Uint16() uint16_b = abi.Uint16() bool_a = abi.Bool() bool_b = abi.Bool() tuple_a = abi.Tuple(abi.TupleTypeSpec(abi.BoolTypeSpec(), abi.BoolTypeSpec())) dynamic_array_a = abi.DynamicArray(abi.DynamicArrayTypeSpec(abi.Uint64TypeSpec())) dynamic_array_b = abi.DynamicArray(abi.DynamicArrayTypeSpec(abi.Uint16TypeSpec())) dynamic_array_c = abi.DynamicArray(abi.DynamicArrayTypeSpec(abi.BoolTypeSpec())) tail_holder = pt.ScratchVar() encoded_tail = pt.ScratchVar() tests: List[EncodeTest] = [ EncodeTest(types=[], expected=pt.Bytes("")), EncodeTest(types=[uint64_a], expected=uint64_a.encode()), EncodeTest( types=[uint64_a, uint64_b], expected=pt.Concat(uint64_a.encode(), uint64_b.encode()), ), EncodeTest(types=[bool_a], expected=bool_a.encode()), EncodeTest( types=[bool_a, bool_b], expected=_encode_bool_sequence([bool_a, bool_b]) ), EncodeTest( types=[bool_a, bool_b, uint64_a], expected=pt.Concat( _encode_bool_sequence([bool_a, bool_b]), uint64_a.encode() ), ), EncodeTest( types=[uint64_a, bool_a, bool_b], expected=pt.Concat( uint64_a.encode(), _encode_bool_sequence([bool_a, bool_b]) ), ), EncodeTest( types=[uint64_a, bool_a, bool_b, uint64_b], expected=pt.Concat( uint64_a.encode(), _encode_bool_sequence([bool_a, bool_b]), uint64_b.encode(), ), ), EncodeTest( types=[uint64_a, bool_a, uint64_b, bool_b], expected=pt.Concat( uint64_a.encode(), bool_a.encode(), uint64_b.encode(), bool_b.encode() ), ), EncodeTest(types=[tuple_a], expected=tuple_a.encode()), EncodeTest( types=[uint64_a, tuple_a, bool_a, bool_b], expected=pt.Concat( uint64_a.encode(), tuple_a.encode(), _encode_bool_sequence([bool_a, bool_b]), ), ), EncodeTest( types=[dynamic_array_a], expected=pt.Concat( pt.Seq( encoded_tail.store(dynamic_array_a.encode()), tail_holder.store(encoded_tail.load()), uint16_a.set(2), uint16_a.encode(), ), tail_holder.load(), ), ), EncodeTest( types=[uint64_a, dynamic_array_a], expected=pt.Concat( uint64_a.encode(), pt.Seq( encoded_tail.store(dynamic_array_a.encode()), tail_holder.store(encoded_tail.load()), uint16_a.set(8 + 2), uint16_a.encode(), ), tail_holder.load(), ), ), EncodeTest( types=[uint64_a, dynamic_array_a, uint64_b], expected=pt.Concat( uint64_a.encode(), pt.Seq( encoded_tail.store(dynamic_array_a.encode()), tail_holder.store(encoded_tail.load()), uint16_a.set(8 + 2 + 8), uint16_a.encode(), ), uint64_b.encode(), tail_holder.load(), ), ), EncodeTest( types=[uint64_a, dynamic_array_a, bool_a, bool_b], expected=pt.Concat( uint64_a.encode(), pt.Seq( encoded_tail.store(dynamic_array_a.encode()), tail_holder.store(encoded_tail.load()), uint16_a.set(8 + 2 + 1), uint16_a.encode(), ), _encode_bool_sequence([bool_a, bool_b]), tail_holder.load(), ), ), EncodeTest( types=[uint64_a, dynamic_array_a, uint64_b, dynamic_array_b], expected=pt.Concat( uint64_a.encode(), pt.Seq( encoded_tail.store(dynamic_array_a.encode()), tail_holder.store(encoded_tail.load()), uint16_a.set(8 + 2 + 8 + 2), uint16_b.set(uint16_a.get() + pt.Len(encoded_tail.load())), uint16_a.encode(), ), uint64_b.encode(), pt.Seq( encoded_tail.store(dynamic_array_b.encode()), tail_holder.store( pt.Concat(tail_holder.load(), encoded_tail.load()) ), uint16_a.set(uint16_b), uint16_a.encode(), ), tail_holder.load(), ), ), EncodeTest( types=[ uint64_a, dynamic_array_a, uint64_b, dynamic_array_b, bool_a, bool_b, dynamic_array_c, ], expected=pt.Concat( uint64_a.encode(), pt.Seq( encoded_tail.store(dynamic_array_a.encode()), tail_holder.store(encoded_tail.load()), uint16_a.set(8 + 2 + 8 + 2 + 1 + 2), uint16_b.set(uint16_a.get() + pt.Len(encoded_tail.load())), uint16_a.encode(), ), uint64_b.encode(), pt.Seq( encoded_tail.store(dynamic_array_b.encode()), tail_holder.store( pt.Concat(tail_holder.load(), encoded_tail.load()) ), uint16_a.set(uint16_b), uint16_b.set(uint16_a.get() + pt.Len(encoded_tail.load())), uint16_a.encode(), ), _encode_bool_sequence([bool_a, bool_b]), pt.Seq( encoded_tail.store(dynamic_array_c.encode()), tail_holder.store( pt.Concat(tail_holder.load(), encoded_tail.load()) ), uint16_a.set(uint16_b), uint16_a.encode(), ), tail_holder.load(), ), ), ] for i, test in enumerate(tests): expr = _encode_tuple(test.types) assert expr.type_of() == pt.TealType.bytes assert not expr.has_return() expected, _ = test.expected.__teal__(options) expected.addIncoming() expected = pt.TealBlock.NormalizeBlocks(expected) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) if any(t.type_spec().is_dynamic() for t in test.types): with pt.TealComponent.Context.ignoreExprEquality(): with pt.TealComponent.Context.ignoreScratchSlotEquality(): assert actual == expected, "Test at index {} failed".format(i) assert pt.TealBlock.MatchScratchSlotReferences( pt.TealBlock.GetReferencedScratchSlots(actual), pt.TealBlock.GetReferencedScratchSlots(expected), ) continue with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected, "Test at index {} failed".format(i)