-
Notifications
You must be signed in to change notification settings - Fork 885
/
get_shares.go
73 lines (62 loc) · 2.1 KB
/
get_shares.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
package ipld
import (
"context"
"github.com/ipfs/boxo/blockservice"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/celestia-node/share"
)
// GetShare fetches and returns the data for leaf `leafIndex` of root `rootCid`.
func GetShare(
ctx context.Context,
bGetter blockservice.BlockGetter,
rootCid cid.Cid,
leafIndex int,
totalLeafs int, // this corresponds to the extended square width
) (share.Share, error) {
nd, err := GetLeaf(ctx, bGetter, rootCid, leafIndex, totalLeafs)
if err != nil {
return nil, err
}
return leafToShare(nd), nil
}
// GetShares walks the tree of a given root and puts shares into the given 'put' func.
// Does not return any error, and returns/unblocks only on success
// (got all shares) or on context cancellation.
func GetShares(ctx context.Context, bg blockservice.BlockGetter, root cid.Cid, shares int, put func(int, share.Share)) {
putNode := func(i int, leaf format.Node) {
put(i, leafToShare(leaf))
}
GetLeaves(ctx, bg, root, shares, putNode)
}
// GetSharesByNamespace walks the tree of a given root and returns its shares within the given
// Namespace. If a share could not be retrieved, err is not nil, and the returned array
// contains nil shares in place of the shares it was unable to retrieve.
func GetSharesByNamespace(
ctx context.Context,
bGetter blockservice.BlockGetter,
root cid.Cid,
namespace share.Namespace,
maxShares int,
) ([]share.Share, *nmt.Proof, error) {
data := NewNamespaceData(maxShares, namespace, WithLeaves(), WithProofs())
err := data.CollectLeavesByNamespace(ctx, bGetter, root)
if err != nil {
return nil, nil, err
}
leaves := data.Leaves()
shares := make([]share.Share, len(leaves))
for i, leaf := range leaves {
if leaf != nil {
shares[i] = leafToShare(leaf)
}
}
return shares, data.Proof(), err
}
// leafToShare converts an NMT leaf into a Share.
func leafToShare(nd format.Node) share.Share {
// * Additional namespace is prepended so that parity data can be identified with a parity
// namespace, which we cut off
return share.GetData(nd.RawData())
}