algorand / pyteal

Algorand Smart Contracts in Python
https://pyteal.readthedocs.io
MIT License
285 stars 131 forks source link

DO NOT MERGE: True-idempotency-fails #654

Closed tzaffi closed 1 year ago

tzaffi commented 1 year ago

Reproducible Example of Scratchslot Instability

With the goal of solving PyTeal compilation non-idempotency, this PR shows how compilations of unrelated programs can cause unpredictability of output. To reproduce the inconsistency, run the following:

pytest -sv tests/unit/sourcemap_test.py

Observe something like the following in the output

tests/unit/sourcemap_test.py::test_config FAILED
tests/unit/sourcemap_test.py::test_idempotent ----------unified_diff(a1, a2, "approval1.teal", "approval2.teal")----------
--- approval1.teal

+++ approval2.teal

@@ -27,13 +27,13 @@

 assert
 txna ApplicationArgs 1
 btoi
-store 6
+store 5
 txna ApplicationArgs 2
 int 0
 getbyte
-store 8
-load 6
-load 8
+store 7
+load 5
+load 7
 callsub withdraw_3
 int 1
 return
@@ -50,9 +50,9 @@

 int 0
 getbyte
 callsub getBalance_2
-store 4
+store 3
 byte 0x151f7c75
-load 4
+load 3
 itob
 concat
 log
@@ -167,11 +167,11 @@

 // deposit
 deposit_1:
-store 3
+store 4
 store 2
 load 2
 gtxns Sender
-load 3
+load 4
 txnas Accounts
 ==
 assert
@@ -180,10 +180,10 @@

 global CurrentApplicationAddress
 ==
 assert
-load 3
-txnas Accounts
-byte "balance"
-load 3
+load 4
+txnas Accounts
+byte "balance"
+load 4
 txnas Accounts
 byte "balance"
 app_local_get
@@ -202,23 +202,23 @@

 // withdraw
 withdraw_3:
-store 7
-store 5
-txn Sender
-byte "balance"
-txn Sender
-byte "balance"
-app_local_get
-load 5
+store 8
+store 6
+txn Sender
+byte "balance"
+txn Sender
+byte "balance"
+app_local_get
+load 6
 -
 app_local_put
 itxn_begin
 int pay
 itxn_field TypeEnum
-load 7
+load 8
 txnas Accounts
 itxn_field Receiver
-load 5
+load 6
 itxn_field Amount
 int 0
 itxn_field Fee
FAILED
tzaffi commented 1 year ago

too many diffs, and too late to change the base branch