Skip to content

Commit

Permalink
lsps2: use paymentFailure chan for timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
JssDWt committed Sep 14, 2023
1 parent 894c80c commit c6a5798
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
20 changes: 8 additions & 12 deletions lsps2/intercept_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type Interceptor struct {
registrationReady chan *registrationReadyEvent
notRegistered chan string
paymentReady chan string
paymentTimeout chan string
paymentFailure chan *paymentFailureEvent
paymentChanOpened chan *paymentChanOpenedEvent
inflightPayments map[string]*paymentState
Expand Down Expand Up @@ -68,7 +67,6 @@ func NewInterceptHandler(
registrationReady: make(chan *registrationReadyEvent, 1000),
notRegistered: make(chan string, 1000),
paymentReady: make(chan string, 1000),
paymentTimeout: make(chan string, 1000),
paymentFailure: make(chan *paymentFailureEvent, 1000),
paymentChanOpened: make(chan *paymentChanOpenedEvent, 1000),
inflightPayments: make(map[string]*paymentState),
Expand Down Expand Up @@ -148,8 +146,6 @@ func (i *Interceptor) Start(ctx context.Context) {
i.handlePartAwaitingRegistration(ev)
case paymentId := <-i.paymentReady:
i.handlePaymentReady(paymentId)
case paymentId := <-i.paymentTimeout:
i.handlePaymentTimeout(paymentId)
case ev := <-i.paymentFailure:
i.handlePaymentFailure(ev.paymentId, ev.code)
case ev := <-i.paymentChanOpened:
Expand All @@ -159,23 +155,27 @@ func (i *Interceptor) Start(ctx context.Context) {
}

func (i *Interceptor) handleNewPart(part *partState) {
payment, paymentExisted := i.inflightPayments[part.req.PaymentId()]
paymentId := part.req.PaymentId()
payment, paymentExisted := i.inflightPayments[paymentId]
if !paymentExisted {
payment = &paymentState{
id: part.req.PaymentId(),
id: paymentId,
fakeScid: part.req.Scid,
parts: make(map[string]*partState),
timeoutChan: make(chan struct{}),
}
i.inflightPayments[part.req.PaymentId()] = payment
i.inflightPayments[paymentId] = payment

go func() {
select {
case <-time.After(i.config.MppTimeout):
// Handle timeout inside the event loop, to make sure there are
// no race conditions, since this timeout watcher is running in
// a goroutine.
i.paymentTimeout <- part.req.PaymentId()
i.paymentFailure <- &paymentFailureEvent{
paymentId: paymentId,
code: shared.FAILURE_TEMPORARY_CHANNEL_FAILURE,
}
case <-payment.timeoutChan:
// Stop listening for timeouts when the payment is ready.
}
Expand Down Expand Up @@ -666,10 +666,6 @@ func (i *Interceptor) handlePaymentChanOpened(event *paymentChanOpenedEvent) {
delete(i.inflightPayments, event.paymentId)
}

func (i *Interceptor) handlePaymentTimeout(paymentId string) {
i.handlePaymentFailure(paymentId, shared.FAILURE_TEMPORARY_CHANNEL_FAILURE)
}

func (i *Interceptor) handlePaymentFailure(
paymentId string,
code shared.InterceptFailureCode,
Expand Down
4 changes: 3 additions & 1 deletion lsps2/intercept_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,12 @@ func runIntercept(i *Interceptor, req shared.InterceptRequest, res *shared.Inter
func assertEmpty(t *testing.T, i *Interceptor) {
assert.Empty(t, i.inflightPayments)
assert.Empty(t, i.newPart)
assert.Empty(t, i.registrationReady)
assert.Empty(t, i.notRegistered)
assert.Empty(t, i.partAwaitingRegistration)
assert.Empty(t, i.paymentChanOpened)
assert.Empty(t, i.paymentFailure)
assert.Empty(t, i.paymentReady)
assert.Empty(t, i.paymentTimeout)
}

// Asserts that a part that is not associated with a bought channel is not
Expand Down

0 comments on commit c6a5798

Please sign in to comment.