Exemplo n.º 1
0
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[])"
    )
Exemplo n.º 2
0
def test_TupleTypeSpec_value_type_specs():
    assert abi.TupleTypeSpec(
        abi.Uint64TypeSpec(), abi.Uint32TypeSpec(), abi.BoolTypeSpec()
    ).value_type_specs() == [
        abi.Uint64TypeSpec(),
        abi.Uint32TypeSpec(),
        abi.BoolTypeSpec(),
    ]
Exemplo n.º 3
0
def test_TupleTypeSpec_eq():
    tupleA = abi.TupleTypeSpec(
        abi.Uint64TypeSpec(), abi.Uint32TypeSpec(), abi.BoolTypeSpec()
    )
    tupleB = abi.TupleTypeSpec(
        abi.Uint64TypeSpec(), abi.Uint32TypeSpec(), abi.BoolTypeSpec()
    )
    tupleC = abi.TupleTypeSpec(
        abi.BoolTypeSpec(), abi.Uint64TypeSpec(), abi.Uint32TypeSpec()
    )
    assert tupleA == tupleA
    assert tupleA == tupleB
    assert tupleA != tupleC
Exemplo n.º 4
0
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()
Exemplo n.º 5
0
def test_Tuple_set_Computed():
    tupleValue = abi.Tuple(
        abi.TupleTypeSpec(
            abi.Uint8TypeSpec(), abi.Uint16TypeSpec(), abi.Uint32TypeSpec()
        )
    )
    computed = ContainerType(
        tupleValue.type_spec(), pt.Bytes("internal representation")
    )
    expr = tupleValue.set(computed)
    assert expr.type_of() == pt.TealType.none
    assert not expr.has_return()

    expected = pt.TealSimpleBlock(
        [
            pt.TealOp(None, pt.Op.byte, '"internal representation"'),
            pt.TealOp(None, pt.Op.store, tupleValue.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):
        tupleValue.set(computed, computed)

    with pytest.raises(pt.TealInputError):
        tupleValue.set(
            ContainerType(abi.TupleTypeSpec(abi.ByteTypeSpec()), pt.Bytes(b"a"))
        )
Exemplo n.º 6
0
def test_TupleTypeSpec_new_instance():
    assert isinstance(
        abi.TupleTypeSpec(
            abi.Uint64TypeSpec(), abi.Uint32TypeSpec(), abi.BoolTypeSpec()
        ).new_instance(),
        abi.Tuple,
    )
Exemplo n.º 7
0
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()
Exemplo n.º 8
0
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)))
Exemplo n.º 9
0
def test_Tuple_set():
    tupleValue = abi.Tuple(
        abi.TupleTypeSpec(
            abi.Uint8TypeSpec(), abi.Uint16TypeSpec(), abi.Uint32TypeSpec()
        )
    )
    uint8 = abi.Uint8()
    uint16 = abi.Uint16()
    uint32 = abi.Uint32()

    with pytest.raises(pt.TealInputError):
        tupleValue.set()

    with pytest.raises(pt.TealInputError):
        tupleValue.set(uint8, uint16)

    with pytest.raises(pt.TealInputError):
        tupleValue.set(uint8, uint16, uint32, uint32)

    with pytest.raises(pt.TealInputError):
        tupleValue.set(uint8, uint32, uint16)

    with pytest.raises(pt.TealInputError):
        tupleValue.set(uint8, uint16, uint16)

    expr = tupleValue.set(uint8, uint16, uint32)
    assert expr.type_of() == pt.TealType.none
    assert not expr.has_return()

    expectedExpr = tupleValue.stored_value.store(_encode_tuple([uint8, uint16, uint32]))
    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
Exemplo n.º 10
0
from typing import List, cast
import pytest

import pyteal as pt
from pyteal import abi

options = pt.CompileOptions(version=5)

STATIC_TYPES: List[abi.TypeSpec] = [
    abi.BoolTypeSpec(),
    abi.Uint8TypeSpec(),
    abi.Uint16TypeSpec(),
    abi.Uint32TypeSpec(),
    abi.Uint64TypeSpec(),
    abi.TupleTypeSpec(),
    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()),
Exemplo n.º 11
0
     expectedEncoding=lambda uintType: pt.SetByte(pt.Bytes(
         b"\x00"), pt.Int(0), uintType.get()),
 ),
 UintTestData(
     uintType=abi.Uint16TypeSpec(),
     instanceType=abi.Uint16,
     expectedBits=16,
     maxValue=2**16 - 1,
     checkUpperBound=True,
     expectedDecoding=lambda encoded, start_index, end_index, length: pt.
     ExtractUint16(encoded, noneToInt0(start_index)),
     expectedEncoding=lambda uintType: pt.Suffix(pt.Itob(uintType.get()),
                                                 pt.Int(6)),
 ),
 UintTestData(
     uintType=abi.Uint32TypeSpec(),
     instanceType=abi.Uint32,
     expectedBits=32,
     maxValue=2**32 - 1,
     checkUpperBound=True,
     expectedDecoding=lambda encoded, start_index, end_index, length: pt.
     ExtractUint32(encoded, noneToInt0(start_index)),
     expectedEncoding=lambda uintType: pt.Suffix(pt.Itob(uintType.get()),
                                                 pt.Int(4)),
 ),
 UintTestData(
     uintType=abi.Uint64TypeSpec(),
     instanceType=abi.Uint64,
     expectedBits=64,
     maxValue=2**64 - 1,
     checkUpperBound=False,