Skip to content

Commit 5a4b22f

Browse files
committed
accounts: add new AddAccountInvoice Store method
And use it instead of UpdateAccount for the InterceptorService's AssociateInvoice method.
1 parent 64606f0 commit 5a4b22f

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
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[lnwire.MilliSatoshi],
227227
newExpiry fn.Option[time.Time]) error
228228

229+
// AddAccountInvoice adds an 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,15 +438,15 @@ 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)
441+
err := s.store.AddAccountInvoice(ctx, id, hash)
442442
if err != nil {
443-
return err
443+
return fmt.Errorf("error adding invoice to account: %w", err)
444444
}
445445

446-
account.Invoices[hash] = struct{}{}
446+
// If the above was successful, then we update our in-memory map.
447447
s.invoiceToAccount[hash] = id
448448

449-
return s.store.UpdateAccount(ctx, account)
449+
return nil
450450
}
451451

452452
// 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 an 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
@@ -177,6 +177,53 @@ func TestAccountUpdateMethods(t *testing.T) {
177177
require.NoError(t, err)
178178
assertBalanceAndExpiry(newBalance, newExpiry)
179179
})
180+
181+
t.Run("AddAccountInvoice", func(t *testing.T) {
182+
store := NewTestDB(t)
183+
184+
acct, err := store.NewAccount(ctx, 0, time.Time{}, "foo")
185+
require.NoError(t, err)
186+
187+
assertInvoices := func(invoices ...lntypes.Hash) {
188+
dbAcct, err := store.Account(ctx, acct.ID)
189+
require.NoError(t, err)
190+
191+
// First make sure the number of invoices match before
192+
// de-duping the hashes.
193+
require.Len(t, dbAcct.Invoices, len(invoices))
194+
195+
dbInvs := make([]lntypes.Hash, 0, len(dbAcct.Invoices))
196+
for hash := range dbAcct.Invoices {
197+
dbInvs = append(dbInvs, hash)
198+
}
199+
200+
require.ElementsMatch(t, invoices, dbInvs)
201+
}
202+
203+
// The account initially has no invoices.
204+
assertInvoices()
205+
206+
// Add an invoice to the account.
207+
hash1 := lntypes.Hash{1, 2, 3, 4}
208+
err = store.AddAccountInvoice(ctx, acct.ID, hash1)
209+
require.NoError(t, err)
210+
211+
assertInvoices(hash1)
212+
213+
// Assert that adding the same invoice again does not change the
214+
// state.
215+
err = store.AddAccountInvoice(ctx, acct.ID, hash1)
216+
require.NoError(t, err)
217+
218+
assertInvoices(hash1)
219+
220+
// Now add a second invoice.
221+
hash2 := lntypes.Hash{5, 6, 7, 8}
222+
err = store.AddAccountInvoice(ctx, acct.ID, hash2)
223+
require.NoError(t, err)
224+
225+
assertInvoices(hash1, hash2)
226+
})
180227
}
181228

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

0 commit comments

Comments
 (0)