Skip to content

Commit

Permalink
Merge pull request #4 from theStack/dedup_parent_of_single_child_logic
Browse files Browse the repository at this point in the history
refactor: deduplicate parent hash propagation for empty (`None`) childs
  • Loading branch information
kcalvinalvin authored Nov 1, 2022
2 parents 072c6ed + 69289ff commit 03f0774
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions pytreexo.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def delete(self, dels: [bytes], proof: Proof):


def parent_hash(left, right):
if left is None: return right
if right is None: return left
return hashlib.new('sha512_256', left+right).digest()


Expand Down Expand Up @@ -157,21 +159,14 @@ def calculate_roots(numleaves: int, dels: [bytes], proof: Proof) -> [bytes]:

next_hash = bytes
if sib_present:
if cur_hash is None:
next_hash = sib_hash
elif sib_hash is None:
next_hash = cur_hash
else:
next_hash = parent_hash(cur_hash, sib_hash)
next_hash = parent_hash(cur_hash, sib_hash)
else:
proofhash = proof.proof.pop(0)

next_hash = proofhash
if cur_hash is not None:
if pos & 1 == 0:
next_hash = parent_hash(cur_hash, proofhash)
else:
next_hash = parent_hash(proofhash, cur_hash)
if pos & 1 == 0:
next_hash = parent_hash(cur_hash, proofhash)
else:
next_hash = parent_hash(proofhash, cur_hash)

next_hashes.append(next_hash)
next_positions.append(parent(pos, total_rows))
Expand Down

0 comments on commit 03f0774

Please sign in to comment.