Skip to content

Commit 0580d9a

Browse files
committed
accounts: add new IncreaseAccountBalance Store method
And use it instead of UpdateAccount in the InterceptorService's invoiceUpdate method.
1 parent 5a4b22f commit 0580d9a

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-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
@@ -598,21 +598,13 @@ func (s *InterceptorService) invoiceUpdate(ctx context.Context,
598598
return nil
599599
}
600600

601-
account, err := s.store.Account(ctx, acctID)
602-
if err != nil {
603-
return s.disableAndErrorfUnsafe(
604-
"error fetching account: %w", err,
605-
)
606-
}
607-
608601
// If we get here, the current account has the invoice associated with
609602
// it that was just paid. Credit the amount to the account and update it
610603
// in the DB.
611-
account.CurrentBalance += int64(invoice.AmountPaid)
612-
if err := s.store.UpdateAccount(ctx, account); err != nil {
613-
return s.disableAndErrorfUnsafe(
614-
"error updating account: %w", err,
615-
)
604+
err := s.store.IncreaseAccountBalance(ctx, acctID, invoice.AmountPaid)
605+
if err != nil {
606+
return s.disableAndErrorfUnsafe("error increasing account "+
607+
"balance account: %w", err)
616608
}
617609

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

accounts/store_kvdb.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/binary"
88
"encoding/hex"
99
"fmt"
10+
"math"
1011
"os"
1112
"time"
1213

@@ -233,6 +234,27 @@ func (s *BoltStore) AddAccountInvoice(_ context.Context, id AccountID,
233234
return s.updateAccount(id, update)
234235
}
235236

237+
// IncreaseAccountBalance increases the balance of the account with the given ID
238+
// by the given amount.
239+
//
240+
// NOTE: This is part of the Store interface.
241+
func (s *BoltStore) IncreaseAccountBalance(_ context.Context, id AccountID,
242+
amount lnwire.MilliSatoshi) error {
243+
244+
update := func(account *OffChainBalanceAccount) error {
245+
if amount > math.MaxInt64 {
246+
return fmt.Errorf("amount %d exceeds the maximum of %d",
247+
amount, math.MaxInt64)
248+
}
249+
250+
account.CurrentBalance += int64(amount)
251+
252+
return nil
253+
}
254+
255+
return s.updateAccount(id, update)
256+
}
257+
236258
func (s *BoltStore) updateAccount(id AccountID,
237259
updateFn func(*OffChainBalanceAccount) error) error {
238260

accounts/store_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ func TestAccountUpdateMethods(t *testing.T) {
203203
// The account initially has no invoices.
204204
assertInvoices()
205205

206+
// Adding an invoice to an account that doesnt exist yet should
207+
// error out.
208+
err = store.AddAccountInvoice(ctx, AccountID{}, lntypes.Hash{})
209+
require.ErrorIs(t, err, ErrAccNotFound)
210+
206211
// Add an invoice to the account.
207212
hash1 := lntypes.Hash{1, 2, 3, 4}
208213
err = store.AddAccountInvoice(ctx, acct.ID, hash1)
@@ -224,6 +229,34 @@ func TestAccountUpdateMethods(t *testing.T) {
224229

225230
assertInvoices(hash1, hash2)
226231
})
232+
233+
t.Run("IncreaseAccountBalance", func(t *testing.T) {
234+
store := NewTestDB(t)
235+
236+
// Increasing the balance of an account that doesn't exist
237+
// should error out.
238+
err := store.IncreaseAccountBalance(ctx, AccountID{}, 100)
239+
require.ErrorIs(t, err, ErrAccNotFound)
240+
241+
acct, err := store.NewAccount(ctx, 123, time.Time{}, "foo")
242+
require.NoError(t, err)
243+
244+
assertBalance := func(balance int64) {
245+
dbAcct, err := store.Account(ctx, acct.ID)
246+
require.NoError(t, err)
247+
require.EqualValues(t, balance, dbAcct.CurrentBalance)
248+
}
249+
250+
// The account initially has a balance of 123.
251+
assertBalance(123)
252+
253+
// Increase the balance by 100 and assert that the new balance
254+
// is 223.
255+
err = store.IncreaseAccountBalance(ctx, acct.ID, 100)
256+
require.NoError(t, err)
257+
258+
assertBalance(223)
259+
})
227260
}
228261

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

0 commit comments

Comments
 (0)