@@ -81,13 +81,12 @@ impl InboundJITChannelState {
81
81
82
82
struct InboundJITChannel {
83
83
state : InboundJITChannelState ,
84
- user_channel_id : u128 ,
85
84
payment_size_msat : Option < u64 > ,
86
85
}
87
86
88
87
impl InboundJITChannel {
89
- fn new ( user_channel_id : u128 , payment_size_msat : Option < u64 > ) -> Self {
90
- Self { user_channel_id , payment_size_msat, state : InboundJITChannelState :: BuyRequested }
88
+ fn new ( payment_size_msat : Option < u64 > ) -> Self {
89
+ Self { payment_size_msat, state : InboundJITChannelState :: BuyRequested }
91
90
}
92
91
93
92
fn invoice_params_received (
@@ -99,17 +98,15 @@ impl InboundJITChannel {
99
98
}
100
99
101
100
struct PeerState {
102
- inbound_channels_by_id : HashMap < u128 , InboundJITChannel > ,
103
101
pending_get_info_requests : HashSet < RequestId > ,
104
- pending_buy_requests : HashMap < RequestId , u128 > ,
102
+ pending_buy_requests : HashMap < RequestId , InboundJITChannel > ,
105
103
}
106
104
107
105
impl PeerState {
108
106
fn new ( ) -> Self {
109
- let inbound_channels_by_id = HashMap :: new ( ) ;
110
107
let pending_get_info_requests = HashSet :: new ( ) ;
111
108
let pending_buy_requests = HashMap :: new ( ) ;
112
- Self { inbound_channels_by_id , pending_get_info_requests, pending_buy_requests }
109
+ Self { pending_get_info_requests, pending_buy_requests }
113
110
}
114
111
}
115
112
@@ -191,9 +188,6 @@ where
191
188
///
192
189
/// The user will receive the LSP's response via an [`InvoiceParametersReady`] event.
193
190
///
194
- /// The user needs to provide a locally unique `user_channel_id` which will be used for
195
- /// tracking the channel state.
196
- ///
197
191
/// If `payment_size_msat` is [`Option::Some`] then the invoice will be for a fixed amount
198
192
/// and MPP can be used to pay it.
199
193
///
@@ -206,36 +200,33 @@ where
206
200
/// [`OpeningParametersReady`]: crate::lsps2::event::LSPS2ClientEvent::OpeningParametersReady
207
201
/// [`InvoiceParametersReady`]: crate::lsps2::event::LSPS2ClientEvent::InvoiceParametersReady
208
202
pub fn select_opening_params (
209
- & self , counterparty_node_id : PublicKey , user_channel_id : u128 ,
210
- payment_size_msat : Option < u64 > , opening_fee_params : OpeningFeeParams ,
211
- ) -> Result < ( ) , APIError > {
203
+ & self , counterparty_node_id : PublicKey , payment_size_msat : Option < u64 > ,
204
+ opening_fee_params : OpeningFeeParams ,
205
+ ) -> Result < RequestId , APIError > {
212
206
let mut outer_state_lock = self . per_peer_state . write ( ) . unwrap ( ) ;
213
207
let inner_state_lock =
214
208
outer_state_lock. entry ( counterparty_node_id) . or_insert ( Mutex :: new ( PeerState :: new ( ) ) ) ;
215
209
let mut peer_state_lock = inner_state_lock. lock ( ) . unwrap ( ) ;
216
210
217
- let jit_channel = InboundJITChannel :: new ( user_channel_id, payment_size_msat) ;
218
- if peer_state_lock. inbound_channels_by_id . insert ( user_channel_id, jit_channel) . is_some ( ) {
211
+ let request_id = crate :: utils:: generate_request_id ( & self . entropy_source ) ;
212
+
213
+ let jit_channel = InboundJITChannel :: new ( payment_size_msat) ;
214
+ if peer_state_lock. pending_buy_requests . insert ( request_id. clone ( ) , jit_channel) . is_some ( ) {
219
215
return Err ( APIError :: APIMisuseError {
220
- err : format ! (
221
- "Failed due to duplicate user_channel_id. Please ensure its uniqueness!"
222
- ) ,
216
+ err : format ! ( "Failed due to duplicate request_id. This should never happen!" ) ,
223
217
} ) ;
224
218
}
225
219
226
- let request_id = crate :: utils:: generate_request_id ( & self . entropy_source ) ;
227
- peer_state_lock. pending_buy_requests . insert ( request_id. clone ( ) , user_channel_id) ;
228
-
229
220
self . pending_messages . enqueue (
230
221
& counterparty_node_id,
231
222
LSPS2Message :: Request (
232
- request_id,
223
+ request_id. clone ( ) ,
233
224
LSPS2Request :: Buy ( BuyRequest { opening_fee_params, payment_size_msat } ) ,
234
225
)
235
226
. into ( ) ,
236
227
) ;
237
228
238
- Ok ( ( ) )
229
+ Ok ( request_id )
239
230
}
240
231
241
232
fn handle_get_info_response (
@@ -314,7 +305,7 @@ where
314
305
Some ( inner_state_lock) => {
315
306
let mut peer_state = inner_state_lock. lock ( ) . unwrap ( ) ;
316
307
317
- let user_channel_id =
308
+ let mut jit_channel =
318
309
peer_state. pending_buy_requests . remove ( & request_id) . ok_or ( LightningError {
319
310
err : format ! (
320
311
"Received buy response for an unknown request: {:?}" ,
@@ -323,21 +314,9 @@ where
323
314
action : ErrorAction :: IgnoreAndLog ( Level :: Info ) ,
324
315
} ) ?;
325
316
326
- let jit_channel = peer_state
327
- . inbound_channels_by_id
328
- . get_mut ( & user_channel_id)
329
- . ok_or ( LightningError {
330
- err : format ! (
331
- "Received buy response for an unknown channel: {:?}" ,
332
- user_channel_id
333
- ) ,
334
- action : ErrorAction :: IgnoreAndLog ( Level :: Info ) ,
335
- } ) ?;
336
-
337
317
// Reject the buy response if we disallow client_trusts_lsp and the LSP requires
338
318
// it.
339
319
if !self . config . client_trusts_lsp && result. client_trusts_lsp {
340
- peer_state. inbound_channels_by_id . remove ( & user_channel_id) ;
341
320
return Err ( LightningError {
342
321
err : format ! (
343
322
"Aborting JIT channel flow as the LSP requires 'client_trusts_lsp' mode, which we disallow"
@@ -346,22 +325,20 @@ where
346
325
} ) ;
347
326
}
348
327
349
- if let Err ( e) = jit_channel. invoice_params_received (
328
+ // Update the channel state
329
+ jit_channel. invoice_params_received (
350
330
result. client_trusts_lsp ,
351
331
result. intercept_scid . clone ( ) ,
352
- ) {
353
- peer_state. inbound_channels_by_id . remove ( & user_channel_id) ;
354
- return Err ( e) ;
355
- }
332
+ ) ?;
356
333
357
334
if let Ok ( intercept_scid) = result. intercept_scid . to_scid ( ) {
358
335
self . pending_events . enqueue ( Event :: LSPS2Client (
359
336
LSPS2ClientEvent :: InvoiceParametersReady {
337
+ request_id,
360
338
counterparty_node_id : * counterparty_node_id,
361
339
intercept_scid,
362
340
cltv_expiry_delta : result. lsp_cltv_expiry_delta ,
363
341
payment_size_msat : jit_channel. payment_size_msat ,
364
- user_channel_id : jit_channel. user_channel_id ,
365
342
} ,
366
343
) ) ;
367
344
} else {
@@ -395,21 +372,11 @@ where
395
372
Some ( inner_state_lock) => {
396
373
let mut peer_state = inner_state_lock. lock ( ) . unwrap ( ) ;
397
374
398
- let user_channel_id =
399
- peer_state. pending_buy_requests . remove ( & request_id) . ok_or ( LightningError {
400
- err : format ! ( "Received buy error for an unknown request: {:?}" , request_id) ,
401
- action : ErrorAction :: IgnoreAndLog ( Level :: Info ) ,
402
- } ) ?;
375
+ peer_state. pending_buy_requests . remove ( & request_id) . ok_or ( LightningError {
376
+ err : format ! ( "Received buy error for an unknown request: {:?}" , request_id) ,
377
+ action : ErrorAction :: IgnoreAndLog ( Level :: Info ) ,
378
+ } ) ?;
403
379
404
- peer_state. inbound_channels_by_id . remove ( & user_channel_id) . ok_or (
405
- LightningError {
406
- err : format ! (
407
- "Received buy error for an unknown channel: {:?}" ,
408
- user_channel_id
409
- ) ,
410
- action : ErrorAction :: IgnoreAndLog ( Level :: Info ) ,
411
- } ,
412
- ) ?;
413
380
Ok ( ( ) )
414
381
}
415
382
None => {
0 commit comments