Skip to content

Commit

Permalink
sanityCheck: only calculate siblingHash if necessary, refactor and re…
Browse files Browse the repository at this point in the history
…duce nesting

Only calculate siblingHash to avoid wasting cycles when unneeded.
Also while here, reverse conditional the code to reduce nesting and
to make reading much simpler.

Fixes celestiaorg#56
  • Loading branch information
odeke-em committed Aug 15, 2021
1 parent a99c0f5 commit 6f91d95
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@ func (proof *SparseMerkleProof) sanityCheck(th *treeHasher) bool {
}

// Check that the sibling data hashes to the first side node if not nil
if proof.SiblingData != nil {
siblingHash := th.digest(proof.SiblingData)
if proof.SideNodes != nil && len(proof.SideNodes) > 0 {
if !bytes.Equal(proof.SideNodes[0], siblingHash) {
return false
}
}
if proof.SiblingData == nil {
return true
}

return true
if len(proof.SideNodes) == 0 {
return true
}

siblingHash := th.digest(proof.SiblingData)
return bytes.Equal(proof.SideNodes[0], siblingHash)
}

// SparseCompactMerkleProof is a compact Merkle proof for an element in a SparseMerkleTree.
Expand Down

0 comments on commit 6f91d95

Please sign in to comment.