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

Commit 54158ca

Browse files
authored
LSPS1: Fix API to maintain consistency (#93)
* API fix: Rename LSPS1 functions * Rename id field to channel_id
1 parent ce404cc commit 54158ca

File tree

3 files changed

+95
-96
lines changed

3 files changed

+95
-96
lines changed

src/lsps1/client.rs

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ enum InboundRequestState {
5656
OptionsSupport { options_supported: OptionsSupported },
5757
OrderRequested { order: OrderParams },
5858
PendingPayment { order_id: OrderId },
59-
AwaitingConfirmation { id: u128, order_id: OrderId },
59+
AwaitingConfirmation { user_channel_id: u128, order_id: OrderId },
6060
}
6161

6262
impl InboundRequestState {
@@ -112,11 +112,11 @@ impl InboundRequestState {
112112
}
113113
}
114114

115-
fn pay_for_channel(&self, channel_id: u128) -> Result<Self, ChannelStateError> {
115+
fn pay_for_channel(&self, user_channel_id: u128) -> Result<Self, ChannelStateError> {
116116
match self {
117117
InboundRequestState::PendingPayment { order_id } => {
118118
Ok(InboundRequestState::AwaitingConfirmation {
119-
id: channel_id,
119+
user_channel_id,
120120
order_id: order_id.clone(),
121121
})
122122
}
@@ -129,13 +129,13 @@ impl InboundRequestState {
129129
}
130130

131131
struct InboundCRChannel {
132-
id: u128,
132+
user_channel_id: u128,
133133
state: InboundRequestState,
134134
}
135135

136136
impl InboundCRChannel {
137-
fn new(id: u128) -> Self {
138-
Self { id, state: InboundRequestState::InfoRequested }
137+
fn new(user_channel_id: u128) -> Self {
138+
Self { user_channel_id, state: InboundRequestState::InfoRequested }
139139
}
140140

141141
fn info_received(&mut self, options: OptionsSupported) -> Result<(), LightningError> {
@@ -171,8 +171,8 @@ impl InboundCRChannel {
171171
Ok(())
172172
}
173173

174-
fn pay_for_channel(&mut self, channel_id: u128) -> Result<(), LightningError> {
175-
self.state = self.state.pay_for_channel(channel_id)?;
174+
fn pay_for_channel(&mut self, user_channel_id: u128) -> Result<(), LightningError> {
175+
self.state = self.state.pay_for_channel(user_channel_id)?;
176176
Ok(())
177177
}
178178
}
@@ -185,16 +185,16 @@ struct PeerState {
185185
}
186186

187187
impl PeerState {
188-
fn insert_inbound_channel(&mut self, id: u128, channel: InboundCRChannel) {
189-
self.inbound_channels_by_id.insert(id, channel);
188+
fn insert_inbound_channel(&mut self, user_channel_id: u128, channel: InboundCRChannel) {
189+
self.inbound_channels_by_id.insert(user_channel_id, channel);
190190
}
191191

192-
fn insert_request(&mut self, request_id: RequestId, channel_id: u128) {
193-
self.request_to_cid.insert(request_id, channel_id);
192+
fn insert_request(&mut self, request_id: RequestId, user_channel_id: u128) {
193+
self.request_to_cid.insert(request_id, user_channel_id);
194194
}
195195

196-
fn remove_inbound_channel(&mut self, id: u128) {
197-
self.inbound_channels_by_id.remove(&id);
196+
fn remove_inbound_channel(&mut self, user_channel_id: u128) {
197+
self.inbound_channels_by_id.remove(&user_channel_id);
198198
}
199199
}
200200

@@ -241,19 +241,19 @@ where
241241
///
242242
/// `counterparty_node_id` is the node_id of the LSP you would like to use.
243243
///
244-
/// 'channel_id' is the id used to uniquely identify the channel with counterparty node.
245-
pub fn request_for_info(&self, counterparty_node_id: PublicKey, channel_id: u128) {
246-
let channel = InboundCRChannel::new(channel_id);
244+
/// `user_channel_id` is the id used to uniquely identify the channel with counterparty node.
245+
pub fn send_get_info_request(&self, counterparty_node_id: PublicKey, user_channel_id: u128) {
246+
let channel = InboundCRChannel::new(user_channel_id);
247247

248248
let mut outer_state_lock = self.per_peer_state.write().unwrap();
249249
let inner_state_lock = outer_state_lock
250250
.entry(counterparty_node_id)
251251
.or_insert(Mutex::new(PeerState::default()));
252252
let mut peer_state_lock = inner_state_lock.lock().unwrap();
253-
peer_state_lock.insert_inbound_channel(channel_id, channel);
253+
peer_state_lock.insert_inbound_channel(user_channel_id, channel);
254254

255255
let request_id = crate::utils::generate_request_id(&self.entropy_source);
256-
peer_state_lock.insert_request(request_id.clone(), channel_id);
256+
peer_state_lock.insert_request(request_id.clone(), user_channel_id);
257257

258258
self.pending_messages.enqueue(
259259
&counterparty_node_id,
@@ -270,7 +270,7 @@ where
270270
Some(inner_state_lock) => {
271271
let mut peer_state_lock = inner_state_lock.lock().unwrap();
272272

273-
let channel_id =
273+
let user_channel_id =
274274
peer_state_lock.request_to_cid.remove(&request_id).ok_or(LightningError {
275275
err: format!(
276276
"Received get_info response for an unknown request: {:?}",
@@ -281,26 +281,25 @@ where
281281

282282
let inbound_channel = peer_state_lock
283283
.inbound_channels_by_id
284-
.get_mut(&channel_id)
284+
.get_mut(&user_channel_id)
285285
.ok_or(LightningError {
286-
err: format!(
287-
"Received get_info response for an unknown channel: {:?}",
288-
channel_id
289-
),
290-
action: ErrorAction::IgnoreAndLog(Level::Info),
291-
})?;
286+
err: format!(
287+
"Received get_info response for an unknown channel: {:?}",
288+
user_channel_id
289+
),
290+
action: ErrorAction::IgnoreAndLog(Level::Info),
291+
})?;
292292

293293
match inbound_channel.info_received(result.options.clone()) {
294294
Ok(()) => (),
295295
Err(e) => {
296-
peer_state_lock.remove_inbound_channel(channel_id);
296+
peer_state_lock.remove_inbound_channel(user_channel_id);
297297
return Err(e);
298298
}
299299
};
300300

301301
self.pending_events.enqueue(Event::LSPS1Client(LSPS1ClientEvent::GetInfoResponse {
302-
id: channel_id,
303-
request_id,
302+
user_channel_id,
304303
counterparty_node_id: *counterparty_node_id,
305304
website: result.website,
306305
options_supported: result.options,
@@ -326,7 +325,7 @@ where
326325
///
327326
/// [`LSPS1ClientEvent::GetInfoResponse`]: crate::lsps1::event::LSPS1ClientEvent::GetInfoResponse
328327
pub fn place_order(
329-
&self, channel_id: u128, counterparty_node_id: &PublicKey, order: OrderParams,
328+
&self, user_channel_id: u128, counterparty_node_id: &PublicKey, order: OrderParams,
330329
) -> Result<(), APIError> {
331330
let outer_state_lock = self.per_peer_state.write().unwrap();
332331

@@ -336,21 +335,21 @@ where
336335

337336
let inbound_channel = peer_state_lock
338337
.inbound_channels_by_id
339-
.get_mut(&channel_id)
338+
.get_mut(&user_channel_id)
340339
.ok_or(APIError::APIMisuseError {
341-
err: format!("Channel with id {} not found", channel_id),
342-
})?;
340+
err: format!("Channel with user_channel_id {} not found", user_channel_id),
341+
})?;
343342

344343
match inbound_channel.order_requested(order.clone()) {
345344
Ok(()) => (),
346345
Err(e) => {
347-
peer_state_lock.remove_inbound_channel(channel_id);
346+
peer_state_lock.remove_inbound_channel(user_channel_id);
348347
return Err(APIError::APIMisuseError { err: e.err });
349348
}
350349
};
351350

352351
let request_id = crate::utils::generate_request_id(&self.entropy_source);
353-
peer_state_lock.insert_request(request_id.clone(), channel_id);
352+
peer_state_lock.insert_request(request_id.clone(), user_channel_id);
354353

355354
self.pending_messages.enqueue(
356355
counterparty_node_id,
@@ -379,7 +378,7 @@ where
379378
Some(inner_state_lock) => {
380379
let mut peer_state_lock = inner_state_lock.lock().unwrap();
381380

382-
let channel_id =
381+
let user_channel_id =
383382
peer_state_lock.request_to_cid.remove(&request_id).ok_or(LightningError {
384383
err: format!(
385384
"Received create_order response for an unknown request: {:?}",
@@ -390,19 +389,19 @@ where
390389

391390
let inbound_channel = peer_state_lock
392391
.inbound_channels_by_id
393-
.get_mut(&channel_id)
392+
.get_mut(&user_channel_id)
394393
.ok_or(LightningError {
395-
err: format!(
396-
"Received create_order response for an unknown channel: {:?}",
397-
channel_id
398-
),
399-
action: ErrorAction::IgnoreAndLog(Level::Info),
400-
})?;
394+
err: format!(
395+
"Received create_order response for an unknown channel: {:?}",
396+
user_channel_id
397+
),
398+
action: ErrorAction::IgnoreAndLog(Level::Info),
399+
})?;
401400

402401
if let Err(e) =
403402
inbound_channel.order_received(&response.order, response.order_id.clone())
404403
{
405-
peer_state_lock.remove_inbound_channel(channel_id);
404+
peer_state_lock.remove_inbound_channel(user_channel_id);
406405
return Err(e);
407406
}
408407

@@ -414,15 +413,15 @@ where
414413
{
415414
self.pending_events.enqueue(Event::LSPS1Client(
416415
LSPS1ClientEvent::DisplayOrder {
417-
id: channel_id,
416+
user_channel_id,
418417
counterparty_node_id: *counterparty_node_id,
419418
order: response.order,
420419
payment: response.payment,
421420
channel: response.channel,
422421
},
423422
));
424423
} else {
425-
peer_state_lock.remove_inbound_channel(channel_id);
424+
peer_state_lock.remove_inbound_channel(user_channel_id);
426425
return Err(LightningError {
427426
err: format!("Fees are too high : {:?}", total_fees),
428427
action: ErrorAction::IgnoreAndLog(Level::Info),
@@ -451,7 +450,7 @@ where
451450
Some(inner_state_lock) => {
452451
let mut peer_state_lock = inner_state_lock.lock().unwrap();
453452

454-
let channel_id =
453+
let user_channel_id =
455454
peer_state_lock.request_to_cid.remove(&request_id).ok_or(LightningError {
456455
err: format!(
457456
"Received create order error for an unknown request: {:?}",
@@ -462,14 +461,14 @@ where
462461

463462
let inbound_channel = peer_state_lock
464463
.inbound_channels_by_id
465-
.get_mut(&channel_id)
464+
.get_mut(&user_channel_id)
466465
.ok_or(LightningError {
467-
err: format!(
468-
"Received create order error for an unknown channel: {:?}",
469-
channel_id
470-
),
471-
action: ErrorAction::IgnoreAndLog(Level::Info),
472-
})?;
466+
err: format!(
467+
"Received create order error for an unknown channel: {:?}",
468+
user_channel_id
469+
),
470+
action: ErrorAction::IgnoreAndLog(Level::Info),
471+
})?;
473472
Ok(())
474473
}
475474
None => {
@@ -484,23 +483,23 @@ where
484483
///
485484
/// [`LSPS1ClientEvent::DisplayOrder`]: crate::lsps1::event::LSPS1ClientEvent::DisplayOrder
486485
pub fn check_order_status(
487-
&self, counterparty_node_id: &PublicKey, order_id: OrderId, channel_id: u128,
486+
&self, counterparty_node_id: &PublicKey, order_id: OrderId, user_channel_id: u128,
488487
) -> Result<(), APIError> {
489488
let outer_state_lock = self.per_peer_state.write().unwrap();
490489
match outer_state_lock.get(&counterparty_node_id) {
491490
Some(inner_state_lock) => {
492491
let mut peer_state_lock = inner_state_lock.lock().unwrap();
493492

494493
if let Some(inbound_channel) =
495-
peer_state_lock.inbound_channels_by_id.get_mut(&channel_id)
494+
peer_state_lock.inbound_channels_by_id.get_mut(&user_channel_id)
496495
{
497-
if let Err(e) = inbound_channel.pay_for_channel(channel_id) {
498-
peer_state_lock.remove_inbound_channel(channel_id);
496+
if let Err(e) = inbound_channel.pay_for_channel(user_channel_id) {
497+
peer_state_lock.remove_inbound_channel(user_channel_id);
499498
return Err(APIError::APIMisuseError { err: e.err });
500499
}
501500

502501
let request_id = crate::utils::generate_request_id(&self.entropy_source);
503-
peer_state_lock.insert_request(request_id.clone(), channel_id);
502+
peer_state_lock.insert_request(request_id.clone(), user_channel_id);
504503

505504
self.pending_messages.enqueue(
506505
counterparty_node_id,
@@ -512,7 +511,7 @@ where
512511
);
513512
} else {
514513
return Err(APIError::APIMisuseError {
515-
err: format!("Channel with id {} not found", channel_id),
514+
err: format!("Channel with user_channel_id {} not found", user_channel_id),
516515
});
517516
}
518517
}
@@ -534,7 +533,7 @@ where
534533
Some(inner_state_lock) => {
535534
let mut peer_state_lock = inner_state_lock.lock().unwrap();
536535

537-
let channel_id =
536+
let user_channel_id =
538537
peer_state_lock.request_to_cid.remove(&request_id).ok_or(LightningError {
539538
err: format!(
540539
"Received get_order response for an unknown request: {:?}",
@@ -545,14 +544,14 @@ where
545544

546545
let inbound_channel = peer_state_lock
547546
.inbound_channels_by_id
548-
.get_mut(&channel_id)
547+
.get_mut(&user_channel_id)
549548
.ok_or(LightningError {
550-
err: format!(
551-
"Received get_order response for an unknown channel: {:?}",
552-
channel_id
553-
),
554-
action: ErrorAction::IgnoreAndLog(Level::Info),
555-
})?;
549+
err: format!(
550+
"Received get_order response for an unknown channel: {:?}",
551+
user_channel_id
552+
),
553+
action: ErrorAction::IgnoreAndLog(Level::Info),
554+
})?;
556555
}
557556
None => {
558557
return Err(LightningError {
@@ -576,7 +575,7 @@ where
576575
Some(inner_state_lock) => {
577576
let mut peer_state_lock = inner_state_lock.lock().unwrap();
578577

579-
let channel_id =
578+
let user_channel_id =
580579
peer_state_lock.request_to_cid.remove(&request_id).ok_or(LightningError {
581580
err: format!(
582581
"Received get_order error for an unknown request: {:?}",
@@ -587,11 +586,11 @@ where
587586

588587
let _inbound_channel = peer_state_lock
589588
.inbound_channels_by_id
590-
.get_mut(&channel_id)
589+
.get_mut(&user_channel_id)
591590
.ok_or(LightningError {
592591
err: format!(
593592
"Received get_order error for an unknown channel: {:?}",
594-
channel_id
593+
user_channel_id
595594
),
596595
action: ErrorAction::IgnoreAndLog(Level::Info),
597596
})?;

0 commit comments

Comments
 (0)