/
helper.go
56 lines (47 loc) · 1.29 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package blob
import (
"bytes"
"sort"
"github.com/tendermint/tendermint/types"
"github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/celestia-node/share"
)
// BlobsToShares accepts blobs and convert them to the Shares.
func BlobsToShares(blobs ...*Blob) ([]share.Share, error) {
b := make([]types.Blob, len(blobs))
for i, blob := range blobs {
namespace := blob.Namespace()
b[i] = types.Blob{
NamespaceVersion: namespace[0],
NamespaceID: namespace[1:],
Data: blob.Data,
ShareVersion: uint8(blob.ShareVersion),
}
}
sort.Slice(b, func(i, j int) bool {
val := bytes.Compare(b[i].NamespaceID, b[j].NamespaceID)
return val < 0
})
rawShares, err := shares.SplitBlobs(b...)
if err != nil {
return nil, err
}
return shares.ToBytes(rawShares), nil
}
// toAppShares converts node's raw shares to the app shares, skipping padding
func toAppShares(shrs ...share.Share) ([]shares.Share, error) {
appShrs := make([]shares.Share, 0, len(shrs))
for _, shr := range shrs {
bShare, err := shares.NewShare(shr)
if err != nil {
return nil, err
}
appShrs = append(appShrs, *bShare)
}
return appShrs, nil
}
func calculateIndex(rowLength, blobIndex int) (row, col int) {
row = blobIndex / rowLength
col = blobIndex - (row * rowLength)
return
}