Skip to content

Commit 3e27820

Browse files
committed
accounts: add new AddAccountInvoice Store method
And use it instead of UpdateAccount for the InterceptorService's AssociateInvoice method.
1 parent d89def2 commit 3e27820

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

accounts/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ type Store interface {
226226
newBalance fn.Option[int64],
227227
newExpiry fn.Option[time.Time]) error
228228

229+
// AddAccountInvoice adds and invoice hash to an account.
230+
AddAccountInvoice(ctx context.Context, id AccountID,
231+
hash lntypes.Hash) error
232+
229233
// RemoveAccount finds an account by its ID and removes it from the¨
230234
// store.
231235
RemoveAccount(ctx context.Context, id AccountID) error

accounts/service.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,15 +438,9 @@ func (s *InterceptorService) AssociateInvoice(ctx context.Context, id AccountID,
438438
s.Lock()
439439
defer s.Unlock()
440440

441-
account, err := s.store.Account(ctx, id)
442-
if err != nil {
443-
return err
444-
}
445-
446-
account.Invoices[hash] = struct{}{}
447441
s.invoiceToAccount[hash] = id
448442

449-
return s.store.UpdateAccount(ctx, account)
443+
return s.store.AddAccountInvoice(ctx, id, hash)
450444
}
451445

452446
// PaymentErrored removes a pending payment from the account's registered

accounts/store_kvdb.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/btcsuite/btcwallet/walletdb"
1414
"github.com/lightningnetwork/lnd/fn"
1515
"github.com/lightningnetwork/lnd/kvdb"
16+
"github.com/lightningnetwork/lnd/lntypes"
1617
"github.com/lightningnetwork/lnd/lnwire"
1718
"go.etcd.io/bbolt"
1819
)
@@ -217,6 +218,21 @@ func (s *BoltStore) UpdateAccountBalanceAndExpiry(_ context.Context,
217218
return s.updateAccount(id, update)
218219
}
219220

221+
// AddAccountInvoice adds and invoice hash to the account with the given ID.
222+
//
223+
// NOTE: This is part of the Store interface.
224+
func (s *BoltStore) AddAccountInvoice(_ context.Context, id AccountID,
225+
hash lntypes.Hash) error {
226+
227+
update := func(account *OffChainBalanceAccount) error {
228+
account.Invoices[hash] = struct{}{}
229+
230+
return nil
231+
}
232+
233+
return s.updateAccount(id, update)
234+
}
235+
220236
func (s *BoltStore) updateAccount(id AccountID,
221237
updateFn func(*OffChainBalanceAccount) error) error {
222238

accounts/store_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,53 @@ func TestAccountUpdateMethods(t *testing.T) {
158158
require.NoError(t, err)
159159
assertBalanceAndExpiry(newBalance, newExpiry)
160160
})
161+
162+
t.Run("AddAccountInvoice", func(t *testing.T) {
163+
store := NewTestDB(t)
164+
165+
acct, err := store.NewAccount(ctx, 0, time.Time{}, "foo")
166+
require.NoError(t, err)
167+
168+
assertInvoices := func(invoices ...lntypes.Hash) {
169+
dbAcct, err := store.Account(ctx, acct.ID)
170+
require.NoError(t, err)
171+
172+
// First make sure the number of invoices match before
173+
// de-duping the hashes.
174+
require.Len(t, dbAcct.Invoices, len(invoices))
175+
176+
dbInvs := make([]lntypes.Hash, 0, len(dbAcct.Invoices))
177+
for hash := range dbAcct.Invoices {
178+
dbInvs = append(dbInvs, hash)
179+
}
180+
181+
require.ElementsMatch(t, invoices, dbInvs)
182+
}
183+
184+
// The account initially has no invoices.
185+
assertInvoices()
186+
187+
// Add an invoice to the account.
188+
hash1 := lntypes.Hash{1, 2, 3, 4}
189+
err = store.AddAccountInvoice(ctx, acct.ID, hash1)
190+
require.NoError(t, err)
191+
192+
assertInvoices(hash1)
193+
194+
// Assert that adding the same invoice again does not change the
195+
// state.
196+
err = store.AddAccountInvoice(ctx, acct.ID, hash1)
197+
require.NoError(t, err)
198+
199+
assertInvoices(hash1)
200+
201+
// Now add a second invoice.
202+
hash2 := lntypes.Hash{5, 6, 7, 8}
203+
err = store.AddAccountInvoice(ctx, acct.ID, hash2)
204+
require.NoError(t, err)
205+
206+
assertInvoices(hash1, hash2)
207+
})
161208
}
162209

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

0 commit comments

Comments
 (0)