@@ -96,7 +96,7 @@ func (s *InterceptorService) Start(lightningClient lndclient.LightningClient,
9696 // also track payments that aren't in a final state yet.
9797 existingAccounts , err := s .store .Accounts ()
9898 if err != nil {
99- return fmt .Errorf ("error querying existing accounts: %v " , err )
99+ return fmt .Errorf ("error querying existing accounts: %w " , err )
100100 }
101101 for _ , acct := range existingAccounts {
102102 acct := acct
@@ -109,15 +109,13 @@ func (s *InterceptorService) Start(lightningClient lndclient.LightningClient,
109109 // state of being in-flight.
110110 for hash , entry := range acct .Payments {
111111 entry := entry
112- if entry .Status == lnrpc .Payment_IN_FLIGHT ||
113- entry .Status == lnrpc .Payment_UNKNOWN {
114-
112+ if ! successState (entry .Status ) {
115113 err := s .TrackPayment (
116114 acct .ID , hash , entry .FullAmount ,
117115 )
118116 if err != nil {
119117 return fmt .Errorf ("error tracking " +
120- "payment: %v " , err )
118+ "payment: %w " , err )
121119 }
122120 }
123121 }
@@ -146,7 +144,7 @@ func (s *InterceptorService) Start(lightningClient lndclient.LightningClient,
146144 s .currentSettleIndex = 0
147145
148146 default :
149- return fmt .Errorf ("error determining last invoice indexes: %v " ,
147+ return fmt .Errorf ("error determining last invoice indexes: %w " ,
150148 err )
151149 }
152150
@@ -157,7 +155,7 @@ func (s *InterceptorService) Start(lightningClient lndclient.LightningClient,
157155 },
158156 )
159157 if err != nil {
160- return fmt .Errorf ("error subscribing invoices: %v " , err )
158+ return fmt .Errorf ("error subscribing invoices: %w " , err )
161159 }
162160
163161 s .wg .Add (1 )
@@ -209,6 +207,16 @@ func (s *InterceptorService) Start(lightningClient lndclient.LightningClient,
209207 return nil
210208}
211209
210+ // Stop shuts down the account service.
211+ func (s * InterceptorService ) Stop () error {
212+ s .contextCancel ()
213+ close (s .quit )
214+
215+ s .wg .Wait ()
216+
217+ return s .store .Close ()
218+ }
219+
212220// NewAccount creates a new OffChainBalanceAccount with the given balance and a
213221// randomly chosen ID.
214222func (s * InterceptorService ) NewAccount (balance lnwire.MilliSatoshi ,
@@ -231,7 +239,7 @@ func (s *InterceptorService) UpdateAccount(accountID AccountID, accountBalance,
231239
232240 account , err := s .store .Account (accountID )
233241 if err != nil {
234- return nil , fmt .Errorf ("error fetching account: %v " , err )
242+ return nil , fmt .Errorf ("error fetching account: %w " , err )
235243 }
236244
237245 // If the expiration date was set, parse it as a unix time stamp. A
@@ -255,7 +263,7 @@ func (s *InterceptorService) UpdateAccount(accountID AccountID, accountBalance,
255263 // Create the actual account in the macaroon account store.
256264 err = s .store .UpdateAccount (account )
257265 if err != nil {
258- return nil , fmt .Errorf ("unable to update account: %v " , err )
266+ return nil , fmt .Errorf ("unable to update account: %w " , err )
259267 }
260268
261269 return account , nil
@@ -395,15 +403,15 @@ func (s *InterceptorService) invoiceUpdate(invoice *lndclient.Invoice) error {
395403
396404 account , err := s .store .Account (acctID )
397405 if err != nil {
398- return fmt .Errorf ("error fetching account: %v " , err )
406+ return fmt .Errorf ("error fetching account: %w " , err )
399407 }
400408
401409 // If we get here, the current account has the invoice associated with
402410 // it that was just paid. Credit the amount to the account and update it
403411 // in the DB.
404412 account .CurrentBalance += int64 (invoice .AmountPaid )
405413 if err := s .store .UpdateAccount (account ); err != nil {
406- return fmt .Errorf ("error updating account: %v " , err )
414+ return fmt .Errorf ("error updating account: %w " , err )
407415 }
408416
409417 // We've now fully processed the invoice and don't need to keep it
@@ -431,15 +439,13 @@ func (s *InterceptorService) TrackPayment(id AccountID, hash lntypes.Hash,
431439 // is a reference in the account with the given state.
432440 account , err := s .store .Account (id )
433441 if err != nil {
434- return fmt .Errorf ("error fetching account: %v " , err )
442+ return fmt .Errorf ("error fetching account: %w " , err )
435443 }
436444
437445 // If the account already stored a terminal state, we also don't need to
438446 // track the payment again.
439447 entry , ok := account .Payments [hash ]
440- if ok && (entry .Status == lnrpc .Payment_SUCCEEDED ||
441- entry .Status == lnrpc .Payment_FAILED ) {
442-
448+ if ok && successState (entry .Status ) {
443449 return nil
444450 }
445451
@@ -450,7 +456,7 @@ func (s *InterceptorService) TrackPayment(id AccountID, hash lntypes.Hash,
450456 FullAmount : fullAmt ,
451457 }
452458 if err := s .store .UpdateAccount (account ); err != nil {
453- return fmt .Errorf ("error updating account: %v " , err )
459+ return fmt .Errorf ("error updating account: %w " , err )
454460 }
455461
456462 // And start the long-running TrackPayment RPC.
@@ -566,7 +572,7 @@ func (s *InterceptorService) paymentUpdate(hash lntypes.Hash,
566572 FullAmount : fullAmount ,
567573 }
568574 if err := s .store .UpdateAccount (account ); err != nil {
569- return terminalState , fmt .Errorf ("error updating account: %v " ,
575+ return terminalState , fmt .Errorf ("error updating account: %w " ,
570576 err )
571577 }
572578
@@ -618,12 +624,7 @@ func (s *InterceptorService) removePayment(hash lntypes.Hash,
618624 return s .store .UpdateAccount (account )
619625}
620626
621- // Stop shuts down the account service.
622- func (s * InterceptorService ) Stop () error {
623- s .contextCancel ()
624- close (s .quit )
625-
626- s .wg .Wait ()
627-
628- return s .store .Close ()
627+ // successState returns true if a payment was completed successfully.
628+ func successState (status lnrpc.Payment_PaymentStatus ) bool {
629+ return status == lnrpc .Payment_SUCCEEDED
629630}
0 commit comments