Skip to content

Commit

Permalink
accounts: add DeleteAccountPayment method to Store
Browse files Browse the repository at this point in the history
And use it instead of UpdateAccount.
  • Loading branch information
ellemouton committed Jan 16, 2025
1 parent 9f10d23 commit 4e07e3f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
6 changes: 6 additions & 0 deletions accounts/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ type Store interface {
status lnrpc.Payment_PaymentStatus,
options ...UpsertPaymentOption) (bool, error)

// DeleteAccountPayment removes a payment entry from the account with
// the given ID. It will return an error if the payment is not
// associated with the account.
DeleteAccountPayment(_ context.Context, id AccountID,
hash lntypes.Hash) error

// RemoveAccount finds an account by its ID and removes it from the¨
// store.
RemoveAccount(ctx context.Context, id AccountID) error
Expand Down
21 changes: 1 addition & 20 deletions accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,26 +467,7 @@ func (s *InterceptorService) PaymentErrored(ctx context.Context, id AccountID,
"has already started")
}

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

// Check that this payment is actually associated with this account.
_, ok = account.Payments[hash]
if !ok {
return fmt.Errorf("payment with hash %s is not associated "+
"with this account", hash)
}

// Delete the payment and update the persisted account.
delete(account.Payments, hash)

if err := s.store.UpdateAccount(ctx, account); err != nil {
return fmt.Errorf("error updating account: %w", err)
}

return nil
return s.store.DeleteAccountPayment(ctx, id, hash)
}

// AssociatePayment associates a payment (hash) with the given account,
Expand Down
26 changes: 26 additions & 0 deletions accounts/store_kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,32 @@ func (s *BoltStore) UpsertAccountPayment(_ context.Context, id AccountID,
return known, s.updateAccount(id, update)
}

// DeleteAccountPayment removes a payment entry from the account with the given
// ID. It will return an error if the payment is not associated with the
// account.
//
// NOTE: This is part of the Store interface.
func (s *BoltStore) DeleteAccountPayment(_ context.Context, id AccountID,
hash lntypes.Hash) error {

update := func(account *OffChainBalanceAccount) error {
// Check that this payment is actually associated with this
// account.
_, ok := account.Payments[hash]
if !ok {
return fmt.Errorf("payment with hash %s is not "+
"associated with this account", hash)
}

// Delete the payment and update the persisted account.
delete(account.Payments, hash)

return nil
}

return s.updateAccount(id, update)
}

func (s *BoltStore) updateAccount(id AccountID,
updateFn func(*OffChainBalanceAccount) error) error {

Expand Down
19 changes: 18 additions & 1 deletion accounts/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestAccountUpdateMethods(t *testing.T) {
t.Parallel()
ctx := context.Background()

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

acct, err := store.NewAccount(ctx, 1000, time.Time{}, "foo")
Expand Down Expand Up @@ -258,6 +258,23 @@ func TestAccountUpdateMethods(t *testing.T) {
FullAmount: 100,
},
})

// Delete the first payment and make sure it is removed from the
// account.
err = store.DeleteAccountPayment(ctx, acct.ID, hash1)
require.NoError(t, err)

assertBalanceAndPayments(400, AccountPayments{
hash2: &PaymentEntry{
Status: lnrpc.Payment_SUCCEEDED,
FullAmount: 100,
},
})

// Test that deleting a payment that does not exist returns an
// error.
err = store.DeleteAccountPayment(ctx, acct.ID, hash1)
require.ErrorContains(t, err, "is not associated")
})
}

Expand Down

0 comments on commit 4e07e3f

Please sign in to comment.