@@ -89,6 +89,15 @@ func NewInterceptor(lnd *lndclient.LndServices, store Store,
89
89
}
90
90
}
91
91
92
+ // interceptContext is a struct that contains all information about a call that
93
+ // is intercepted by the interceptor.
94
+ type interceptContext struct {
95
+ mainCtx context.Context
96
+ opts []grpc.CallOption
97
+ metadata * metadata.MD
98
+ token * Token
99
+ }
100
+
92
101
// UnaryInterceptor is an interceptor method that can be used directly by gRPC
93
102
// for unary calls. If the store contains a token, it is attached as credentials
94
103
// to every call before patching it through. The response error is also
@@ -105,38 +114,73 @@ func (i *Interceptor) UnaryInterceptor(ctx context.Context, method string,
105
114
i .lock .Lock ()
106
115
defer i .lock .Unlock ()
107
116
108
- addLsatCredentials := func (token * Token ) error {
109
- macaroon , err := token .PaidMacaroon ()
110
- if err != nil {
111
- return err
112
- }
113
- opts = append (opts , grpc .PerRPCCredentials (
114
- macaroons .NewMacaroonCredential (macaroon ),
115
- ))
116
- return nil
117
+ // Create the context that we'll use to initiate the real request. This
118
+ // contains the means to extract response headers and possibly also an
119
+ // auth token, if we already have paid for one.
120
+ iCtx , err := i .newInterceptContext (ctx , opts )
121
+ if err != nil {
122
+ return err
123
+ }
124
+
125
+ // Try executing the call now. If anything goes wrong, we only handle
126
+ // the LSAT error message that comes in the form of a gRPC status error.
127
+ rpcCtx , cancel := context .WithTimeout (ctx , i .callTimeout )
128
+ defer cancel ()
129
+ err = invoker (rpcCtx , method , req , reply , cc , iCtx .opts ... )
130
+ if ! isPaymentRequired (err ) {
131
+ return err
132
+ }
133
+
134
+ // Find out if we need to pay for a new token or perhaps resume
135
+ // a previously aborted payment.
136
+ err = i .handlePayment (iCtx )
137
+ if err != nil {
138
+ return err
139
+ }
140
+
141
+ // Execute the same request again, now with the LSAT
142
+ // token added as an RPC credential.
143
+ rpcCtx2 , cancel2 := context .WithTimeout (ctx , i .callTimeout )
144
+ defer cancel2 ()
145
+ return invoker (rpcCtx2 , method , req , reply , cc , iCtx .opts ... )
146
+ }
147
+
148
+ // newInterceptContext creates the initial intercept context that can capture
149
+ // metadata from the server and sends the local token to the server if one
150
+ // already exists.
151
+ func (i * Interceptor ) newInterceptContext (ctx context.Context ,
152
+ opts []grpc.CallOption ) (* interceptContext , error ) {
153
+
154
+ iCtx := & interceptContext {
155
+ mainCtx : ctx ,
156
+ opts : opts ,
157
+ metadata : & metadata.MD {},
117
158
}
118
159
119
160
// Let's see if the store already contains a token and what state it
120
161
// might be in. If a previous call was aborted, we might have a pending
121
162
// token that needs to be handled separately.
122
- token , err := i .store .CurrentToken ()
163
+ var err error
164
+ iCtx .token , err = i .store .CurrentToken ()
123
165
switch {
124
166
// If there is no token yet, nothing to do at this point.
125
167
case err == ErrNoToken :
126
168
127
169
// Some other error happened that we have to surface.
128
170
case err != nil :
129
171
log .Errorf ("Failed to get token from store: %v" , err )
130
- return fmt .Errorf ("getting token from store failed: %v" , err )
172
+ return nil , fmt .Errorf ("getting token from store failed: %v" ,
173
+ err )
131
174
132
175
// Only if we have a paid token append it. We don't resume a pending
133
176
// payment just yet, since we don't even know if a token is required for
134
177
// this call. We also never send a pending payment to the server since
135
178
// we know it's not valid.
136
- case ! token .isPending ():
137
- if err = addLsatCredentials (token ); err != nil {
179
+ case ! iCtx . token .isPending ():
180
+ if err = i . addLsatCredentials (iCtx ); err != nil {
138
181
log .Errorf ("Adding macaroon to request failed: %v" , err )
139
- return fmt .Errorf ("adding macaroon failed: %v" , err )
182
+ return nil , fmt .Errorf ("adding macaroon failed: %v" ,
183
+ err )
140
184
}
141
185
}
142
186
@@ -145,60 +189,59 @@ func (i *Interceptor) UnaryInterceptor(ctx context.Context, method string,
145
189
// option. We execute the request and inspect the error. If it's the
146
190
// LSAT specific payment required error, we might execute the same
147
191
// method again later with the paid LSAT token.
148
- trailerMetadata := & metadata.MD {}
149
- opts = append (opts , grpc .Trailer (trailerMetadata ))
150
- rpcCtx , cancel := context .WithTimeout (ctx , i .callTimeout )
151
- defer cancel ()
152
- err = invoker (rpcCtx , method , req , reply , cc , opts ... )
153
-
154
- // Only handle the LSAT error message that comes in the form of
155
- // a gRPC status error.
156
- if isPaymentRequired (err ) {
157
- paidToken , err := i .handlePayment (ctx , token , trailerMetadata )
158
- if err != nil {
159
- return err
160
- }
161
- if err = addLsatCredentials (paidToken ); err != nil {
162
- log .Errorf ("Adding macaroon to request failed: %v" , err )
163
- return fmt .Errorf ("adding macaroon failed: %v" , err )
164
- }
165
-
166
- // Execute the same request again, now with the LSAT
167
- // token added as an RPC credential.
168
- rpcCtx2 , cancel2 := context .WithTimeout (ctx , i .callTimeout )
169
- defer cancel2 ()
170
- return invoker (rpcCtx2 , method , req , reply , cc , opts ... )
171
- }
172
- return err
192
+ iCtx .opts = append (iCtx .opts , grpc .Trailer (iCtx .metadata ))
193
+ return iCtx , nil
173
194
}
174
195
175
196
// handlePayment tries to obtain a valid token by either tracking the payment
176
197
// status of a pending token or paying for a new one.
177
- func (i * Interceptor ) handlePayment (ctx context.Context , token * Token ,
178
- md * metadata.MD ) (* Token , error ) {
179
-
198
+ func (i * Interceptor ) handlePayment (iCtx * interceptContext ) error {
180
199
switch {
181
200
// Resume/track a pending payment if it was interrupted for some reason.
182
- case token != nil && token .isPending ():
201
+ case iCtx . token != nil && iCtx . token .isPending ():
183
202
log .Infof ("Payment of LSAT token is required, resuming/" +
184
203
"tracking previous payment from pending LSAT token" )
185
- err := i .trackPayment (ctx , token )
204
+ err := i .trackPayment (iCtx . mainCtx , iCtx . token )
186
205
if err != nil {
187
- return nil , err
206
+ return err
188
207
}
189
- return token , nil
190
208
191
209
// We don't have a token yet, try to get a new one.
192
- case token == nil :
210
+ case iCtx . token == nil :
193
211
// We don't have a token yet, get a new one.
194
212
log .Infof ("Payment of LSAT token is required, paying invoice" )
195
- return i .payLsatToken (ctx , md )
213
+ var err error
214
+ iCtx .token , err = i .payLsatToken (iCtx .mainCtx , iCtx .metadata )
215
+ if err != nil {
216
+ return err
217
+ }
196
218
197
219
// We have a token and it's valid, nothing more to do here.
198
220
default :
199
221
log .Debugf ("Found valid LSAT token to add to request" )
200
- return token , nil
201
222
}
223
+
224
+ if err := i .addLsatCredentials (iCtx ); err != nil {
225
+ log .Errorf ("Adding macaroon to request failed: %v" , err )
226
+ return fmt .Errorf ("adding macaroon failed: %v" , err )
227
+ }
228
+ return nil
229
+ }
230
+
231
+ // addLsatCredentials adds an LSAT token to the given intercept context.
232
+ func (i * Interceptor ) addLsatCredentials (iCtx * interceptContext ) error {
233
+ if iCtx .token == nil {
234
+ return fmt .Errorf ("cannot add nil token to context" )
235
+ }
236
+
237
+ macaroon , err := iCtx .token .PaidMacaroon ()
238
+ if err != nil {
239
+ return err
240
+ }
241
+ iCtx .opts = append (iCtx .opts , grpc .PerRPCCredentials (
242
+ macaroons .NewMacaroonCredential (macaroon ),
243
+ ))
244
+ return nil
202
245
}
203
246
204
247
// payLsatToken reads the payment challenge from the response metadata and tries
0 commit comments