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

Commit 61746a3

Browse files
allow user selected user_channel_id
1 parent 0f2c520 commit 61746a3

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/lsps2/event.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ pub enum LSPS2ServiceEvent {
126126
amt_to_forward_msat: u64,
127127
/// The fee earned for opening the channel.
128128
opening_fee_msat: u64,
129-
/// An internal id used to track channel open.
129+
/// A user specified id used to track channel open.
130130
user_channel_id: u128,
131+
/// The short channel id to use in the route hint.
132+
scid: u64,
131133
},
132134
}

src/lsps2/service.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use lightning::util::logger::Level;
2626

2727
use bitcoin::secp256k1::PublicKey;
2828

29-
use core::convert::TryInto;
3029
use core::ops::Deref;
3130

3231
use crate::lsps2::msgs::{
@@ -180,17 +179,19 @@ struct OutboundJITChannel {
180179
scid: u64,
181180
cltv_expiry_delta: u32,
182181
client_trusts_lsp: bool,
182+
user_channel_id: u128,
183183
}
184184

185185
impl OutboundJITChannel {
186186
fn new(
187187
scid: u64, cltv_expiry_delta: u32, client_trusts_lsp: bool, payment_size_msat: Option<u64>,
188-
opening_fee_params: OpeningFeeParams,
188+
opening_fee_params: OpeningFeeParams, user_channel_id: u128,
189189
) -> Self {
190190
Self {
191191
scid,
192192
cltv_expiry_delta,
193193
client_trusts_lsp,
194+
user_channel_id,
194195
state: OutboundJITChannelState::new(payment_size_msat, opening_fee_params),
195196
}
196197
}
@@ -240,14 +241,16 @@ impl OutboundJITChannel {
240241

241242
struct PeerState {
242243
outbound_channels_by_scid: HashMap<u64, OutboundJITChannel>,
244+
scid_by_user_channel_id: HashMap<u128, u64>,
243245
pending_requests: HashMap<RequestId, LSPS2Request>,
244246
}
245247

246248
impl PeerState {
247249
fn new() -> Self {
248250
let outbound_channels_by_scid = HashMap::new();
249251
let pending_requests = HashMap::new();
250-
Self { outbound_channels_by_scid, pending_requests }
252+
let scid_by_user_channel_id = HashMap::new();
253+
Self { outbound_channels_by_scid, pending_requests, scid_by_user_channel_id }
251254
}
252255

253256
fn insert_outbound_channel(&mut self, scid: u64, channel: OutboundJITChannel) {
@@ -382,7 +385,7 @@ where
382385
/// [`LSPS2ServiceEvent::BuyRequest`]: crate::lsps2::event::LSPS2ServiceEvent::BuyRequest
383386
pub fn invoice_parameters_generated(
384387
&self, counterparty_node_id: &PublicKey, request_id: RequestId, scid: u64,
385-
cltv_expiry_delta: u32, client_trusts_lsp: bool,
388+
cltv_expiry_delta: u32, client_trusts_lsp: bool, user_channel_id: u128,
386389
) -> Result<(), APIError> {
387390
let outer_state_lock = self.per_peer_state.read().unwrap();
388391

@@ -403,8 +406,10 @@ where
403406
client_trusts_lsp,
404407
buy_request.payment_size_msat,
405408
buy_request.opening_fee_params,
409+
user_channel_id,
406410
);
407411

412+
peer_state.scid_by_user_channel_id.insert(user_channel_id, scid);
408413
peer_state.insert_outbound_channel(scid, outbound_jit_channel);
409414

410415
self.enqueue_response(
@@ -460,7 +465,8 @@ where
460465
their_network_key: counterparty_node_id.clone(),
461466
amt_to_forward_msat,
462467
opening_fee_msat,
463-
user_channel_id: scid as u128,
468+
user_channel_id: jit_channel.user_channel_id,
469+
scid,
464470
},
465471
));
466472
}
@@ -496,11 +502,13 @@ where
496502
pub fn channel_ready(
497503
&self, user_channel_id: u128, channel_id: &ChannelId, counterparty_node_id: &PublicKey,
498504
) -> Result<(), APIError> {
499-
if let Ok(scid) = user_channel_id.try_into() {
500-
let outer_state_lock = self.per_peer_state.read().unwrap();
501-
match outer_state_lock.get(counterparty_node_id) {
502-
Some(inner_state_lock) => {
503-
let mut peer_state = inner_state_lock.lock().unwrap();
505+
let outer_state_lock = self.per_peer_state.read().unwrap();
506+
match outer_state_lock.get(counterparty_node_id) {
507+
Some(inner_state_lock) => {
508+
let mut peer_state = inner_state_lock.lock().unwrap();
509+
if let Some(scid) =
510+
peer_state.scid_by_user_channel_id.get(&user_channel_id).copied()
511+
{
504512
if let Some(jit_channel) = peer_state.outbound_channels_by_scid.get_mut(&scid) {
505513
match jit_channel.channel_ready() {
506514
Ok((htlcs, total_amt_to_forward_msat)) => {
@@ -537,13 +545,20 @@ where
537545
),
538546
});
539547
}
540-
}
541-
None => {
548+
} else {
542549
return Err(APIError::APIMisuseError {
543-
err: format!("No counterparty state for: {}", counterparty_node_id),
550+
err: format!(
551+
"Could not find a channel with that user_channel_id {}",
552+
user_channel_id
553+
),
544554
});
545555
}
546556
}
557+
None => {
558+
return Err(APIError::APIMisuseError {
559+
err: format!("No counterparty state for: {}", counterparty_node_id),
560+
});
561+
}
547562
}
548563

549564
Ok(())

0 commit comments

Comments
 (0)