Skip to content

Commit

Permalink
Merge pull request #642 from ViktorTigerstrom/2023-09-dont-stop-lit-o…
Browse files Browse the repository at this point in the history
…n-account-system-error

Don't shutdown lit on accounts service critical error, and register to status server
  • Loading branch information
ellemouton authored Sep 28, 2023
2 parents bd8993e + 294e9d0 commit 6ed39b0
Show file tree
Hide file tree
Showing 12 changed files with 662 additions and 104 deletions.
13 changes: 13 additions & 0 deletions accounts/checkers.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
}

// The invoice is optional.
var paymentHash lntypes.Hash
if len(invoice) > 0 {
payReq, err := zpay32.Decode(invoice, chainParams)
if err != nil {
Expand All @@ -531,6 +532,10 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params,
if payReq.MilliSat != nil && *payReq.MilliSat > sendAmt {
sendAmt = *payReq.MilliSat
}

if payReq.PaymentHash != nil {
paymentHash = *payReq.PaymentHash
}
}

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

emptyHash := lntypes.Hash{}
if paymentHash != emptyHash {
err = service.AssociatePayment(acct.ID, paymentHash, sendAmt)
if err != nil {
return fmt.Errorf("error associating payment: %w", err)
}
}

return nil
}

Expand Down
10 changes: 10 additions & 0 deletions accounts/checkers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func (m *mockService) AssociateInvoice(id AccountID, hash lntypes.Hash) error {
return nil
}

func (m *mockService) AssociatePayment(id AccountID, paymentHash lntypes.Hash,
amt lnwire.MilliSatoshi) error {

return nil
}

func (m *mockService) TrackPayment(_ AccountID, hash lntypes.Hash,
amt lnwire.MilliSatoshi) error {

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

func (*mockService) IsRunning() bool {
return true
}

var _ Service = (*mockService)(nil)

// TestAccountChecker makes sure all round trip checkers can be instantiated
Expand Down
11 changes: 11 additions & 0 deletions accounts/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ func (s *InterceptorService) Intercept(ctx context.Context,
s.requestMtx.Lock()
defer s.requestMtx.Unlock()

// If the account service is not running, we reject all requests.
// Note that this is by no means a guarantee that the account service
// will be running throughout processing the request, but at least we
// can stop requests early if the service was already disabled when the
// request came in.
if !s.IsRunning() {
return mid.RPCErrString(
req, "the account service has been stopped",
)
}

mac := &macaroon.Macaroon{}
err := mac.UnmarshalBinary(req.RawMacaroon)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions accounts/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ var (
ErrNotSupportedWithAccounts = errors.New("this RPC call is not " +
"supported with restricted account macaroons")

// ErrAccountServiceDisabled is the error that is returned when the
// account service has been disabled due to an error being thrown
// in the service that cannot be recovered from.
ErrAccountServiceDisabled = errors.New("the account service has been " +
"stopped")

// MacaroonPermissions are the permissions required for an account
// macaroon.
MacaroonPermissions = []bakery.Op{{
Expand Down Expand Up @@ -240,4 +246,10 @@ type Service interface {
// longer needs to be tracked. The payment is certain to never succeed,
// so we never need to debit the amount from the account.
RemovePayment(hash lntypes.Hash) error

// AssociatePayment associates a payment (hash) with the given account,
// ensuring that the payment will be tracked for a user when LiT is
// restarted.
AssociatePayment(id AccountID, paymentHash lntypes.Hash,
fullAmt lnwire.MilliSatoshi) error
}
Loading

0 comments on commit 6ed39b0

Please sign in to comment.