Skip to content

Commit

Permalink
accounts: use UpsertAccountPayment in removePayment
Browse files Browse the repository at this point in the history
Instead of UpdateAccount.
  • Loading branch information
ellemouton committed Jan 16, 2025
1 parent 4e07e3f commit daa5c0b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
4 changes: 4 additions & 0 deletions accounts/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ var (
// if the WithErrAlreadySucceeded option is used and the payment has
// already succeeded.
ErrAlreadySucceeded = errors.New("payment has already succeeded")

// ErrPaymentUnknown is returned by the UpsertAccountPayment method if
// the WithErrIfUnknown option is used and the payment is not yet known.
ErrPaymentUnknown = errors.New("payment unknown")
)
11 changes: 11 additions & 0 deletions accounts/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ type upsertAcctPaymentOption struct {
errIfAlreadyPending bool
usePendingAmount bool
errIfAlreadySucceeded bool
errIfUnknown bool
}

// newUpsertPaymentOption creates a new upsertAcctPaymentOption with default
Expand All @@ -339,6 +340,7 @@ func newUpsertPaymentOption() *upsertAcctPaymentOption {
errIfAlreadyPending: false,
usePendingAmount: false,
errIfAlreadySucceeded: false,
errIfUnknown: false,
}
}

Expand Down Expand Up @@ -378,3 +380,12 @@ func WithPendingAmount() UpsertPaymentOption {
o.usePendingAmount = true
}
}

// WithErrIfUnknown is a functional option that can be passed to the
// UpsertAccountPayment method to indicate that the ErrPaymentUnknown error
// should be returned if the payment is not associated with the account.
func WithErrIfUnknown() UpsertPaymentOption {
return func(o *upsertAcctPaymentOption) {
o.errIfUnknown = true
}
}
25 changes: 13 additions & 12 deletions accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,23 +845,24 @@ func (s *InterceptorService) removePayment(ctx context.Context,
return nil
}

account, err := s.store.Account(ctx, pendingPayment.accountID)
if err != nil {
return err
}

pendingPayment.cancel()
delete(s.pendingPayments, hash)

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

// If we did, let's set the status correctly in the DB now.
account.Payments[hash].Status = status
return s.store.UpdateAccount(ctx, account)
return nil
}

// successState returns true if a payment was completed successfully.
Expand Down
2 changes: 2 additions & 0 deletions accounts/store_kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ func (s *BoltStore) UpsertAccountPayment(_ context.Context, id AccountID,
if opts.usePendingAmount {
fullAmount = entry.FullAmount
}
} else if opts.errIfUnknown {
return ErrPaymentUnknown
}

account.Payments[paymentHash] = &PaymentEntry{
Expand Down
9 changes: 9 additions & 0 deletions accounts/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ func TestAccountUpdateMethods(t *testing.T) {
// error.
err = store.DeleteAccountPayment(ctx, acct.ID, hash1)
require.ErrorContains(t, err, "is not associated")

// Try once more to insert a payment that is currently unknown
// but this time add the WithErrIfUnknown option. This should
// return the ErrPaymentUnknown error.
_, err = store.UpsertAccountPayment(
ctx, acct.ID, hash1, 600, lnrpc.Payment_SUCCEEDED,
WithErrIfUnknown(),
)
require.ErrorIs(t, err, ErrPaymentUnknown)
})
}

Expand Down

0 comments on commit daa5c0b

Please sign in to comment.