-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
verifreg.go
532 lines (436 loc) · 12.4 KB
/
verifreg.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
package main
import (
"encoding/hex"
"fmt"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
verifregtypes "github.com/filecoin-project/go-state-types/builtin/v8/verifreg"
"github.com/filecoin-project/go-state-types/crypto"
verifreg2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/blockstore"
"github.com/filecoin-project/lotus/build/buildconstants"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/adt"
"github.com/filecoin-project/lotus/chain/actors/builtin/multisig"
"github.com/filecoin-project/lotus/chain/actors/builtin/verifreg"
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
)
var verifRegCmd = &cli.Command{
Name: "verifreg",
Usage: "Interact with the verified registry actor",
Flags: []cli.Flag{},
Subcommands: []*cli.Command{
verifRegAddVerifierFromMsigCmd,
verifRegAddVerifierFromAccountCmd,
verifRegVerifyClientCmd,
verifRegListVerifiersCmd,
verifRegCheckClientCmd,
verifRegCheckVerifierCmd,
verifRegRemoveVerifiedClientDataCapCmd,
},
}
var verifRegAddVerifierFromMsigCmd = &cli.Command{
Name: "add-verifier",
Usage: "make a given account a verifier",
ArgsUsage: "<message sender> <new verifier> <allowance>",
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 3 {
return lcli.IncorrectNumArgs(cctx)
}
sender, err := address.NewFromString(cctx.Args().Get(0))
if err != nil {
return err
}
verifier, err := address.NewFromString(cctx.Args().Get(1))
if err != nil {
return err
}
allowance, err := types.BigFromString(cctx.Args().Get(2))
if err != nil {
return err
}
// TODO: ActorUpgrade: Abstract
params, err := actors.SerializeParams(&verifreg2.AddVerifierParams{Address: verifier, Allowance: allowance})
if err != nil {
return err
}
srv, err := lcli.GetFullNodeServices(cctx)
if err != nil {
return err
}
defer srv.Close() //nolint:errcheck
api := srv.FullNodeAPI()
ctx := lcli.ReqContext(cctx)
vrk, err := api.StateVerifiedRegistryRootKey(ctx, types.EmptyTSK)
if err != nil {
return err
}
proto, err := api.MsigPropose(ctx, vrk, verifreg.Address, big.Zero(), sender, uint64(verifreg.Methods.AddVerifier), params)
if err != nil {
return err
}
sm, _, err := srv.PublishMessage(ctx, proto, false)
if err != nil {
return err
}
msgCid := sm.Cid()
fmt.Printf("message sent, now waiting on cid: %s\n", msgCid)
mwait, err := api.StateWaitMsg(ctx, msgCid, uint64(cctx.Int("confidence")), policy.ChainFinality, true)
if err != nil {
return err
}
if mwait.Receipt.ExitCode.IsError() {
return fmt.Errorf("failed to add verifier: %d", mwait.Receipt.ExitCode)
}
//TODO: Internal msg might still have failed
return nil
},
}
var verifRegAddVerifierFromAccountCmd = &cli.Command{
Name: "add-verifier-from-account",
Usage: "make a given account a verifier",
ArgsUsage: "<verifier root key> <new verifier> <allowance>",
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 3 {
return lcli.IncorrectNumArgs(cctx)
}
sender, err := address.NewFromString(cctx.Args().Get(0))
if err != nil {
return err
}
verifier, err := address.NewFromString(cctx.Args().Get(1))
if err != nil {
return err
}
allowance, err := types.BigFromString(cctx.Args().Get(2))
if err != nil {
return err
}
// TODO: ActorUpgrade: Abstract
params, err := actors.SerializeParams(&verifreg2.AddVerifierParams{Address: verifier, Allowance: allowance})
if err != nil {
return err
}
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
msg := &types.Message{
To: verifreg.Address,
From: sender,
Method: verifreg.Methods.AddVerifier,
Params: params,
}
smsg, err := api.MpoolPushMessage(ctx, msg, nil)
if err != nil {
return err
}
fmt.Printf("message sent, now waiting on cid: %s\n", smsg.Cid())
mwait, err := api.StateWaitMsg(ctx, smsg.Cid(), buildconstants.MessageConfidence)
if err != nil {
return err
}
if mwait.Receipt.ExitCode.IsError() {
return fmt.Errorf("failed to add verified client: %d", mwait.Receipt.ExitCode)
}
return nil
},
}
var verifRegVerifyClientCmd = &cli.Command{
Name: "verify-client",
Usage: "make a given account a verified client",
Hidden: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "from",
Usage: "specify your verifier address to send the message from",
},
},
Action: func(cctx *cli.Context) error {
fmt.Println("DEPRECATED: This behavior is being moved to `lotus filplus`")
froms := cctx.String("from")
if froms == "" {
return fmt.Errorf("must specify from address with --from")
}
fromk, err := address.NewFromString(froms)
if err != nil {
return err
}
if cctx.NArg() != 2 {
return lcli.IncorrectNumArgs(cctx)
}
target, err := address.NewFromString(cctx.Args().Get(0))
if err != nil {
return err
}
allowance, err := types.BigFromString(cctx.Args().Get(1))
if err != nil {
return err
}
params, err := actors.SerializeParams(&verifreg2.AddVerifiedClientParams{Address: target, Allowance: allowance})
if err != nil {
return err
}
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
msg := &types.Message{
To: verifreg.Address,
From: fromk,
Method: verifreg.Methods.AddVerifiedClient,
Params: params,
}
smsg, err := api.MpoolPushMessage(ctx, msg, nil)
if err != nil {
return err
}
fmt.Printf("message sent, now waiting on cid: %s\n", smsg.Cid())
mwait, err := api.StateWaitMsg(ctx, smsg.Cid(), buildconstants.MessageConfidence)
if err != nil {
return err
}
if mwait.Receipt.ExitCode.IsError() {
return fmt.Errorf("failed to add verified client: %d", mwait.Receipt.ExitCode)
}
return nil
},
}
var verifRegListVerifiersCmd = &cli.Command{
Name: "list-verifiers",
Usage: "list all verifiers",
Hidden: true,
Action: func(cctx *cli.Context) error {
fmt.Println("DEPRECATED: This behavior is being moved to `lotus filplus`")
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
act, err := api.StateGetActor(ctx, verifreg.Address, types.EmptyTSK)
if err != nil {
return err
}
apibs := blockstore.NewAPIBlockstore(api)
store := adt.WrapStore(ctx, cbor.NewCborStore(apibs))
st, err := verifreg.Load(store, act)
if err != nil {
return err
}
return st.ForEachVerifier(func(addr address.Address, dcap abi.StoragePower) error {
_, err := fmt.Printf("%s: %s\n", addr, dcap)
return err
})
},
}
var verifRegCheckClientCmd = &cli.Command{
Name: "check-client",
Usage: "check verified client remaining bytes",
Hidden: true,
Action: func(cctx *cli.Context) error {
fmt.Println("DEPRECATED: This behavior is being moved to `lotus filplus`")
if !cctx.Args().Present() {
return fmt.Errorf("must specify client address to check")
}
caddr, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
}
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
dcap, err := api.StateVerifiedClientStatus(ctx, caddr, types.EmptyTSK)
if err != nil {
return err
}
if dcap == nil {
return xerrors.Errorf("client %s is not a verified client", err)
}
fmt.Println(*dcap)
return nil
},
}
var verifRegCheckVerifierCmd = &cli.Command{
Name: "check-verifier",
Usage: "check verifiers remaining bytes",
Hidden: true,
Action: func(cctx *cli.Context) error {
fmt.Println("DEPRECATED: This behavior is being moved to `lotus filplus`")
if !cctx.Args().Present() {
return fmt.Errorf("must specify verifier address to check")
}
vaddr, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
}
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
head, err := api.ChainHead(ctx)
if err != nil {
return err
}
vid, err := api.StateLookupID(ctx, vaddr, head.Key())
if err != nil {
return err
}
act, err := api.StateGetActor(ctx, verifreg.Address, head.Key())
if err != nil {
return err
}
apibs := blockstore.NewAPIBlockstore(api)
store := adt.WrapStore(ctx, cbor.NewCborStore(apibs))
st, err := verifreg.Load(store, act)
if err != nil {
return err
}
found, dcap, err := st.VerifierDataCap(vid)
if err != nil {
return err
}
if !found {
return fmt.Errorf("not found")
}
fmt.Println(dcap)
return nil
},
}
var verifRegRemoveVerifiedClientDataCapCmd = &cli.Command{
Name: "remove-verified-client-data-cap",
Usage: "Remove data cap from verified client",
ArgsUsage: "<message sender> <client address> <allowance to remove> <verifier 1 address> <verifier 1 signature> <verifier 2 address> <verifier 2 signature>",
Action: func(cctx *cli.Context) error {
if cctx.NArg() != 7 {
return lcli.IncorrectNumArgs(cctx)
}
srv, err := lcli.GetFullNodeServices(cctx)
if err != nil {
return err
}
defer srv.Close() //nolint:errcheck
api := srv.FullNodeAPI()
ctx := lcli.ReqContext(cctx)
sender, err := address.NewFromString(cctx.Args().Get(0))
if err != nil {
return err
}
client, err := address.NewFromString(cctx.Args().Get(1))
if err != nil {
return err
}
allowanceToRemove, err := types.BigFromString(cctx.Args().Get(2))
if err != nil {
return err
}
verifier1Addr, err := address.NewFromString(cctx.Args().Get(3))
if err != nil {
return err
}
verifier1Sig, err := hex.DecodeString(cctx.Args().Get(4))
if err != nil {
return err
}
verifier2Addr, err := address.NewFromString(cctx.Args().Get(5))
if err != nil {
return err
}
verifier2Sig, err := hex.DecodeString(cctx.Args().Get(6))
if err != nil {
return err
}
var sig1 crypto.Signature
if err := sig1.UnmarshalBinary(verifier1Sig); err != nil {
return xerrors.Errorf("couldn't unmarshal sig: %w", err)
}
var sig2 crypto.Signature
if err := sig2.UnmarshalBinary(verifier2Sig); err != nil {
return xerrors.Errorf("couldn't unmarshal sig: %w", err)
}
params, err := actors.SerializeParams(&verifregtypes.RemoveDataCapParams{
VerifiedClientToRemove: client,
DataCapAmountToRemove: allowanceToRemove,
VerifierRequest1: verifregtypes.RemoveDataCapRequest{
Verifier: verifier1Addr,
VerifierSignature: sig1,
},
VerifierRequest2: verifregtypes.RemoveDataCapRequest{
Verifier: verifier2Addr,
VerifierSignature: sig2,
},
})
if err != nil {
return err
}
vrk, err := api.StateVerifiedRegistryRootKey(ctx, types.EmptyTSK)
if err != nil {
return err
}
vrkState, err := api.StateGetActor(ctx, vrk, types.EmptyTSK)
if err != nil {
return err
}
apibs := blockstore.NewAPIBlockstore(api)
store := adt.WrapStore(ctx, cbor.NewCborStore(apibs))
st, err := multisig.Load(store, vrkState)
if err != nil {
return fmt.Errorf("load vrk failed: %w ", err)
}
signers, err := st.Signers()
if err != nil {
return err
}
senderIsSigner := false
senderIdAddr, err := address.IDFromAddress(sender)
if err != nil {
return err
}
for _, signer := range signers {
signerIdAddr, err := address.IDFromAddress(signer)
if err != nil {
return err
}
if signerIdAddr == senderIdAddr {
senderIsSigner = true
}
}
if !senderIsSigner {
return fmt.Errorf("sender must be a vrk signer")
}
proto, err := api.MsigPropose(ctx, vrk, verifreg.Address, big.Zero(), sender, uint64(verifreg.Methods.RemoveVerifiedClientDataCap), params)
if err != nil {
return err
}
sm, err := lcli.InteractiveSend(ctx, cctx, srv, proto)
if err != nil {
return err
}
msgCid := sm.Cid()
fmt.Println("sending msg: ", msgCid)
mwait, err := api.StateWaitMsg(ctx, msgCid, uint64(cctx.Int("confidence")), policy.ChainFinality, true)
if err != nil {
return err
}
if mwait.Receipt.ExitCode.IsError() {
return fmt.Errorf("failed to removed verified data cap: %d", mwait.Receipt.ExitCode)
}
//TODO: Internal msg might still have failed
return nil
},
}