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

     1  //go:build !no_autopilotrpc
     2  // +build !no_autopilotrpc
     3  
     4  package main
     5  
     6  import (
     7  	"github.com/decred/dcrlnd/lnrpc/autopilotrpc"
     8  	"github.com/urfave/cli"
     9  )
    10  
    11  func getAutopilotClient(ctx *cli.Context) (autopilotrpc.AutopilotClient, func()) {
    12  	conn := getClientConn(ctx, false)
    13  
    14  	cleanUp := func() {
    15  		conn.Close()
    16  	}
    17  
    18  	return autopilotrpc.NewAutopilotClient(conn), cleanUp
    19  }
    20  
    21  var getStatusCommand = cli.Command{
    22  	Name:        "status",
    23  	Usage:       "Get the active status of autopilot.",
    24  	Description: "",
    25  	Action:      actionDecorator(getStatus),
    26  }
    27  
    28  func getStatus(ctx *cli.Context) error {
    29  	ctxc := getContext()
    30  	client, cleanUp := getAutopilotClient(ctx)
    31  	defer cleanUp()
    32  
    33  	req := &autopilotrpc.StatusRequest{}
    34  
    35  	resp, err := client.Status(ctxc, req)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	printRespJSON(resp)
    41  	return nil
    42  }
    43  
    44  var enableCommand = cli.Command{
    45  	Name:        "enable",
    46  	Usage:       "Enable the autopilot.",
    47  	Description: "",
    48  	Action:      actionDecorator(enable),
    49  }
    50  
    51  var disableCommand = cli.Command{
    52  	Name:        "disable",
    53  	Usage:       "Disable the active autopilot.",
    54  	Description: "",
    55  	Action:      actionDecorator(disable),
    56  }
    57  
    58  func enable(ctx *cli.Context) error {
    59  	ctxc := getContext()
    60  	client, cleanUp := getAutopilotClient(ctx)
    61  	defer cleanUp()
    62  
    63  	// We will enable the autopilot.
    64  	req := &autopilotrpc.ModifyStatusRequest{
    65  		Enable: true,
    66  	}
    67  
    68  	resp, err := client.ModifyStatus(ctxc, req)
    69  	if err != nil {
    70  		return err
    71  	}
    72  
    73  	printRespJSON(resp)
    74  	return nil
    75  }
    76  
    77  func disable(ctx *cli.Context) error {
    78  	ctxc := getContext()
    79  	client, cleanUp := getAutopilotClient(ctx)
    80  	defer cleanUp()
    81  
    82  	// We will disable the autopilot.
    83  	req := &autopilotrpc.ModifyStatusRequest{
    84  		Enable: false,
    85  	}
    86  
    87  	resp, err := client.ModifyStatus(ctxc, req)
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	printRespJSON(resp)
    93  	return nil
    94  }
    95  
    96  var queryScoresCommand = cli.Command{
    97  	Name:        "query",
    98  	Usage:       "Query the autopilot heuristics for nodes' scores.",
    99  	ArgsUsage:   "[flags] <pubkey> <pubkey> <pubkey> ...",
   100  	Description: "",
   101  	Action:      actionDecorator(queryScores),
   102  	Flags: []cli.Flag{
   103  		cli.BoolFlag{
   104  			Name: "ignorelocalstate, i",
   105  			Usage: "Ignore local channel state when calculating " +
   106  				"scores.",
   107  		},
   108  	},
   109  }
   110  
   111  func queryScores(ctx *cli.Context) error {
   112  	ctxc := getContext()
   113  	client, cleanUp := getAutopilotClient(ctx)
   114  	defer cleanUp()
   115  
   116  	args := ctx.Args()
   117  	var pubs []string
   118  
   119  	// Keep reading pubkeys as long as there are arguments.
   120  loop:
   121  	for {
   122  		switch {
   123  		case args.Present():
   124  			pubs = append(pubs, args.First())
   125  			args = args.Tail()
   126  		default:
   127  			break loop
   128  		}
   129  	}
   130  
   131  	req := &autopilotrpc.QueryScoresRequest{
   132  		Pubkeys:          pubs,
   133  		IgnoreLocalState: ctx.Bool("ignorelocalstate"),
   134  	}
   135  
   136  	resp, err := client.QueryScores(ctxc, req)
   137  	if err != nil {
   138  		return err
   139  	}
   140  
   141  	printRespJSON(resp)
   142  	return nil
   143  }
   144  
   145  // autopilotCommands will return the set of commands to enable for autopilotrpc
   146  // builds.
   147  func autopilotCommands() []cli.Command {
   148  	return []cli.Command{
   149  		{
   150  			Name:        "autopilot",
   151  			Category:    "Autopilot",
   152  			Usage:       "Interact with a running autopilot.",
   153  			Description: "",
   154  			Subcommands: []cli.Command{
   155  				getStatusCommand,
   156  				enableCommand,
   157  				disableCommand,
   158  				queryScoresCommand,
   159  			},
   160  		},
   161  	}
   162  }