-
Notifications
You must be signed in to change notification settings - Fork 19
/
lassie-wrapper.go
183 lines (160 loc) · 4.81 KB
/
lassie-wrapper.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"errors"
"fmt"
"net/url"
"github.com/filecoin-project/lassie/pkg/indexerlookup"
"github.com/filecoin-project/lassie/pkg/lassie"
"github.com/filecoin-project/lassie/pkg/net/host"
"github.com/filecoin-project/lassie/pkg/retriever"
"github.com/filecoin-project/lassie/pkg/types"
"github.com/ipfs/go-cid"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/storage"
"github.com/ipld/go-ipld-prime/storage/memstore"
"github.com/libp2p/go-libp2p"
"github.com/urfave/cli/v2"
"k8s.io/klog/v2"
)
type lassieWrapper struct {
lassie *lassie.Lassie
}
type RwStorage interface {
storage.Storage
storage.WritableStorage
storage.ReadableStorage
}
func (l *lassieWrapper) GetNodeByCid(ctx context.Context, wantedCid cid.Cid) ([]byte, error) {
store := NewWrappedMemStore()
{
_, err := l.Fetch(
ctx,
wantedCid,
"",
types.DagScopeBlock,
store,
)
if err != nil {
return nil, err
}
}
for key, node := range store.Bag {
if cid.MustParse([]byte(key)).Equals(wantedCid) {
return node, nil
}
}
return nil, nil
}
func (l *lassieWrapper) GetSubgraph(ctx context.Context, wantedCid cid.Cid) (*WrappedMemStore, error) {
store := NewWrappedMemStore()
{
_, err := l.Fetch(
ctx,
wantedCid,
"",
types.DagScopeAll,
store,
)
if err != nil {
return nil, err
}
}
return store, nil
}
func (l *lassieWrapper) Fetch(
ctx context.Context,
rootCid cid.Cid,
path string,
dagScope types.DagScope,
store RwStorage,
) (*types.RetrievalStats, error) {
request, err := types.NewRequestForPath(store, rootCid, path, types.DagScope(dagScope))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
request.PreloadLinkSystem = cidlink.DefaultLinkSystem()
request.PreloadLinkSystem.SetReadStorage(store)
request.PreloadLinkSystem.SetWriteStorage(store)
request.PreloadLinkSystem.TrustedStorage = true
stats, err := l.lassie.Fetch(ctx, request, func(types.RetrievalEvent) {})
if err != nil {
return stats, fmt.Errorf("failed to fetch: %w", err)
}
return stats, nil
}
func newLassieWrapper(cctx *cli.Context) (*lassieWrapper, error) {
ctx := cctx.Context
providerTimeout := cctx.Duration("provider-timeout")
globalTimeout := cctx.Duration("global-timeout")
bitswapConcurrency := cctx.Int("bitswap-concurrency")
eventRecorderURL := cctx.String("event-recorder-url")
authToken := cctx.String("event-recorder-auth")
instanceID := cctx.String("event-recorder-instance-id")
providerTimeoutOpt := lassie.WithProviderTimeout(providerTimeout)
host, err := host.InitHost(ctx, []libp2p.Option{})
if err != nil {
return nil, err
}
hostOpt := lassie.WithHost(host)
lassieOpts := []lassie.LassieOption{providerTimeoutOpt, hostOpt}
if len(fetchProviderAddrInfos) > 0 {
finderOpt := lassie.WithFinder(retriever.NewDirectCandidateFinder(host, fetchProviderAddrInfos))
if cctx.IsSet("ipni-endpoint") {
klog.Warning("Ignoring ipni-endpoint flag since direct provider is specified")
}
lassieOpts = append(lassieOpts, finderOpt)
} else if cctx.IsSet("ipni-endpoint") {
endpoint := cctx.String("ipni-endpoint")
endpointUrl, err := url.Parse(endpoint)
if err != nil {
klog.Error("Failed to parse IPNI endpoint as URL", "err", err)
return nil, fmt.Errorf("cannot parse given IPNI endpoint %s as valid URL: %w", endpoint, err)
}
finder, err := indexerlookup.NewCandidateFinder(indexerlookup.WithHttpEndpoint(endpointUrl))
if err != nil {
klog.Error("Failed to instantiate IPNI candidate finder", "err", err)
return nil, err
}
lassieOpts = append(lassieOpts, lassie.WithFinder(finder))
klog.Info("Using explicit IPNI endpoint to find candidates", "endpoint", endpoint)
}
if len(providerBlockList) > 0 {
lassieOpts = append(lassieOpts, lassie.WithProviderBlockList(providerBlockList))
}
if len(protocols) > 0 {
lassieOpts = append(lassieOpts, lassie.WithProtocols(protocols))
}
if globalTimeout > 0 {
lassieOpts = append(lassieOpts, lassie.WithGlobalTimeout(globalTimeout))
}
if bitswapConcurrency > 0 {
lassieOpts = append(lassieOpts, lassie.WithBitswapConcurrency(bitswapConcurrency))
}
lassie, err := lassie.NewLassie(ctx, lassieOpts...)
if err != nil {
return nil, err
}
// create and subscribe an event recorder API if configured
setupLassieEventRecorder(ctx, eventRecorderURL, authToken, instanceID, lassie)
return &lassieWrapper{
lassie: lassie,
}, nil
}
type WrappedMemStore struct {
*memstore.Store
}
func NewWrappedMemStore() *WrappedMemStore {
return &WrappedMemStore{Store: &memstore.Store{}}
}
func (w *WrappedMemStore) Each(ctx context.Context, cb func(cid.Cid, []byte) error) error {
for key, value := range w.Store.Bag {
if err := cb(cid.MustParse([]byte(key)), value); err != nil {
if errors.Is(err, ErrStopIteration) {
return nil
}
return err
}
}
return nil
}