-
Notifications
You must be signed in to change notification settings - Fork 465
/
paych.go
475 lines (447 loc) · 14.4 KB
/
paych.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
package cmd
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"sort"
"strconv"
"strings"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/builtin/v8/paych"
"github.com/filecoin-project/venus/app/node"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/paychmgr"
lpaych "github.com/filecoin-project/venus/venus-shared/actors/builtin/paych"
"github.com/filecoin-project/venus/venus-shared/types"
cmds "github.com/ipfs/go-ipfs-cmds"
)
var paychCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Manage payment channels",
},
Subcommands: map[string]*cmds.Command{
"add-funds": addFundsCmd,
"list": listCmd,
"voucher": voucherCmd,
"settle": settleCmd,
"status": statusCmd,
"status-by-from-to": sbftCmd,
"collect": collectCmd,
},
}
var addFundsCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Add funds to the payment channel between fromAddress and toAddress. Creates the payment channel if it doesn't already exist.",
},
Arguments: []cmds.Argument{
cmds.StringArg("from_addr", true, false, "From Address is the payment channel sender"),
cmds.StringArg("to_addr", true, false, "To Address is the payment channel recipient"),
cmds.StringArg("amount", true, false, "Amount is the deposits funds in the payment channel"),
},
Options: []cmds.Option{
cmds.BoolOption("restart-retrievals", "restart stalled retrieval deals on this payment channel").WithDefault(true),
cmds.BoolOption("reserve", "mark funds as reserved"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
fromAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
toAddr, err := address.NewFromString(req.Arguments[1])
if err != nil {
return err
}
amt, err := types.ParseFIL(req.Arguments[2])
if err != nil {
return err
}
var chanInfo *types.ChannelInfo
if reserve, _ := req.Options["reserve"].(bool); reserve {
chanInfo, err = env.(*node.Env).PaychAPI.PaychGet(req.Context, fromAddr, toAddr, types.BigInt(amt), types.PaychGetOpts{
OffChain: false,
})
} else {
chanInfo, err = env.(*node.Env).PaychAPI.PaychFund(req.Context, fromAddr, toAddr, types.BigInt(amt))
}
if err != nil {
return err
}
chAddr, err := env.(*node.Env).PaychAPI.PaychGetWaitReady(req.Context, chanInfo.WaitSentinel)
if err != nil {
return err
}
return re.Emit(chAddr)
},
}
var listCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List all locally registered payment channels",
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
addrs, err := env.(*node.Env).PaychAPI.PaychList(req.Context)
if err != nil {
return err
}
return re.Emit(addrs)
},
}
var voucherCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Interact with payment channel vouchers",
},
Subcommands: map[string]*cmds.Command{
"create": voucherCreateCmd,
"check": voucherCheckCmd,
"add": voucherAddCmd,
"list": voucherListCmd,
"best-spendable": voucherBestSpendableCmd,
"submit": voucherSubmitCmd,
},
}
var settleCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Settle a payment channel",
},
Arguments: []cmds.Argument{
cmds.StringArg("channel_addr", true, false, "The given payment channel address"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
chanAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
mcid, err := env.(*node.Env).PaychAPI.PaychSettle(req.Context, chanAddr)
if err != nil {
return err
}
if err != nil {
return err
}
mwait, err := env.(*node.Env).ChainAPI.StateWaitMsg(req.Context, mcid, constants.MessageConfidence, constants.LookbackNoLimit, true)
if err != nil {
return err
}
if mwait.Receipt.ExitCode != 0 {
return fmt.Errorf("settle message execution failed (exit code %d)", mwait.Receipt.ExitCode)
}
return re.Emit(fmt.Sprintf("Settled channel %s", chanAddr))
},
}
var statusCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Show the status of an outbound payment channel",
},
Arguments: []cmds.Argument{
cmds.StringArg("channel_addr", true, false, "The given payment channel address"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
chanAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
av, err := env.(*node.Env).PaychAPI.PaychAvailableFunds(req.Context, chanAddr)
if err != nil {
return err
}
// re.Emit(av)
w := bytes.NewBuffer(nil)
paychStatus(w, av)
return re.Emit(w)
},
}
var sbftCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Show the status of an active outbound payment channel by from/to addresses",
},
Arguments: []cmds.Argument{
cmds.StringArg("from_addr", true, false, "Gets a channel accessor for a given from / to pair"),
cmds.StringArg("to_addr", true, false, "Gets a channel accessor for a given from / to pair"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
fromAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
toAddr, err := address.NewFromString(req.Arguments[1])
if err != nil {
return err
}
av, err := env.(*node.Env).PaychAPI.PaychAvailableFundsByFromTo(req.Context, fromAddr, toAddr)
if err != nil {
return err
}
w := bytes.NewBuffer(nil)
paychStatus(w, av)
return re.Emit(w)
},
}
var collectCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Create a signed payment channel voucher",
},
Arguments: []cmds.Argument{
cmds.StringArg("channel_addr", true, false, "The given payment channel address"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
chanAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
mcid, err := env.(*node.Env).PaychAPI.PaychCollect(req.Context, chanAddr)
if err != nil {
return err
}
mwait, err := env.(*node.Env).ChainAPI.StateWaitMsg(req.Context, mcid, constants.MessageConfidence, constants.LookbackNoLimit, true)
if err != nil {
return err
}
if mwait.Receipt.ExitCode != 0 {
return fmt.Errorf("collect message execution failed (exit code %d)", mwait.Receipt.ExitCode)
}
return re.Emit(fmt.Sprintf("Collected funds for channel %s", chanAddr))
},
}
var voucherCreateCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Create a signed payment channel voucher",
},
Arguments: []cmds.Argument{
cmds.StringArg("channel_addr", true, false, "The given payment channel address"),
cmds.StringArg("amount", true, false, "The value that will be used to create the voucher"),
cmds.StringArg("lane", true, false, "Specify payment channel lane to use"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
chanAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
amtFil, err := types.ParseFIL(req.Arguments[1])
if err != nil {
return err
}
lane, err := strconv.ParseUint(req.Arguments[2], 10, 64)
if err != nil {
return err
}
res, err := env.(*node.Env).PaychAPI.PaychVoucherCreate(req.Context, chanAddr, big.NewFromGo(amtFil.Int), lane)
if err != nil {
return err
}
if res.Voucher == nil {
return fmt.Errorf("could not create voucher: insufficient funds in channel, shortfall: %d", res.Shortfall)
}
enc, err := encodedString(res.Voucher)
if err != nil {
return err
}
return re.Emit(enc)
},
}
var voucherCheckCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Check validity of payment channel voucher",
},
Arguments: []cmds.Argument{
cmds.StringArg("channel_addr", true, false, "The given payment channel address"),
cmds.StringArg("voucher", true, false, "The voucher in the payment channel"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
chanAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
voucher, err := lpaych.DecodeSignedVoucher(req.Arguments[1])
if err != nil {
return err
}
err = env.(*node.Env).PaychAPI.PaychVoucherCheckValid(req.Context, chanAddr, voucher)
if err != nil {
return err
}
return re.Emit("voucher is valid")
},
}
var voucherAddCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Add payment channel voucher to local datastore",
},
Arguments: []cmds.Argument{
cmds.StringArg("channel_addr", true, false, "The given payment channel address"),
cmds.StringArg("voucher", true, false, "The voucher in the payment channel"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
chanAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
voucher, err := lpaych.DecodeSignedVoucher(req.Arguments[1])
if err != nil {
return err
}
_, err = env.(*node.Env).PaychAPI.PaychVoucherAdd(req.Context, chanAddr, voucher, nil, big.NewInt(0))
if err != nil {
return err
}
return re.Emit("add voucher successfully")
},
}
var voucherListCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "List stored vouchers for a given payment channel",
},
Arguments: []cmds.Argument{
cmds.StringArg("channel_addr", true, false, "The given payment channel address"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
chanAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
vs, err := env.(*node.Env).PaychAPI.PaychVoucherList(req.Context, chanAddr)
if err != nil {
return err
}
buff := bytes.NewBuffer(nil)
for _, v := range sortVouchers(vs) {
str, err := encodedString(v)
if err != nil {
return err
}
fmt.Fprintf(buff, "Lane %d, Nonce %d: %s, voucher: %s\n", v.Lane, v.Nonce, v.Amount.String(), str)
}
return re.Emit(buff)
},
}
var voucherBestSpendableCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Print vouchers with highest value that is currently spendable for each lane",
},
Arguments: []cmds.Argument{
cmds.StringArg("channel_addr", true, false, "The given payment channel address"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
chanAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
vouchersByLane, err := paychmgr.BestSpendableByLane(req.Context, env.(*node.Env).PaychAPI, chanAddr)
if err != nil {
return err
}
var vouchers []*paych.SignedVoucher
for _, vchr := range vouchersByLane {
vouchers = append(vouchers, vchr)
}
buff := bytes.NewBuffer(nil)
for _, v := range sortVouchers(vouchers) {
str, err := encodedString(v)
if err != nil {
return err
}
fmt.Fprintf(buff, "Lane %d, Nonce %d: %s, voucher: %s\n", v.Lane, v.Nonce, v.Amount.String(), str)
}
return re.Emit(buff)
},
}
var voucherSubmitCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Submit voucher to chain to update payment channel state",
},
Arguments: []cmds.Argument{
cmds.StringArg("channel_addr", true, false, "The given payment channel address"),
cmds.StringArg("voucher", true, false, "The voucher in the payment channel"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
chanAddr, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
voucher, err := lpaych.DecodeSignedVoucher(req.Arguments[1])
if err != nil {
return err
}
mcid, err := env.(*node.Env).PaychAPI.PaychVoucherSubmit(req.Context, chanAddr, voucher, nil, nil)
if err != nil {
return err
}
mwait, err := env.(*node.Env).ChainAPI.StateWaitMsg(req.Context, mcid, constants.MessageConfidence, constants.LookbackNoLimit, true)
if err != nil {
return err
}
if mwait.Receipt.ExitCode != 0 {
return fmt.Errorf("message execution failed (exit code %d)", mwait.Receipt.ExitCode)
}
return re.Emit("channel updated successfully")
},
}
func encodedString(sv *paych.SignedVoucher) (string, error) {
buf := new(bytes.Buffer)
if err := sv.MarshalCBOR(buf); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(buf.Bytes()), nil
}
func sortVouchers(vouchers []*paych.SignedVoucher) []*paych.SignedVoucher {
sort.Slice(vouchers, func(i, j int) bool {
if vouchers[i].Lane == vouchers[j].Lane {
return vouchers[i].Nonce < vouchers[j].Nonce
}
return vouchers[i].Lane < vouchers[j].Lane
})
return vouchers
}
func paychStatus(writer io.Writer, avail *types.ChannelAvailableFunds) {
if avail.Channel == nil {
if avail.PendingWaitSentinel != nil {
fmt.Fprint(writer, "Creating channel\n")
fmt.Fprintf(writer, " From: %s\n", avail.From)
fmt.Fprintf(writer, " To: %s\n", avail.To)
fmt.Fprintf(writer, " Pending Amt: %s\n", types.FIL(avail.PendingAmt))
fmt.Fprintf(writer, " Wait Sentinel: %s\n", avail.PendingWaitSentinel)
return
}
fmt.Fprint(writer, "Channel does not exist\n")
fmt.Fprintf(writer, " From: %s\n", avail.From)
fmt.Fprintf(writer, " To: %s\n", avail.To)
return
}
if avail.PendingWaitSentinel != nil {
fmt.Fprint(writer, "Adding Funds to channel\n")
} else {
fmt.Fprint(writer, "Channel exists\n")
}
nameValues := [][]string{
{"Channel", avail.Channel.String()},
{"From", avail.From.String()},
{"To", avail.To.String()},
{"Confirmed Amt", fmt.Sprintf("%s", types.FIL(avail.ConfirmedAmt))},
{"Available Amt", fmt.Sprintf("%s", types.FIL(avail.NonReservedAmt))},
{"Voucher Redeemed Amt", fmt.Sprintf("%s", types.FIL(avail.VoucherReedeemedAmt))},
{"Pending Amt", fmt.Sprintf("%s", types.FIL(avail.PendingAmt))},
{"Pending Available Amt", fmt.Sprintf("%s", types.FIL(avail.PendingAvailableAmt))},
{"Queued Amt", fmt.Sprintf("%s", types.FIL(avail.QueuedAmt))},
}
if avail.PendingWaitSentinel != nil {
nameValues = append(nameValues, []string{
"Add Funds Wait Sentinel",
avail.PendingWaitSentinel.String(),
})
}
fmt.Fprint(writer, formatNameValues(nameValues))
}
func formatNameValues(nameValues [][]string) string {
maxLen := 0
for _, nv := range nameValues {
if len(nv[0]) > maxLen {
maxLen = len(nv[0])
}
}
out := make([]string, len(nameValues))
for i, nv := range nameValues {
namePad := strings.Repeat(" ", maxLen-len(nv[0]))
out[i] = " " + nv[0] + ": " + namePad + nv[1]
}
return strings.Join(out, "\n") + "\n"
}