Skip to content

Commit 568d7ad

Browse files
committed
accounts+itest: fix linter and code style issues
1 parent 27b04eb commit 568d7ad

File tree

10 files changed

+97
-88
lines changed

10 files changed

+97
-88
lines changed

accounts/checkers.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
525525
if len(invoice) > 0 {
526526
payReq, err := zpay32.Decode(invoice, chainParams)
527527
if err != nil {
528-
return fmt.Errorf("error decoding pay req: %v", err)
528+
return fmt.Errorf("error decoding pay req: %w", err)
529529
}
530530

531531
if payReq.MilliSat != nil && *payReq.MilliSat > sendAmt {
@@ -546,7 +546,7 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
546546

547547
err = service.CheckBalance(acct.ID, sendAmt)
548548
if err != nil {
549-
return fmt.Errorf("error validating account balance: %v", err)
549+
return fmt.Errorf("error validating account balance: %w", err)
550550
}
551551

552552
return nil
@@ -609,7 +609,7 @@ func checkSendToRoute(ctx context.Context, service Service,
609609

610610
err = service.CheckBalance(acct.ID, sendAmt)
611611
if err != nil {
612-
return fmt.Errorf("error validating account balance: %v", err)
612+
return fmt.Errorf("error validating account balance: %w", err)
613613
}
614614

615615
return nil

accounts/checkers_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ type mockService struct {
4141
acctBalanceMsat lnwire.MilliSatoshi
4242

4343
trackedInvoices map[lntypes.Hash]AccountID
44-
trackedPayments map[lntypes.Hash]*PaymentEntry
44+
trackedPayments AccountPayments
4545
}
4646

4747
func newMockService() *mockService {
4848
return &mockService{
4949
acctBalanceMsat: 0,
5050
trackedInvoices: make(map[lntypes.Hash]AccountID),
51-
trackedPayments: make(map[lntypes.Hash]*PaymentEntry),
51+
trackedPayments: make(AccountPayments),
5252
}
5353
}
5454

@@ -68,7 +68,7 @@ func (m *mockService) AssociateInvoice(id AccountID, hash lntypes.Hash) error {
6868
return nil
6969
}
7070

71-
func (m *mockService) TrackPayment(id AccountID, hash lntypes.Hash,
71+
func (m *mockService) TrackPayment(_ AccountID, hash lntypes.Hash,
7272
amt lnwire.MilliSatoshi) error {
7373

7474
m.trackedPayments[hash] = &PaymentEntry{
@@ -403,8 +403,8 @@ func TestAccountCheckers(t *testing.T) {
403403
acct := &OffChainBalanceAccount{
404404
ID: testID,
405405
Type: TypeInitialBalance,
406-
Invoices: make(map[lntypes.Hash]struct{}),
407-
Payments: make(map[lntypes.Hash]*PaymentEntry),
406+
Invoices: make(AccountInvoices),
407+
Payments: make(AccountPayments),
408408
}
409409
ctx := AddToContext(
410410
context.Background(), KeyAccount, acct,

accounts/interceptor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func parseRPCMessage(msg *lnrpc.RPCMessage) (proto.Message, error) {
153153
// No, it's a normal message.
154154
parsedMsg, err := mid.ParseProtobuf(msg.TypeName, msg.Serialized)
155155
if err != nil {
156-
return nil, fmt.Errorf("error parsing proto of type %v: %v",
156+
return nil, fmt.Errorf("error parsing proto of type %v: %w",
157157
msg.TypeName, err)
158158
}
159159

accounts/interface.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func ParseAccountID(idStr string) (*AccountID, error) {
4444

4545
idBytes, err := hex.DecodeString(idStr)
4646
if err != nil {
47-
return nil, fmt.Errorf("error decoding account ID: %v", err)
47+
return nil, fmt.Errorf("error decoding account ID: %w", err)
4848
}
4949

5050
var id AccountID
@@ -67,6 +67,12 @@ type PaymentEntry struct {
6767
FullAmount lnwire.MilliSatoshi
6868
}
6969

70+
// AccountInvoices is the set of invoices that are associated with an account.
71+
type AccountInvoices map[lntypes.Hash]struct{}
72+
73+
// AccountPayments is the set of payments that are associated with an account.
74+
type AccountPayments map[lntypes.Hash]*PaymentEntry
75+
7076
// OffChainBalanceAccount holds all information that is needed to keep track of
7177
// a user's off-chain account balance. This balance can only be spent by paying
7278
// invoices.
@@ -99,11 +105,11 @@ type OffChainBalanceAccount struct {
99105

100106
// Invoices is a list of all invoices that are associated with the
101107
// account.
102-
Invoices map[lntypes.Hash]struct{}
108+
Invoices AccountInvoices
103109

104110
// Payments is a list of all payments that are associated with the
105111
// account and the last status we were aware of.
106-
Payments map[lntypes.Hash]*PaymentEntry
112+
Payments AccountPayments
107113

108114
// Label is an optional label that can be set for the account. If it is
109115
// not empty then it must be unique.

accounts/rpcserver.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
7474
balanceMsat, expirationDate, req.Label,
7575
)
7676
if err != nil {
77-
return nil, fmt.Errorf("unable to create account: %v", err)
77+
return nil, fmt.Errorf("unable to create account: %w", err)
7878
}
7979

8080
var rootKeyIdSuffix [4]byte
@@ -93,12 +93,12 @@ func (s *RPCServer) CreateAccount(ctx context.Context,
9393
}},
9494
})
9595
if err != nil {
96-
return nil, fmt.Errorf("error baking account macaroon: %v", err)
96+
return nil, fmt.Errorf("error baking account macaroon: %w", err)
9797
}
9898

9999
macBytes, err := hex.DecodeString(macHex)
100100
if err != nil {
101-
return nil, fmt.Errorf("error decoding account macaroon: %v",
101+
return nil, fmt.Errorf("error decoding account macaroon: %w",
102102
err)
103103
}
104104

@@ -141,7 +141,7 @@ func (s *RPCServer) ListAccounts(context.Context,
141141
// Retrieve all accounts from the macaroon account store.
142142
accts, err := s.service.Accounts()
143143
if err != nil {
144-
return nil, fmt.Errorf("unable to list accounts: %v", err)
144+
return nil, fmt.Errorf("unable to list accounts: %w", err)
145145
}
146146

147147
// Map the response into the proper response type and return it.
@@ -191,7 +191,7 @@ func (s *RPCServer) RemoveAccount(_ context.Context,
191191
// Now remove the account.
192192
err = s.service.RemoveAccount(accountID)
193193
if err != nil {
194-
return nil, fmt.Errorf("error removing account: %v", err)
194+
return nil, fmt.Errorf("error removing account: %w", err)
195195
}
196196

197197
return &litrpc.RemoveAccountResponse{}, nil
@@ -211,7 +211,7 @@ func (s *RPCServer) findAccount(id string, label string) (AccountID, error) {
211211
decoded, err := hex.DecodeString(id)
212212
if err != nil {
213213
return AccountID{}, fmt.Errorf("error decoding "+
214-
"account ID: %v", err)
214+
"account ID: %w", err)
215215
}
216216
copy(accountID[:], decoded)
217217

accounts/service.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
214222
func (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
}

accounts/service_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,10 @@ func TestAccountService(t *testing.T) {
186186
ID: testID,
187187
Type: TypeInitialBalance,
188188
CurrentBalance: 1234,
189-
Invoices: map[lntypes.Hash]struct{}{
189+
Invoices: AccountInvoices{
190190
testHash: {},
191191
},
192-
Payments: make(map[lntypes.Hash]*PaymentEntry),
192+
Payments: make(AccountPayments),
193193
}
194194

195195
err := s.store.UpdateAccount(acct)
@@ -213,7 +213,7 @@ func TestAccountService(t *testing.T) {
213213

214214
acct.Invoices[testHash] = struct{}{}
215215
acct.Payments[testHash] = &PaymentEntry{
216-
Status: lnrpc.Payment_FAILED,
216+
Status: lnrpc.Payment_SUCCEEDED,
217217
FullAmount: 1234,
218218
}
219219

@@ -235,10 +235,10 @@ func TestAccountService(t *testing.T) {
235235
ID: testID,
236236
Type: TypeInitialBalance,
237237
CurrentBalance: 1234,
238-
Invoices: map[lntypes.Hash]struct{}{
238+
Invoices: AccountInvoices{
239239
testHash: {},
240240
},
241-
Payments: map[lntypes.Hash]*PaymentEntry{
241+
Payments: AccountPayments{
242242
testHash: {
243243
Status: lnrpc.Payment_IN_FLIGHT,
244244
FullAmount: 1234,
@@ -362,10 +362,10 @@ func TestAccountService(t *testing.T) {
362362
ID: testID,
363363
Type: TypeInitialBalance,
364364
CurrentBalance: 1234,
365-
Invoices: map[lntypes.Hash]struct{}{
365+
Invoices: AccountInvoices{
366366
testHash: {},
367367
},
368-
Payments: make(map[lntypes.Hash]*PaymentEntry),
368+
Payments: make(AccountPayments),
369369
}
370370

371371
err := s.store.UpdateAccount(acct)
@@ -400,10 +400,10 @@ func TestAccountService(t *testing.T) {
400400
ID: testID,
401401
Type: TypeInitialBalance,
402402
CurrentBalance: 5000,
403-
Invoices: map[lntypes.Hash]struct{}{
403+
Invoices: AccountInvoices{
404404
testHash: {},
405405
},
406-
Payments: map[lntypes.Hash]*PaymentEntry{
406+
Payments: AccountPayments{
407407
testHash: {
408408
Status: lnrpc.Payment_IN_FLIGHT,
409409
FullAmount: 2000,

accounts/store.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/btcsuite/btcwallet/walletdb"
1313
"github.com/lightningnetwork/lnd/kvdb"
14-
"github.com/lightningnetwork/lnd/lntypes"
1514
"github.com/lightningnetwork/lnd/lnwire"
1615
"go.etcd.io/bbolt"
1716
)
@@ -146,8 +145,8 @@ func (s *BoltStore) NewAccount(balance lnwire.MilliSatoshi,
146145
CurrentBalance: int64(balance),
147146
ExpirationDate: expirationDate,
148147
LastUpdate: time.Now(),
149-
Invoices: make(map[lntypes.Hash]struct{}),
150-
Payments: make(map[lntypes.Hash]*PaymentEntry),
148+
Invoices: make(AccountInvoices),
149+
Payments: make(AccountPayments),
151150
Label: label,
152151
}
153152

0 commit comments

Comments
 (0)