Skip to content

Commit 6ed39b0

Browse files
authored
Merge pull request #642 from ViktorTigerstrom/2023-09-dont-stop-lit-on-account-system-error
Don't shutdown lit on accounts service critical error, and register to status server
2 parents bd8993e + 294e9d0 commit 6ed39b0

File tree

12 files changed

+662
-104
lines changed

12 files changed

+662
-104
lines changed

accounts/checkers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
522522
}
523523

524524
// The invoice is optional.
525+
var paymentHash lntypes.Hash
525526
if len(invoice) > 0 {
526527
payReq, err := zpay32.Decode(invoice, chainParams)
527528
if err != nil {
@@ -531,6 +532,10 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
531532
if payReq.MilliSat != nil && *payReq.MilliSat > sendAmt {
532533
sendAmt = *payReq.MilliSat
533534
}
535+
536+
if payReq.PaymentHash != nil {
537+
paymentHash = *payReq.PaymentHash
538+
}
534539
}
535540

536541
// We also add the max fee to the amount to check. This might mean that
@@ -549,6 +554,14 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
549554
return fmt.Errorf("error validating account balance: %w", err)
550555
}
551556

557+
emptyHash := lntypes.Hash{}
558+
if paymentHash != emptyHash {
559+
err = service.AssociatePayment(acct.ID, paymentHash, sendAmt)
560+
if err != nil {
561+
return fmt.Errorf("error associating payment: %w", err)
562+
}
563+
}
564+
552565
return nil
553566
}
554567

accounts/checkers_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ func (m *mockService) AssociateInvoice(id AccountID, hash lntypes.Hash) error {
6868
return nil
6969
}
7070

71+
func (m *mockService) AssociatePayment(id AccountID, paymentHash lntypes.Hash,
72+
amt lnwire.MilliSatoshi) error {
73+
74+
return nil
75+
}
76+
7177
func (m *mockService) TrackPayment(_ AccountID, hash lntypes.Hash,
7278
amt lnwire.MilliSatoshi) error {
7379

@@ -85,6 +91,10 @@ func (m *mockService) RemovePayment(hash lntypes.Hash) error {
8591
return nil
8692
}
8793

94+
func (*mockService) IsRunning() bool {
95+
return true
96+
}
97+
8898
var _ Service = (*mockService)(nil)
8999

90100
// TestAccountChecker makes sure all round trip checkers can be instantiated

accounts/interceptor.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ func (s *InterceptorService) Intercept(ctx context.Context,
5252
s.requestMtx.Lock()
5353
defer s.requestMtx.Unlock()
5454

55+
// If the account service is not running, we reject all requests.
56+
// Note that this is by no means a guarantee that the account service
57+
// will be running throughout processing the request, but at least we
58+
// can stop requests early if the service was already disabled when the
59+
// request came in.
60+
if !s.IsRunning() {
61+
return mid.RPCErrString(
62+
req, "the account service has been stopped",
63+
)
64+
}
65+
5566
mac := &macaroon.Macaroon{}
5667
err := mac.UnmarshalBinary(req.RawMacaroon)
5768
if err != nil {

accounts/interface.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ var (
160160
ErrNotSupportedWithAccounts = errors.New("this RPC call is not " +
161161
"supported with restricted account macaroons")
162162

163+
// ErrAccountServiceDisabled is the error that is returned when the
164+
// account service has been disabled due to an error being thrown
165+
// in the service that cannot be recovered from.
166+
ErrAccountServiceDisabled = errors.New("the account service has been " +
167+
"stopped")
168+
163169
// MacaroonPermissions are the permissions required for an account
164170
// macaroon.
165171
MacaroonPermissions = []bakery.Op{{
@@ -240,4 +246,10 @@ type Service interface {
240246
// longer needs to be tracked. The payment is certain to never succeed,
241247
// so we never need to debit the amount from the account.
242248
RemovePayment(hash lntypes.Hash) error
249+
250+
// AssociatePayment associates a payment (hash) with the given account,
251+
// ensuring that the payment will be tracked for a user when LiT is
252+
// restarted.
253+
AssociatePayment(id AccountID, paymentHash lntypes.Hash,
254+
fullAmt lnwire.MilliSatoshi) error
243255
}

0 commit comments

Comments
 (0)