Skip to content

Commit

Permalink
fix: ParseShares failed to append last sequence (#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Oct 14, 2022
1 parent 6d79f68 commit 35e465e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions pkg/shares/share_merging.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,38 @@ type ShareSequence struct {
Shares []Share
}

func ParseShares(rawShares [][]byte) (result []ShareSequence, err error) {
func ParseShares(rawShares [][]byte) ([]ShareSequence, error) {
sequences := []ShareSequence{}
currentSequence := ShareSequence{}

for _, rawShare := range rawShares {
share, err := NewShare(rawShare)
if err != nil {
return result, err
return sequences, err
}
infoByte, err := share.InfoByte()
if err != nil {
return result, err
return sequences, err
}
if infoByte.IsMessageStart() {
if len(currentSequence.Shares) > 0 {
result = append(result, currentSequence)
sequences = append(sequences, currentSequence)
}
currentSequence = ShareSequence{
Shares: []Share{share},
NamespaceID: share.NamespaceID(),
}
} else {
if !bytes.Equal(currentSequence.NamespaceID, share.NamespaceID()) {
return result, fmt.Errorf("share sequence %v has inconsistent namespace IDs with share %v", currentSequence, share)
return sequences, fmt.Errorf("share sequence %v has inconsistent namespace IDs with share %v", currentSequence, share)
}
currentSequence.Shares = append(currentSequence.Shares, share)
}
}

return result, nil
if len(currentSequence.Shares) > 0 {
sequences = append(sequences, currentSequence)
}

return sequences, nil
}
2 changes: 1 addition & 1 deletion pkg/shares/share_merging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestParseShares(t *testing.T) {
if tt.expectErr && err == nil {
t.Errorf("ParseShares() error %v, expectErr %v", err, tt.expectErr)
}
if reflect.DeepEqual(got, tt.want) {
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseShares() got %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 35e465e

Please sign in to comment.