예제 #1
0
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
예제 #2
0
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
예제 #3
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"))
        )
예제 #4
0
def test_ApplicationTypeSpec_eq():
    assert abi.ApplicationTypeSpec() == abi.ApplicationTypeSpec()

    for otherType in (
            abi.ByteTypeSpec(),
            abi.Uint8TypeSpec(),
            abi.AddressTypeSpec(),
    ):
        assert abi.ApplicationTypeSpec() != otherType
예제 #5
0
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()
예제 #6
0
def test_AccountTypeSpec_eq():
    assert abi.AccountTypeSpec() == abi.AccountTypeSpec()

    for otherType in (
            abi.ByteTypeSpec(),
            abi.Uint8TypeSpec(),
            abi.AddressTypeSpec(),
    ):
        assert abi.AccountTypeSpec() != otherType
예제 #7
0
def test_boolAwareStaticByteLength():
    class ByteLengthTest(NamedTuple):
        types: List[abi.TypeSpec]
        expectedLength: int

    tests: List[ByteLengthTest] = [
        ByteLengthTest(types=[], expectedLength=0),
        ByteLengthTest(types=[abi.Uint64TypeSpec()], expectedLength=8),
        ByteLengthTest(types=[abi.BoolTypeSpec()], expectedLength=1),
        ByteLengthTest(types=[abi.BoolTypeSpec()] * 8, expectedLength=1),
        ByteLengthTest(types=[abi.BoolTypeSpec()] * 9, expectedLength=2),
        ByteLengthTest(types=[abi.BoolTypeSpec()] * 16, expectedLength=2),
        ByteLengthTest(types=[abi.BoolTypeSpec()] * 17, expectedLength=3),
        ByteLengthTest(types=[abi.BoolTypeSpec()] * 100, expectedLength=13),
        ByteLengthTest(
            types=[abi.BoolTypeSpec(),
                   abi.ByteTypeSpec(),
                   abi.BoolTypeSpec()],
            expectedLength=3,
        ),
        ByteLengthTest(
            types=[
                abi.BoolTypeSpec(),
                abi.BoolTypeSpec(),
                abi.ByteTypeSpec(),
                abi.BoolTypeSpec(),
                abi.BoolTypeSpec(),
            ],
            expectedLength=3,
        ),
        ByteLengthTest(
            types=[abi.BoolTypeSpec()] * 16 +
            [abi.ByteTypeSpec(),
             abi.BoolTypeSpec(),
             abi.BoolTypeSpec()],
            expectedLength=4,
        ),
    ]

    for i, test in enumerate(tests):
        actual = _bool_aware_static_byte_length(test.types)
        assert actual == test.expectedLength, "Test at index {} failed".format(
            i)
예제 #8
0
def test_TransactionTypeSpec_eq():
    for tv in TransactionValues:
        assert tv.ts == tv.ts

        for otherType in (
            abi.ByteTypeSpec(),
            abi.Uint8TypeSpec(),
            abi.AddressTypeSpec(),
        ):
            assert tv.ts != otherType
예제 #9
0
def test_Address_set_StaticArray():
    value_to_set = abi.StaticArray(
        abi.StaticArrayTypeSpec(abi.ByteTypeSpec(), abi.AddressLength.Bytes))
    value = abi.Address()
    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.load, value_to_set.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):
        bogus = abi.StaticArray(abi.StaticArrayTypeSpec(
            abi.ByteTypeSpec(), 10))
        value.set(bogus)
예제 #10
0
def test_ByteUint8_mutual_conversion():
    cases: List[Tuple[abi.UintTypeSpec, abi.UintTypeSpec]] = [
        (abi.Uint8TypeSpec(), abi.ByteTypeSpec()),
        (abi.ByteTypeSpec(), abi.Uint8TypeSpec()),
    ]
    for type_a, type_b in cases:
        type_b_instance = type_b.new_instance()
        other = type_a.new_instance()
        expr = type_b_instance.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, type_b_instance.stored_value.slot),
        ])

        actual, _ = expr.__teal__(options)
        actual.addIncoming()
        actual = pt.TealBlock.NormalizeBlocks(actual)

        with pt.TealComponent.Context.ignoreExprEquality():
            assert actual == expected
예제 #11
0
def test_StaticArrayTypeSpec_eq():
    for elementType in STATIC_TYPES + DYNAMIC_TYPES:
        for length in range(256):
            staticArrayType = abi.StaticArrayTypeSpec(elementType, length)
            assert staticArrayType == staticArrayType
            assert staticArrayType != abi.StaticArrayTypeSpec(
                elementType, length + 1)
            assert staticArrayType != abi.StaticArrayTypeSpec(
                abi.TupleTypeSpec(elementType), length)

    for length in range(256):
        staticBytesType = abi.StaticBytesTypeSpec(length)
        assert staticBytesType == staticBytesType
        assert staticBytesType != abi.StaticBytesTypeSpec(length + 1)
        assert staticBytesType != abi.StaticArrayTypeSpec(
            abi.TupleTypeSpec(abi.ByteTypeSpec()), length)
예제 #12
0
    def string_reverse_factory(self) -> pt.ABIReturnSubroutine:
        """
        Assume strings are python utf-8 compliant and therefore each byte value is at most 127
        """
        if self.length is None:
            self.length = DEFAULT_DYNAMIC_ARRAY_LENGTH

        char_type_spec = abi.ByteTypeSpec()

        @pt.ABIReturnSubroutine
        def string_reverse(x: self.annotation, *, output: self.annotation):  # type: ignore[name-defined]
            insts = [char_type_spec.new_instance() for _ in range(self.length)]  # type: ignore[arg-type]
            setters = [inst.set(x[i]) for i, inst in enumerate(reversed(insts))]
            return pt.Seq(*(setters + [output.set(insts)]))

        return string_reverse
예제 #13
0
def test_String_set_computed():
    bv = pt.Bytes("base16", "0x0004DEADBEEF")
    computed_value = ContainerType(abi.StringTypeSpec(), bv)

    value = abi.String()
    expr = value.set(computed_value)
    assert expr.type_of() == pt.TealType.none
    assert not expr.has_return()

    _, byte_ops = bv.__teal__(options)
    expected = pt.TealSimpleBlock([
        byte_ops.ops[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

    with pytest.raises(pt.TealInputError):
        value.set(ContainerType(abi.ByteTypeSpec(), pt.Int(0x01)))
예제 #14
0
def test_Uint_set_computed():
    byte_computed_value = ContainerType(abi.ByteTypeSpec(), pt.Int(0x22))

    for test in testData:
        computed_value = ContainerType(test.uintType, pt.Int(0x44))
        value = test.uintType.new_instance()
        expr = value.set(computed_value)
        assert expr.type_of() == pt.TealType.none
        assert not expr.has_return()

        expected = pt.TealSimpleBlock([
            pt.TealOp(None, pt.Op.int, 0x44),
            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(byte_computed_value)
예제 #15
0
def test_Address_set_computed():
    av = pt.Addr("MDDKJUCTY57KA2PBFI44CLTJ5YHY5YVS4SVQUPZAWSRV2ZAVFKI33O6YPE")
    computed_value = ContainerType(abi.AddressTypeSpec(), av)

    value = abi.Address()
    expr = value.set(computed_value)
    assert expr.type_of() == pt.TealType.none
    assert not expr.has_return()

    _, byte_ops = av.__teal__(options)
    expected = pt.TealSimpleBlock([
        byte_ops.ops[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

    with pytest.raises(pt.TealInputError):
        value.set(ContainerType(abi.ByteTypeSpec(), pt.Int(0x01)))
예제 #16
0
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)
예제 #17
0
def test_consecutiveBool():
    class ConsecutiveTest(NamedTuple):
        types: List[abi.TypeSpec]
        start: int
        expected: int

    tests: List[ConsecutiveTest] = [
        ConsecutiveTest(types=[], start=0, expected=0),
        ConsecutiveTest(types=[abi.Uint16TypeSpec()], start=0, expected=0),
        ConsecutiveTest(types=[abi.BoolTypeSpec()], start=0, expected=1),
        ConsecutiveTest(types=[abi.BoolTypeSpec()], start=1, expected=0),
        ConsecutiveTest(types=[abi.BoolTypeSpec(),
                               abi.BoolTypeSpec()],
                        start=0,
                        expected=2),
        ConsecutiveTest(types=[abi.BoolTypeSpec(),
                               abi.BoolTypeSpec()],
                        start=1,
                        expected=1),
        ConsecutiveTest(types=[abi.BoolTypeSpec(),
                               abi.BoolTypeSpec()],
                        start=2,
                        expected=0),
        ConsecutiveTest(types=[abi.BoolTypeSpec() for _ in range(10)],
                        start=0,
                        expected=10),
        ConsecutiveTest(
            types=[
                abi.BoolTypeSpec(),
                abi.BoolTypeSpec(),
                abi.ByteTypeSpec(),
                abi.BoolTypeSpec(),
            ],
            start=0,
            expected=2,
        ),
        ConsecutiveTest(
            types=[
                abi.BoolTypeSpec(),
                abi.BoolTypeSpec(),
                abi.ByteTypeSpec(),
                abi.BoolTypeSpec(),
            ],
            start=2,
            expected=0,
        ),
        ConsecutiveTest(
            types=[
                abi.BoolTypeSpec(),
                abi.BoolTypeSpec(),
                abi.ByteTypeSpec(),
                abi.BoolTypeSpec(),
            ],
            start=3,
            expected=1,
        ),
        ConsecutiveTest(
            types=[
                abi.ByteTypeSpec(),
                abi.BoolTypeSpec(),
                abi.BoolTypeSpec(),
                abi.ByteTypeSpec(),
            ],
            start=0,
            expected=0,
        ),
        ConsecutiveTest(
            types=[
                abi.ByteTypeSpec(),
                abi.BoolTypeSpec(),
                abi.BoolTypeSpec(),
                abi.ByteTypeSpec(),
            ],
            start=1,
            expected=2,
        ),
    ]

    for i, test in enumerate(tests):
        actual = _consecutive_bool_type_spec_num(test.types, test.start)
        assert actual == test.expected, "Test at index {} failed".format(i)

        actual = _consecutive_bool_instance_num(
            [t.new_instance() for t in test.types], test.start)
        assert actual == test.expected, "Test at index {} failed".format(i)
예제 #18
0
def test_UintTypeSpec_str():
    for test in testData:
        assert str(test.uintType) == "uint{}".format(test.expectedBits)
    assert str(abi.ByteTypeSpec()) == "byte"
예제 #19
0
def test_UintTypeSpec_new_instance():
    for test in testData:
        assert isinstance(test.uintType.new_instance(), test.instanceType)
    assert isinstance(abi.ByteTypeSpec().new_instance(), abi.Byte)
예제 #20
0
def test_UintTypeSpec_is_dynamic():
    for test in testData:
        assert not test.uintType.is_dynamic()
    assert not abi.ByteTypeSpec().is_dynamic()