-
Notifications
You must be signed in to change notification settings - Fork 893
/
get.go
80 lines (67 loc) · 2.28 KB
/
get.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
package share
import (
"context"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
"github.com/celestiaorg/nmt"
"github.com/celestiaorg/nmt/namespace"
"github.com/celestiaorg/celestia-node/share/ipld"
)
// 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, error) {
nd, err := ipld.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, bGetter blockservice.BlockGetter, root cid.Cid, shares int, put func(int, Share)) {
ctx, span := tracer.Start(ctx, "get-shares")
defer span.End()
putNode := func(i int, leaf format.Node) {
put(i, leafToShare(leaf))
}
ipld.GetLeaves(ctx, bGetter, root, shares, putNode)
}
// GetSharesByNamespace walks the tree of a given root and returns its shares within the given
// namespace.ID. 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,
nID namespace.ID,
maxShares int,
) ([]Share, *nmt.Proof, error) {
ctx, span := tracer.Start(ctx, "get-shares-by-namespace")
defer span.End()
data := ipld.NewNamespaceData(maxShares, nID, ipld.WithLeaves(), ipld.WithProofs())
err := data.CollectLeavesByNamespace(ctx, bGetter, root)
if err != nil {
return nil, nil, err
}
leaves := data.Leaves()
shares := make([]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 {
// * Additional namespace is prepended so that parity data can be identified with a parity
// namespace, which we cut off
return nd.RawData()[NamespaceSize:]
}