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:
parent
5181f100ed
commit
b0b6151cc1
97
rpcserver.go
97
rpcserver.go
@ -2254,56 +2254,34 @@ func (r *rpcServer) ClosedChannels(ctx context.Context,
|
||||
continue
|
||||
}
|
||||
|
||||
nodePub := dbChannel.RemotePub
|
||||
nodeID := hex.EncodeToString(nodePub.SerializeCompressed())
|
||||
|
||||
var closeType lnrpc.ChannelCloseSummary_ClosureType
|
||||
switch dbChannel.CloseType {
|
||||
case channeldb.CooperativeClose:
|
||||
if filterResults && !in.Cooperative {
|
||||
continue
|
||||
}
|
||||
closeType = lnrpc.ChannelCloseSummary_COOPERATIVE_CLOSE
|
||||
case channeldb.LocalForceClose:
|
||||
if filterResults && !in.LocalForce {
|
||||
continue
|
||||
}
|
||||
closeType = lnrpc.ChannelCloseSummary_LOCAL_FORCE_CLOSE
|
||||
case channeldb.RemoteForceClose:
|
||||
if filterResults && !in.RemoteForce {
|
||||
continue
|
||||
}
|
||||
closeType = lnrpc.ChannelCloseSummary_REMOTE_FORCE_CLOSE
|
||||
case channeldb.BreachClose:
|
||||
if filterResults && !in.Breach {
|
||||
continue
|
||||
}
|
||||
closeType = lnrpc.ChannelCloseSummary_BREACH_CLOSE
|
||||
case channeldb.FundingCanceled:
|
||||
if filterResults && !in.FundingCanceled {
|
||||
continue
|
||||
}
|
||||
closeType = lnrpc.ChannelCloseSummary_FUNDING_CANCELED
|
||||
case channeldb.Abandoned:
|
||||
if filterResults && !in.Abandoned {
|
||||
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)
|
||||
}
|
||||
|
||||
@ -2339,14 +2317,8 @@ func (r *rpcServer) ListChannels(ctx context.Context,
|
||||
|
||||
for _, dbChannel := range dbChannels {
|
||||
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)
|
||||
|
||||
var peerOnline bool
|
||||
if _, err := r.server.FindPeer(nodePub); err == nil {
|
||||
peerOnline = true
|
||||
@ -2364,7 +2336,7 @@ func (r *rpcServer) ListChannels(ctx context.Context,
|
||||
// Next, we'll determine whether we should add this channel to
|
||||
// our list depending on the type of channels requested to us.
|
||||
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
|
||||
// for a specific kind and this channel doesn't satisfy it.
|
||||
@ -2373,12 +2345,34 @@ func (r *rpcServer) ListChannels(ctx context.Context,
|
||||
continue
|
||||
case in.InactiveOnly && isActive:
|
||||
continue
|
||||
case in.PublicOnly && !isPublic:
|
||||
case in.PublicOnly && channel.Private:
|
||||
continue
|
||||
case in.PrivateOnly && isPublic:
|
||||
case in.PrivateOnly && !channel.Private:
|
||||
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
|
||||
// the weight of the commitment transaction. We also add on the
|
||||
// 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
|
||||
}
|
||||
|
||||
resp.Channels = append(resp.Channels, channel)
|
||||
return channel
|
||||
}
|
||||
|
||||
// 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 resp, nil
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user