@@ -7,10 +7,11 @@ import (
77 "sync"
88 "time"
99
10+ "github.com/btcsuite/btcd/btcutil"
1011 "github.com/btcsuite/btcd/chaincfg"
1112 "github.com/lightninglabs/lndclient"
12- "github.com/lightninglabs/taproot-assets/fn"
1313 "github.com/lightningnetwork/lnd/channeldb"
14+ "github.com/lightningnetwork/lnd/fn"
1415 invpkg "github.com/lightningnetwork/lnd/invoices"
1516 "github.com/lightningnetwork/lnd/lnrpc"
1617 "github.com/lightningnetwork/lnd/lntypes"
@@ -299,7 +300,7 @@ func (s *InterceptorService) NewAccount(ctx context.Context,
299300// UpdateAccount writes an account to the database, overwriting the existing one
300301// if it exists.
301302func (s * InterceptorService ) UpdateAccount (ctx context.Context ,
302- accountID AccountID , accountBalance ,
303+ accountID AccountID , accountBalance btcutil. Amount ,
303304 expirationDate int64 ) (* OffChainBalanceAccount , error ) {
304305
305306 s .Lock ()
@@ -313,36 +314,35 @@ func (s *InterceptorService) UpdateAccount(ctx context.Context,
313314 return nil , ErrAccountServiceDisabled
314315 }
315316
316- account , err := s .store .Account (ctx , accountID )
317- if err != nil {
318- return nil , fmt .Errorf ("error fetching account: %w" , err )
319- }
320-
321317 // If the expiration date was set, parse it as a unix time stamp. A
322318 // value of -1 signals "don't update the expiration date".
319+ var expiry fn.Option [time.Time ]
323320 if expirationDate > 0 {
324- account . ExpirationDate = time .Unix (expirationDate , 0 )
321+ expiry = fn . Some ( time .Unix (expirationDate , 0 ) )
325322 } else if expirationDate == 0 {
326323 // Setting the expiration to 0 means don't expire in which case
327324 // we use a zero time (zero unix time would still be 1970, so
328325 // that doesn't work for us).
329- account . ExpirationDate = time.Time {}
326+ expiry = fn . Some ( time.Time {})
330327 }
331328
332329 // If the new account balance was set, parse it as millisatoshis. A
333330 // value of -1 signals "don't update the balance".
331+ var balance fn.Option [lnwire.MilliSatoshi ]
334332 if accountBalance >= 0 {
335333 // Convert from satoshis to millisatoshis for storage.
336- account . CurrentBalance = int64 ( accountBalance ) * 1000
334+ balance = fn . Some ( lnwire . MilliSatoshi ( accountBalance ) * 1000 )
337335 }
338336
339337 // Create the actual account in the macaroon account store.
340- err = s .store .UpdateAccount (ctx , account )
338+ err := s .store .UpdateAccountBalanceAndExpiry (
339+ ctx , accountID , balance , expiry ,
340+ )
341341 if err != nil {
342342 return nil , fmt .Errorf ("unable to update account: %w" , err )
343343 }
344344
345- return account , nil
345+ return s . store . Account ( ctx , accountID )
346346}
347347
348348// Account retrieves an account from the bolt DB and un-marshals it. If the
@@ -439,15 +439,15 @@ func (s *InterceptorService) AssociateInvoice(ctx context.Context, id AccountID,
439439 s .Lock ()
440440 defer s .Unlock ()
441441
442- account , err := s .store .Account (ctx , id )
442+ err := s .store .AddAccountInvoice (ctx , id , hash )
443443 if err != nil {
444- return err
444+ return fmt . Errorf ( "error adding invoice to account: %w" , err )
445445 }
446446
447- account . Invoices [ hash ] = struct {}{}
447+ // If the above was successful, then we update our in-memory map.
448448 s .invoiceToAccount [hash ] = id
449449
450- return s . store . UpdateAccount ( ctx , account )
450+ return nil
451451}
452452
453453// PaymentErrored removes a pending payment from the account's registered
@@ -599,21 +599,13 @@ func (s *InterceptorService) invoiceUpdate(ctx context.Context,
599599 return nil
600600 }
601601
602- account , err := s .store .Account (ctx , acctID )
603- if err != nil {
604- return s .disableAndErrorfUnsafe (
605- "error fetching account: %w" , err ,
606- )
607- }
608-
609602 // If we get here, the current account has the invoice associated with
610603 // it that was just paid. Credit the amount to the account and update it
611604 // in the DB.
612- account .CurrentBalance += int64 (invoice .AmountPaid )
613- if err := s .store .UpdateAccount (ctx , account ); err != nil {
614- return s .disableAndErrorfUnsafe (
615- "error updating account: %w" , err ,
616- )
605+ err := s .store .IncreaseAccountBalance (ctx , acctID , invoice .AmountPaid )
606+ if err != nil {
607+ return s .disableAndErrorfUnsafe ("error increasing account " +
608+ "balance account: %w" , err )
617609 }
618610
619611 // We've now fully processed the invoice and don't need to keep it
0 commit comments