github.com/decred/dcrlnd@v0.7.6/cmd/dcrlncli/cmd_import_mission_control.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"strconv"
     8  
     9  	"github.com/decred/dcrlnd/lnrpc/routerrpc"
    10  	"github.com/decred/dcrlnd/routing/route"
    11  	"github.com/urfave/cli"
    12  )
    13  
    14  const argsStr = "[source node] [dest node] [unix ts seconds] [amount in msat]"
    15  
    16  var importMissionControlCommand = cli.Command{
    17  	Name:      "importmc",
    18  	Category:  "Payments",
    19  	Usage:     "Import a result to the internal mission control state.",
    20  	ArgsUsage: fmt.Sprintf("importmc %v", argsStr),
    21  	Action:    actionDecorator(importMissionControl),
    22  	Flags: []cli.Flag{
    23  		cli.BoolFlag{
    24  			Name:  "failure",
    25  			Usage: "whether the routing history entry was a failure",
    26  		},
    27  		cli.BoolFlag{
    28  			Name:  "force",
    29  			Usage: "whether to force the history entry import",
    30  		},
    31  	},
    32  }
    33  
    34  func importMissionControl(ctx *cli.Context) error {
    35  	conn := getClientConn(ctx, false)
    36  	defer conn.Close()
    37  
    38  	if ctx.NArg() != 4 {
    39  		return fmt.Errorf("please provide args: %v", argsStr)
    40  	}
    41  
    42  	args := ctx.Args()
    43  
    44  	sourceNode, err := route.NewVertexFromStr(args[0])
    45  	if err != nil {
    46  		return fmt.Errorf("please provide valid source node: %v", err)
    47  	}
    48  
    49  	destNode, err := route.NewVertexFromStr(args[1])
    50  	if err != nil {
    51  		return fmt.Errorf("please provide valid dest node: %v", err)
    52  	}
    53  
    54  	ts, err := strconv.ParseInt(args[2], 10, 64)
    55  	if err != nil {
    56  		return fmt.Errorf("please provide unix timestamp "+
    57  			"in seconds: %v", err)
    58  	}
    59  
    60  	if ts <= 0 {
    61  		return errors.New("please provide positive timestamp")
    62  	}
    63  
    64  	amt, err := strconv.ParseInt(args[3], 10, 64)
    65  	if err != nil {
    66  		return fmt.Errorf("please provide amount in msat: %v", err)
    67  	}
    68  
    69  	if amt <= 0 {
    70  		return errors.New("amount must be >0")
    71  	}
    72  
    73  	client := routerrpc.NewRouterClient(conn)
    74  
    75  	importResult := &routerrpc.PairHistory{
    76  		NodeFrom: sourceNode[:],
    77  		NodeTo:   destNode[:],
    78  		History:  &routerrpc.PairData{},
    79  	}
    80  
    81  	if ctx.IsSet("failure") {
    82  		importResult.History.FailAmtMAtoms = amt
    83  		importResult.History.FailTime = ts
    84  	} else {
    85  		importResult.History.SuccessAmtMAtoms = amt
    86  		importResult.History.SuccessTime = ts
    87  	}
    88  
    89  	req := &routerrpc.XImportMissionControlRequest{
    90  		Pairs: []*routerrpc.PairHistory{
    91  			importResult,
    92  		},
    93  		Force: ctx.IsSet("force"),
    94  	}
    95  
    96  	rpcCtx := context.Background()
    97  	_, err = client.XImportMissionControl(rpcCtx, req)
    98  	return err
    99  }