channeldb+routing: apply method Terminated to decide a payment's terminal state

This commit applies the new method `Terminated`. A side effect from
using this method is, we can now save one query `fetchPayment` inside
`FetchInFlightPayments`.
This commit is contained in:
yyforyongyu authored and Olaoluwa Osuntokun committed 2023-10-06 16:34:47 -07:00
1 parent fac6044501
commit c175386c4d
4 files changed
+26 -19

No files matched your search

+2 -2
View File
@@ -280,7 +280,7 @@ func (p *controlTower) SubscribePayment(paymentHash lntypes.Hash) (
// updates. Otherwise this update is the final update and the incoming
// channel can be closed. This will close the queue's outgoing channel
// when all updates have been written.
if payment.Status == channeldb.StatusInFlight {
if !payment.Terminated() {
p.subscribersMtx.Lock()
p.subscribers[paymentHash] = append(
p.subscribers[paymentHash], subscriber,
@@ -344,7 +344,7 @@ func (p *controlTower) notifySubscribers(paymentHash lntypes.Hash,
// If the payment reached a terminal state, the subscriber list can be
// cleared. There won't be any more updates.
terminal := event.Status != channeldb.StatusInFlight
terminal := event.Terminated()
if terminal {
delete(p.subscribers, paymentHash)
}
+12 -7
View File
@@ -119,9 +119,9 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
subscriber1, subscriber2, subscriber3,
}
for _, s := range subscribers {
for i, s := range subscribers {
var result *channeldb.MPPayment
for result == nil || result.Status == channeldb.StatusInFlight {
for result == nil || !result.Terminated() {
select {
case item := <-s.Updates():
result = item.(*channeldb.MPPayment)
@@ -130,9 +130,10 @@ func TestControlTowerSubscribeSuccess(t *testing.T) {
}
}
if result.Status != channeldb.StatusSucceeded {
t.Fatal("unexpected payment state")
}
require.Equalf(t, channeldb.StatusSucceeded, result.Status,
"subscriber %v failed, want %s, got %s", i,
channeldb.StatusSucceeded, result.Status)
settle, _ := result.TerminalInfo()
if settle.Preimage != preimg {
t.Fatal("unexpected preimage")
@@ -474,9 +475,9 @@ func testPaymentControlSubscribeFail(t *testing.T, registerAttempt,
subscriber1, subscriber2,
}
for _, s := range subscribers {
for i, s := range subscribers {
var result *channeldb.MPPayment
for result == nil || result.Status == channeldb.StatusInFlight {
for result == nil || !result.Terminated() {
select {
case item := <-s.Updates():
result = item.(*channeldb.MPPayment)
@@ -510,6 +511,10 @@ func testPaymentControlSubscribeFail(t *testing.T, registerAttempt,
len(result.HTLCs))
}
require.Equalf(t, channeldb.StatusFailed, result.Status,
"subscriber %v failed, want %s, got %s", i,
channeldb.StatusFailed, result.Status)
if *result.FailureReason != channeldb.FailureReasonTimeout {
t.Fatal("unexpected failure reason")
}