Skip to content

Commit

Permalink
accounts: add new IncreaseAccountBalance Store method
Browse files Browse the repository at this point in the history
And use it instead of UpdateAccount in the InterceptorService's
invoiceUpdate method.
  • Loading branch information
ellemouton committed Jan 16, 2025
1 parent 3e27820 commit b0eb915
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
5 changes: 5 additions & 0 deletions accounts/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ type Store interface {
AddAccountInvoice(ctx context.Context, id AccountID,
hash lntypes.Hash) error

// IncreaseAccountBalance increases the balance of the account with the
// given ID by the given amount.
IncreaseAccountBalance(ctx context.Context, id AccountID,
amount lnwire.MilliSatoshi) error

// RemoveAccount finds an account by its ID and removes it from the¨
// store.
RemoveAccount(ctx context.Context, id AccountID) error
Expand Down
16 changes: 4 additions & 12 deletions accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,21 +592,13 @@ func (s *InterceptorService) invoiceUpdate(ctx context.Context,
return nil
}

account, err := s.store.Account(ctx, acctID)
if err != nil {
return s.disableAndErrorfUnsafe(
"error fetching account: %w", err,
)
}

// If we get here, the current account has the invoice associated with
// it that was just paid. Credit the amount to the account and update it
// in the DB.
account.CurrentBalance += int64(invoice.AmountPaid)
if err := s.store.UpdateAccount(ctx, account); err != nil {
return s.disableAndErrorfUnsafe(
"error updating account: %w", err,
)
err := s.store.IncreaseAccountBalance(ctx, acctID, invoice.AmountPaid)
if err != nil {
return s.disableAndErrorfUnsafe("error updating account: %w",
err)
}

// We've now fully processed the invoice and don't need to keep it
Expand Down
16 changes: 16 additions & 0 deletions accounts/store_kvdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,22 @@ func (s *BoltStore) AddAccountInvoice(_ context.Context, id AccountID,
return s.updateAccount(id, update)
}

// IncreaseAccountBalance increases the balance of the account with the given ID
// by the given amount.
//
// NOTE: This is part of the Store interface.
func (s *BoltStore) IncreaseAccountBalance(_ context.Context, id AccountID,
amount lnwire.MilliSatoshi) error {

update := func(account *OffChainBalanceAccount) error {
account.CurrentBalance += int64(amount)

return nil
}

return s.updateAccount(id, update)
}

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

Expand Down
23 changes: 23 additions & 0 deletions accounts/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,29 @@ func TestAccountUpdateMethods(t *testing.T) {

assertInvoices(hash1, hash2)
})

t.Run("IncreaseAccountBalance", func(t *testing.T) {
store := NewTestDB(t)

acct, err := store.NewAccount(ctx, 123, time.Time{}, "foo")
require.NoError(t, err)

assertBalance := func(balance int64) {
dbAcct, err := store.Account(ctx, acct.ID)
require.NoError(t, err)
require.EqualValues(t, balance, dbAcct.CurrentBalance)
}

// The account initially has a balance of 123.
assertBalance(123)

// Increase the balance by 100 and assert that the new balance
// is 223.
err = store.IncreaseAccountBalance(ctx, acct.ID, 100)
require.NoError(t, err)

assertBalance(223)
})
}

// TestLastInvoiceIndexes makes sure the last known invoice indexes can be
Expand Down

0 comments on commit b0eb915

Please sign in to comment.