Skip to content

Commit 1a69a8b

Browse files
committed
accounts: use UpsertAccountPayment in removePayment
Instead of UpdateAccount.
1 parent 077bbec commit 1a69a8b

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed

accounts/errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ var (
1313
// if the WithErrAlreadySucceeded option is used and the payment has
1414
// already succeeded.
1515
ErrAlreadySucceeded = errors.New("payment has already succeeded")
16+
17+
// ErrPaymentUnknown is returned by the UpsertAccountPayment method if
18+
// the WithErrIfUnknown option is used and the payment is not yet known.
19+
ErrPaymentUnknown = errors.New("payment unknown")
1620
)

accounts/interface.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ type upsertAcctPaymentOption struct {
345345
errIfAlreadyPending bool
346346
usePendingAmount bool
347347
errIfAlreadySucceeded bool
348+
errIfUnknown bool
348349
}
349350

350351
// newUpsertPaymentOption creates a new upsertAcctPaymentOption with default
@@ -355,6 +356,7 @@ func newUpsertPaymentOption() *upsertAcctPaymentOption {
355356
errIfAlreadyPending: false,
356357
usePendingAmount: false,
357358
errIfAlreadySucceeded: false,
359+
errIfUnknown: false,
358360
}
359361
}
360362

@@ -394,3 +396,12 @@ func WithPendingAmount() UpsertPaymentOption {
394396
o.usePendingAmount = true
395397
}
396398
}
399+
400+
// WithErrIfUnknown is a functional option that can be passed to the
401+
// UpsertAccountPayment method to indicate that the ErrPaymentUnknown error
402+
// should be returned if the payment is not associated with the account.
403+
func WithErrIfUnknown() UpsertPaymentOption {
404+
return func(o *upsertAcctPaymentOption) {
405+
o.errIfUnknown = true
406+
}
407+
}

accounts/service.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -840,23 +840,24 @@ func (s *InterceptorService) removePayment(ctx context.Context,
840840
return nil
841841
}
842842

843-
account, err := s.store.Account(ctx, pendingPayment.accountID)
844-
if err != nil {
845-
return err
846-
}
847-
848843
pendingPayment.cancel()
849844
delete(s.pendingPayments, hash)
850845

851-
// Have we associated the payment with the account already?
852-
_, ok = account.Payments[hash]
853-
if !ok {
854-
return nil
846+
_, err := s.store.UpsertAccountPayment(
847+
ctx, pendingPayment.accountID, hash, 0, status,
848+
// We don't want the payment to be inserted if it isn't already
849+
// known. So we pass in this option to ensure that the call
850+
// exists early if the payment is unknown.
851+
WithErrIfUnknown(),
852+
// Otherwise, we just want to update the status of the payment
853+
// and use the existing pending amount.
854+
WithPendingAmount(),
855+
)
856+
if err != nil && !errors.Is(err, ErrPaymentUnknown) {
857+
return fmt.Errorf("error updating account: %w", err)
855858
}
856859

857-
// If we did, let's set the status correctly in the DB now.
858-
account.Payments[hash].Status = status
859-
return s.store.UpdateAccount(ctx, account)
860+
return nil
860861
}
861862

862863
// successState returns true if a payment was completed successfully.

accounts/store_kvdb.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ func (s *BoltStore) UpsertAccountPayment(_ context.Context, id AccountID,
300300
if opts.usePendingAmount {
301301
fullAmount = entry.FullAmount
302302
}
303+
} else if opts.errIfUnknown {
304+
return ErrPaymentUnknown
303305
}
304306

305307
account.Payments[paymentHash] = &PaymentEntry{

accounts/store_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,15 @@ func TestAccountUpdateMethods(t *testing.T) {
430430
// error.
431431
err = store.DeleteAccountPayment(ctx, acct.ID, hash1)
432432
require.ErrorContains(t, err, "is not associated")
433+
434+
// Try once more to insert a payment that is currently unknown
435+
// but this time add the WithErrIfUnknown option. This should
436+
// return the ErrPaymentUnknown error.
437+
_, err = store.UpsertAccountPayment(
438+
ctx, acct.ID, hash1, 600, lnrpc.Payment_SUCCEEDED,
439+
WithErrIfUnknown(),
440+
)
441+
require.ErrorIs(t, err, ErrPaymentUnknown)
433442
})
434443
}
435444

0 commit comments

Comments
 (0)