Skip to content

Commit 0ab9846

Browse files
committed
rpcserver: remove duplicated htlcs in report
1 parent c632965 commit 0ab9846

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

rpcserver.go

+39-3
Original file line numberDiff line numberDiff line change
@@ -3774,8 +3774,10 @@ func (r *rpcServer) ChannelBalance(ctx context.Context,
37743774
}
37753775

37763776
type (
3777+
pendingForceClose *lnrpc.PendingChannelsResponse_ForceClosedChannel
3778+
37773779
pendingOpenChannels []*lnrpc.PendingChannelsResponse_PendingOpenChannel
3778-
pendingForceClose []*lnrpc.PendingChannelsResponse_ForceClosedChannel
3780+
pendingForceCloses []*lnrpc.PendingChannelsResponse_ForceClosedChannel
37793781
waitingCloseChannels []*lnrpc.PendingChannelsResponse_WaitingCloseChannel
37803782
)
37813783

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

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

3880-
result := make(pendingForceClose, 0)
3882+
result := make(pendingForceCloses, 0)
38813883
limboBalance := int64(0)
38823884

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

39943996
limboBalance += forceClose.LimboBalance
3997+
3998+
// Dedup pending HTLCs.
3999+
dedupPendingHTLCs(forceClose)
39954000
result = append(result, forceClose)
39964001
}
39974002
}
39984003

39994004
return result, limboBalance, nil
40004005
}
40014006

4007+
// dedupPendingHTLCs takes a pending force close response and removes the
4008+
// duplicated HTLCs found in the report.
4009+
//
4010+
// TODO(yy): remove the utxo nursery so there won't be duplicate reports.
4011+
func dedupPendingHTLCs(forceClose pendingForceClose) {
4012+
// Make an outpoint map for lookup.
4013+
ops := make(map[string]struct{}, len(forceClose.PendingHtlcs))
4014+
4015+
// Create a set to store the result.
4016+
htlcSet := make([]*lnrpc.PendingHTLC, 0, len(forceClose.PendingHtlcs))
4017+
4018+
// Go through each pending HTLC, if it's outpoint hasn't been seen
4019+
// before, it will be added to the set.
4020+
for _, htlc := range forceClose.PendingHtlcs {
4021+
op := htlc.Outpoint
4022+
_, found := ops[op]
4023+
4024+
// Already seen, skip.
4025+
if found {
4026+
continue
4027+
}
4028+
4029+
// Otherwise save it to the set.
4030+
ops[op] = struct{}{}
4031+
htlcSet = append(htlcSet, htlc)
4032+
}
4033+
4034+
// Attach the dedupped set.
4035+
forceClose.PendingHtlcs = htlcSet
4036+
}
4037+
40024038
// fetchWaitingCloseChannels queries the database for a list of channels
40034039
// that have their closing transactions broadcast but not confirmed yet.
40044040
// The returned result is used in the response of the PendingChannels RPC.

0 commit comments

Comments
 (0)