Skip to content

Commit 7fb7faa

Browse files
committed
accounts: add DeleteAccountPayment method to Store
And use it instead of UpdateAccount.
1 parent c386008 commit 7fb7faa

File tree

5 files changed

+58
-21
lines changed

5 files changed

+58
-21
lines changed

accounts/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ 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+
// ErrPaymentNotAssociated indicate that the payment with the given hash
18+
// has not yet been associated with the account in question.
19+
ErrPaymentNotAssociated = errors.New(
20+
"payment not associated with account",
21+
)
1622
)

accounts/interface.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ type Store interface {
246246
status lnrpc.Payment_PaymentStatus,
247247
options ...UpsertPaymentOption) (bool, error)
248248

249+
// DeleteAccountPayment removes a payment entry from the account with
250+
// the given ID. It will return the ErrPaymentNotAssociated error if the
251+
// payment is not associated with the account.
252+
DeleteAccountPayment(_ context.Context, id AccountID,
253+
hash lntypes.Hash) error
254+
249255
// RemoveAccount finds an account by its ID and removes it from the¨
250256
// store.
251257
RemoveAccount(ctx context.Context, id AccountID) error

accounts/service.go

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -467,26 +467,7 @@ func (s *InterceptorService) PaymentErrored(ctx context.Context, id AccountID,
467467
"has already started")
468468
}
469469

470-
account, err := s.store.Account(ctx, id)
471-
if err != nil {
472-
return err
473-
}
474-
475-
// Check that this payment is actually associated with this account.
476-
_, ok = account.Payments[hash]
477-
if !ok {
478-
return fmt.Errorf("payment with hash %s is not associated "+
479-
"with this account", hash)
480-
}
481-
482-
// Delete the payment and update the persisted account.
483-
delete(account.Payments, hash)
484-
485-
if err := s.store.UpdateAccount(ctx, account); err != nil {
486-
return fmt.Errorf("error updating account: %w", err)
487-
}
488-
489-
return nil
470+
return s.store.DeleteAccountPayment(ctx, id, hash)
490471
}
491472

492473
// AssociatePayment associates a payment (hash) with the given account,

accounts/store_kvdb.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,33 @@ func (s *BoltStore) UpsertAccountPayment(_ context.Context, id AccountID,
317317
return known, s.updateAccount(id, update)
318318
}
319319

320+
// DeleteAccountPayment removes a payment entry from the account with the given
321+
// ID. It will return the ErrPaymentNotAssociated error if the payment is not
322+
// associated with the account.
323+
//
324+
// NOTE: This is part of the Store interface.
325+
func (s *BoltStore) DeleteAccountPayment(_ context.Context, id AccountID,
326+
hash lntypes.Hash) error {
327+
328+
update := func(account *OffChainBalanceAccount) error {
329+
// Check that this payment is actually associated with this
330+
// account.
331+
_, ok := account.Payments[hash]
332+
if !ok {
333+
return fmt.Errorf("payment with hash %s is not "+
334+
"associated with this account: %w", hash,
335+
ErrPaymentNotAssociated)
336+
}
337+
338+
// Delete the payment and update the persisted account.
339+
delete(account.Payments, hash)
340+
341+
return nil
342+
}
343+
344+
return s.updateAccount(id, update)
345+
}
346+
320347
func (s *BoltStore) updateAccount(id AccountID,
321348
updateFn func(*OffChainBalanceAccount) error) error {
322349

accounts/store_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestAccountUpdateMethods(t *testing.T) {
258258
assertBalance(223)
259259
})
260260

261-
t.Run("UpsertAccountPayment", func(t *testing.T) {
261+
t.Run("Upsert and Delete AccountPayment", func(t *testing.T) {
262262
store := NewTestDB(t)
263263

264264
acct, err := store.NewAccount(ctx, 1000, time.Time{}, "foo")
@@ -413,6 +413,23 @@ func TestAccountUpdateMethods(t *testing.T) {
413413
FullAmount: 100,
414414
},
415415
})
416+
417+
// Delete the first payment and make sure it is removed from the
418+
// account.
419+
err = store.DeleteAccountPayment(ctx, acct.ID, hash1)
420+
require.NoError(t, err)
421+
422+
assertBalanceAndPayments(400, AccountPayments{
423+
hash2: &PaymentEntry{
424+
Status: lnrpc.Payment_SUCCEEDED,
425+
FullAmount: 100,
426+
},
427+
})
428+
429+
// Test that deleting a payment that does not exist returns an
430+
// error.
431+
err = store.DeleteAccountPayment(ctx, acct.ID, hash1)
432+
require.ErrorIs(t, err, ErrPaymentNotAssociated)
416433
})
417434
}
418435

0 commit comments

Comments
 (0)