Skip to content

Commit

Permalink
rpcserver: remove duplicated htlcs in report
Browse files Browse the repository at this point in the history
  • Loading branch information
yyforyongyu committed Oct 29, 2024
1 parent 4ef5f66 commit a5d0e99
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3774,8 +3774,10 @@ func (r *rpcServer) ChannelBalance(ctx context.Context,
}

type (
pendingForceClose *lnrpc.PendingChannelsResponse_ForceClosedChannel

pendingOpenChannels []*lnrpc.PendingChannelsResponse_PendingOpenChannel
pendingForceClose []*lnrpc.PendingChannelsResponse_ForceClosedChannel
pendingForceCloses []*lnrpc.PendingChannelsResponse_ForceClosedChannel
waitingCloseChannels []*lnrpc.PendingChannelsResponse_WaitingCloseChannel
)

Expand Down Expand Up @@ -3861,7 +3863,7 @@ func (r *rpcServer) fetchPendingOpenChannels() (pendingOpenChannels, error) {
// fetchPendingForceCloseChannels queries the database for a list of channels
// that have their closing transactions confirmed but not fully resolved yet.
// The returned result is used in the response of the PendingChannels RPC.
func (r *rpcServer) fetchPendingForceCloseChannels() (pendingForceClose,
func (r *rpcServer) fetchPendingForceCloseChannels() (pendingForceCloses,
int64, error) {

_, currentHeight, err := r.server.cc.ChainIO.GetBestBlock()
Expand All @@ -3877,7 +3879,7 @@ func (r *rpcServer) fetchPendingForceCloseChannels() (pendingForceClose,
return nil, 0, err
}

result := make(pendingForceClose, 0)
result := make(pendingForceCloses, 0)
limboBalance := int64(0)

for _, pendingClose := range channels {
Expand Down Expand Up @@ -3992,13 +3994,47 @@ func (r *rpcServer) fetchPendingForceCloseChannels() (pendingForceClose,
}

limboBalance += forceClose.LimboBalance

// Dedup pending HTLCs.
dedupPendingHTLCs(forceClose)
result = append(result, forceClose)
}
}

return result, limboBalance, nil
}

// dedupPendingHTLCs takes a pending force close response and removes the
// duplicated HTLCs found in the report.
//
// TODO(yy): remove the utxo nursery so there won't be duplicate reports.
func dedupPendingHTLCs(forceClose pendingForceClose) {
// Make an outpoint map for lookup.
ops := make(map[string]struct{}, len(forceClose.PendingHtlcs))

// Create a set to store the result.
htlcSet := make([]*lnrpc.PendingHTLC, 0, len(forceClose.PendingHtlcs))

// Go through each pending HTLC, if it's outpoint hasn't been seen
// before, it will be added to the set.
for _, htlc := range forceClose.PendingHtlcs {
op := htlc.Outpoint
_, found := ops[op]

// Already seen, skip.
if found {
continue
}

// Otherwise save it to the set.
ops[op] = struct{}{}
htlcSet = append(htlcSet, htlc)
}

// Attach the dedupped set.
forceClose.PendingHtlcs = htlcSet
}

// fetchWaitingCloseChannels queries the database for a list of channels
// that have their closing transactions broadcast but not confirmed yet.
// The returned result is used in the response of the PendingChannels RPC.
Expand Down

0 comments on commit a5d0e99

Please sign in to comment.