Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 6b42a38

Browse files
committed
Return request_id from select_opening_params
.. and expose it via the corresponding event. This allows the client to track which previously-made request an event corresponds to. It also allows to drop our requirements and additional bookkeeping around `user_channel_id`.
1 parent a01069b commit 6b42a38

File tree

2 files changed

+27
-60
lines changed

2 files changed

+27
-60
lines changed

src/lsps2/client.rs

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,12 @@ impl InboundJITChannelState {
8181

8282
struct InboundJITChannel {
8383
state: InboundJITChannelState,
84-
user_channel_id: u128,
8584
payment_size_msat: Option<u64>,
8685
}
8786

8887
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 }
9190
}
9291

9392
fn invoice_params_received(
@@ -99,17 +98,15 @@ impl InboundJITChannel {
9998
}
10099

101100
struct PeerState {
102-
inbound_channels_by_id: HashMap<u128, InboundJITChannel>,
103101
pending_get_info_requests: HashSet<RequestId>,
104-
pending_buy_requests: HashMap<RequestId, u128>,
102+
pending_buy_requests: HashMap<RequestId, InboundJITChannel>,
105103
}
106104

107105
impl PeerState {
108106
fn new() -> Self {
109-
let inbound_channels_by_id = HashMap::new();
110107
let pending_get_info_requests = HashSet::new();
111108
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 }
113110
}
114111
}
115112

@@ -191,9 +188,6 @@ where
191188
///
192189
/// The user will receive the LSP's response via an [`InvoiceParametersReady`] event.
193190
///
194-
/// The user needs to provide a locally unique `user_channel_id` which will be used for
195-
/// tracking the channel state.
196-
///
197191
/// If `payment_size_msat` is [`Option::Some`] then the invoice will be for a fixed amount
198192
/// and MPP can be used to pay it.
199193
///
@@ -206,36 +200,33 @@ where
206200
/// [`OpeningParametersReady`]: crate::lsps2::event::LSPS2ClientEvent::OpeningParametersReady
207201
/// [`InvoiceParametersReady`]: crate::lsps2::event::LSPS2ClientEvent::InvoiceParametersReady
208202
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> {
212206
let mut outer_state_lock = self.per_peer_state.write().unwrap();
213207
let inner_state_lock =
214208
outer_state_lock.entry(counterparty_node_id).or_insert(Mutex::new(PeerState::new()));
215209
let mut peer_state_lock = inner_state_lock.lock().unwrap();
216210

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() {
219215
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!"),
223217
});
224218
}
225219

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-
229220
self.pending_messages.enqueue(
230221
&counterparty_node_id,
231222
LSPS2Message::Request(
232-
request_id,
223+
request_id.clone(),
233224
LSPS2Request::Buy(BuyRequest { opening_fee_params, payment_size_msat }),
234225
)
235226
.into(),
236227
);
237228

238-
Ok(())
229+
Ok(request_id)
239230
}
240231

241232
fn handle_get_info_response(
@@ -314,7 +305,7 @@ where
314305
Some(inner_state_lock) => {
315306
let mut peer_state = inner_state_lock.lock().unwrap();
316307

317-
let user_channel_id =
308+
let mut jit_channel =
318309
peer_state.pending_buy_requests.remove(&request_id).ok_or(LightningError {
319310
err: format!(
320311
"Received buy response for an unknown request: {:?}",
@@ -323,21 +314,9 @@ where
323314
action: ErrorAction::IgnoreAndLog(Level::Info),
324315
})?;
325316

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-
337317
// Reject the buy response if we disallow client_trusts_lsp and the LSP requires
338318
// it.
339319
if !self.config.client_trusts_lsp && result.client_trusts_lsp {
340-
peer_state.inbound_channels_by_id.remove(&user_channel_id);
341320
return Err(LightningError {
342321
err: format!(
343322
"Aborting JIT channel flow as the LSP requires 'client_trusts_lsp' mode, which we disallow"
@@ -346,22 +325,20 @@ where
346325
});
347326
}
348327

349-
if let Err(e) = jit_channel.invoice_params_received(
328+
// Update the channel state
329+
jit_channel.invoice_params_received(
350330
result.client_trusts_lsp,
351331
result.intercept_scid.clone(),
352-
) {
353-
peer_state.inbound_channels_by_id.remove(&user_channel_id);
354-
return Err(e);
355-
}
332+
)?;
356333

357334
if let Ok(intercept_scid) = result.intercept_scid.to_scid() {
358335
self.pending_events.enqueue(Event::LSPS2Client(
359336
LSPS2ClientEvent::InvoiceParametersReady {
337+
request_id,
360338
counterparty_node_id: *counterparty_node_id,
361339
intercept_scid,
362340
cltv_expiry_delta: result.lsp_cltv_expiry_delta,
363341
payment_size_msat: jit_channel.payment_size_msat,
364-
user_channel_id: jit_channel.user_channel_id,
365342
},
366343
));
367344
} else {
@@ -395,21 +372,11 @@ where
395372
Some(inner_state_lock) => {
396373
let mut peer_state = inner_state_lock.lock().unwrap();
397374

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+
})?;
403379

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-
)?;
413380
Ok(())
414381
}
415382
None => {

src/lsps2/event.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ pub enum LSPS2ClientEvent {
4848
/// When the invoice is paid, the LSP will open a channel with the previously agreed upon
4949
/// parameters to you.
5050
InvoiceParametersReady {
51-
/// A user-specified identifier used to track the channel open.
52-
///
53-
/// This is the same value as previously passed to
51+
/// The identifier of the issued LSPS2 `buy` request, as returned by
5452
/// [`LSPS2ClientHandler::select_opening_params`].
5553
///
54+
/// This can be used to track which request this event corresponds to.
55+
///
5656
/// [`LSPS2ClientHandler::select_opening_params`]: crate::lsps2::client::LSPS2ClientHandler::select_opening_params
57-
user_channel_id: u128,
57+
request_id: RequestId,
5858
/// The node id of the LSP.
5959
counterparty_node_id: PublicKey,
6060
/// The intercept short channel id to use in the route hint.

0 commit comments

Comments
 (0)