Skip to content

Commit

Permalink
Panic on hopefully-unreachable condition instead of silently skipping.
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerjohn committed Mar 25, 2021
1 parent b70b103 commit ff636c4
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions types/shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"bytes"
"encoding/binary"
"fmt"

"github.com/lazyledger/nmt/namespace"
)
Expand Down Expand Up @@ -108,6 +109,9 @@ func split(rawData []byte, nid namespace.ID) []NamespacedShare {
return shares
}

// getNextChunk gets the next chunk for contiguous shares
// Precondition: none of the slices in rawDatas is zero-length
// This precondition should always hold at this point since zero-length txs are simply invalid.
func getNextChunk(rawDatas [][]byte, outerIndex int, innerIndex int, width int) ([]byte, int, int, int) {
rawData := make([]byte, 0, width)
startIndex := 0
Expand All @@ -116,11 +120,8 @@ func getNextChunk(rawDatas [][]byte, outerIndex int, innerIndex int, width int)
curIndex := 0
for curIndex < width && outerIndex < len(rawDatas) {
bytesToFetch := min(len(rawDatas[outerIndex])-innerIndex, width-curIndex)
// Prune any extra 0-data (this should never happen, but just in case)
if bytesToFetch == 0 {
innerIndex = 0
outerIndex++
continue
panic(fmt.Sprintf("zero-length contiguous share data is invalid"))
}
if curIndex == 0 {
firstBytesToFetch = bytesToFetch
Expand All @@ -140,17 +141,6 @@ func getNextChunk(rawDatas [][]byte, outerIndex int, innerIndex int, width int)
curIndex += bytesToFetch
}

// Prune any extra 0-data (this should never happen, but just in case)
for outerIndex < len(rawDatas) {
bytesToFetch := len(rawDatas[outerIndex]) - innerIndex
if bytesToFetch == 0 {
innerIndex = 0
outerIndex++
continue
}
break
}

return rawData, outerIndex, innerIndex, startIndex
}

Expand Down

0 comments on commit ff636c4

Please sign in to comment.