From ee593b273c322c47740423c1b6d3d55320c391c2 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 26 Oct 2016 15:09:01 -0700 Subject: [PATCH] rpc: query the database instead of active peers for the ListChannel RPC This commit takes advantage of the newly added channeldb.FetchAllChannels method to return the state of all active channels for the ListChannels RPC command. With this change the state of all channels can now be queried regardless of if any/all the peers are currently online. In a future modification a bit will be added to the channel information which indicates if the LinkNode the channel was created with is currently online or not. --- peer.go | 5 +++++ rpcserver.go | 56 ++++++++++++++++++++++++++++------------------------ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/peer.go b/peer.go index d1c197a4..b2004735 100644 --- a/peer.go +++ b/peer.go @@ -590,6 +590,11 @@ out: req.resp <- snapshots case pendingChanPoint := <-p.barrierInits: + // A new channel has almost finished the funding + // process. In order to properly synchronize with the + // writeHandler goroutine, we add a new channel to the + // barriers map which will be closed once the channel + // is fully open. p.barrierMtx.Lock() peerLog.Tracef("Creating chan barrier for "+ "ChannelPoint(%v)", pendingChanPoint) diff --git a/rpcserver.go b/rpcserver.go index 8cf31294..bbb74307 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -454,41 +454,45 @@ func (r *rpcServer) PendingChannels(ctx context.Context, // 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 +// TODO(roasbeef): add 'online' bit to response 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() + dbChannels, err := r.server.chanDB.FetchAllChannels() + if err != nil { + return nil, err + } - 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, - } - } + rpcsLog.Infof("[listchannels] fetched %v channels from DB", + len(dbChannels)) - resp.Channels = append(resp.Channels, channel) + for _, dbChannel := range dbChannels { + nodePub := dbChannel.IdentityPub.SerializeCompressed() + nodeID := hex.EncodeToString(nodePub) + + channel := &lnrpc.ActiveChannel{ + RemotePubkey: nodeID, + ChannelPoint: dbChannel.ChanID.String(), + Capacity: int64(dbChannel.Capacity), + LocalBalance: int64(dbChannel.OurBalance), + RemoteBalance: int64(dbChannel.TheirBalance), + NumUpdates: dbChannel.NumUpdates, + PendingHtlcs: make([]*lnrpc.HTLC, len(dbChannel.Htlcs)), } + for i, htlc := range dbChannel.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