Пример #1
0
def test_neq_overload():
    args_int = [pt.Int(2), pt.Int(3)]
    expr_int = args_int[0] != args_int[1]
    assert expr_int.type_of() == pt.TealType.uint64

    expected_int = pt.TealSimpleBlock([
        pt.TealOp(args_int[0], pt.Op.int, 2),
        pt.TealOp(args_int[1], pt.Op.int, 3),
        pt.TealOp(expr_int, pt.Op.neq),
    ])

    actual_int, _ = expr_int.__teal__(avm2Options)
    actual_int.addIncoming()
    actual_int = pt.TealBlock.NormalizeBlocks(actual_int)

    assert actual_int == expected_int

    args_bytes = [pt.Txn.receiver(), pt.Txn.sender()]
    expr_bytes = args_bytes[0] != args_bytes[1]
    assert expr_bytes.type_of() == pt.TealType.uint64

    expected_bytes = pt.TealSimpleBlock([
        pt.TealOp(args_bytes[0], pt.Op.txn, "Receiver"),
        pt.TealOp(args_bytes[1], pt.Op.txn, "Sender"),
        pt.TealOp(expr_bytes, pt.Op.neq),
    ])

    actual_bytes, _ = expr_bytes.__teal__(avm2Options)
    actual_bytes.addIncoming()
    actual_bytes = pt.TealBlock.NormalizeBlocks(actual_bytes)

    assert actual_bytes == expected_bytes
Пример #2
0
def test_sort_multiple_branch():
    blockTrueTrue = pt.TealSimpleBlock(
        [pt.TealOp(None, pt.Op.byte, '"true true"')])
    blockTrueFalse = pt.TealSimpleBlock(
        [pt.TealOp(None, pt.Op.byte, '"true false"')])
    blockTrueBranch = pt.TealConditionalBlock([])
    blockTrueBranch.setTrueBlock(blockTrueTrue)
    blockTrueBranch.setFalseBlock(blockTrueFalse)
    blockTrue = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')])
    blockTrue.setNextBlock(blockTrueBranch)
    blockFalse = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')])
    block = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)])
    block.setTrueBlock(blockTrue)
    block.setFalseBlock(blockFalse)
    block.addIncoming()
    block.validateTree()

    expected = [
        block,
        blockTrue,
        blockTrueBranch,
        blockTrueFalse,
        blockTrueTrue,
        blockFalse,
    ]
    actual = sortBlocks(block, blockFalse)

    assert actual == expected
Пример #3
0
def test_pop():
    arg_int = pt.Int(3)
    expr_int = pt.Pop(arg_int)
    assert expr_int.type_of() == pt.TealType.none

    expected_int = pt.TealSimpleBlock(
        [pt.TealOp(arg_int, pt.Op.int, 3), pt.TealOp(expr_int, pt.Op.pop)]
    )

    actual_int, _ = expr_int.__teal__(avm2Options)
    actual_int.addIncoming()
    actual_int = pt.TealBlock.NormalizeBlocks(actual_int)

    assert actual_int == expected_int

    arg_bytes = pt.Txn.receiver()
    expr_bytes = pt.Pop(arg_bytes)
    assert expr_bytes.type_of() == pt.TealType.none

    expected_bytes = pt.TealSimpleBlock(
        [pt.TealOp(arg_bytes, pt.Op.txn, "Receiver"), pt.TealOp(expr_bytes, pt.Op.pop)]
    )

    actual_bytes, _ = expr_bytes.__teal__(avm2Options)
    actual_bytes.addIncoming()
    actual_bytes = pt.TealBlock.NormalizeBlocks(actual_bytes)

    assert actual_bytes == expected_bytes
Пример #4
0
def test_true_block():
    block = pt.TealConditionalBlock([])
    block.setTrueBlock(pt.TealSimpleBlock([pt.TealOp(None, pt.Op.substring3)]))
    assert block.trueBlock == pt.TealSimpleBlock([pt.TealOp(None, pt.Op.substring3)])
    assert block.getOutgoing() == [
        pt.TealSimpleBlock([pt.TealOp(None, pt.Op.substring3)])
    ]
Пример #5
0
def test_iterate_multiple_branch():
    blockTrueTrue = pt.TealSimpleBlock(
        [pt.TealOp(None, pt.Op.byte, '"true true"')])
    blockTrueFalse = pt.TealSimpleBlock(
        [pt.TealOp(None, pt.Op.byte, '"true false"')])
    blockTrueBranch = pt.TealConditionalBlock([])
    blockTrueBranch.setTrueBlock(blockTrueTrue)
    blockTrueBranch.setFalseBlock(blockTrueFalse)
    blockTrue = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')])
    blockTrue.setNextBlock(blockTrueBranch)
    blockFalse = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')])
    block = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)])
    block.setTrueBlock(blockTrue)
    block.setFalseBlock(blockFalse)

    blocks = list(pt.TealBlock.Iterate(block))

    assert blocks == [
        block,
        blockTrue,
        blockFalse,
        blockTrueBranch,
        blockTrueTrue,
        blockTrueFalse,
    ]
Пример #6
0
def test_flattenBlocks_branch():
    blockTrue = pt.TealSimpleBlock([
        pt.TealOp(None, pt.Op.byte, '"true"'),
        pt.TealOp(None, pt.Op.return_)
    ])
    blockFalse = pt.TealSimpleBlock([
        pt.TealOp(None, pt.Op.byte, '"false"'),
        pt.TealOp(None, pt.Op.return_)
    ])
    block = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)])
    block.setTrueBlock(blockTrue)
    block.setFalseBlock(blockFalse)
    block.addIncoming()
    block.validateTree()
    blocks = [block, blockFalse, blockTrue]

    expected = [
        pt.TealOp(None, pt.Op.int, 1),
        pt.TealOp(None, pt.Op.bnz, pt.LabelReference("l2")),
        pt.TealOp(None, pt.Op.byte, '"false"'),
        pt.TealOp(None, pt.Op.return_),
        pt.TealLabel(None, pt.LabelReference("l2")),
        pt.TealOp(None, pt.Op.byte, '"true"'),
        pt.TealOp(None, pt.Op.return_),
    ]
    actual = flattenBlocks(blocks)

    assert actual == expected
Пример #7
0
def test_assignScratchSlotsToSubroutines_slot_used_before_assignment():
    def sub1Impl():
        return None

    def sub2Impl(a1):
        return None

    def sub3Impl(a1, a2, a3):
        return None

    subroutine1 = pt.SubroutineDefinition(sub1Impl, pt.TealType.uint64)
    subroutine2 = pt.SubroutineDefinition(sub2Impl, pt.TealType.bytes)
    subroutine3 = pt.SubroutineDefinition(sub3Impl, pt.TealType.none)

    globalSlot1 = pt.ScratchSlot()

    subroutine1Slot1 = pt.ScratchSlot()
    subroutine1Slot2 = pt.ScratchSlot()
    subroutine1Ops = [
        pt.TealOp(None, pt.Op.int, 1),
        pt.TealOp(None, pt.Op.store, subroutine1Slot1),
        pt.TealOp(None, pt.Op.int, 3),
        pt.TealOp(None, pt.Op.store, subroutine1Slot2),
        pt.TealOp(None, pt.Op.load, globalSlot1),
        pt.TealOp(None, pt.Op.retsub),
    ]

    subroutine2Slot1 = pt.ScratchSlot()
    subroutine2Ops = [
        pt.TealOp(None, pt.Op.byte, '"value"'),
        pt.TealOp(None, pt.Op.store, subroutine2Slot1),
        pt.TealOp(None, pt.Op.load, subroutine2Slot1),
        pt.TealOp(None, pt.Op.retsub),
    ]

    subroutine3Ops = [
        pt.TealOp(None, pt.Op.retsub),
    ]

    mainSlot1 = pt.ScratchSlot()
    mainSlot2 = pt.ScratchSlot()
    mainOps = [
        pt.TealOp(None, pt.Op.int, 7),
        pt.TealOp(None, pt.Op.store, globalSlot1),
        pt.TealOp(None, pt.Op.int, 2),
        pt.TealOp(None, pt.Op.store, mainSlot2),
        pt.TealOp(None, pt.Op.load, mainSlot1),
        pt.TealOp(None, pt.Op.return_),
    ]

    subroutineBlocks = {
        None: pt.TealSimpleBlock(mainOps),
        subroutine1: pt.TealSimpleBlock(subroutine1Ops),
        subroutine2: pt.TealSimpleBlock(subroutine2Ops),
        subroutine3: pt.TealSimpleBlock(subroutine3Ops),
    }

    with pytest.raises(pt.TealInternalError):
        assignScratchSlotsToSubroutines(subroutineBlocks)
Пример #8
0
def test_constructor():
    block1 = pt.TealSimpleBlock([])
    assert block1.ops == []
    assert block1.nextBlock is None

    block2 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 1)])
    assert block2.ops == [pt.TealOp(None, pt.Op.int, 1)]
    assert block2.nextBlock is None
Пример #9
0
def test_normalize_single():
    original = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 1)])

    expected = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 1)])

    original.addIncoming()
    actual = pt.TealBlock.NormalizeBlocks(original)
    actual.validateTree()

    assert actual == expected
Пример #10
0
def test_outgoing():
    emptyBlock = pt.TealSimpleBlock([])
    assert emptyBlock.getOutgoing() == []

    block = pt.TealSimpleBlock([])
    block.setNextBlock(
        pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"nextBlock"')]))
    assert block.getOutgoing() == [
        pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"nextBlock"')])
    ]
Пример #11
0
def test_scratch_load_index_expression():
    expr = pt.ScratchLoad(slot=None, index_expression=pt.Int(1337))
    assert expr.type_of() == pt.TealType.anytype

    expected = pt.TealSimpleBlock([pt.TealOp(pt.Int(1337), pt.Op.int, 1337)])
    expected.setNextBlock(pt.TealSimpleBlock([pt.TealOp(None, pt.Op.loads)]))

    actual, _ = expr.__teal__(options)

    with pt.TealComponent.Context.ignoreExprEquality():
        assert actual == expected
Пример #12
0
def test_iterate_branch_converge():
    blockEnd = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.return_)])
    blockTrue = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')])
    blockTrue.setNextBlock(blockEnd)
    blockFalse = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')])
    blockFalse.setNextBlock(blockEnd)
    block = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)])
    block.setTrueBlock(blockTrue)
    block.setFalseBlock(blockFalse)

    blocks = list(pt.TealBlock.Iterate(block))

    assert blocks == [block, blockTrue, blockFalse, blockEnd]
Пример #13
0
def test_normalize_branch_converge():
    blockEnd = pt.TealSimpleBlock([])
    blockTrueNext = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 4)])
    blockTrueNext.setNextBlock(blockEnd)
    blockTrue = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')])
    blockTrue.setNextBlock(blockTrueNext)
    blockFalse = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')])
    blockFalse.setNextBlock(blockEnd)
    blockBranch = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)])
    blockBranch.setTrueBlock(blockTrue)
    blockBranch.setFalseBlock(blockFalse)
    original = pt.TealSimpleBlock([])
    original.setNextBlock(blockBranch)

    expectedEnd = pt.TealSimpleBlock([])
    expectedTrue = pt.TealSimpleBlock(
        [pt.TealOp(None, pt.Op.byte, '"true"'),
         pt.TealOp(None, pt.Op.int, 4)])
    expectedTrue.setNextBlock(expectedEnd)
    expectedFalse = pt.TealSimpleBlock(
        [pt.TealOp(None, pt.Op.byte, '"false"')])
    expectedFalse.setNextBlock(expectedEnd)
    expected = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)])
    expected.setTrueBlock(expectedTrue)
    expected.setFalseBlock(expectedFalse)

    original.addIncoming()
    actual = pt.TealBlock.NormalizeBlocks(original)
    actual.validateTree()

    assert actual == expected
Пример #14
0
def test_iterate_sequence():
    block5 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 5)])
    block4 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 4)])
    block4.setNextBlock(block5)
    block3 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 3)])
    block3.setNextBlock(block4)
    block2 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 2)])
    block2.setNextBlock(block3)
    block1 = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 1)])
    block1.setNextBlock(block2)

    blocks = list(pt.TealBlock.Iterate(block1))

    assert blocks == [block1, block2, block3, block4, block5]
Пример #15
0
def test_outgoing():
    emptyBlock = pt.TealConditionalBlock([])
    assert emptyBlock.getOutgoing() == []

    trueBlock = pt.TealConditionalBlock([])
    trueBlock.setTrueBlock(pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')]))
    assert trueBlock.getOutgoing() == [
        pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')])
    ]

    falseBlock = pt.TealConditionalBlock([])
    falseBlock.setFalseBlock(
        pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')])
    )
    assert falseBlock.getOutgoing() == [
        pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')])
    ]

    bothBlock = pt.TealConditionalBlock([])
    bothBlock.setTrueBlock(pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')]))
    bothBlock.setFalseBlock(
        pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')])
    )
    assert bothBlock.getOutgoing() == [
        pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')]),
        pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')]),
    ]
Пример #16
0
def test_flattenBlocks_multiple_branch_converge():
    blockEnd = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.return_)])
    blockTrueTrue = pt.TealSimpleBlock(
        [pt.TealOp(None, pt.Op.byte, '"true true"')])
    blockTrueTrue.setNextBlock(blockEnd)
    blockTrueFalse = pt.TealSimpleBlock([
        pt.TealOp(None, pt.Op.byte, '"true false"'),
        pt.TealOp(None, pt.Op.err)
    ])
    blockTrueBranch = pt.TealConditionalBlock([])
    blockTrueBranch.setTrueBlock(blockTrueTrue)
    blockTrueBranch.setFalseBlock(blockTrueFalse)
    blockTrue = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')])
    blockTrue.setNextBlock(blockTrueBranch)
    blockFalse = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')])
    blockFalse.setNextBlock(blockEnd)
    block = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)])
    block.setTrueBlock(blockTrue)
    block.setFalseBlock(blockFalse)
    block.addIncoming()
    block.validateTree()
    blocks = [
        block,
        blockFalse,
        blockTrue,
        blockTrueBranch,
        blockTrueFalse,
        blockTrueTrue,
        blockEnd,
    ]

    expected = [
        pt.TealOp(None, pt.Op.int, 1),
        pt.TealOp(None, pt.Op.bnz, pt.LabelReference("l2")),
        pt.TealOp(None, pt.Op.byte, '"false"'),
        pt.TealOp(None, pt.Op.b, pt.LabelReference("l6")),
        pt.TealLabel(None, pt.LabelReference("l2")),
        pt.TealOp(None, pt.Op.byte, '"true"'),
        pt.TealOp(None, pt.Op.bnz, pt.LabelReference("l5")),
        pt.TealOp(None, pt.Op.byte, '"true false"'),
        pt.TealOp(None, pt.Op.err),
        pt.TealLabel(None, pt.LabelReference("l5")),
        pt.TealOp(None, pt.Op.byte, '"true true"'),
        pt.TealLabel(None, pt.LabelReference("l6")),
        pt.TealOp(None, pt.Op.return_),
    ]
    actual = flattenBlocks(blocks)

    assert actual == expected
Пример #17
0
def test_flattenBlocks_single_empty():
    blocks = [pt.TealSimpleBlock([])]

    expected = []
    actual = flattenBlocks(blocks)

    assert actual == expected
Пример #18
0
def test_err():
    expr = pt.Err()
    assert expr.type_of() == pt.TealType.none
    assert expr.has_return()
    expected = pt.TealSimpleBlock([pt.TealOp(expr, pt.Op.err)])
    actual, _ = expr.__teal__(pt.CompileOptions())
    assert actual == expected
Пример #19
0
def test_ecdsa_verify_basic(curve: pt.EcdsaCurve):
    args = [pt.Bytes("data"), pt.Bytes("sigA"), pt.Bytes("sigB")]
    pubkey = (pt.Bytes("X"), pt.Bytes("Y"))
    expr = pt.EcdsaVerify(curve, args[0], args[1], args[2], pubkey)
    assert expr.type_of() == pt.TealType.uint64

    expected = pt.TealSimpleBlock([
        pt.TealOp(args[0], pt.Op.byte, '"data"'),
        pt.TealOp(args[1], pt.Op.byte, '"sigA"'),
        pt.TealOp(args[2], pt.Op.byte, '"sigB"'),
        pt.TealOp(pubkey[0], pt.Op.byte, '"X"'),
        pt.TealOp(pubkey[1], pt.Op.byte, '"Y"'),
        pt.TealOp(expr, pt.Op.ecdsa_verify, curve.arg_name),
    ])

    actual, _ = expr.__teal__(curve_options_map[curve])
    actual.addIncoming()
    actual = pt.TealBlock.NormalizeBlocks(actual)

    assert actual == expected

    # compile without errors this is necessary so assembly is also tested
    pt.compileTeal(
        pt.Seq(pt.Pop(expr), pt.Approve()),
        pt.Mode.Application,
        version=curve.min_version,
    )

    with pytest.raises(pt.TealInputError):
        pt.compileTeal(
            pt.Seq(pt.Pop(expr), pt.Approve()),
            pt.Mode.Application,
            version=curve.min_version - 1,
        )
Пример #20
0
def test_sort_branch_converge():
    blockEnd = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.return_)])
    blockTrue = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"true"')])
    blockTrue.setNextBlock(blockEnd)
    blockFalse = pt.TealSimpleBlock([pt.TealOp(None, pt.Op.byte, '"false"')])
    blockFalse.setNextBlock(blockEnd)
    block = pt.TealConditionalBlock([pt.TealOp(None, pt.Op.int, 1)])
    block.setTrueBlock(blockTrue)
    block.setFalseBlock(blockFalse)
    block.addIncoming()
    block.validateTree()

    expected = [block, blockFalse, blockTrue, blockEnd]
    actual = sortBlocks(block, blockEnd)

    assert actual == expected
Пример #21
0
def test_for():
    i = pt.ScratchVar()
    items = [
        (i.store(pt.Int(0))),
        i.load() < pt.Int(10),
        i.store(i.load() + pt.Int(1)),
        pt.App.globalPut(pt.Itob(i.load()), i.load() * pt.Int(2)),
    ]
    expr = pt.For(items[0], items[1], items[2]).Do(pt.Seq([items[3]]))

    assert expr.type_of() == pt.TealType.none
    assert not expr.has_return()

    expected, varEnd = items[0].__teal__(options)
    condStart, condEnd = items[1].__teal__(options)
    stepStart, stepEnd = items[2].__teal__(options)
    do, doEnd = pt.Seq([items[3]]).__teal__(options)
    expectedBranch = pt.TealConditionalBlock([])
    end = pt.TealSimpleBlock([])

    varEnd.setNextBlock(condStart)
    doEnd.setNextBlock(stepStart)

    expectedBranch.setTrueBlock(do)
    expectedBranch.setFalseBlock(end)
    condEnd.setNextBlock(expectedBranch)
    stepEnd.setNextBlock(condStart)

    actual, _ = expr.__teal__(options)

    assert actual == expected
Пример #22
0
def test_while_break():
    i = pt.ScratchVar()
    i.store(pt.Int(0))
    items = [
        i.load() < pt.Int(2),
        i.store(i.load() + pt.Int(1)),
        pt.If(i.load() == pt.Int(1), pt.Break()),
    ]
    expr = pt.While(items[0]).Do(pt.Seq(items[1], items[2]))
    assert expr.type_of() == pt.TealType.none
    assert not expr.has_return()

    options.enterLoop()

    expected, condEnd = items[0].__teal__(options)
    do, doEnd = pt.Seq([items[1], items[2]]).__teal__(options)
    expectedBranch = pt.TealConditionalBlock([])
    end = pt.TealSimpleBlock([])

    expectedBranch.setTrueBlock(do)
    expectedBranch.setFalseBlock(end)
    condEnd.setNextBlock(expectedBranch)
    doEnd.setNextBlock(expected)

    breakBlocks, _ = options.exitLoop()

    for block in breakBlocks:
        block.setNextBlock(end)

    actual, _ = expr.__teal__(options)

    assert actual == expected
Пример #23
0
def test_ComputedType_use():
    for value in (0, 1, 2, 3, 12345):
        dummyComputedType = ContainerType(abi.Uint64TypeSpec(), pt.Int(value))
        expr = dummyComputedType.use(lambda output: pt.Int(2) * output.get())
        assert expr.type_of() == pt.TealType.uint64
        assert not expr.has_return()

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

        assert type(actual) is pt.TealSimpleBlock
        assert actual.ops[1].op == pt.Op.store
        assert type(actual.ops[1].args[0]) is pt.ScratchSlot
        actualSlot = actual.ops[1].args[0]

        expected = pt.TealSimpleBlock([
            pt.TealOp(None, pt.Op.int, value),
            pt.TealOp(None, pt.Op.store, actualSlot),
            pt.TealOp(None, pt.Op.int, 2),
            pt.TealOp(None, pt.Op.load, actualSlot),
            pt.TealOp(None, pt.Op.mul),
        ])

        with pt.TealComponent.Context.ignoreExprEquality():
            assert actual == expected
Пример #24
0
def test_ecdsa_decompress(curve: pt.EcdsaCurve):
    compressed_pubkey = pt.Bytes("XY")
    pubkey = pt.EcdsaDecompress(curve, compressed_pubkey)
    assert pubkey.type_of() == pt.TealType.none

    expected = pt.TealSimpleBlock([
        pt.TealOp(compressed_pubkey, pt.Op.byte, '"XY"'),
        pt.TealOp(pubkey, pt.Op.ecdsa_pk_decompress, curve.arg_name),
        pt.TealOp(pubkey.output_slots[1].store(), pt.Op.store,
                  pubkey.output_slots[1]),
        pt.TealOp(pubkey.output_slots[0].store(), pt.Op.store,
                  pubkey.output_slots[0]),
    ])

    actual, _ = pubkey.__teal__(curve_options_map[curve])
    actual.addIncoming()
    actual = pt.TealBlock.NormalizeBlocks(actual)

    with pt.TealComponent.Context.ignoreExprEquality():
        assert actual == expected

    # compile without errors this is necessary so assembly is also tested
    pt.compileTeal(pt.Seq(pubkey, pt.Approve()),
                   pt.Mode.Application,
                   version=curve.min_version)

    with pytest.raises(pt.TealInputError):
        pt.compileTeal(
            pt.Seq(pubkey, pt.Approve()),
            pt.Mode.Application,
            version=curve.min_version - 1,
        )
Пример #25
0
def test_elseif_multiple():
    args = [pt.Int(0), pt.Int(1), pt.Int(2), pt.Int(3), pt.Int(4), pt.Int(5), pt.Int(6)]
    expr = (
        pt.If(args[0])
        .Then(args[1])
        .ElseIf(args[2])
        .Then(args[3])
        .ElseIf(args[4])
        .Then(args[5])
        .Else(args[6])
    )
    assert expr.type_of() == pt.TealType.uint64

    elseIfExpr = pt.If(args[2], args[3], pt.If(args[4], args[5], args[6]))
    expected, _ = args[0].__teal__(options)
    thenBlock, _ = args[1].__teal__(options)
    elseStart, elseEnd = elseIfExpr.__teal__(options)
    expectedBranch = pt.TealConditionalBlock([])
    expectedBranch.setTrueBlock(thenBlock)
    expectedBranch.setFalseBlock(elseStart)
    expected.setNextBlock(expectedBranch)
    end = pt.TealSimpleBlock([])
    thenBlock.setNextBlock(end)
    elseEnd.setNextBlock(end)

    actual, _ = expr.__teal__(options)

    assert actual == expected
Пример #26
0
def test_Uint_set_expr():
    for test in testData:
        value = test.uintType.new_instance()
        expr = value.set(pt.Int(10) + pt.Int(1))
        assert expr.type_of() == pt.TealType.none
        assert not expr.has_return()

        upperBoundCheck = []
        if test.checkUpperBound:
            upperBoundCheck = [
                pt.TealOp(None, pt.Op.load, value.stored_value.slot),
                pt.TealOp(None, pt.Op.int, test.maxValue + 1),
                pt.TealOp(None, pt.Op.lt),
                pt.TealOp(None, pt.Op.assert_),
            ]

        expected = pt.TealSimpleBlock([
            pt.TealOp(None, pt.Op.int, 10),
            pt.TealOp(None, pt.Op.int, 1),
            pt.TealOp(None, pt.Op.add),
            pt.TealOp(None, pt.Op.store, value.stored_value.slot),
        ] + upperBoundCheck)

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

        with pt.TealComponent.Context.ignoreExprEquality():
            assert actual == expected
Пример #27
0
def test_flattenBlocks_single_one():
    blocks = [pt.TealSimpleBlock([pt.TealOp(None, pt.Op.int, 1)])]

    expected = [pt.TealOp(None, pt.Op.int, 1)]
    actual = flattenBlocks(blocks)

    assert actual == expected
Пример #28
0
def test_Uint_set_static():
    for test in testData:
        for value_to_set in (0, 1, 100, test.maxValue):
            value = test.uintType.new_instance()
            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, value_to_set),
                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(test.maxValue + 1)

        with pytest.raises(pt.TealInputError):
            value.set(-1)
Пример #29
0
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
Пример #30
0
def test_String_set_expr():
    for value_to_set in (pt.Bytes("hi"), pt.Bytes("base16", "0xdeadbeef")):
        value = abi.String()
        expr = value.set(value_to_set)
        assert expr.type_of() == pt.TealType.none
        assert not expr.has_return()

        value_start, value_end = value_to_set.__teal__(options)
        expected_body = pt.TealSimpleBlock([
            pt.TealOp(None, pt.Op.store, value.stored_value.slot),
            pt.TealOp(None, pt.Op.load, value.stored_value.slot),
            pt.TealOp(None, pt.Op.len),
            pt.TealOp(None, pt.Op.itob),
            pt.TealOp(None, pt.Op.extract, 6, 0),
            pt.TealOp(None, pt.Op.load, value.stored_value.slot),
            pt.TealOp(None, pt.Op.concat),
            pt.TealOp(None, pt.Op.store, value.stored_value.slot),
        ])
        value_end.setNextBlock(expected_body)
        expected = value_start
        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