-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.go
140 lines (120 loc) · 3.03 KB
/
pieces.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
package main
import (
"fmt"
"os"
"text/tabwriter"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/ipfs/go-cid"
"github.com/urfave/cli/v2"
)
var piecesCmd = &cli.Command{
Name: "pieces",
Usage: "interact with the piecestore",
Description: "The piecestore is a database that tracks and manages data that is made available to the retrieval market",
Subcommands: []*cli.Command{
piecesListPiecesCmd,
piecesListCidInfosCmd,
piecesInfoCmd,
piecesCidInfoCmd,
},
}
var piecesListPiecesCmd = &cli.Command{
Name: "list-pieces",
Usage: "list registered pieces",
Action: func(cctx *cli.Context) error {
nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
pieceCids, err := nodeApi.PiecesListPieces(ctx)
if err != nil {
return err
}
for _, pc := range pieceCids {
fmt.Println(pc)
}
return nil
},
}
var piecesListCidInfosCmd = &cli.Command{
Name: "list-cids",
Usage: "list registered payload CIDs",
Action: func(cctx *cli.Context) error {
nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
cids, err := nodeApi.PiecesListCidInfos(ctx)
if err != nil {
return err
}
for _, c := range cids {
fmt.Println(c)
}
return nil
},
}
var piecesInfoCmd = &cli.Command{
Name: "piece-info",
Usage: "get registered information for a given piece CID",
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return lcli.ShowHelp(cctx, fmt.Errorf("must specify piece cid"))
}
nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
c, err := cid.Decode(cctx.Args().First())
if err != nil {
return err
}
pi, err := nodeApi.PiecesGetPieceInfo(ctx, c)
if err != nil {
return err
}
fmt.Println("Piece: ", pi.PieceCID)
w := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintln(w, "Deals:\nDealID\tSectorID\tLength\tOffset")
for _, d := range pi.Deals {
fmt.Fprintf(w, "%d\t%d\t%d\t%d\n", d.DealID, d.SectorID, d.Length, d.Offset)
}
return w.Flush()
},
}
var piecesCidInfoCmd = &cli.Command{
Name: "cid-info",
Usage: "get registered information for a given payload CID",
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return lcli.ShowHelp(cctx, fmt.Errorf("must specify payload cid"))
}
nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
c, err := cid.Decode(cctx.Args().First())
if err != nil {
return err
}
ci, err := nodeApi.PiecesGetCIDInfo(ctx, c)
if err != nil {
return err
}
fmt.Println("Info for: ", ci.CID)
w := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintf(w, "PieceCid\tOffset\tSize\n")
for _, loc := range ci.PieceBlockLocations {
fmt.Fprintf(w, "%s\t%d\t%d\n", loc.PieceCID, loc.RelOffset, loc.BlockSize)
}
return w.Flush()
},
}