Skip to content

Commit 9d186e3

Browse files
authored
feat: dagstore lookup-cid command (#751)
* add dagstore lookup-cid command * add subcommand * rename command
1 parent 85347fc commit 9d186e3

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

cmd/boostd/dagstore.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/filecoin-project/lotus/lib/tablewriter"
9+
"github.com/ipfs/go-cid"
910

1011
"github.com/fatih/color"
1112
bapi "github.com/filecoin-project/boost/api"
@@ -25,6 +26,7 @@ var dagstoreCmd = &cli.Command{
2526
dagstoreListShardsCmd,
2627
dagstoreGcCmd,
2728
dagstoreDestroyShardCmd,
29+
dagstoreLookupCmd,
2830
},
2931
}
3032

@@ -309,3 +311,47 @@ var dagstoreDestroyShardCmd = &cli.Command{
309311
return nil
310312
},
311313
}
314+
315+
var dagstoreLookupCmd = &cli.Command{
316+
Name: "lookup-piece-cid",
317+
ArgsUsage: "[key]",
318+
Usage: "Performs a reverse lookup with payload CID to get Piece CID",
319+
Aliases: []string{"lpc"},
320+
Flags: []cli.Flag{
321+
&cli.BoolFlag{
322+
Name: "color",
323+
Usage: "use color in display output",
324+
DefaultText: "depends on output being a TTY",
325+
},
326+
},
327+
Action: func(cctx *cli.Context) error {
328+
if cctx.IsSet("color") {
329+
color.NoColor = !cctx.Bool("color")
330+
}
331+
332+
if cctx.NArg() != 1 {
333+
return fmt.Errorf("must provide a single payload CID")
334+
}
335+
336+
napi, closer, err := bcli.GetBoostAPI(cctx)
337+
if err != nil {
338+
return err
339+
}
340+
defer closer()
341+
342+
ctx := lcli.ReqContext(cctx)
343+
344+
shardKey := cctx.Args().First()
345+
payloadCid, err := cid.Parse(shardKey)
346+
if err != nil {
347+
return fmt.Errorf("Unable to parse the provided CID: %s", shardKey)
348+
}
349+
pieceCid, err := napi.BoostDagstorePiecesContainingMultihash(ctx, payloadCid.Hash())
350+
if err != nil {
351+
return err
352+
}
353+
354+
fmt.Printf("Given CID was found in the following pieces: %s", pieceCid)
355+
return nil
356+
},
357+
}

0 commit comments

Comments
 (0)