コード例 #1
0
ファイル: scratchvar_test.py プロジェクト: algorand/pyteal
def test_scratchvar_type():
    myvar_default = pt.ScratchVar()
    assert myvar_default.storage_type() == pt.TealType.anytype
    assert myvar_default.store(pt.Bytes("value")).type_of() == pt.TealType.none
    assert myvar_default.load().type_of() == pt.TealType.anytype

    with pytest.raises(pt.TealTypeError):
        myvar_default.store(pt.Pop(pt.Int(1)))

    myvar_int = pt.ScratchVar(pt.TealType.uint64)
    assert myvar_int.storage_type() == pt.TealType.uint64
    assert myvar_int.store(pt.Int(1)).type_of() == pt.TealType.none
    assert myvar_int.load().type_of() == pt.TealType.uint64

    with pytest.raises(pt.TealTypeError):
        myvar_int.store(pt.Bytes("value"))

    with pytest.raises(pt.TealTypeError):
        myvar_int.store(pt.Pop(pt.Int(1)))

    myvar_bytes = pt.ScratchVar(pt.TealType.bytes)
    assert myvar_bytes.storage_type() == pt.TealType.bytes
    assert myvar_bytes.store(pt.Bytes("value")).type_of() == pt.TealType.none
    assert myvar_bytes.load().type_of() == pt.TealType.bytes

    with pytest.raises(pt.TealTypeError):
        myvar_bytes.store(pt.Int(0))

    with pytest.raises(pt.TealTypeError):
        myvar_bytes.store(pt.Pop(pt.Int(1)))
コード例 #2
0
ファイル: ecdsa_test.py プロジェクト: algorand/pyteal
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,
        )
コード例 #3
0
ファイル: unaryexpr_test.py プロジェクト: algorand/pyteal
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_seq_invalid():
    with pytest.raises(pt.TealTypeError):
        pt.Seq([pt.Int(1), pt.Pop(pt.Int(2))])

    with pytest.raises(pt.TealTypeError):
        pt.Seq([pt.Int(1), pt.Int(2)])

    with pytest.raises(pt.TealTypeError):
        pt.Seq([pt.Seq([pt.Pop(pt.Int(1)), pt.Int(2)]), pt.Int(3)])
コード例 #5
0
ファイル: subroutine_test.py プロジェクト: algorand/pyteal
def test_decorator():
    assert callable(pt.Subroutine)
    assert callable(pt.Subroutine(pt.TealType.anytype))

    @pt.Subroutine(pt.TealType.none)
    def mySubroutine(a):
        return pt.Return()

    assert isinstance(mySubroutine, pt.SubroutineFnWrapper)

    invocation = mySubroutine(pt.Int(1))
    assert isinstance(invocation, pt.SubroutineCall)

    with pytest.raises(pt.TealInputError):
        mySubroutine()

    with pytest.raises(pt.TealInputError):
        mySubroutine(pt.Int(1), pt.Int(2))

    with pytest.raises(pt.TealInputError):
        mySubroutine(pt.Pop(pt.Int(1)))

    with pytest.raises(pt.TealInputError):
        mySubroutine(1)

    with pytest.raises(pt.TealInputError):
        mySubroutine(a=pt.Int(1))
コード例 #6
0
def test_while_invalid():

    with pytest.raises(TypeError):
        expr = pt.While()

    with pytest.raises(pt.TealCompileError):
        expr = pt.While(pt.Int(2))
        expr.type_of()

    with pytest.raises(pt.TealCompileError):
        expr = pt.While(pt.Int(2))
        expr.__teal__(options)

    with pytest.raises(pt.TealCompileError):
        expr = pt.While(pt.Int(2))
        expr.type_of()

    with pytest.raises(pt.TealCompileError):
        expr = pt.While(pt.Int(2))
        expr.__str__()

    with pytest.raises(pt.TealTypeError):
        expr = pt.While(pt.Int(2)).Do(pt.Int(2))

    with pytest.raises(pt.TealTypeError):
        expr = pt.While(pt.Int(2)).Do(pt.Pop(pt.Int(2)), pt.Int(2))

    with pytest.raises(pt.TealCompileError):
        expr = pt.While(pt.Int(0)).Do(pt.Continue()).Do(pt.Continue())
        expr.__str__()
コード例 #7
0
def test_for_multi():
    i = pt.ScratchVar()
    items = [
        (i.store(pt.Int(0))),
        i.load() < pt.Int(10),
        i.store(i.load() + pt.Int(1)),
        [pt.Pop(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(*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
コード例 #8
0
ファイル: ecdsa_test.py プロジェクト: algorand/pyteal
def test_ecdsa_verify_recovered_pk():
    curve = pt.EcdsaCurve.Secp256k1
    args = [pt.Bytes("data"), pt.Int(1), pt.Bytes("sigA"), pt.Bytes("sigB")]
    pubkey = pt.EcdsaRecover(curve, args[0], args[1], args[2], args[3])
    expr = pt.EcdsaVerify(curve, args[0], args[2], args[3], 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.int, 1),
        pt.TealOp(args[2], pt.Op.byte, '"sigA"'),
        pt.TealOp(args[3], pt.Op.byte, '"sigB"'),
        pt.TealOp(pubkey, pt.Op.ecdsa_pk_recover, 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]),
        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.output_slots[0].load(), pt.Op.load,
                  pubkey.output_slots[0]),
        pt.TealOp(pubkey.output_slots[1].load(), pt.Op.load,
                  pubkey.output_slots[1]),
        pt.TealOp(expr, pt.Op.ecdsa_verify, curve.arg_name),
    ])

    actual, _ = expr.__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(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,
        )
コード例 #9
0
def test_use_seq_if_multiple_overloads_equivalence():
    items = [pt.Pop(pt.Int(1)), pt.Int(2)]
    expr = _use_seq_if_multiple(items)

    expected = pt.Seq(items).__teal__(options)
    actual = expr.__teal__(options)

    assert actual == expected
コード例 #10
0
def test_local_put_invalid():
    with pytest.raises(pt.TealTypeError):
        pt.App.localPut(pt.Txn.sender(), pt.Int(55), pt.Int(5))

    with pytest.raises(pt.TealTypeError):
        pt.App.localPut(pt.Int(1), pt.Int(0), pt.Int(5))

    with pytest.raises(pt.TealTypeError):
        pt.App.localPut(pt.Int(1), pt.Bytes("key"), pt.Pop(pt.Int(1)))
コード例 #11
0
ファイル: gtxn_test.py プロジェクト: algorand/pyteal
def test_gtxn_invalid():
    for f, e in [
        (lambda: pt.Gtxn[-1], pt.TealInputError),
        (lambda: pt.Gtxn[pt.MAX_GROUP_SIZE + 1], pt.TealInputError),
        (lambda: pt.Gtxn[pt.Pop(pt.Int(0))], pt.TealTypeError),
        (lambda: pt.Gtxn[pt.Bytes("index")], pt.TealTypeError),
    ]:
        with pytest.raises(e):
            f()
コード例 #12
0
def test_if_none():
    args = [pt.Int(0), pt.Pop(pt.Txn.sender()), pt.Pop(pt.Txn.receiver())]
    expr = pt.If(args[0], args[1], args[2])
    assert expr.type_of() == pt.TealType.none

    expected, _ = args[0].__teal__(options)
    thenBlockStart, thenBlockEnd = args[1].__teal__(options)
    elseBlockStart, elseBlockEnd = args[2].__teal__(options)
    expectedBranch = pt.TealConditionalBlock([])
    expectedBranch.setTrueBlock(thenBlockStart)
    expectedBranch.setFalseBlock(elseBlockStart)
    expected.setNextBlock(expectedBranch)
    end = pt.TealSimpleBlock([])
    thenBlockEnd.setNextBlock(end)
    elseBlockEnd.setNextBlock(end)

    actual, _ = expr.__teal__(options)

    assert actual == expected
コード例 #13
0
def test_optimize_subroutine_with_global_var():
    global_var = pt.ScratchVar(pt.TealType.uint64)

    @pt.Subroutine(pt.TealType.uint64)
    def add(a1: pt.Expr) -> pt.Expr:
        return pt.Seq(global_var.store(pt.Int(2)), global_var.load() + a1)

    program = pt.Seq([
        pt.If(pt.Txn.sender() == pt.Global.creator_address()).Then(
            pt.Pop(add(pt.Int(1)))),
        global_var.store(pt.Int(5)),
        pt.Approve(),
    ])

    optimize_options = OptimizeOptions()

    # unoptimized
    expected = """#pragma version 4
txn Sender
global CreatorAddress
==
bz main_l2
int 1
callsub add_0
pop
main_l2:
int 5
store 0
int 1
return

// add
add_0:
store 1
int 2
store 0
load 0
load 1
+
retsub
    """.strip()
    actual = pt.compileTeal(program,
                            version=4,
                            mode=pt.Mode.Application,
                            optimize=optimize_options)
    assert actual == expected

    # optimization should not apply to global vars
    optimize_options = OptimizeOptions(scratch_slots=True)
    actual = pt.compileTeal(program,
                            version=4,
                            mode=pt.Mode.Application,
                            optimize=optimize_options)
    assert actual == expected
コード例 #14
0
ファイル: cond_test.py プロジェクト: algorand/pyteal
def test_cond_invalid():
    with pytest.raises(pt.TealInputError):
        pt.Cond()

    with pytest.raises(pt.TealInputError):
        pt.Cond([])

    with pytest.raises(pt.TealInputError):
        pt.Cond([pt.Int(1)], [pt.Int(2), pt.Pop(pt.Txn.receiver())])

    with pytest.raises(pt.TealTypeError):
        pt.Cond([pt.Int(1), pt.Int(2)], [pt.Int(2), pt.Txn.receiver()])

    with pytest.raises(pt.TealTypeError):
        pt.Cond([pt.Arg(0), pt.Int(2)])

    with pytest.raises(pt.TealTypeError):
        pt.Cond([pt.Int(1), pt.Int(2)], [pt.Int(2), pt.Pop(pt.Int(2))])

    with pytest.raises(pt.TealTypeError):
        pt.Cond([pt.Int(1), pt.Pop(pt.Int(1))], [pt.Int(2), pt.Int(2)])
コード例 #15
0
def test_if_invalid_alt_syntax():
    with pytest.raises(pt.TealCompileError):
        expr = pt.If(pt.Int(0)).ElseIf(pt.Int(1))
        expr.__teal__(options)

    with pytest.raises(pt.TealCompileError):
        expr = pt.If(pt.Int(0)).ElseIf(pt.Int(1)).Then(pt.Int(2))
        expr.__teal__(options)

    with pytest.raises(pt.TealCompileError):
        expr = pt.If(pt.Int(0)).Then(pt.Int(1)).ElseIf(pt.Int(2))
        expr.__teal__(options)

    with pytest.raises(pt.TealCompileError):
        expr = pt.If(pt.Int(0)).Then(pt.Int(1)).ElseIf(pt.Int(2))
        expr.__teal__(options)

    with pytest.raises(pt.TealTypeError):
        expr = pt.If(pt.Int(0)).Then(pt.Int(2))
        expr.type_of()

    with pytest.raises(pt.TealTypeError):
        expr = pt.If(pt.Int(0)).Then(pt.Pop(pt.Int(1)), pt.Int(2))
        expr.type_of()

    with pytest.raises(pt.TealInputError):
        pt.If(pt.Int(0)).Else(pt.Int(1)).Then(pt.Int(2))

    with pytest.raises(pt.TealInputError):
        expr = pt.If(pt.Int(0)).Else(pt.Int(1))
        expr.__teal__(options)

    with pytest.raises(pt.TealInputError):
        expr = pt.If(pt.Int(0)).Else(pt.Int(1)).Then(pt.Int(2))

    with pytest.raises(pt.TealInputError):
        expr = pt.If(pt.Int(0)).Else(pt.Int(1)).Else(pt.Int(2))

    with pytest.raises(pt.TealInputError):
        expr = pt.If(pt.Int(0), pt.Pop(pt.Int(1))).Else(pt.Int(2))
コード例 #16
0
def test_optimize_subroutine_with_reserved_local_var():
    local_var = pt.ScratchVar(pt.TealType.uint64, 0)

    @pt.Subroutine(pt.TealType.uint64)
    def add(a1: pt.Expr) -> pt.Expr:
        return pt.Seq(local_var.store(pt.Int(2)), local_var.load() + a1)

    program = pt.Seq([
        pt.If(pt.Txn.sender() == pt.Global.creator_address()).Then(
            pt.Pop(add(pt.Int(1)))),
        pt.Approve(),
    ])

    optimize_options = OptimizeOptions()

    # unoptimized
    expected = """#pragma version 4
txn Sender
global CreatorAddress
==
bz main_l2
int 1
callsub add_0
pop
main_l2:
int 1
return

// add
add_0:
store 1
int 2
store 0
load 0
load 1
+
retsub
    """.strip()
    actual = pt.compileTeal(program,
                            version=4,
                            mode=pt.Mode.Application,
                            optimize=optimize_options)
    assert actual == expected

    # The optimization must skip over the reserved slot id so the expected result
    # hasn't changed.
    optimize_options = OptimizeOptions(scratch_slots=True)
    actual = pt.compileTeal(program,
                            version=4,
                            mode=pt.Mode.Application,
                            optimize=optimize_options)
    assert actual == expected
コード例 #17
0
ファイル: pragma_test.py プロジェクト: algorand/pyteal
def test_pragma_expr_does_not_change():
    without_pragma = pt.Seq(pt.Pop(pt.Add(pt.Int(1), pt.Int(2))),
                            pt.Return(pt.Int(1)))
    pragma = pt.Pragma(without_pragma, compiler_version=">=0.0.0")

    compiled_with_pragma = pt.compileTeal(pragma,
                                          mode=pt.Mode.Application,
                                          version=6)
    compiled_without_pragma = pt.compileTeal(without_pragma,
                                             mode=pt.Mode.Application,
                                             version=6)

    assert compiled_with_pragma == compiled_without_pragma
コード例 #18
0
def test_seq_overloads_equivalence():
    items = [
        pt.App.localPut(pt.Int(0), pt.Bytes("key1"), pt.Int(1)),
        pt.App.localPut(pt.Int(1), pt.Bytes("key2"), pt.Bytes("value2")),
        pt.Pop(pt.Bytes("end")),
    ]
    expr1 = pt.Seq(items)
    expr2 = pt.Seq(*items)

    expected = expr1.__teal__(options)
    actual = expr2.__teal__(options)

    assert actual == expected
コード例 #19
0
ファイル: cond_test.py プロジェクト: algorand/pyteal
def test_cond_two_pred_multi():
    args = [
        pt.Int(1),
        [pt.Pop(pt.Int(1)), pt.Bytes("one")],
        pt.Int(0),
        [pt.Pop(pt.Int(2)), pt.Bytes("zero")],
    ]
    expr = pt.Cond(
        [args[0]] + args[1],
        [args[2]] + args[3],
    )
    assert expr.type_of() == pt.TealType.bytes

    cond1, _ = args[0].__teal__(options)
    pred1, pred1End = pt.Seq(args[1]).__teal__(options)
    cond1Branch = pt.TealConditionalBlock([])
    cond2, _ = args[2].__teal__(options)
    pred2, pred2End = pt.Seq(args[3]).__teal__(options)
    cond2Branch = pt.TealConditionalBlock([])
    end = pt.TealSimpleBlock([])

    cond1.setNextBlock(cond1Branch)
    cond1Branch.setTrueBlock(pred1)
    cond1Branch.setFalseBlock(cond2)
    pred1End.setNextBlock(end)

    cond2.setNextBlock(cond2Branch)
    cond2Branch.setTrueBlock(pred2)
    cond2Branch.setFalseBlock(pt.Err().__teal__(options)[0])
    pred2End.setNextBlock(end)

    expected = cond1

    actual, _ = expr.__teal__(options)

    with pt.TealComponent.Context.ignoreExprEquality():
        assert actual == expected
コード例 #20
0
def test_elseif_multiple_with_multi():
    args = [
        pt.Int(0),
        [pt.Pop(pt.Int(1)), pt.Int(2)],
        pt.Int(3),
        [pt.Pop(pt.Int(4)), pt.Int(5)],
        pt.Int(6),
        [pt.Pop(pt.Int(7)), pt.Int(8)],
        [pt.Pop(pt.Int(9)), pt.Int(10)],
    ]
    expr = (
        pt.If(args[0])
        .Then(*args[1])
        .ElseIf(args[2])
        .Then(*args[3])
        .ElseIf(args[4])
        .Then(*args[5])
        .Else(*args[6])
    )

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

    actual, _ = expr.__teal__(options)

    assert actual == expected
コード例 #21
0
def test_optimize_multi_value():
    # note: this is incorrect usage of the app_global_get_ex opcode
    program = pt.Seq(
        pt.MultiValue(
            pt.Op.app_global_get_ex,
            [pt.TealType.uint64, pt.TealType.uint64],
            immediate_args=[],
            args=[pt.Int(0), pt.Int(1)],
        ).outputReducer(lambda value, hasValue: pt.Pop(value + hasValue)),
        pt.Approve(),
    )

    optimize_options = OptimizeOptions()

    # unoptimized
    expected = """#pragma version 4
int 0
int 1
app_global_get_ex
store 1
store 0
load 0
load 1
+
pop
int 1
return""".strip()
    actual = pt.compileTeal(program,
                            version=4,
                            mode=pt.Mode.Application,
                            optimize=optimize_options)
    assert actual == expected

    # optimized
    expected = """#pragma version 4
int 0
int 1
app_global_get_ex
+
pop
int 1
return""".strip()
    optimize_options = OptimizeOptions(scratch_slots=True)
    actual = pt.compileTeal(program,
                            version=4,
                            mode=pt.Mode.Application,
                            optimize=optimize_options)
    assert actual == expected
コード例 #22
0
def test_else_alt_multi():
    args = [pt.Int(0), pt.Int(1), [pt.Pop(pt.Int(2)), pt.Int(3)]]
    expr = pt.If(args[0]).Then(args[1]).Else(*args[2])

    expected, _ = args[0].__teal__(options)
    thenBlockStart, thenBlockEnd = args[1].__teal__(options)
    elseBlockStart, elseBlockEnd = pt.Seq(*args[2]).__teal__(options)
    expectedBranch = pt.TealConditionalBlock([])
    expectedBranch.setTrueBlock(thenBlockStart)
    expectedBranch.setFalseBlock(elseBlockStart)
    expected.setNextBlock(expectedBranch)
    end = pt.TealSimpleBlock([])
    thenBlockEnd.setNextBlock(end)
    elseBlockEnd.setNextBlock(end)

    actual, _ = expr.__teal__(options)

    assert actual == expected
コード例 #23
0
def test_use_seq_if_multiple():
    # Test a single expression
    items = [pt.Int(1)]
    expr = _use_seq_if_multiple(*items)

    expected = items[0].__teal__(options)
    actual = expr.__teal__(options)

    assert actual == expected

    # Test multiple expressions
    items = [pt.Pop(pt.Int(1)), pt.Int(2)]
    expr = _use_seq_if_multiple(*items)

    expected = pt.Seq(items).__teal__(options)
    actual = expr.__teal__(options)

    assert actual == expected
コード例 #24
0
def test_invalid_for():
    with pytest.raises(TypeError):
        expr = pt.For()

    with pytest.raises(TypeError):
        expr = pt.For(pt.Int(2))

    with pytest.raises(TypeError):
        expr = pt.For(pt.Int(1), pt.Int(2))

    with pytest.raises(pt.TealTypeError):
        i = pt.ScratchVar()
        expr = pt.For(i.store(pt.Int(0)), pt.Int(1), pt.Int(2))
        expr.__teal__(options)

    with pytest.raises(pt.TealCompileError):
        i = pt.ScratchVar()
        expr = pt.For(i.store(pt.Int(0)), pt.Int(1), i.store(i.load() + pt.Int(1)))
        expr.type_of()

    with pytest.raises(pt.TealCompileError):
        i = pt.ScratchVar()
        expr = pt.For(i.store(pt.Int(0)), pt.Int(1), i.store(i.load() + pt.Int(1)))
        expr.__str__()

    with pytest.raises(pt.TealTypeError):
        i = pt.ScratchVar()
        expr = pt.For(i.store(pt.Int(0)), pt.Int(1), i.store(i.load() + pt.Int(1))).Do(
            pt.Int(0)
        )

    with pytest.raises(pt.TealTypeError):
        i = pt.ScratchVar()
        expr = pt.For(i.store(pt.Int(0)), pt.Int(1), i.store(i.load() + pt.Int(1))).Do(
            pt.Pop(pt.Int(1)), pt.Int(2)
        )

    with pytest.raises(pt.TealCompileError):
        expr = (
            pt.For(i.store(pt.Int(0)), pt.Int(1), i.store(i.load() + pt.Int(1)))
            .Do(pt.Continue())
            .Do(pt.Continue())
        )
        expr.__str__()
コード例 #25
0
def test_optimize_dynamic_var():
    myvar = pt.DynamicScratchVar()
    regvar = pt.ScratchVar()
    program = pt.Seq(
        regvar.store(pt.Int(1)),
        myvar.set_index(regvar),
        regvar.store(pt.Int(2)),
        pt.Pop(regvar.load()),
        pt.Approve(),
    )

    optimize_options = OptimizeOptions()

    # unoptimized
    expected = """#pragma version 4
int 1
store 1
int 1
store 0
int 2
store 1
load 1
pop
int 1
return""".strip()
    actual = pt.compileTeal(program,
                            version=4,
                            mode=pt.Mode.Application,
                            optimize=optimize_options)
    assert actual == expected

    # optimization should not change the code because the candidate slot
    # is used by the dynamic slot variable.
    optimize_options = OptimizeOptions(scratch_slots=True)
    actual = pt.compileTeal(program,
                            version=4,
                            mode=pt.Mode.Application,
                            optimize=optimize_options)
    assert actual == expected
コード例 #26
0
def test_while_multi():
    i = pt.ScratchVar()
    i.store(pt.Int(0))
    items = [
        i.load() < pt.Int(2),
        [pt.Pop(pt.Int(1)), i.store(i.load() + pt.Int(1))]
    ]
    expr = pt.While(items[0]).Do(*items[1])
    assert expr.type_of() == pt.TealType.none
    assert not expr.has_return()

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

    expectedBranch.setTrueBlock(do)
    expectedBranch.setFalseBlock(end)
    condEnd.setNextBlock(expectedBranch)
    doEnd.setNextBlock(expected)
    actual, _ = expr.__teal__(options)

    assert actual == expected
コード例 #27
0
def test_seq_three():
    items = [
        pt.App.localPut(pt.Int(0), pt.Bytes("key1"), pt.Int(1)),
        pt.App.localPut(pt.Int(1), pt.Bytes("key2"), pt.Bytes("value2")),
        pt.Pop(pt.Bytes("end")),
    ]
    expr = pt.Seq(items)
    assert expr.type_of() == items[-1].type_of()

    expected, first_end = items[0].__teal__(options)
    second_start, second_end = items[1].__teal__(options)
    first_end.setNextBlock(second_start)
    third_start, _ = items[2].__teal__(options)
    second_end.setNextBlock(third_start)

    expected.addIncoming()
    expected = pt.TealBlock.NormalizeBlocks(expected)

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

    assert actual == expected
コード例 #28
0
def test_global_put_invalid():
    with pytest.raises(pt.TealTypeError):
        pt.App.globalPut(pt.Int(0), pt.Int(5))

    with pytest.raises(pt.TealTypeError):
        pt.App.globalPut(pt.Bytes("key"), pt.Pop(pt.Int(1)))
コード例 #29
0
ファイル: unaryexpr_test.py プロジェクト: algorand/pyteal
def test_pop_invalid():
    expr = pt.Pop(pt.Int(0))
    with pytest.raises(pt.TealTypeError):
        pt.Pop(expr)
コード例 #30
0
ファイル: pre_v6_test.py プロジェクト: algorand/pyteal
def sub_even():
    return pt.Seq(
        pt.Pop(recursiveIsEven(pt.Int(1000))),
        recursiveIsEven(pt.Int(1001)),
    )