Skip to content

Commit 6cc5854

Browse files
committed
accounts: add new IncreaseAccountBalance Store method
And use it instead of UpdateAccount in the InterceptorService's invoiceUpdate method.
1 parent ee4ff20 commit 6cc5854

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

accounts/interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ type Store interface {
230230
AddAccountInvoice(ctx context.Context, id AccountID,
231231
hash lntypes.Hash) error
232232

233+
// IncreaseAccountBalance increases the balance of the account with the
234+
// given ID by the given amount.
235+
IncreaseAccountBalance(ctx context.Context, id AccountID,
236+
amount lnwire.MilliSatoshi) error
237+
233238
// RemoveAccount finds an account by its ID and removes it from the¨
234239
// store.
235240
RemoveAccount(ctx context.Context, id AccountID) error

accounts/service.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -592,21 +592,13 @@ func (s *InterceptorService) invoiceUpdate(ctx context.Context,
592592
return nil
593593
}
594594

595-
account, err := s.store.Account(ctx, acctID)
596-
if err != nil {
597-
return s.disableAndErrorfUnsafe(
598-
"error fetching account: %w", err,
599-
)
600-
}
601-
602595
// If we get here, the current account has the invoice associated with
603596
// it that was just paid. Credit the amount to the account and update it
604597
// in the DB.
605-
account.CurrentBalance += int64(invoice.AmountPaid)
606-
if err := s.store.UpdateAccount(ctx, account); err != nil {
607-
return s.disableAndErrorfUnsafe(
608-
"error updating account: %w", err,
609-
)
598+
err := s.store.IncreaseAccountBalance(ctx, acctID, invoice.AmountPaid)
599+
if err != nil {
600+
return s.disableAndErrorfUnsafe("error updating account: %w",
601+
err)
610602
}
611603

612604
// We've now fully processed the invoice and don't need to keep it

accounts/store_kvdb.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,22 @@ func (s *BoltStore) AddAccountInvoice(_ context.Context, id AccountID,
233233
return s.updateAccount(id, update)
234234
}
235235

236+
// IncreaseAccountBalance increases the balance of the account with the given ID
237+
// by the given amount.
238+
//
239+
// NOTE: This is part of the Store interface.
240+
func (s *BoltStore) IncreaseAccountBalance(_ context.Context, id AccountID,
241+
amount lnwire.MilliSatoshi) error {
242+
243+
update := func(account *OffChainBalanceAccount) error {
244+
account.CurrentBalance += int64(amount)
245+
246+
return nil
247+
}
248+
249+
return s.updateAccount(id, update)
250+
}
251+
236252
func (s *BoltStore) updateAccount(id AccountID,
237253
updateFn func(*OffChainBalanceAccount) error) error {
238254

accounts/store_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,29 @@ func TestAccountUpdateMethods(t *testing.T) {
205205

206206
assertInvoices(hash1, hash2)
207207
})
208+
209+
t.Run("IncreaseAccountBalance", func(t *testing.T) {
210+
store := NewTestDB(t)
211+
212+
acct, err := store.NewAccount(ctx, 123, time.Time{}, "foo")
213+
require.NoError(t, err)
214+
215+
assertBalance := func(balance int64) {
216+
dbAcct, err := store.Account(ctx, acct.ID)
217+
require.NoError(t, err)
218+
require.EqualValues(t, balance, dbAcct.CurrentBalance)
219+
}
220+
221+
// The account initially has a balance of 123.
222+
assertBalance(123)
223+
224+
// Increase the balance by 100 and assert that the new balance
225+
// is 223.
226+
err = store.IncreaseAccountBalance(ctx, acct.ID, 100)
227+
require.NoError(t, err)
228+
229+
assertBalance(223)
230+
})
208231
}
209232

210233
// TestLastInvoiceIndexes makes sure the last known invoice indexes can be

0 commit comments

Comments
 (0)