-
Notifications
You must be signed in to change notification settings - Fork 65
/
retrieve_cmd.go
353 lines (299 loc) · 9.53 KB
/
retrieve_cmd.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"fmt"
"net/url"
"os"
"strings"
"time"
blockstore "github.com/ipfs/boxo/blockstore"
flatfs "github.com/ipfs/go-ds-flatfs"
levelds "github.com/ipfs/go-ds-leveldb"
"github.com/mitchellh/go-homedir"
"github.com/dustin/go-humanize"
clinode "github.com/filecoin-project/boost/cli/node"
"github.com/filecoin-project/boost/cmd"
"github.com/filecoin-project/boost/markets/utils"
rc "github.com/filecoin-project/boost/retrievalmarket/client"
"github.com/filecoin-project/boostd-data/shared/cliutil"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/ipfs/boxo/blockservice"
offline "github.com/ipfs/boxo/exchange/offline"
"github.com/ipfs/boxo/files"
"github.com/ipfs/boxo/ipld/merkledag"
unixfile "github.com/ipfs/boxo/ipld/unixfs/file"
"github.com/ipfs/go-cid"
"github.com/ipld/go-car"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/node/basicnode"
"github.com/ipld/go-ipld-prime/traversal"
"github.com/ipld/go-ipld-prime/traversal/selector"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
textselector "github.com/ipld/go-ipld-selector-text-lite"
"github.com/urfave/cli/v2"
"golang.org/x/term"
"golang.org/x/xerrors"
)
var flagProvider = &cli.StringFlag{
Name: "provider",
Aliases: []string{"p"},
Usage: "The miner ID of the Storage Provider",
Required: true,
}
var flagOutput = &cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "The path to the output file",
}
var flagCar = &cli.BoolFlag{
Name: "car",
Usage: "Whether to output the data as a CAR file",
}
var flagDmPathSel = &cli.StringFlag{
Name: "datamodel-path-selector",
Usage: "a rudimentary (DM-level-only) text-path selector, allowing for sub-selection within a deal",
}
var retrieveCmd = &cli.Command{
Name: "retrieve",
Usage: "Retrieve a file by payload CID from a miner",
ArgsUsage: "<cid>",
Flags: []cli.Flag{
flagProvider,
flagOutput,
flagDmPathSel,
flagCar,
},
Action: func(cctx *cli.Context) error {
ctx := cliutil.ReqContext(cctx)
cidStr := cctx.Args().First()
if cidStr == "" {
return fmt.Errorf("please specify a payload CID to retrieve")
}
c, err := cid.Decode(cidStr)
if err != nil {
return fmt.Errorf("decoding retrieval cid %s: %w", cidStr, err)
}
cfgdir := cctx.String(cmd.FlagRepo.Name)
cfgdir, err = homedir.Expand(cfgdir)
if err != nil {
return fmt.Errorf("expanding homedir: %w", err)
}
node, err := clinode.Setup(cfgdir)
if err != nil {
return fmt.Errorf("setting up CLI node: %w", err)
}
dmSelText := textselector.Expression(cctx.String(flagDmPathSel.Name))
miner, err := address.NewFromString(cctx.String(flagProvider.Name))
if err != nil {
return fmt.Errorf("failed to parse miner %s: %w", cctx.String(flagProvider.Name), err)
}
// Get the output path of the file
output := cctx.String("output")
if output == "" {
output = cidStr
if dmSelText != "" {
output += "_" + url.QueryEscape(string(dmSelText))
}
}
// The output path must not exist already
_, err = os.Stat(output)
if err == nil {
return fmt.Errorf("there is already a file at output path %s", output)
}
if !os.IsNotExist(err) {
return fmt.Errorf("checking output path %s: %w", output, err)
}
// Get subselector node
var selNode ipld.Node
if dmSelText != "" {
ssb := builder.NewSelectorSpecBuilder(basicnode.Prototype.Any)
selspec, err := textselector.SelectorSpecFromPath(
dmSelText,
true,
// URGH - this is a direct copy from https://github.com/filecoin-project/go-fil-markets/blob/v1.12.0/shared/selectors.go#L10-L16
// Unable to use it because we need the SelectorSpec, and markets exposes just a reified node
ssb.ExploreRecursive(
selector.RecursionLimitNone(),
ssb.ExploreAll(ssb.ExploreRecursiveEdge()),
),
)
if err != nil {
return xerrors.Errorf("failed to parse text-selector '%s': %w", dmSelText, err)
}
selNode = selspec.Node()
}
api, closer, err := lcli.GetGatewayAPI(cctx)
if err != nil {
return fmt.Errorf("setting up gateway connection: %w", err)
}
defer closer()
addr, err := node.Wallet.GetDefault()
if err != nil {
return err
}
// Set up a blockstore in a temp directory
bstoreTmpDir, err := os.MkdirTemp("", "retrieve-blockstore")
if err != nil {
return fmt.Errorf("setting up temp dir: %w", err)
}
// Clean up the temp directory before exiting
defer os.RemoveAll(bstoreTmpDir)
bstoreDatastore, err := flatfs.CreateOrOpen(bstoreTmpDir, flatfs.NextToLast(3), false)
bstore := blockstore.NewBlockstore(bstoreDatastore, blockstore.NoPrefix())
if err != nil {
return fmt.Errorf("could not open blockstore: %w", err)
}
// Set up a datastore in a temp directory
datastoreTmpDir, err := os.MkdirTemp("", "retrieve-datastore")
if err != nil {
return fmt.Errorf("setting up temp dir: %w", err)
}
// Clean up the temp directory before exiting
defer os.RemoveAll(datastoreTmpDir)
ds, err := levelds.NewDatastore(datastoreTmpDir, nil)
if err != nil {
return fmt.Errorf("could not create datastore: %w", err)
}
// Create the retrieval client
fc, err := rc.NewClient(node.Host, api, node.Wallet, addr, bstore, ds, cfgdir)
if err != nil {
return err
}
query, err := fc.RetrievalQuery(ctx, miner, c)
if err != nil {
return fmt.Errorf("retrieval query for miner %s failed: %v", miner, err)
}
proposal, err := rc.RetrievalProposalForAsk(query, c, selNode)
if err != nil {
return fmt.Errorf("Failed to create retrieval proposal with candidate miner %s: %v", miner, err)
}
// Retrieve the data
stats, err := fc.RetrieveContentWithProgressCallback(
ctx,
miner,
proposal,
func(bytesReceived_ uint64) {
printProgress(bytesReceived_)
},
)
if err != nil {
return fmt.Errorf("Failed to retrieve content with candidate miner %s: %v", miner, err)
}
printRetrievalStats(&FILRetrievalStats{RetrievalStats: *stats})
dservOffline := merkledag.NewDAGService(blockservice.New(bstore, offline.Exchange(bstore)))
// if we used a selector - need to find the sub-root the user actually wanted to retrieve
if dmSelText != "" {
var subRootFound bool
// no err check - we just compiled this before starting, but now we do not wrap a `*`
selspec, _ := textselector.SelectorSpecFromPath(dmSelText, true, nil) //nolint:errcheck
if err := utils.TraverseDag(
ctx,
dservOffline,
c,
selspec.Node(),
func(p traversal.Progress, n ipld.Node, r traversal.VisitReason) error {
if r == traversal.VisitReason_SelectionMatch {
if p.LastBlock.Path.String() != p.Path.String() {
return xerrors.Errorf("unsupported selection path '%s' does not correspond to a node boundary (a.k.a. CID link)", p.Path.String())
}
cidLnk, castOK := p.LastBlock.Link.(cidlink.Link)
if !castOK {
return xerrors.Errorf("cidlink cast unexpectedly failed on '%s'", p.LastBlock.Link.String())
}
c = cidLnk.Cid
subRootFound = true
}
return nil
},
); err != nil {
return xerrors.Errorf("error while locating partial retrieval sub-root: %w", err)
}
if !subRootFound {
return xerrors.Errorf("path selection '%s' does not match a node within %s", dmSelText, c)
}
}
dnode, err := dservOffline.Get(ctx, c)
if err != nil {
return err
}
if cctx.Bool(flagCar.Name) {
// Write file as car file
file, err := os.Create(output + ".car")
if err != nil {
return err
}
_ = car.WriteCar(ctx, dservOffline, []cid.Cid{c}, file)
fmt.Println("Saved .car output to", output+".car")
} else {
// Otherwise write file as UnixFS File
ufsFile, err := unixfile.NewUnixfsFile(ctx, dservOffline, dnode)
if err != nil {
return err
}
if err := files.WriteTo(ufsFile, output); err != nil {
return err
}
fmt.Println("Saved output to", output)
}
return nil
},
}
type RetrievalStats interface {
GetByteSize() uint64
GetDuration() time.Duration
GetAverageBytesPerSecond() uint64
}
type FILRetrievalStats struct {
rc.RetrievalStats
}
func (stats *FILRetrievalStats) GetByteSize() uint64 {
return stats.Size
}
func (stats *FILRetrievalStats) GetDuration() time.Duration {
return stats.Duration
}
func (stats *FILRetrievalStats) GetAverageBytesPerSecond() uint64 {
return stats.AverageSpeed
}
func printProgress(bytesReceived uint64) {
str := fmt.Sprintf("%v (%v)", bytesReceived, humanize.IBytes(bytesReceived))
termWidth, _, err := term.GetSize(int(os.Stdin.Fd()))
strLen := len(str)
if err == nil {
if strLen < termWidth {
// If the string is shorter than the terminal width, pad right side
// with spaces to remove old text
str = strings.Join([]string{str, strings.Repeat(" ", termWidth-strLen)}, "")
} else if strLen > termWidth {
// If the string doesn't fit in the terminal, cut it down to a size
// that fits
str = str[:termWidth]
}
}
fmt.Fprintf(os.Stderr, "%s\r", str)
}
func printRetrievalStats(stats RetrievalStats) {
switch stats := stats.(type) {
case *FILRetrievalStats:
fmt.Printf(`RETRIEVAL STATS (FIL)
-----
Size: %v (%v)
Duration: %v
Average Speed: %v (%v/s)
Ask Price: %v (%v)
Total Payment: %v (%v)
Num Payments: %v
Peer: %v
`,
stats.Size, humanize.IBytes(stats.Size),
stats.Duration,
stats.AverageSpeed, humanize.IBytes(stats.AverageSpeed),
stats.AskPrice, types.FIL(stats.AskPrice),
stats.TotalPayment, types.FIL(stats.TotalPayment),
stats.NumPayments,
stats.Peer,
)
}
}