From abcf2b1018367e2388b4fe1cb196c8e3feddf7e3 Mon Sep 17 00:00:00 2001 From: Hang Su Date: Tue, 10 May 2022 11:04:26 -0400 Subject: [PATCH 1/4] wire continue-next to do block --- pyteal/ast/while_.py | 2 +- pyteal/compiler/compiler_test.py | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/pyteal/ast/while_.py b/pyteal/ast/while_.py index 5dd6fd09b..5be293fd9 100644 --- a/pyteal/ast/while_.py +++ b/pyteal/ast/while_.py @@ -52,7 +52,7 @@ def __teal__(self, options: "CompileOptions"): block.setNextBlock(end) for block in continueBlocks: - block.setNextBlock(doStart) + block.setNextBlock(condStart) return condStart, end diff --git a/pyteal/compiler/compiler_test.py b/pyteal/compiler/compiler_test.py index 24f30dc18..5d04171e7 100644 --- a/pyteal/compiler/compiler_test.py +++ b/pyteal/compiler/compiler_test.py @@ -652,11 +652,10 @@ def test_compile_continue(): int 3 < bz main_l4 -main_l2: load 0 int 2 == -bnz main_l2 +bnz main_l1 load 0 int 1 + @@ -744,11 +743,11 @@ def test_compile_continue_break_nested(): expected = """#pragma version 4 int 0 store 0 +main_l1: load 0 int 10 < -bz main_l2 -main_l1: +bz main_l3 load 0 int 1 + @@ -757,7 +756,7 @@ def test_compile_continue_break_nested(): int 4 < bnz main_l1 -main_l2: +main_l3: int 1 return """.strip() @@ -799,7 +798,6 @@ def test_compile_continue_break_nested(): int 10 < bz main_l8 -main_l2: load 0 int 8 == @@ -813,7 +811,7 @@ def test_compile_continue_break_nested(): load 0 int 5 < -bnz main_l2 +bnz main_l1 load 0 int 1 + From 8d50c4f1531a79918348c109f2898f98cc0f63db Mon Sep 17 00:00:00 2001 From: Hang Su Date: Tue, 10 May 2022 11:30:25 -0400 Subject: [PATCH 2/4] update compiler test --- pyteal/compiler/compiler_test.py | 36 +++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/pyteal/compiler/compiler_test.py b/pyteal/compiler/compiler_test.py index 5d04171e7..85101fd61 100644 --- a/pyteal/compiler/compiler_test.py +++ b/pyteal/compiler/compiler_test.py @@ -664,7 +664,41 @@ def test_compile_continue(): main_l4: int 1 return - """.strip() + """.strip() + actual = pt.compileTeal( + program, pt.Mode.Application, version=4, assembleConstants=False + ) + assert expected == actual + + # pt.While + program = pt.Seq( + i.store(pt.Int(0)), + pt.While(i.load() < pt.Int(30)).Do( + pt.Seq( + i.store(i.load() + pt.Int(1)), + pt.Continue(), + ) + ), + pt.Return(pt.Int(1)) + ) + + expected = """#pragma version 4 +int 0 +store 0 +main_l1: +load 0 +int 30 +< +bz main_l3 +load 0 +int 1 ++ +store 0 +b main_l1 +main_l3: +int 1 +return + """.strip() actual = pt.compileTeal( program, pt.Mode.Application, version=4, assembleConstants=False ) From 3c9c14b067405c806836425e4bfa2eb868938085 Mon Sep 17 00:00:00 2001 From: Hang Su Date: Tue, 10 May 2022 11:31:41 -0400 Subject: [PATCH 3/4] formatting you bummer --- pyteal/compiler/compiler_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyteal/compiler/compiler_test.py b/pyteal/compiler/compiler_test.py index 85101fd61..0d6fdbe09 100644 --- a/pyteal/compiler/compiler_test.py +++ b/pyteal/compiler/compiler_test.py @@ -679,7 +679,7 @@ def test_compile_continue(): pt.Continue(), ) ), - pt.Return(pt.Int(1)) + pt.Return(pt.Int(1)), ) expected = """#pragma version 4 From 613d6169a181f39de1998bd51a45f7c086fbf848 Mon Sep 17 00:00:00 2001 From: Hang Su Date: Tue, 10 May 2022 13:12:23 -0400 Subject: [PATCH 4/4] add one semantic test --- tests/integration/graviton_test.py | 43 +++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/tests/integration/graviton_test.py b/tests/integration/graviton_test.py index b620962ff..da1a9ab5a 100644 --- a/tests/integration/graviton_test.py +++ b/tests/integration/graviton_test.py @@ -13,6 +13,9 @@ Mode, ScratchVar, Seq, + While, + Continue, + Return, Subroutine, SubroutineFnWrapper, TealType, @@ -770,9 +773,47 @@ def euclid(x, y): Invariant(predicate).validates(property, inputs, inspectors) +def blackbox_pyteal_while_continue_test(): + @Blackbox(input_types=[TealType.uint64]) + @Subroutine(TealType.uint64) + def while_continue_accumulation(n): + i = ScratchVar(TealType.uint64) + return Seq( + i.store(Int(0)), + While(i.load() < n).Do( + Seq( + i.store(i.load() + Int(1)), + Continue(), + ) + ), + Return(i.load()), + ) + + approval_lsig = blackbox_pyteal(while_continue_accumulation, Mode.Signature) + lsig_teal = compileTeal(approval_lsig(), Mode.Signature, version=6) + algod = algod_with_assertion() + + for x in range(30): + args = [x] + lsig_result = DryRunExecutor.dryrun_logicsig(algod, lsig_teal, args) + if x == 0: + assert not lsig_result.passed() + else: + assert lsig_result.passed() + + assert lsig_result.stack_top() == x, lsig_result.report( + args, "stack_top() gave unexpected results for lsig" + ) + + @pytest.mark.parametrize( "example", - [blackbox_pyteal_example1, blackbox_pyteal_example2, blackbox_pyteal_example3], + [ + blackbox_pyteal_example1, + blackbox_pyteal_example2, + blackbox_pyteal_example3, + blackbox_pyteal_while_continue_test, + ], ) def test_blackbox_pyteal_examples(example): example()