cmd/lncli: add --peer flag to list channels

This commit adds a flag to listchannels that filters by remote pubkey.
This commit is contained in:
Conner Fromknecht 2020-03-03 05:11:33 -08:00
parent c2ec4a450d
commit 11532df5f3
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
5 changed files with 685 additions and 628 deletions

View File

@ -1990,6 +1990,12 @@ var listChannelsCommand = cli.Command{
Name: "private_only",
Usage: "only list channels which are currently private",
},
cli.StringFlag{
Name: "peer",
Usage: "(optional) only display channels with a " +
"particular peer, accepts 66-byte, " +
"hex-encoded pubkeys",
},
},
Action: actionDecorator(listChannels),
}
@ -1999,11 +2005,26 @@ func listChannels(ctx *cli.Context) error {
client, cleanUp := getClient(ctx)
defer cleanUp()
peer := ctx.String("peer")
// If the user requested channels with a particular key, parse the
// provided pubkey.
var peerKey []byte
if len(peer) > 0 {
pk, err := route.NewVertexFromStr(peer)
if err != nil {
return fmt.Errorf("invalid --peer pubkey: %v", err)
}
peerKey = pk[:]
}
req := &lnrpc.ListChannelsRequest{
ActiveOnly: ctx.Bool("active_only"),
InactiveOnly: ctx.Bool("inactive_only"),
PublicOnly: ctx.Bool("public_only"),
PrivateOnly: ctx.Bool("private_only"),
Peer: peerKey,
}
resp, err := client.ListChannels(ctxb, req)
@ -2011,8 +2032,6 @@ func listChannels(ctx *cli.Context) error {
return err
}
// TODO(roasbeef): defer close the client for the all
printRespJSON(resp)
return nil

File diff suppressed because it is too large Load Diff

View File

@ -1385,6 +1385,12 @@ message ListChannelsRequest {
bool inactive_only = 2;
bool public_only = 3;
bool private_only = 4;
/**
Filters the response for channels with a target peer's pubkey. If peer is
empty, all channels will be returned.
*/
bytes peer = 5;
}
message ListChannelsResponse {
/// The list of active channels

View File

@ -116,6 +116,14 @@
"required": false,
"type": "boolean",
"format": "boolean"
},
{
"name": "peer",
"description": "*\nFilters the response for channels with a target peer's pubkey. If peer is\nempty, all channels will be returned.",
"in": "query",
"required": false,
"type": "string",
"format": "byte"
}
],
"tags": [

View File

@ -2948,6 +2948,11 @@ func (r *rpcServer) ListChannels(ctx context.Context,
"`private_only` can be set, but not both")
}
if len(in.Peer) > 0 && len(in.Peer) != 33 {
_, err := route.NewVertexFromBytes(in.Peer)
return nil, fmt.Errorf("invalid `peer` key: %v", err)
}
resp := &lnrpc.ListChannelsResponse{}
graph := r.server.chanDB.ChannelGraph()
@ -2962,8 +2967,15 @@ func (r *rpcServer) ListChannels(ctx context.Context,
for _, dbChannel := range dbChannels {
nodePub := dbChannel.IdentityPub
nodePubBytes := nodePub.SerializeCompressed()
chanPoint := dbChannel.FundingOutpoint
// If the caller requested channels for a target node, skip any
// that don't match the provided pubkey.
if len(in.Peer) > 0 && !bytes.Equal(nodePubBytes, in.Peer) {
continue
}
var peerOnline bool
if _, err := r.server.FindPeer(nodePub); err == nil {
peerOnline = true