def test_Bool_decode(): value = abi.Bool() encoded = pt.Bytes("encoded") 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=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 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), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def named_tuple_field_access( a_0: abi.Bool, a_1: abi.Address, a_2: abi.Tuple2[abi.Uint64, abi.Bool], a_3: abi.StaticArray[abi.Byte, L[10]], a_4: abi.StaticArray[abi.Bool, L[4]], a_5: abi.Uint64, ): return Seq( (v_tuple := NamedTupleExample()).set(a_0, a_1, a_2, a_3, a_4, a_5), (v_a := abi.Bool()).set(v_tuple.a), (v_b := abi.Address()).set(v_tuple.b), (v_c := abi.make(abi.Tuple2[abi.Uint64, abi.Bool])).set(v_tuple.c), (v_d := abi.make(abi.StaticArray[abi.Byte, L[10]])).set(v_tuple.d), (v_e := abi.make(abi.StaticArray[abi.Bool, L[4]])).set(v_tuple.e), (v_f := abi.Uint64()).set(v_tuple.f), Return( And( a_0.get() == v_a.get(), a_1.get() == v_b.get(), a_2.encode() == v_c.encode(), a_3.encode() == v_d.encode(), a_4.encode() == v_e.encode(), a_5.get() == v_f.get(), )), )
def test_Bool_set_copy(): other = abi.Bool() value = abi.Bool() expr = value.set(other) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.load, other.stored_value.slot), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected with pytest.raises(pt.TealInputError): value.set(abi.Uint16())
def test_Bool_get(): value = abi.Bool() expr = value.get() assert expr.type_of() == pt.TealType.uint64 assert not expr.has_return() expected = pt.TealSimpleBlock( [pt.TealOp(expr, pt.Op.load, value.stored_value.slot)]) actual, _ = expr.__teal__(options) assert actual == expected
def test_encodeBoolSequence(): class EncodeSeqTest(NamedTuple): types: List[abi.Bool] expectedLength: int tests: List[EncodeSeqTest] = [ EncodeSeqTest(types=[], expectedLength=0), EncodeSeqTest(types=[abi.Bool()], expectedLength=1), EncodeSeqTest(types=[abi.Bool() for _ in range(4)], expectedLength=1), EncodeSeqTest(types=[abi.Bool() for _ in range(8)], expectedLength=1), EncodeSeqTest(types=[abi.Bool() for _ in range(9)], expectedLength=2), EncodeSeqTest(types=[abi.Bool() for _ in range(100)], expectedLength=13), ] for i, test in enumerate(tests): expr = _encode_bool_sequence(test.types) assert expr.type_of() == pt.TealType.bytes assert not expr.has_return() setBits = [[ pt.TealOp(None, pt.Op.int, j), pt.TealOp(None, pt.Op.load, testType.stored_value.slot), pt.TealOp(None, pt.Op.setbit), ] for j, testType in enumerate(test.types)] expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.byte, "0x" + ("00" * test.expectedLength)), ] + [expr for setBit in setBits for expr in setBit]) 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)
def test_Bool_set_static(): value = abi.Bool() for value_to_set in (True, False): expr = value.set(value_to_set) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.int, 1 if value_to_set else 0), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_Bool_encode(): value = abi.Bool() expr = value.encode() assert expr.type_of() == pt.TealType.bytes assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.byte, "0x00"), pt.TealOp(None, pt.Op.int, 0), pt.TealOp(None, pt.Op.load, value.stored_value.slot), pt.TealOp(None, pt.Op.setbit), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_Bool_set_expr(): value = abi.Bool() expr = value.set(pt.Int(0).Or(pt.Int(1))) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.int, 0), pt.TealOp(None, pt.Op.int, 1), pt.TealOp(None, pt.Op.logic_or), pt.TealOp(None, pt.Op.logic_not), pt.TealOp(None, pt.Op.logic_not), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_Bool_decode_bit(): value = abi.Bool() bitIndex = pt.Int(17) encoded = pt.Bytes("encoded") expr = value.decode_bit(encoded, bitIndex) 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, 17), pt.TealOp(None, pt.Op.getbit), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected
def test_Bool_set_computed(): value = abi.Bool() computed = ContainerType(abi.BoolTypeSpec(), pt.Int(0x80)) expr = value.set(computed) assert expr.type_of() == pt.TealType.none assert not expr.has_return() expected = pt.TealSimpleBlock([ pt.TealOp(None, pt.Op.int, 0x80), pt.TealOp(None, pt.Op.store, value.stored_value.slot), ]) actual, _ = expr.__teal__(options) actual.addIncoming() actual = pt.TealBlock.NormalizeBlocks(actual) with pt.TealComponent.Context.ignoreExprEquality(): assert actual == expected with pytest.raises(pt.TealInputError): value.set(ContainerType(abi.Uint32TypeSpec(), pt.Int(65537)))
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)