rpcserver: add helper functions to format open and closed RPC channels.

`createRPCChannel` is used by the `listchannels` RPC call and will be
used by `subscribechannels` as well.
`createRPCClosedChannel` is used by the `closedchannels` RPC call and
will also be used by `subscribechannels`.

This is used by the `listchannels` RPC call and will be used by
`subscribechannels` as well. Its purpose is to mitigate code duplication
between the two RPC calls.
This commit is contained in:
Valentine Wallace authored and Valentine Wallace committed 2019-02-05 18:17:54 -08:00
1 parent 5181f100ed
commit b0b6151cc1
1 file changed
+63 -34
+63 -34
View File
@@ -2254,56 +2254,34 @@ func (r *rpcServer) ClosedChannels(ctx context.Context,
continue continue
} }
nodePub := dbChannel.RemotePub
nodeID := hex.EncodeToString(nodePub.SerializeCompressed())
var closeType lnrpc.ChannelCloseSummary_ClosureType
switch dbChannel.CloseType { switch dbChannel.CloseType {
case channeldb.CooperativeClose: case channeldb.CooperativeClose:
if filterResults && !in.Cooperative { if filterResults && !in.Cooperative {
continue continue
} }
closeType = lnrpc.ChannelCloseSummary_COOPERATIVE_CLOSE
case channeldb.LocalForceClose: case channeldb.LocalForceClose:
if filterResults && !in.LocalForce { if filterResults && !in.LocalForce {
continue continue
} }
closeType = lnrpc.ChannelCloseSummary_LOCAL_FORCE_CLOSE
case channeldb.RemoteForceClose: case channeldb.RemoteForceClose:
if filterResults && !in.RemoteForce { if filterResults && !in.RemoteForce {
continue continue
} }
closeType = lnrpc.ChannelCloseSummary_REMOTE_FORCE_CLOSE
case channeldb.BreachClose: case channeldb.BreachClose:
if filterResults && !in.Breach { if filterResults && !in.Breach {
continue continue
} }
closeType = lnrpc.ChannelCloseSummary_BREACH_CLOSE
case channeldb.FundingCanceled: case channeldb.FundingCanceled:
if filterResults && !in.FundingCanceled { if filterResults && !in.FundingCanceled {
continue continue
} }
closeType = lnrpc.ChannelCloseSummary_FUNDING_CANCELED
case channeldb.Abandoned: case channeldb.Abandoned:
if filterResults && !in.Abandoned { if filterResults && !in.Abandoned {
continue continue
} }
closeType = lnrpc.ChannelCloseSummary_ABANDONED
}
channel := &lnrpc.ChannelCloseSummary{
Capacity: int64(dbChannel.Capacity),
RemotePubkey: nodeID,
CloseHeight: dbChannel.CloseHeight,
CloseType: closeType,
ChannelPoint: dbChannel.ChanPoint.String(),
ChanId: dbChannel.ShortChanID.ToUint64(),
SettledBalance: int64(dbChannel.SettledBalance),
TimeLockedBalance: int64(dbChannel.TimeLockedBalance),
ChainHash: dbChannel.ChainHash.String(),
ClosingTxHash: dbChannel.ClosingTXID.String(),
} }
channel := createRPCClosedChannel(dbChannel)
resp.Channels = append(resp.Channels, channel) resp.Channels = append(resp.Channels, channel)
} }
@@ -2339,14 +2317,8 @@ func (r *rpcServer) ListChannels(ctx context.Context,
for _, dbChannel := range dbChannels { for _, dbChannel := range dbChannels {
nodePub := dbChannel.IdentityPub nodePub := dbChannel.IdentityPub
nodeID := hex.EncodeToString(nodePub.SerializeCompressed())
chanPoint := dbChannel.FundingOutpoint chanPoint := dbChannel.FundingOutpoint
// With the channel point known, retrieve the network channel
// ID from the database.
var chanID uint64
chanID, _ = graph.ChannelID(&chanPoint)
var peerOnline bool var peerOnline bool
if _, err := r.server.FindPeer(nodePub); err == nil { if _, err := r.server.FindPeer(nodePub); err == nil {
peerOnline = true peerOnline = true
@@ -2364,7 +2336,7 @@ func (r *rpcServer) ListChannels(ctx context.Context,
// Next, we'll determine whether we should add this channel to // Next, we'll determine whether we should add this channel to
// our list depending on the type of channels requested to us. // our list depending on the type of channels requested to us.
isActive := peerOnline && linkActive isActive := peerOnline && linkActive
isPublic := dbChannel.ChannelFlags&lnwire.FFAnnounceChannel != 0 channel := createRPCOpenChannel(r, graph, dbChannel, isActive)
// We'll only skip returning this channel if we were requested // We'll only skip returning this channel if we were requested
// for a specific kind and this channel doesn't satisfy it. // for a specific kind and this channel doesn't satisfy it.
@@ -2373,12 +2345,34 @@ func (r *rpcServer) ListChannels(ctx context.Context,
continue continue
case in.InactiveOnly && isActive: case in.InactiveOnly && isActive:
continue continue
case in.PublicOnly && !isPublic: case in.PublicOnly && channel.Private:
continue continue
case in.PrivateOnly && isPublic: case in.PrivateOnly && !channel.Private:
continue continue
} }
resp.Channels = append(resp.Channels, channel)
}
return resp, nil
}
// createRPCOpenChannel creates an *lnrpc.Channel from the *channeldb.Channel.
func createRPCOpenChannel(r *rpcServer, graph *channeldb.ChannelGraph,
dbChannel *channeldb.OpenChannel, isActive bool) *lnrpc.Channel {
nodePub := dbChannel.IdentityPub
nodeID := hex.EncodeToString(nodePub.SerializeCompressed())
chanPoint := dbChannel.FundingOutpoint
// With the channel point known, retrieve the network channel
// ID from the database.
var chanID uint64
chanID, _ = graph.ChannelID(&chanPoint)
// Next, we'll determine whether the channel is public or not.
isPublic := dbChannel.ChannelFlags&lnwire.FFAnnounceChannel != 0
// As this is required for display purposes, we'll calculate // As this is required for display purposes, we'll calculate
// the weight of the commitment transaction. We also add on the // the weight of the commitment transaction. We also add on the
// estimated weight of the witness to calculate the weight of // estimated weight of the witness to calculate the weight of
@@ -2439,10 +2433,45 @@ func (r *rpcServer) ListChannels(ctx context.Context,
channel.UnsettledBalance += channel.PendingHtlcs[i].Amount channel.UnsettledBalance += channel.PendingHtlcs[i].Amount
} }
resp.Channels = append(resp.Channels, channel) return channel
} }
return resp, nil // createRPCClosedChannel creates an *lnrpc.ClosedChannelSummary from a
// *channeldb.ChannelCloseSummary.
func createRPCClosedChannel(
dbChannel *channeldb.ChannelCloseSummary) *lnrpc.ChannelCloseSummary {
nodePub := dbChannel.RemotePub
nodeID := hex.EncodeToString(nodePub.SerializeCompressed())
var closeType lnrpc.ChannelCloseSummary_ClosureType
switch dbChannel.CloseType {
case channeldb.CooperativeClose:
closeType = lnrpc.ChannelCloseSummary_COOPERATIVE_CLOSE
case channeldb.LocalForceClose:
closeType = lnrpc.ChannelCloseSummary_LOCAL_FORCE_CLOSE
case channeldb.RemoteForceClose:
closeType = lnrpc.ChannelCloseSummary_REMOTE_FORCE_CLOSE
case channeldb.BreachClose:
closeType = lnrpc.ChannelCloseSummary_BREACH_CLOSE
case channeldb.FundingCanceled:
closeType = lnrpc.ChannelCloseSummary_FUNDING_CANCELED
case channeldb.Abandoned:
closeType = lnrpc.ChannelCloseSummary_ABANDONED
}
return &lnrpc.ChannelCloseSummary{
Capacity: int64(dbChannel.Capacity),
RemotePubkey: nodeID,
CloseHeight: dbChannel.CloseHeight,
CloseType: closeType,
ChannelPoint: dbChannel.ChanPoint.String(),
ChanId: dbChannel.ShortChanID.ToUint64(),
SettledBalance: int64(dbChannel.SettledBalance),
TimeLockedBalance: int64(dbChannel.TimeLockedBalance),
ChainHash: dbChannel.ChainHash.String(),
ClosingTxHash: dbChannel.ClosingTXID.String(),
}
} }
// savePayment saves a successfully completed payment to the database for // savePayment saves a successfully completed payment to the database for