-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Prevent iteration of arc4 containers with mutable items
BREAKING CHANGE: Direct iteration of arc4 containers with mutable items is no longer possible due to issues with the reference vs value semantics, instead use `for <index> in urange(<array>.length)` and access/update elements by index.
- Loading branch information
Showing
20 changed files
with
716 additions
and
685 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,29 @@ | ||
import typing | ||
|
||
from algopy import ( | ||
BigUInt, | ||
arc4, | ||
op, | ||
subroutine, | ||
) | ||
from algopy import BigUInt, Bytes, arc4, op, subroutine, urange | ||
|
||
Bytes32: typing.TypeAlias = arc4.StaticArray[arc4.Byte, typing.Literal[32]] | ||
Proof: typing.TypeAlias = arc4.DynamicArray[Bytes32] | ||
|
||
|
||
class MerkleTree(arc4.ARC4Contract): | ||
@subroutine | ||
def hash_pair(self, a: Bytes32, b: Bytes32) -> Bytes32: | ||
hash_bytes = op.sha256( | ||
a.bytes + b.bytes | ||
if BigUInt.from_bytes(a.bytes) < BigUInt.from_bytes(b.bytes) | ||
else b.bytes + a.bytes | ||
) | ||
return Bytes32.from_bytes(hash_bytes) | ||
|
||
@subroutine | ||
def compute_root_hash(self, proof: Proof, leaf: Bytes32) -> Bytes32: | ||
computed = leaf.copy() | ||
for proof_hash in proof: | ||
computed = self.hash_pair(computed, proof_hash) | ||
return computed | ||
|
||
@arc4.abimethod(create=True) | ||
def create(self, root: Bytes32) -> None: | ||
self.root = root.copy() | ||
self.root = root.bytes | ||
|
||
@arc4.abimethod | ||
def verify(self, proof: Proof, leaf: Bytes32) -> bool: | ||
return self.root == self.compute_root_hash(proof, leaf) | ||
return self.root == compute_root_hash(proof, leaf.bytes) | ||
|
||
|
||
@subroutine | ||
def compute_root_hash(proof: Proof, leaf: Bytes) -> Bytes: | ||
computed = leaf | ||
for idx in urange(proof.length): | ||
computed = hash_pair(computed, proof[idx].bytes) | ||
return computed | ||
|
||
|
||
@subroutine | ||
def hash_pair(a: Bytes, b: Bytes) -> Bytes: | ||
return op.sha256(a + b if BigUInt.from_bytes(a) < BigUInt.from_bytes(b) else b + a) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.