Skip to content

Commit 211865a

Browse files
authored
Merge pull request #934 from ellemouton/sql1Accounts1
[sql-1]accounts: preparatory commits for SQL-izing accounts
2 parents 54cf58b + 6f131a1 commit 211865a

File tree

12 files changed

+301
-188
lines changed

12 files changed

+301
-188
lines changed

accounts/checkers.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func NewAccountChecker(service Service,
131131
}
132132

133133
return nil, service.AssociateInvoice(
134-
acct.ID, hash,
134+
ctx, acct.ID, hash,
135135
)
136136
}, mid.PassThroughErrorHandler,
137137
),
@@ -615,12 +615,12 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
615615
fee := lnrpc.CalculateFeeLimit(limit, sendAmt)
616616
sendAmt += fee
617617

618-
err = service.CheckBalance(acct.ID, sendAmt)
618+
err = service.CheckBalance(ctx, acct.ID, sendAmt)
619619
if err != nil {
620620
return fmt.Errorf("error validating account balance: %w", err)
621621
}
622622

623-
err = service.AssociatePayment(acct.ID, pHash, sendAmt)
623+
err = service.AssociatePayment(ctx, acct.ID, pHash, sendAmt)
624624
if err != nil {
625625
return fmt.Errorf("error associating payment: %w", err)
626626
}
@@ -661,11 +661,13 @@ func checkSendResponse(ctx context.Context, service Service,
661661
if status == lnrpc.Payment_FAILED {
662662
service.DeleteValues(reqID)
663663

664-
return nil, service.RemovePayment(hash)
664+
return nil, service.RemovePayment(ctx, hash)
665665
}
666666

667667
// If there is no immediate failure, make sure we track the payment.
668-
err = service.TrackPayment(acct.ID, hash, lnwire.MilliSatoshi(fullAmt))
668+
err = service.TrackPayment(
669+
ctx, acct.ID, hash, lnwire.MilliSatoshi(fullAmt),
670+
)
669671
if err != nil {
670672
return nil, err
671673
}
@@ -713,12 +715,12 @@ func checkSendToRoute(ctx context.Context, service Service, paymentHash []byte,
713715
}
714716
sendAmt += fee
715717

716-
err = service.CheckBalance(acct.ID, sendAmt)
718+
err = service.CheckBalance(ctx, acct.ID, sendAmt)
717719
if err != nil {
718720
return fmt.Errorf("error validating account balance: %w", err)
719721
}
720722

721-
err = service.AssociatePayment(acct.ID, hash, sendAmt)
723+
err = service.AssociatePayment(ctx, acct.ID, hash, sendAmt)
722724
if err != nil {
723725
return fmt.Errorf("error associating payment with hash %s: %w",
724726
hash, err)
@@ -749,7 +751,7 @@ func erroredPaymentHandler(service Service) mid.ErrorHandler {
749751
"hash: %s and amount: %d", reqVals.PaymentHash,
750752
reqVals.PaymentAmount)
751753

752-
err = service.PaymentErrored(acct.ID, reqVals.PaymentHash)
754+
err = service.PaymentErrored(ctx, acct.ID, reqVals.PaymentHash)
753755
if err != nil {
754756
return nil, err
755757
}
@@ -812,7 +814,7 @@ func sendToRouteHTLCResponseHandler(service Service) func(ctx context.Context,
812814
}
813815

814816
err = service.TrackPayment(
815-
acct.ID, reqValues.PaymentHash,
817+
ctx, acct.ID, reqValues.PaymentHash,
816818
lnwire.MilliSatoshi(totalAmount),
817819
)
818820
if err != nil {

accounts/checkers_test.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func newMockService() *mockService {
7171
}
7272
}
7373

74-
func (m *mockService) CheckBalance(_ AccountID,
74+
func (m *mockService) CheckBalance(_ context.Context, _ AccountID,
7575
wantBalance lnwire.MilliSatoshi) error {
7676

7777
if wantBalance > m.acctBalanceMsat {
@@ -81,24 +81,28 @@ func (m *mockService) CheckBalance(_ AccountID,
8181
return nil
8282
}
8383

84-
func (m *mockService) AssociateInvoice(id AccountID, hash lntypes.Hash) error {
84+
func (m *mockService) AssociateInvoice(_ context.Context, id AccountID,
85+
hash lntypes.Hash) error {
86+
8587
m.trackedInvoices[hash] = id
8688

8789
return nil
8890
}
8991

90-
func (m *mockService) AssociatePayment(id AccountID, paymentHash lntypes.Hash,
91-
amt lnwire.MilliSatoshi) error {
92+
func (m *mockService) AssociatePayment(_ context.Context, id AccountID,
93+
paymentHash lntypes.Hash, amt lnwire.MilliSatoshi) error {
9294

9395
return nil
9496
}
9597

96-
func (m *mockService) PaymentErrored(id AccountID, hash lntypes.Hash) error {
98+
func (m *mockService) PaymentErrored(_ context.Context, id AccountID,
99+
hash lntypes.Hash) error {
100+
97101
return nil
98102
}
99103

100-
func (m *mockService) TrackPayment(_ AccountID, hash lntypes.Hash,
101-
amt lnwire.MilliSatoshi) error {
104+
func (m *mockService) TrackPayment(_ context.Context, _ AccountID,
105+
hash lntypes.Hash, amt lnwire.MilliSatoshi) error {
102106

103107
m.trackedPayments[hash] = &PaymentEntry{
104108
Status: lnrpc.Payment_UNKNOWN,
@@ -108,7 +112,9 @@ func (m *mockService) TrackPayment(_ AccountID, hash lntypes.Hash,
108112
return nil
109113
}
110114

111-
func (m *mockService) RemovePayment(hash lntypes.Hash) error {
115+
func (m *mockService) RemovePayment(_ context.Context,
116+
hash lntypes.Hash) error {
117+
112118
delete(m.trackedPayments, hash)
113119

114120
return nil
@@ -517,14 +523,15 @@ func testSendPayment(t *testing.T, uri string) {
517523
errFunc := func(err error) {
518524
lndMock.mainErrChan <- err
519525
}
520-
service, err := NewService(t.TempDir(), errFunc)
526+
store := NewTestDB(t)
527+
service, err := NewService(store, errFunc)
521528
require.NoError(t, err)
522529

523530
err = service.Start(ctx, lndMock, routerMock, chainParams)
524531
require.NoError(t, err)
525532

526533
assertBalance := func(id AccountID, expectedBalance int64) {
527-
acct, err := service.Account(id)
534+
acct, err := service.Account(ctx, id)
528535
require.NoError(t, err)
529536

530537
require.Equal(t, expectedBalance,
@@ -539,7 +546,7 @@ func testSendPayment(t *testing.T, uri string) {
539546

540547
// Create an account and add it to the context.
541548
acct, err := service.NewAccount(
542-
5000, time.Now().Add(time.Hour), "test",
549+
ctx, 5000, time.Now().Add(time.Hour), "test",
543550
)
544551
require.NoError(t, err)
545552

@@ -713,14 +720,15 @@ func TestSendPaymentV2(t *testing.T) {
713720
errFunc := func(err error) {
714721
lndMock.mainErrChan <- err
715722
}
716-
service, err := NewService(t.TempDir(), errFunc)
723+
store := NewTestDB(t)
724+
service, err := NewService(store, errFunc)
717725
require.NoError(t, err)
718726

719727
err = service.Start(ctx, lndMock, routerMock, chainParams)
720728
require.NoError(t, err)
721729

722730
assertBalance := func(id AccountID, expectedBalance int64) {
723-
acct, err := service.Account(id)
731+
acct, err := service.Account(ctx, id)
724732
require.NoError(t, err)
725733

726734
require.Equal(t, expectedBalance,
@@ -735,7 +743,7 @@ func TestSendPaymentV2(t *testing.T) {
735743

736744
// Create an account and add it to the context.
737745
acct, err := service.NewAccount(
738-
5000, time.Now().Add(time.Hour), "test",
746+
ctx, 5000, time.Now().Add(time.Hour), "test",
739747
)
740748
require.NoError(t, err)
741749

@@ -900,14 +908,15 @@ func TestSendToRouteV2(t *testing.T) {
900908
errFunc := func(err error) {
901909
lndMock.mainErrChan <- err
902910
}
903-
service, err := NewService(t.TempDir(), errFunc)
911+
store := NewTestDB(t)
912+
service, err := NewService(store, errFunc)
904913
require.NoError(t, err)
905914

906915
err = service.Start(ctx, lndMock, routerMock, chainParams)
907916
require.NoError(t, err)
908917

909918
assertBalance := func(id AccountID, expectedBalance int64) {
910-
acct, err := service.Account(id)
919+
acct, err := service.Account(ctx, id)
911920
require.NoError(t, err)
912921

913922
require.Equal(t, expectedBalance,
@@ -922,7 +931,7 @@ func TestSendToRouteV2(t *testing.T) {
922931

923932
// Create an account and add it to the context.
924933
acct, err := service.NewAccount(
925-
5000, time.Now().Add(time.Hour), "test",
934+
ctx, 5000, time.Now().Add(time.Hour), "test",
926935
)
927936
require.NoError(t, err)
928937

accounts/errors.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package accounts
2+
3+
import "errors"
4+
5+
var (
6+
// ErrLabelAlreadyExists is returned by the CreateAccount method if the
7+
// account label is already used by an existing account.
8+
ErrLabelAlreadyExists = errors.New(
9+
"account label uniqueness constraint violation",
10+
)
11+
)

accounts/interceptor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (s *InterceptorService) Intercept(ctx context.Context,
8484
"macaroon caveat")
8585
}
8686

87-
acct, err := s.Account(*acctID)
87+
acct, err := s.Account(ctx, *acctID)
8888
if err != nil {
8989
return mid.RPCErrString(
9090
req, "error getting account %x: %v", acctID[:], err,

accounts/interface.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package accounts
22

33
import (
4+
"context"
45
"encoding/hex"
56
"errors"
67
"fmt"
@@ -201,30 +202,34 @@ var (
201202
type Store interface {
202203
// NewAccount creates a new OffChainBalanceAccount with the given
203204
// balance and a randomly chosen ID.
204-
NewAccount(balance lnwire.MilliSatoshi, expirationDate time.Time,
205-
label string) (*OffChainBalanceAccount, error)
205+
NewAccount(ctx context.Context, balance lnwire.MilliSatoshi,
206+
expirationDate time.Time, label string) (
207+
*OffChainBalanceAccount, error)
206208

207209
// UpdateAccount writes an account to the database, overwriting the
208210
// existing one if it exists.
209-
UpdateAccount(account *OffChainBalanceAccount) error
211+
UpdateAccount(ctx context.Context,
212+
account *OffChainBalanceAccount) error
210213

211214
// Account retrieves an account from the Store and un-marshals it. If
212215
// the account cannot be found, then ErrAccNotFound is returned.
213-
Account(id AccountID) (*OffChainBalanceAccount, error)
216+
Account(ctx context.Context, id AccountID) (*OffChainBalanceAccount,
217+
error)
214218

215219
// Accounts retrieves all accounts from the store and un-marshals them.
216-
Accounts() ([]*OffChainBalanceAccount, error)
220+
Accounts(ctx context.Context) ([]*OffChainBalanceAccount, error)
217221

218222
// RemoveAccount finds an account by its ID and removes it from the¨
219223
// store.
220-
RemoveAccount(id AccountID) error
224+
RemoveAccount(ctx context.Context, id AccountID) error
221225

222226
// LastIndexes returns the last invoice add and settle index or
223227
// ErrNoInvoiceIndexKnown if no indexes are known yet.
224-
LastIndexes() (uint64, uint64, error)
228+
LastIndexes(ctx context.Context) (uint64, uint64, error)
225229

226230
// StoreLastIndexes stores the last invoice add and settle index.
227-
StoreLastIndexes(addIndex, settleIndex uint64) error
231+
StoreLastIndexes(ctx context.Context, addIndex,
232+
settleIndex uint64) error
228233

229234
// Close closes the underlying store.
230235
Close() error
@@ -234,34 +239,37 @@ type Store interface {
234239
type Service interface {
235240
// CheckBalance ensures an account is valid and has a balance equal to
236241
// or larger than the amount that is required.
237-
CheckBalance(id AccountID, requiredBalance lnwire.MilliSatoshi) error
242+
CheckBalance(ctx context.Context, id AccountID,
243+
requiredBalance lnwire.MilliSatoshi) error
238244

239245
// AssociateInvoice associates a generated invoice with the given
240246
// account, making it possible for the account to be credited in case
241247
// the invoice is paid.
242-
AssociateInvoice(id AccountID, hash lntypes.Hash) error
248+
AssociateInvoice(ctx context.Context, id AccountID,
249+
hash lntypes.Hash) error
243250

244251
// TrackPayment adds a new payment to be tracked to the service. If the
245252
// payment is eventually settled, its amount needs to be debited from
246253
// the given account.
247-
TrackPayment(id AccountID, hash lntypes.Hash,
254+
TrackPayment(ctx context.Context, id AccountID, hash lntypes.Hash,
248255
fullAmt lnwire.MilliSatoshi) error
249256

250257
// RemovePayment removes a failed payment from the service because it no
251258
// longer needs to be tracked. The payment is certain to never succeed,
252259
// so we never need to debit the amount from the account.
253-
RemovePayment(hash lntypes.Hash) error
260+
RemovePayment(ctx context.Context, hash lntypes.Hash) error
254261

255262
// AssociatePayment associates a payment (hash) with the given account,
256263
// ensuring that the payment will be tracked for a user when LiT is
257264
// restarted.
258-
AssociatePayment(id AccountID, paymentHash lntypes.Hash,
259-
fullAmt lnwire.MilliSatoshi) error
265+
AssociatePayment(ctx context.Context, id AccountID,
266+
paymentHash lntypes.Hash, fullAmt lnwire.MilliSatoshi) error
260267

261268
// PaymentErrored removes a pending payment from the accounts
262269
// registered payment list. This should only ever be called if we are
263270
// sure that the payment request errored out.
264-
PaymentErrored(id AccountID, hash lntypes.Hash) error
271+
PaymentErrored(ctx context.Context, id AccountID,
272+
hash lntypes.Hash) error
265273

266274
RequestValuesStore
267275
}

0 commit comments

Comments
 (0)