-
Notifications
You must be signed in to change notification settings - Fork 892
/
store.go
122 lines (107 loc) · 3.48 KB
/
store.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package getters
import (
"context"
"errors"
"fmt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/celestiaorg/rsmt2d"
"github.com/celestiaorg/celestia-node/header"
"github.com/celestiaorg/celestia-node/libs/utils"
"github.com/celestiaorg/celestia-node/share"
"github.com/celestiaorg/celestia-node/share/eds"
"github.com/celestiaorg/celestia-node/share/ipld"
)
var _ share.Getter = (*StoreGetter)(nil)
// StoreGetter is a share.Getter that retrieves shares from an eds.Store. No results are saved to
// the eds.Store after retrieval.
type StoreGetter struct {
store *eds.Store
}
// NewStoreGetter creates a new share.Getter that retrieves shares from an eds.Store.
func NewStoreGetter(store *eds.Store) *StoreGetter {
return &StoreGetter{
store: store,
}
}
// GetShare gets a single share at the given EDS coordinates from the eds.Store through the
// corresponding CAR-level blockstore.
func (sg *StoreGetter) GetShare(ctx context.Context, header *header.ExtendedHeader, row, col int) (share.Share, error) {
dah := header.DAH
var err error
ctx, span := tracer.Start(ctx, "store/get-share", trace.WithAttributes(
attribute.Int("row", row),
attribute.Int("col", col),
))
defer func() {
utils.SetStatusAndEnd(span, err)
}()
upperBound := len(dah.RowRoots)
if row >= upperBound || col >= upperBound {
err := share.ErrOutOfBounds
span.RecordError(err)
return nil, err
}
root, leaf := ipld.Translate(dah, row, col)
bs, err := sg.store.CARBlockstore(ctx, dah.Hash())
if errors.Is(err, eds.ErrNotFound) {
// convert error to satisfy getter interface contract
err = share.ErrNotFound
}
if err != nil {
return nil, fmt.Errorf("getter/store: failed to retrieve blockstore: %w", err)
}
defer func() {
if err := bs.Close(); err != nil {
log.Warnw("closing blockstore", "err", err)
}
}()
// wrap the read-only CAR blockstore in a getter
blockGetter := eds.NewBlockGetter(bs)
s, err := ipld.GetShare(ctx, blockGetter, root, leaf, len(dah.RowRoots))
if errors.Is(err, ipld.ErrNodeNotFound) {
// convert error to satisfy getter interface contract
err = share.ErrNotFound
}
if err != nil {
return nil, fmt.Errorf("getter/store: failed to retrieve share: %w", err)
}
return s, nil
}
// GetEDS gets the EDS identified by the given root from the EDS store.
func (sg *StoreGetter) GetEDS(
ctx context.Context, header *header.ExtendedHeader,
) (data *rsmt2d.ExtendedDataSquare, err error) {
ctx, span := tracer.Start(ctx, "store/get-eds")
defer func() {
utils.SetStatusAndEnd(span, err)
}()
data, err = sg.store.Get(ctx, header.DAH.Hash())
if errors.Is(err, eds.ErrNotFound) {
// convert error to satisfy getter interface contract
err = share.ErrNotFound
}
if err != nil {
return nil, fmt.Errorf("getter/store: failed to retrieve eds: %w", err)
}
return data, nil
}
// GetSharesByNamespace gets all EDS shares in the given namespace from the EDS store through the
// corresponding CAR-level blockstore.
func (sg *StoreGetter) GetSharesByNamespace(
ctx context.Context,
header *header.ExtendedHeader,
namespace share.Namespace,
) (shares share.NamespacedShares, err error) {
ctx, span := tracer.Start(ctx, "store/get-shares-by-namespace", trace.WithAttributes(
attribute.String("namespace", namespace.String()),
))
defer func() {
utils.SetStatusAndEnd(span, err)
}()
ns, err := eds.RetrieveNamespaceFromStore(ctx, sg.store, header.DAH, namespace)
if err != nil {
return nil, fmt.Errorf("getter/store: %w", err)
}
return ns, nil
}