From 7a6905d6976eedbfe0c62c5efefb7d726ae18ccb Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Sun, 25 Sep 2016 20:04:03 -0700 Subject: [PATCH] rpcserver: implement the ListChannels RPC --- rpcserver.go | 56 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/rpcserver.go b/rpcserver.go index 304022ece..ad8dc9398 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -354,20 +354,6 @@ func (r *rpcServer) ListPeers(ctx context.Context, BytesSent: atomic.LoadUint64(&serverPeer.bytesSent), } - chanSnapshots := serverPeer.ChannelSnapshots() - peer.Channels = make([]*lnrpc.ActiveChannel, 0, len(chanSnapshots)) - for _, chanSnapshot := range chanSnapshots { - channel := &lnrpc.ActiveChannel{ - RemoteId: lnID, - ChannelPoint: chanSnapshot.ChannelPoint.String(), - Capacity: int64(chanSnapshot.Capacity), - LocalBalance: int64(chanSnapshot.LocalBalance), - RemoteBalance: int64(chanSnapshot.RemoteBalance), - NumUpdates: chanSnapshot.NumUpdates, - } - peer.Channels = append(peer.Channels, channel) - } - resp.Peers = append(resp.Peers, peer) } @@ -446,6 +432,48 @@ func (r *rpcServer) PendingChannels(ctx context.Context, }, nil } +// ListChannels returns a description of all direct active, open channels the +// node knows of. +// TODO(roasbeef): read all active channels from the DB? +// * add 'online' toggle bit +func (r *rpcServer) ListChannels(ctx context.Context, + in *lnrpc.ListChannelsRequest) (*lnrpc.ListChannelsResponse, error) { + + resp := &lnrpc.ListChannelsResponse{} + peers := r.server.Peers() + + for _, peer := range peers { + lnID := hex.EncodeToString(peer.identityPub.SerializeCompressed()) + chanSnapshots := peer.ChannelSnapshots() + + for _, chanSnapshot := range chanSnapshots { + channel := &lnrpc.ActiveChannel{ + RemotePubkey: lnID, + ChannelPoint: chanSnapshot.ChannelPoint.String(), + Capacity: int64(chanSnapshot.Capacity), + LocalBalance: int64(chanSnapshot.LocalBalance), + RemoteBalance: int64(chanSnapshot.RemoteBalance), + NumUpdates: chanSnapshot.NumUpdates, + PendingHtlcs: make([]*lnrpc.HTLC, len(chanSnapshot.Htlcs)), + } + for i, htlc := range chanSnapshot.Htlcs { + channel.PendingHtlcs[i] = &lnrpc.HTLC{ + Incoming: htlc.Incoming, + Amount: int64(htlc.Amt), + HashLock: htlc.RHash[:], + ExpirationHeight: htlc.RefundTimeout, + RevocationDelay: htlc.RevocationDelay, + } + } + + resp.Channels = append(resp.Channels, channel) + } + + } + + return resp, nil +} + // SendPayment dispatches a bi-directional streaming RPC for sending payments // through the Lightning Network. A single RPC invocation creates a persistent // bi-directional stream allowing clients to rapidly send payments through the