Skip to content

Commit 0c7ad20

Browse files
committed
Use PeerState::{get_order, has_active_requests} instead of map
Previously, we'd directly access the internal `outbound_` map of `PeerState`. Here we refactor the code to avoid this. Note this also highlighted a bug in that we currently don't actually update/persist the order state in `update_order_state`. We don't fix this here, but just improve isolation for now, as all state update behavior will be reworked later.
1 parent 9642ba3 commit 0c7ad20

File tree

2 files changed

+37
-33
lines changed

2 files changed

+37
-33
lines changed

lightning-liquidity/src/lsps1/peer_state.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::prelude::HashMap;
1616

1717
#[derive(Default)]
1818
pub(super) struct PeerState {
19-
pub(super) outbound_channels_by_order_id: HashMap<LSPS1OrderId, OutboundCRChannel>,
19+
outbound_channels_by_order_id: HashMap<LSPS1OrderId, OutboundCRChannel>,
2020
pub(super) pending_requests: HashMap<LSPSRequestId, LSPS1Request>,
2121
}
2222

@@ -26,25 +26,32 @@ impl PeerState {
2626
created_at: LSPSDateTime, payment_details: LSPS1PaymentInfo,
2727
) {
2828
let channel = OutboundCRChannel::new(order_params, created_at, payment_details);
29-
3029
self.outbound_channels_by_order_id.insert(order_id, channel);
3130
}
31+
32+
pub(super) fn get_order<'a>(&'a self, order_id: &LSPS1OrderId) -> Option<&'a ChannelOrder> {
33+
self.outbound_channels_by_order_id.get(order_id).map(|channel| &channel.order)
34+
}
35+
36+
pub(super) fn has_active_requests(&self) -> bool {
37+
!self.outbound_channels_by_order_id.is_empty()
38+
}
3239
}
3340

34-
pub(super) struct OutboundLSPS1Config {
35-
pub(super) order: LSPS1OrderParams,
41+
pub(super) struct ChannelOrder {
42+
pub(super) order_params: LSPS1OrderParams,
3643
pub(super) created_at: LSPSDateTime,
37-
pub(super) payment: LSPS1PaymentInfo,
44+
pub(super) payment_details: LSPS1PaymentInfo,
3845
}
3946

40-
pub(super) struct OutboundCRChannel {
41-
pub(super) config: OutboundLSPS1Config,
47+
struct OutboundCRChannel {
48+
order: ChannelOrder,
4249
}
4350

4451
impl OutboundCRChannel {
45-
pub(super) fn new(
46-
order: LSPS1OrderParams, created_at: LSPSDateTime, payment: LSPS1PaymentInfo,
52+
fn new(
53+
order_params: LSPS1OrderParams, created_at: LSPSDateTime, payment_details: LSPS1PaymentInfo,
4754
) -> Self {
48-
Self { config: OutboundLSPS1Config { order, created_at, payment } }
55+
Self { order: ChannelOrder { order_params, created_at, payment_details } }
4956
}
5057
}

lightning-liquidity/src/lsps1/service.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ where
101101
let outer_state_lock = self.per_peer_state.read().unwrap();
102102
outer_state_lock.get(counterparty_node_id).map_or(false, |inner| {
103103
let peer_state = inner.lock().unwrap();
104-
!peer_state.outbound_channels_by_order_id.is_empty()
104+
peer_state.has_active_requests()
105105
})
106106
}
107107

@@ -275,29 +275,26 @@ where
275275

276276
match outer_state_lock.get(&counterparty_node_id) {
277277
Some(inner_state_lock) => {
278-
let mut peer_state_lock = inner_state_lock.lock().unwrap();
279-
280-
if let Some(outbound_channel) =
281-
peer_state_lock.outbound_channels_by_order_id.get_mut(&order_id)
282-
{
283-
let config = &outbound_channel.config;
284-
285-
let response = LSPS1Response::GetOrder(LSPS1CreateOrderResponse {
286-
order_id,
287-
order: config.order.clone(),
288-
order_state,
289-
created_at: config.created_at.clone(),
290-
payment: config.payment.clone(),
291-
channel,
292-
});
293-
let msg = LSPS1Message::Response(request_id, response).into();
294-
message_queue_notifier.enqueue(&counterparty_node_id, msg);
295-
Ok(())
296-
} else {
297-
Err(APIError::APIMisuseError {
278+
let peer_state_lock = inner_state_lock.lock().unwrap();
279+
let order =
280+
peer_state_lock.get_order(&order_id).ok_or(APIError::APIMisuseError {
298281
err: format!("Channel with order_id {} not found", order_id.0),
299-
})
300-
}
282+
})?;
283+
284+
// FIXME: we need to actually remember the order state (and eventually persist it)
285+
// here.
286+
287+
let response = LSPS1Response::GetOrder(LSPS1CreateOrderResponse {
288+
order_id,
289+
order: order.order_params.clone(),
290+
order_state,
291+
created_at: order.created_at.clone(),
292+
payment: order.payment_details.clone(),
293+
channel,
294+
});
295+
let msg = LSPS1Message::Response(request_id, response).into();
296+
message_queue_notifier.enqueue(&counterparty_node_id, msg);
297+
Ok(())
301298
},
302299
None => Err(APIError::APIMisuseError {
303300
err: format!("No existing state with counterparty {}", counterparty_node_id),

0 commit comments

Comments
 (0)