Skip to content

Commit 077bbec

Browse files
committed
accounts: add DeleteAccountPayment method to Store
And use it instead of UpdateAccount.
1 parent a365038 commit 077bbec

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

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 an error if the payment is not
251+
// 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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,32 @@ 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 an error if the payment is not associated with the
322+
// 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", hash)
335+
}
336+
337+
// Delete the payment and update the persisted account.
338+
delete(account.Payments, hash)
339+
340+
return nil
341+
}
342+
343+
return s.updateAccount(id, update)
344+
}
345+
320346
func (s *BoltStore) updateAccount(id AccountID,
321347
updateFn func(*OffChainBalanceAccount) error) error {
322348

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.ErrorContains(t, err, "is not associated")
416433
})
417434
}
418435

0 commit comments

Comments
 (0)