-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdatacap.go
442 lines (392 loc) · 11.3 KB
/
datacap.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package main
import (
"context"
"errors"
"fmt"
"os"
"sort"
"strconv"
"text/tabwriter"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/builtin"
verifregtypes "github.com/filecoin-project/go-state-types/builtin/v10/verifreg"
"github.com/filecoin-project/venus/venus-shared/actors"
v1 "github.com/filecoin-project/venus/venus-shared/api/chain/v1"
"github.com/filecoin-project/venus/venus-shared/types"
sharedutils "github.com/filecoin-project/venus/venus-shared/utils"
msgparser "github.com/filecoin-project/venus/venus-shared/utils/msg_parser"
cli2 "github.com/ipfs-force-community/droplet/v2/cli"
"github.com/urfave/cli/v2"
)
var datacapCmds = &cli.Command{
Name: "datacap",
Usage: "datacap helper commands",
Subcommands: []*cli.Command{
datacapExtendCmd,
datacapClaimsListCmd,
datacapAllocationListCmd,
},
}
var datacapExtendCmd = &cli.Command{
Name: "extend",
Usage: "extend datacap expiration",
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "max-term",
Usage: "datacap max term",
Required: true,
},
&cli.Uint64SliceFlag{
Name: "claimId",
Usage: "claim id array",
},
&cli.StringFlag{
Name: "from",
Usage: "address to send the message",
},
&cli.BoolFlag{
Name: "auto",
Usage: "automatically select eligible datacap renewals",
},
&cli.Int64Flag{
Name: "expiration-cutoff",
Usage: "when use --auto flag, skip datacap whose current expiration is more than <cutoff> epochs from now (infinity if unspecified)",
},
},
ArgsUsage: "<provider address>",
Action: func(cliCtx *cli.Context) error {
if cliCtx.Args().Len() == 0 {
return fmt.Errorf("must pass provider")
}
api, closer, err := cli2.NewMarketClientNode(cliCtx)
if err != nil {
return err
}
defer closer()
ctx := cli2.ReqContext(cliCtx)
fapi, fcloser, err := cli2.NewFullNode(cliCtx, cli2.OldClientRepoPath)
if err != nil {
return err
}
defer fcloser()
provider, err := address.NewFromString(cliCtx.Args().First())
if err != nil {
return fmt.Errorf("parse provider failed: %v", err)
}
providerID, err := addressToActorID(provider)
if err != nil {
return err
}
var fromAddr address.Address
if cliCtx.IsSet("from") {
fromAddr, err = address.NewFromString(cliCtx.String("from"))
if err != nil {
return err
}
} else {
fromAddr, err = api.DefaultAddress(ctx)
if err != nil {
return err
}
}
idAddr, err := fapi.StateLookupID(ctx, fromAddr, types.EmptyTSK)
if err != nil {
return err
}
fromID, err := addressToActorID(idAddr)
if err != nil {
return err
}
termMax := abi.ChainEpoch(cliCtx.Int64("max-term"))
if termMax > types.MaximumVerifiedAllocationTerm {
return fmt.Errorf("max term %d greater than %d", termMax, types.MaximumVerifiedAllocationTerm)
}
head, err := fapi.ChainHead(ctx)
if err != nil {
return err
}
claims, err := fapi.StateGetClaims(ctx, provider, types.EmptyTSK)
if err != nil {
return err
}
claimTermsParams := &types.ExtendClaimTermsParams{}
if cliCtx.Bool("auto") {
cutoff := abi.ChainEpoch(cliCtx.Int64("expiration-cutoff"))
for id, claim := range claims {
if err := checkClaim(ctx, fapi, head, provider, fromID, termMax, claim, cutoff); err != nil {
if !errors.Is(err, errNotNeedExtend) {
fmt.Printf("check claim %d error: %v\n", id, err)
}
continue
}
claimTermsParams.Terms = append(claimTermsParams.Terms, types.ClaimTerm{
Provider: providerID,
ClaimId: id,
TermMax: termMax,
})
}
} else if cliCtx.IsSet("claimId") {
claimIds := cliCtx.Uint64Slice("claimId")
for _, id := range claimIds {
claim, ok := claims[types.ClaimId(id)]
if !ok {
continue
}
if err := checkClaim(ctx, fapi, head, provider, fromID, termMax, claim, -1); err != nil {
if !errors.Is(err, errNotNeedExtend) {
fmt.Printf("check claim %d error: %v\n", id, err)
}
continue
}
claimTermsParams.Terms = append(claimTermsParams.Terms, types.ClaimTerm{
Provider: providerID,
ClaimId: types.ClaimId(id),
TermMax: termMax,
})
}
} else {
return fmt.Errorf("must pass --claimId flag or --auto flag")
}
if len(claimTermsParams.Terms) == 0 {
fmt.Println("no claim need extend")
return nil
}
params, err := actors.SerializeParams(claimTermsParams)
if err != nil {
return err
}
msg := types.Message{
From: fromAddr,
To: builtin.VerifiedRegistryActorAddr,
Method: builtin.MethodsVerifiedRegistry.ExtendClaimTerms,
Params: params,
}
msgCID, err := api.MessagerPushMessage(ctx, &msg, nil)
if err != nil {
return fmt.Errorf("push message error: %v", err)
}
fmt.Printf("wait message: %v\n", msgCID)
msgLookup, err := api.MessagerWaitMessage(ctx, msgCID)
if err != nil {
return err
}
if msgLookup.Receipt.ExitCode.IsError() {
return fmt.Errorf("message execute error, exit code: %v", msgLookup.Receipt.ExitCode)
}
if err := sharedutils.LoadBuiltinActors(ctx, fapi); err != nil {
return err
}
parser, err := msgparser.NewMessageParser(fapi)
if err != nil {
return err
}
_, ret, err := parser.ParseMessage(ctx, &msg, &msgLookup.Receipt)
if err != nil {
return fmt.Errorf("parse message error: %v", err)
}
claimTermsReturn, ok := ret.(*verifregtypes.ExtendClaimTermsReturn)
if !ok {
return fmt.Errorf("expect type %T, actual type %T", &verifregtypes.ExtendClaimTermsReturn{}, ret)
}
if len(claimTermsReturn.FailCodes) > 0 {
w := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintln(w, "\nError occurred:\nClaimID\tErrorCode")
for _, failCode := range claimTermsReturn.FailCodes {
fmt.Fprintf(w, "%d\t%d\n", claimTermsParams.Terms[failCode.Idx], failCode.Code)
}
return w.Flush()
}
return nil
},
}
var errNotNeedExtend = fmt.Errorf("not need extend")
func checkClaim(ctx context.Context,
fapi v1.FullNode,
head *types.TipSet,
provider address.Address,
fromID abi.ActorID,
termMax abi.ChainEpoch,
claim types.Claim,
cutoff abi.ChainEpoch,
) error {
if claim.Client != fromID {
return fmt.Errorf("client %d not match form actor id %d", claim.Client, fromID)
}
if claim.TermMax >= termMax {
return fmt.Errorf("new term max(%d) smaller than old term max(%d)", termMax, claim.TermMax)
}
expiration := claim.TermStart + claim.TermMax - head.Height()
if expiration <= 0 {
// already expiration
return fmt.Errorf("claim already expiration")
}
// if cutoff is negative number, skip check
if cutoff >= 0 {
if expiration > cutoff {
return errNotNeedExtend
}
}
sectorExpiration, err := fapi.StateSectorExpiration(ctx, provider, claim.Sector, types.EmptyTSK)
if err != nil {
return fmt.Errorf("got sector %d expiration failed: %v", claim.Sector, err)
} else if sectorExpiration.OnTime <= head.Height() ||
(sectorExpiration.Early != 0 && sectorExpiration.Early <= head.Height()) {
return fmt.Errorf("sector already expiration")
}
return nil
}
type claimWithID struct {
id uint64
types.Claim
}
var datacapClaimsListCmd = &cli.Command{
Name: "list-claim",
Usage: "list claims by provider address",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "client",
Usage: "output only claims containing client",
},
},
ArgsUsage: "<provider address>",
Action: func(cliCtx *cli.Context) error {
if cliCtx.Args().Len() == 0 {
return fmt.Errorf("must pass provider address")
}
fapi, fcloser, err := cli2.NewFullNode(cliCtx, cli2.OldClientRepoPath)
if err != nil {
return err
}
defer fcloser()
ctx := cli2.ReqContext(cliCtx)
provider, err := address.NewFromString(cliCtx.Args().First())
if err != nil {
return err
}
claims, err := fapi.StateGetClaims(ctx, provider, types.EmptyTSK)
if err != nil {
return err
}
if len(claims) == 0 {
return nil
}
client := abi.ActorID(0)
if cliCtx.IsSet("client") {
clientAddr, err := address.NewFromString(cliCtx.String("client"))
if err != nil {
return err
}
idAddr, err := fapi.StateLookupID(ctx, clientAddr, types.EmptyTSK)
if err != nil {
return err
}
client, err = addressToActorID(idAddr)
if err != nil {
return err
}
}
claimWithIDs := make([]claimWithID, 0, len(claims))
for id, claim := range claims {
if client != 0 && client != claim.Client {
continue
}
claimWithIDs = append(claimWithIDs, claimWithID{
id: uint64(id),
Claim: claim,
})
}
sort.Slice(claimWithIDs, func(i, j int) bool {
return claimWithIDs[i].id < claimWithIDs[j].id
})
w := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintln(w, "ClaimID\tProvider\tClient\tExpiration\tSize\tTermMin\tTermMax\tTermStart\tSector\tData")
for _, claim := range claimWithIDs {
fmt.Fprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%v\n", claim.id, claim.Provider, claim.Client, claim.TermStart+claim.TermMax,
claim.Size, claim.TermMin, claim.TermMax, claim.TermStart, claim.Sector, claim.Data)
}
return w.Flush()
},
}
type allocationWithID struct {
id uint64
types.Allocation
}
var datacapAllocationListCmd = &cli.Command{
Name: "list-allocation",
Usage: "list allocations by client address",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "provider",
Usage: "output only allocations containing provider",
},
},
ArgsUsage: "<client address>",
Action: func(cliCtx *cli.Context) error {
if cliCtx.Args().Len() == 0 {
return fmt.Errorf("must pass client address")
}
fapi, fcloser, err := cli2.NewFullNode(cliCtx, cli2.OldClientRepoPath)
if err != nil {
return err
}
defer fcloser()
ctx := cli2.ReqContext(cliCtx)
client, err := address.NewFromString(cliCtx.Args().First())
if err != nil {
return err
}
allocations, err := fapi.StateGetAllocations(ctx, client, types.EmptyTSK)
if err != nil {
return err
}
if len(allocations) == 0 {
return nil
}
provider := abi.ActorID(0)
if cliCtx.IsSet("provider") {
providerAddr, err := address.NewFromString(cliCtx.String("provider"))
if err != nil {
return err
}
idAddr, err := fapi.StateLookupID(ctx, providerAddr, types.EmptyTSK)
if err != nil {
return err
}
provider, err = addressToActorID(idAddr)
if err != nil {
return err
}
}
allocationWithIDs := make([]allocationWithID, 0, len(allocations))
for id, allocation := range allocations {
if provider != 0 && provider != allocation.Provider {
continue
}
allocationWithIDs = append(allocationWithIDs, allocationWithID{
id: uint64(id),
Allocation: allocation,
})
}
sort.Slice(allocationWithIDs, func(i, j int) bool {
return allocationWithIDs[i].id < allocationWithIDs[j].id
})
w := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprintln(w, "AllocationID\tClient\tProvider\tExpiration\tSize\tTermMin\tTermMax\tData")
for _, allocation := range allocationWithIDs {
fmt.Fprintf(w, "%d\t%d\t%d\t%d\t%d\t%d\t%d\t%v\n", allocation.id, allocation.Client, allocation.Provider, allocation.Expiration,
allocation.Size, allocation.TermMin, allocation.TermMax, allocation.Data)
}
return w.Flush()
},
}
func addressToActorID(addr address.Address) (abi.ActorID, error) {
if addr.Protocol() != address.ID {
return 0, fmt.Errorf("%s not id address", addr)
}
id, err := strconv.ParseUint(addr.String()[2:], 10, 64)
if err != nil {
return 0, err
}
return abi.ActorID(id), nil
}