2019-08-13 04:27:54 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-02-13 03:50:37 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2019-08-13 04:27:54 +00:00
|
|
|
"fmt"
|
2020-08-18 15:27:00 +00:00
|
|
|
"io"
|
|
|
|
"sort"
|
2020-09-03 16:03:42 +00:00
|
|
|
"strings"
|
2020-08-18 15:27:00 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-09-04 11:44:09 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2022-04-20 21:34:28 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin/v8/paych"
|
2020-07-23 19:31:28 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
lapi "github.com/filecoin-project/lotus/api"
|
2020-08-13 20:37:09 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2022-04-20 21:34:28 +00:00
|
|
|
lpaych "github.com/filecoin-project/lotus/chain/actors/builtin/paych"
|
2020-09-28 21:25:58 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/paychmgr"
|
2019-08-13 04:27:54 +00:00
|
|
|
)
|
|
|
|
|
2024-04-01 15:30:35 +00:00
|
|
|
var PaychCmd = &cli.Command{
|
2019-08-13 04:27:54 +00:00
|
|
|
Name: "paych",
|
|
|
|
Usage: "Manage payment channels",
|
|
|
|
Subcommands: []*cli.Command{
|
2020-09-03 13:45:09 +00:00
|
|
|
paychAddFundsCmd,
|
2019-08-13 04:27:54 +00:00
|
|
|
paychListCmd,
|
|
|
|
paychVoucherCmd,
|
2020-07-22 15:20:47 +00:00
|
|
|
paychSettleCmd,
|
2020-09-10 23:00:23 +00:00
|
|
|
paychStatusCmd,
|
2020-09-11 22:53:11 +00:00
|
|
|
paychStatusByFromToCmd,
|
2020-07-22 15:20:47 +00:00
|
|
|
paychCloseCmd,
|
2019-08-13 04:27:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-03 13:45:09 +00:00
|
|
|
var paychAddFundsCmd = &cli.Command{
|
|
|
|
Name: "add-funds",
|
|
|
|
Usage: "Add funds to the payment channel between fromAddress and toAddress. Creates the payment channel if it doesn't already exist.",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[fromAddress toAddress amount]",
|
2020-09-04 05:34:59 +00:00
|
|
|
Flags: []cli.Flag{
|
2022-01-04 22:31:22 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "reserve",
|
|
|
|
Usage: "mark funds as reserved",
|
|
|
|
},
|
2020-09-04 05:34:59 +00:00
|
|
|
},
|
2019-08-13 04:27:54 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 3 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
from, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
2020-07-23 19:31:28 +00:00
|
|
|
return ShowHelp(cctx, fmt.Errorf("failed to parse from address: %s", err))
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
to, err := address.NewFromString(cctx.Args().Get(1))
|
|
|
|
if err != nil {
|
2020-07-23 19:31:28 +00:00
|
|
|
return ShowHelp(cctx, fmt.Errorf("failed to parse to address: %s", err))
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 01:54:21 +00:00
|
|
|
amt, err := types.ParseFIL(cctx.Args().Get(2))
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
2020-07-23 19:31:28 +00:00
|
|
|
return ShowHelp(cctx, fmt.Errorf("parsing amount failed: %s", err))
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 22:31:22 +00:00
|
|
|
api, closer, err := GetFullNodeAPIV1(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2020-08-06 18:54:17 +00:00
|
|
|
// Send a message to chain to create channel / add funds to existing
|
|
|
|
// channel
|
2022-02-14 18:56:02 +00:00
|
|
|
var info *lapi.ChannelInfo
|
|
|
|
if cctx.Bool("reserve") {
|
|
|
|
info, err = api.PaychGet(ctx, from, to, types.BigInt(amt), lapi.PaychGetOpts{
|
|
|
|
OffChain: false,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
info, err = api.PaychFund(ctx, from, to, types.BigInt(amt))
|
|
|
|
}
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:54:17 +00:00
|
|
|
// Wait for the message to be confirmed
|
2022-06-08 08:43:49 +00:00
|
|
|
fmt.Println("waiting for confirmation..")
|
2020-08-11 14:45:45 +00:00
|
|
|
chAddr, err := api.PaychGetWaitReady(ctx, info.WaitSentinel)
|
2020-08-06 18:54:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprintln(cctx.App.Writer, chAddr)
|
2019-08-13 04:27:54 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:44:09 +00:00
|
|
|
var paychStatusByFromToCmd = &cli.Command{
|
|
|
|
Name: "status-by-from-to",
|
|
|
|
Usage: "Show the status of an active outbound payment channel by from/to addresses",
|
2020-09-03 16:03:42 +00:00
|
|
|
ArgsUsage: "[fromAddress toAddress]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 2 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2020-09-03 16:03:42 +00:00
|
|
|
}
|
2020-09-11 22:52:09 +00:00
|
|
|
ctx := ReqContext(cctx)
|
2020-09-03 16:03:42 +00:00
|
|
|
|
|
|
|
from, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return ShowHelp(cctx, fmt.Errorf("failed to parse from address: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
to, err := address.NewFromString(cctx.Args().Get(1))
|
|
|
|
if err != nil {
|
|
|
|
return ShowHelp(cctx, fmt.Errorf("failed to parse to address: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
2020-09-11 22:52:09 +00:00
|
|
|
avail, err := api.PaychAvailableFundsByFromTo(ctx, from, to)
|
2020-09-03 16:03:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-04 11:44:09 +00:00
|
|
|
paychStatus(cctx.App.Writer, avail)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychStatusCmd = &cli.Command{
|
|
|
|
Name: "status",
|
|
|
|
Usage: "Show the status of an outbound payment channel",
|
|
|
|
ArgsUsage: "[channelAddress]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 1 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2020-09-03 16:03:42 +00:00
|
|
|
}
|
2020-09-11 22:52:09 +00:00
|
|
|
ctx := ReqContext(cctx)
|
2020-09-03 16:03:42 +00:00
|
|
|
|
2020-09-04 11:44:09 +00:00
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return ShowHelp(cctx, fmt.Errorf("failed to parse channel address: %s", err))
|
2020-09-03 16:03:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 11:44:09 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-09-03 16:03:42 +00:00
|
|
|
}
|
2020-09-04 11:44:09 +00:00
|
|
|
defer closer()
|
|
|
|
|
2020-09-11 22:52:09 +00:00
|
|
|
avail, err := api.PaychAvailableFunds(ctx, ch)
|
2020-09-04 11:44:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-09-03 16:03:42 +00:00
|
|
|
}
|
2020-09-04 11:44:09 +00:00
|
|
|
|
|
|
|
paychStatus(cctx.App.Writer, avail)
|
2020-09-03 16:03:42 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-06 15:04:39 +00:00
|
|
|
func paychStatus(writer io.Writer, avail *lapi.ChannelAvailableFunds) {
|
2020-09-04 11:44:09 +00:00
|
|
|
if avail.Channel == nil {
|
|
|
|
if avail.PendingWaitSentinel != nil {
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = 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)
|
2020-09-04 11:44:09 +00:00
|
|
|
return
|
|
|
|
}
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprint(writer, "Channel does not exist\n")
|
|
|
|
_, _ = fmt.Fprintf(writer, " From: %s\n", avail.From)
|
|
|
|
_, _ = fmt.Fprintf(writer, " To: %s\n", avail.To)
|
2020-09-04 11:44:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if avail.PendingWaitSentinel != nil {
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprint(writer, "Adding Funds to channel\n")
|
2020-09-04 11:44:09 +00:00
|
|
|
} else {
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprint(writer, "Channel exists\n")
|
2020-09-04 11:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nameValues := [][]string{
|
|
|
|
{"Channel", avail.Channel.String()},
|
|
|
|
{"From", avail.From.String()},
|
|
|
|
{"To", avail.To.String()},
|
2022-01-04 21:53:59 +00:00
|
|
|
{"Confirmed Amt", fmt.Sprintf("%s", types.FIL(avail.ConfirmedAmt))},
|
2022-02-16 19:39:43 +00:00
|
|
|
{"Available Amt", fmt.Sprintf("%s", types.FIL(avail.NonReservedAmt))},
|
2022-01-05 11:11:19 +00:00
|
|
|
{"Voucher Redeemed Amt", fmt.Sprintf("%s", types.FIL(avail.VoucherReedeemedAmt))},
|
|
|
|
{"Pending Amt", fmt.Sprintf("%s", types.FIL(avail.PendingAmt))},
|
2022-01-04 22:20:11 +00:00
|
|
|
{"Pending Available Amt", fmt.Sprintf("%s", types.FIL(avail.PendingAvailableAmt))},
|
2022-01-04 21:53:59 +00:00
|
|
|
{"Queued Amt", fmt.Sprintf("%s", types.FIL(avail.QueuedAmt))},
|
2020-09-04 11:44:09 +00:00
|
|
|
}
|
|
|
|
if avail.PendingWaitSentinel != nil {
|
|
|
|
nameValues = append(nameValues, []string{
|
|
|
|
"Add Funds Wait Sentinel",
|
|
|
|
avail.PendingWaitSentinel.String(),
|
|
|
|
})
|
|
|
|
}
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprint(writer, formatNameValues(nameValues))
|
2020-09-04 11:44:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 16:03:42 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2019-08-13 04:27:54 +00:00
|
|
|
var paychListCmd = &cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "List all locally registered payment channels",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
chs, err := api.PaychList(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range chs {
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprintln(cctx.App.Writer, v.String())
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-22 15:20:47 +00:00
|
|
|
var paychSettleCmd = &cli.Command{
|
|
|
|
Name: "settle",
|
|
|
|
Usage: "Settle a payment channel",
|
|
|
|
ArgsUsage: "[channelAddress]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 1 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2020-07-22 15:20:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse payment channel address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
mcid, err := api.PaychSettle(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mwait, err := api.StateWaitMsg(ctx, mcid, build.MessageConfidence)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-14 18:53:11 +00:00
|
|
|
if mwait.Receipt.ExitCode.IsError() {
|
2020-07-22 15:20:47 +00:00
|
|
|
return fmt.Errorf("settle message execution failed (exit code %d)", mwait.Receipt.ExitCode)
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprintf(cctx.App.Writer, "Settled channel %s\n", ch)
|
2020-07-22 15:20:47 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychCloseCmd = &cli.Command{
|
|
|
|
Name: "collect",
|
|
|
|
Usage: "Collect funds for a payment channel",
|
|
|
|
ArgsUsage: "[channelAddress]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 1 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2020-07-22 15:20:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse payment channel address: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
mcid, err := api.PaychCollect(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mwait, err := api.StateWaitMsg(ctx, mcid, build.MessageConfidence)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-09-14 18:53:11 +00:00
|
|
|
if mwait.Receipt.ExitCode.IsError() {
|
2020-07-22 15:20:47 +00:00
|
|
|
return fmt.Errorf("collect message execution failed (exit code %d)", mwait.Receipt.ExitCode)
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprintf(cctx.App.Writer, "Collected funds for channel %s\n", ch)
|
2020-07-22 15:20:47 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-13 04:27:54 +00:00
|
|
|
var paychVoucherCmd = &cli.Command{
|
|
|
|
Name: "voucher",
|
|
|
|
Usage: "Interact with payment channel vouchers",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
paychVoucherCreateCmd,
|
|
|
|
paychVoucherCheckCmd,
|
|
|
|
paychVoucherAddCmd,
|
|
|
|
paychVoucherListCmd,
|
|
|
|
paychVoucherBestSpendableCmd,
|
|
|
|
paychVoucherSubmitCmd,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherCreateCmd = &cli.Command{
|
2020-03-06 19:01:28 +00:00
|
|
|
Name: "create",
|
|
|
|
Usage: "Create a signed payment channel voucher",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[channelAddress amount]",
|
2019-08-13 04:27:54 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "lane",
|
|
|
|
Value: 0,
|
|
|
|
Usage: "specify payment channel lane to use",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 2 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:33:44 +00:00
|
|
|
amt, err := types.ParseFIL(cctx.Args().Get(1))
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
2020-09-01 14:33:44 +00:00
|
|
|
return ShowHelp(cctx, fmt.Errorf("parsing amount failed: %s", err))
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lane := cctx.Int("lane")
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2020-09-01 14:33:44 +00:00
|
|
|
v, err := api.PaychVoucherCreate(ctx, ch, types.BigInt(amt), uint64(lane))
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:33:44 +00:00
|
|
|
if v.Voucher == nil {
|
2024-06-04 02:53:29 +00:00
|
|
|
return fmt.Errorf("could not create voucher: insufficient funds in channel, shortfall: %d", v.Shortfall)
|
2020-09-01 14:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := EncodedString(v.Voucher)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprintln(cctx.App.Writer, enc)
|
2019-08-13 04:27:54 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherCheckCmd = &cli.Command{
|
2020-03-06 19:01:28 +00:00
|
|
|
Name: "check",
|
|
|
|
Usage: "Check validity of payment channel voucher",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[channelAddress voucher]",
|
2019-08-13 04:27:54 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 2 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
sv, err := lpaych.DecodeSignedVoucher(cctx.Args().Get(1))
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
if err := api.PaychVoucherCheckValid(ctx, ch, sv); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprintln(cctx.App.Writer, "voucher is valid")
|
2019-08-13 04:27:54 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherAddCmd = &cli.Command{
|
2020-03-06 19:01:28 +00:00
|
|
|
Name: "add",
|
|
|
|
Usage: "Add payment channel voucher to local datastore",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[channelAddress voucher]",
|
2019-08-13 04:27:54 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 2 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
sv, err := lpaych.DecodeSignedVoucher(cctx.Args().Get(1))
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2019-09-09 13:59:41 +00:00
|
|
|
// TODO: allow passing proof bytes
|
2019-09-16 21:25:23 +00:00
|
|
|
if _, err := api.PaychVoucherAdd(ctx, ch, sv, nil, types.NewInt(0)); err != nil {
|
2019-08-13 04:27:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherListCmd = &cli.Command{
|
2020-03-06 19:01:28 +00:00
|
|
|
Name: "list",
|
|
|
|
Usage: "List stored vouchers for a given payment channel",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[channelAddress]",
|
2019-09-09 13:59:41 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2019-09-09 16:02:57 +00:00
|
|
|
Name: "export",
|
2020-08-18 15:27:00 +00:00
|
|
|
Usage: "Print voucher as serialized string",
|
2019-09-09 13:59:41 +00:00
|
|
|
},
|
|
|
|
},
|
2019-08-13 04:27:54 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 1 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
vouchers, err := api.PaychVoucherList(ctx, ch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:27:00 +00:00
|
|
|
for _, v := range sortVouchers(vouchers) {
|
|
|
|
export := cctx.Bool("export")
|
|
|
|
err := outputVoucher(cctx.App.Writer, v, export)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-09-09 13:59:41 +00:00
|
|
|
}
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherBestSpendableCmd = &cli.Command{
|
2020-03-06 19:01:28 +00:00
|
|
|
Name: "best-spendable",
|
2020-08-18 15:27:00 +00:00
|
|
|
Usage: "Print vouchers with highest value that is currently spendable for each lane",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[channelAddress]",
|
2020-08-18 15:27:00 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "export",
|
|
|
|
Usage: "Print voucher as serialized string",
|
|
|
|
},
|
|
|
|
},
|
2019-08-13 04:27:54 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 1 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2020-08-18 15:27:00 +00:00
|
|
|
vouchersByLane, err := paychmgr.BestSpendableByLane(ctx, api, ch)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:27:00 +00:00
|
|
|
var vouchers []*paych.SignedVoucher
|
|
|
|
for _, vchr := range vouchersByLane {
|
|
|
|
vouchers = append(vouchers, vchr)
|
|
|
|
}
|
|
|
|
for _, best := range sortVouchers(vouchers) {
|
|
|
|
export := cctx.Bool("export")
|
|
|
|
err := outputVoucher(cctx.App.Writer, best, export)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 15:27:00 +00:00
|
|
|
return 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
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
2020-08-18 15:27:00 +00:00
|
|
|
return vouchers[i].Lane < vouchers[j].Lane
|
|
|
|
})
|
|
|
|
return vouchers
|
|
|
|
}
|
2019-08-13 04:27:54 +00:00
|
|
|
|
2020-08-18 15:27:00 +00:00
|
|
|
func outputVoucher(w io.Writer, v *paych.SignedVoucher, export bool) error {
|
|
|
|
var enc string
|
|
|
|
if export {
|
|
|
|
var err error
|
|
|
|
enc, err = EncodedString(v)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-18 15:27:00 +00:00
|
|
|
}
|
2019-08-13 04:27:54 +00:00
|
|
|
|
2024-06-04 02:53:29 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "Lane %d, Nonce %d: %s", v.Lane, v.Nonce, types.FIL(v.Amount)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-18 15:27:00 +00:00
|
|
|
if export {
|
2024-06-04 02:53:29 +00:00
|
|
|
if _, err := fmt.Fprintf(w, "; %s", enc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-18 15:27:00 +00:00
|
|
|
}
|
2024-06-04 02:53:29 +00:00
|
|
|
_, err := fmt.Fprintln(w)
|
|
|
|
return err
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var paychVoucherSubmitCmd = &cli.Command{
|
2020-03-06 19:01:28 +00:00
|
|
|
Name: "submit",
|
|
|
|
Usage: "Submit voucher to chain to update payment channel state",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[channelAddress voucher]",
|
2019-08-13 04:27:54 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 2 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return IncorrectNumArgs(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ch, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
sv, err := lpaych.DecodeSignedVoucher(cctx.Args().Get(1))
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2020-08-20 16:09:52 +00:00
|
|
|
mcid, err := api.PaychVoucherSubmit(ctx, ch, sv, nil, nil)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-03 21:42:06 +00:00
|
|
|
mwait, err := api.StateWaitMsg(ctx, mcid, build.MessageConfidence)
|
2019-08-13 04:27:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-14 18:53:11 +00:00
|
|
|
if mwait.Receipt.ExitCode.IsError() {
|
2019-08-13 04:27:54 +00:00
|
|
|
return fmt.Errorf("message execution failed (exit code %d)", mwait.Receipt.ExitCode)
|
|
|
|
}
|
|
|
|
|
2024-06-04 02:53:29 +00:00
|
|
|
_, _ = fmt.Fprintln(cctx.App.Writer, "channel updated successfully")
|
2019-08-13 04:27:54 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2020-02-13 03:50:37 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|