From 89ac071e5605387bfc12afcd2d3ea7eac968d948 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Mon, 6 Mar 2023 15:48:24 +0800 Subject: [PATCH] channeldb: add `HasSettledHTLC` and `PaymentFailed` fields to state --- channeldb/mp_payment.go | 19 ++++++++++++------- channeldb/mp_payment_test.go | 32 +++++++++++--------------------- 2 files changed, 23 insertions(+), 28 deletions(-) diff --git a/channeldb/mp_payment.go b/channeldb/mp_payment.go index 58fe66d53..df10385a9 100644 --- a/channeldb/mp_payment.go +++ b/channeldb/mp_payment.go @@ -167,6 +167,14 @@ type MPPaymentState struct { // shards should be launched. This value is true if we have an HTLC // settled or the payment has an error. Terminate bool + + // HasSettledHTLC is true if at least one of the payment's HTLCs is + // settled. + HasSettledHTLC bool + + // PaymentFailed is true if the payment has been marked as failed with + // a reason. + PaymentFailed bool } // MPPayment is a wrapper around a payment's PaymentCreationInfo and @@ -274,11 +282,6 @@ func (m *MPPayment) GetAttempt(id uint64) (*HTLCAttempt, error) { // registrations when it's newly created, or none of its HTLCs is in a terminal // state. func (m *MPPayment) Registrable() error { - // Get the terminal info. - settle, reason := m.TerminalInfo() - settled := settle != nil - failed := reason != nil - // If updating the payment is not allowed, we can't register new HTLCs. // Otherwise, the status must be either `StatusInitiated` or // `StatusInFlight`. @@ -294,12 +297,12 @@ func (m *MPPayment) Registrable() error { // There are still inflight HTLCs and we need to check whether there // are settled HTLCs or the payment is failed. If we already have // settled HTLCs, we won't allow adding more HTLCs. - if settled { + if m.State.HasSettledHTLC { return ErrPaymentPendingSettled } // If the payment is already failed, we won't allow adding more HTLCs. - if failed { + if m.State.PaymentFailed { return ErrPaymentPendingFailed } @@ -341,6 +344,8 @@ func (m *MPPayment) setState() error { RemainingAmt: totalAmt - sentAmt, FeesPaid: fees, Terminate: terminate, + HasSettledHTLC: settle != nil, + PaymentFailed: failure != nil, } m.Status = status diff --git a/channeldb/mp_payment_test.go b/channeldb/mp_payment_test.go index 920e79a33..e18cf5989 100644 --- a/channeldb/mp_payment_test.go +++ b/channeldb/mp_payment_test.go @@ -87,29 +87,15 @@ func TestRegistrable(t *testing.T) { }, } - // Create test objects. - reason := FailureReasonError - htlcSettled := HTLCAttempt{ - Settle: &HTLCSettleInfo{}, - } - for i, tc := range testCases { i, tc := i, tc p := &MPPayment{ Status: tc.status, - } - - // Add the settled htlc to the payment if needed. - htlcs := make([]HTLCAttempt, 0) - if tc.hasSettledHTLC { - htlcs = append(htlcs, htlcSettled) - } - p.HTLCs = htlcs - - // Add the failure reason if needed. - if tc.paymentFailed { - p.FailureReason = &reason + State: &MPPaymentState{ + HasSettledHTLC: tc.hasSettledHTLC, + PaymentFailed: tc.paymentFailed, + }, } name := fmt.Sprintf("test_%d_%s", i, p.Status.String()) @@ -173,7 +159,8 @@ func TestPaymentSetState(t *testing.T) { NumAttemptsInFlight: 1, RemainingAmt: 1000 - 90, FeesPaid: 10, - Terminate: false, + HasSettledHTLC: false, + PaymentFailed: false, }, }, { @@ -193,7 +180,8 @@ func TestPaymentSetState(t *testing.T) { NumAttemptsInFlight: 0, RemainingAmt: 1000 - 90, FeesPaid: 10, - Terminate: true, + HasSettledHTLC: true, + PaymentFailed: false, }, }, { @@ -211,13 +199,15 @@ func TestPaymentSetState(t *testing.T) { NumAttemptsInFlight: 0, RemainingAmt: 1000, FeesPaid: 0, - Terminate: true, + HasSettledHTLC: false, + PaymentFailed: true, }, }, } for _, tc := range testCases { tc := tc + t.Run(tc.name, func(t *testing.T) { t.Parallel()