Skip to content

Commit

Permalink
fix: Enforce copying of arc4 mutable types when passing them around s…
Browse files Browse the repository at this point in the history
…o that developers are not fooled by the illusion of reference imutability as arc4 encoded data is still a value type underneath
  • Loading branch information
tristanmenzel authored and achidlow committed Jan 12, 2024
1 parent 23029fe commit c20e621
Show file tree
Hide file tree
Showing 49 changed files with 2,810 additions and 2,276 deletions.
22 changes: 16 additions & 6 deletions examples/arc4_types/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,33 @@ def approval_program(self) -> bool:
return True

@subroutine
def mutation_problems(self) -> None:
def mutating_copies(self) -> None:
my_array = StaticArray(UInt8(1), UInt8(2), UInt8(3), UInt8(4))
my_struct = TestStruct(
b_val=Bool(True),
u_val=UInt8(50),
s_val_1=String("Happy"),
s_val_2=String("Days"),
)

my_array_copy = my_array
my_array_copy = my_array.copy()

my_array[2] = UInt8(5)

assert my_array_copy[2] == UInt8(5)

self.other_routine(my_array)
self.other_routine(my_array.copy(), my_struct.copy())

assert my_array[1] == UInt8(5)
assert my_array_copy[1] == UInt8(5)
assert my_array[1] == UInt8(2)
assert my_array_copy[1] == UInt8(2)
assert my_struct.s_val_1 == String("Happy")

@subroutine
def other_routine(self, array: StaticArray[UInt8, typing.Literal[4]]) -> None:
def other_routine(
self, array: StaticArray[UInt8, typing.Literal[4]], struct: TestStruct
) -> None:
array[1] = UInt8(5)
struct.s_val_1 = String("AARRGH!")

def clear_state_program(self) -> bool:
return True
Expand Down
1,242 changes: 621 additions & 621 deletions examples/arc4_types/out/mutation.approval.debug.teal

Large diffs are not rendered by default.

2,864 changes: 1,432 additions & 1,432 deletions examples/arc4_types/out/mutation.approval_unoptimized.debug.teal

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions examples/arc4_types/out/mutation.awst

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/arc4_types/out/mutation.clear.debug.teal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/arc4_types/out/mutation.clear_unoptimized.debug.teal

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions examples/arc4_types/out/mutation_Arc4MutationContract.cssa.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions examples/arc4_types/out/mutation_Arc4MutationContract.final.ir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c20e621

Please sign in to comment.