Skip to content

Commit

Permalink
remove panic from IntervalDigestFromBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan committed Mar 11, 2021
1 parent b22170d commit a5c8085
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
9 changes: 5 additions & 4 deletions namespace/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ type IntervalDigest struct {
// IntervalDigestFromBytes is the inverse function to IntervalDigest.Bytes().
// In other words, it assumes that the passed in digestBytes are of the form
// d.Min() || d.Max() || d.Hash() for an IntervalDigest d.
func IntervalDigestFromBytes(nIDLen IDSize, digestBytes []byte) IntervalDigest {
func IntervalDigestFromBytes(nIDLen IDSize, digestBytes []byte) (IntervalDigest, error) {
if len(digestBytes) < int(2*nIDLen) {
panic(fmt.Sprintf("invalid digest: %x, expected length >= %v, got: %v",
digestBytes, 2*nIDLen, len(digestBytes)))
return IntervalDigest{}, fmt.Errorf("invalid digest: %x, expected length >= %v, got: %v",
digestBytes, 2*nIDLen, len(digestBytes))
}

return IntervalDigest{
min: digestBytes[:nIDLen],
max: digestBytes[nIDLen : 2*nIDLen],
digest: digestBytes[2*nIDLen:],
}
}, nil
}

func (d IntervalDigest) Min() ID {
Expand Down
8 changes: 4 additions & 4 deletions namespace/digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func TestIntervalDigestFromBytesPanic(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
func() {
//nolint:errcheck
defer func() { recover() }()
IntervalDigestFromBytes(tt.nIDLen, tt.digestBytes)
t.Errorf("should have panicked")
_, err := IntervalDigestFromBytes(tt.nIDLen, tt.digestBytes)
if err == nil {
t.Errorf("should have errored")
}
}()
})
}
Expand Down
2 changes: 1 addition & 1 deletion nmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ func (n *NamespacedMerkleTree) Push(id namespace.ID, data []byte) error {
// Return the namespaced Merkle Tree's root together with the
// min. and max. namespace ID.
func (n *NamespacedMerkleTree) Root() namespace.IntervalDigest {
return namespace.IntervalDigestFromBytes(n.NamespaceSize(), n.computeRoot(0, len(n.leaves)))
return mustIntervalDigestFromBytes(n.NamespaceSize(), n.computeRoot(0, len(n.leaves)))
}

func (n NamespacedMerkleTree) computeRoot(start, end int) []byte {
Expand Down
14 changes: 12 additions & 2 deletions proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,15 @@ func (proof Proof) verifyLeafHashes(nth *Hasher, verifyCompleteness bool, nID na
if verifyCompleteness {
// leftSubtrees contains the subtree roots upto [0, r.Start)
for _, subtree := range leftSubtrees {
leftSubTreeMax := namespace.IntervalDigestFromBytes(nth.NamespaceSize(), subtree).Max()
leftSubTreeMax := mustIntervalDigestFromBytes(nth.NamespaceSize(), subtree).Max()
if nID.LessOrEqual(leftSubTreeMax) {
return false
}
}
// rightSubtrees only contains the subtrees after [0, r.Start)
rightSubtrees := proof.nodes
for _, subtree := range rightSubtrees {
rightSubTreeMin := namespace.IntervalDigestFromBytes(nth.NamespaceSize(), subtree).Min()
rightSubTreeMin := mustIntervalDigestFromBytes(nth.NamespaceSize(), subtree).Min()
if rightSubTreeMin.LessOrEqual(nID) {
return false
}
Expand Down Expand Up @@ -220,3 +220,13 @@ func nextSubtreeSize(start, end uint64) int {
}
return 1 << uint(ideal)
}

// mustIntervalDigestFromBytes optimistially converts bytes to IntervalDigest or panics
func mustIntervalDigestFromBytes(idlen namespace.IDSize, bytes []byte) namespace.IntervalDigest {
id, err := namespace.IntervalDigestFromBytes(idlen, bytes)
if err != nil {
panic(err)
}

return id
}

0 comments on commit a5c8085

Please sign in to comment.