routing+lnd: prepare closed channel SCIDs in server

The method `FetchClosedChannels` sometimes prematurely mark a pending
force closing channel as finalized, therefore we need to furthur check
`FetchPendingChannels` to make sure the channel is indeed finalized.
This commit is contained in:
yyforyongyu
2023-11-17 21:07:53 +08:00
committed by yyforyongyu
parent e8f292edf4
commit 188aa9a4d4
3 changed files with 83 additions and 53 deletions

View File

@@ -286,12 +286,10 @@ type Config struct {
// graph that we received from a payment failure.
ApplyChannelUpdate func(msg *lnwire.ChannelUpdate) bool
// FetchClosedChannels is used by the router to fetch closed channels.
// ClosedSCIDs is used by the router to fetch closed channels.
//
// TODO(yy): remove this method once the root cause of stuck payments
// is found.
FetchClosedChannels func(pendingOnly bool) (
[]*channeldb.ChannelCloseSummary, error)
// TODO(yy): remove it once the root cause of stuck payments is found.
ClosedSCIDs map[lnwire.ShortChannelID]struct{}
}
// EdgeLocator is a struct used to identify a specific edge.
@@ -1391,19 +1389,6 @@ func (r *ChannelRouter) BuildRoute(amt fn.Option[lnwire.MilliSatoshi],
// resumePayments fetches inflight payments and resumes their payment
// lifecycles.
func (r *ChannelRouter) resumePayments() error {
// Get a list of closed channels.
channels, err := r.cfg.FetchClosedChannels(false)
if err != nil {
return err
}
closedSCIDs := make(map[lnwire.ShortChannelID]struct{}, len(channels))
for _, c := range channels {
if !c.IsPending {
closedSCIDs[c.ShortChanID] = struct{}{}
}
}
// Get all payments that are inflight.
payments, err := r.cfg.Control.FetchInFlightPayments()
if err != nil {
@@ -1422,9 +1407,7 @@ func (r *ChannelRouter) resumePayments() error {
// Try to fail the attempt if the route contains a dead
// channel.
r.failStaleAttempt(
a, p.Info.PaymentIdentifier, closedSCIDs,
)
r.failStaleAttempt(a, p.Info.PaymentIdentifier)
}
}
@@ -1510,7 +1493,7 @@ func (r *ChannelRouter) resumePayments() error {
// - https://github.com/lightningnetwork/lnd/issues/8146
// - https://github.com/lightningnetwork/lnd/pull/8174
func (r *ChannelRouter) failStaleAttempt(a channeldb.HTLCAttempt,
payHash lntypes.Hash, closedSCIDs map[lnwire.ShortChannelID]struct{}) {
payHash lntypes.Hash) {
// We can only fail inflight HTLCs so we skip the settled/failed ones.
if a.Failure != nil || a.Settle != nil {
@@ -1571,12 +1554,12 @@ func (r *ChannelRouter) failStaleAttempt(a channeldb.HTLCAttempt,
}
// The channel link is not active, we now check whether this
// channel is already closed. If so, we fail it as there's no
// need to wait for the network result because it won't be
// re-sent. If the channel is still pending, we'll keep waiting
// for the result as we may get a contract resolution for this
// HTLC.
if _, ok := closedSCIDs[scid]; ok {
// channel is already closed. If so, we fail the HTLC attempt
// as there's no need to wait for its network result because
// there's no link. If the channel is still pending, we'll keep
// waiting for the result as we may get a contract resolution
// for this HTLC.
if _, ok := r.cfg.ClosedSCIDs[scid]; ok {
shouldFail = true
}
}