forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.go
86 lines (72 loc) · 1.86 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package blob
import (
"bytes"
"sort"
"github.com/tendermint/tendermint/types"
"github.com/celestiaorg/celestia-app/pkg/shares"
"github.com/celestiaorg/celestia-node/share"
)
// SharesToBlobs takes raw shares and converts them to the blobs.
func SharesToBlobs(rawShares []share.Share) ([]*Blob, error) {
if len(rawShares) == 0 {
return nil, ErrBlobNotFound
}
appShares, err := toAppShares(rawShares...)
if err != nil {
return nil, err
}
return parseShares(appShares)
}
// parseShares takes shares and converts them to the []*Blob.
func parseShares(appShrs []shares.Share) ([]*Blob, error) {
shareSequences, err := shares.ParseShares(appShrs, true)
if err != nil {
return nil, err
}
// ensure that sequence length is not 0
if len(shareSequences) == 0 {
return nil, ErrBlobNotFound
}
blobs := make([]*Blob, len(shareSequences))
for i, sequence := range shareSequences {
data, err := sequence.RawData()
if err != nil {
return nil, err
}
if len(data) == 0 {
continue
}
shareVersion, err := sequence.Shares[0].Version()
if err != nil {
return nil, err
}
blob, err := NewBlob(shareVersion, sequence.Namespace.Bytes(), data)
if err != nil {
return nil, err
}
blobs[i] = blob
}
return blobs, nil
}
// 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
}