Skip to content

Commit 38b0372

Browse files
committed
Respond to GetOrder requests from our saved state
Previously, we'd use an event to have the user check the order status and then call back in. As we already track the order status, we here change that to a model where we respond immediately based on our state and have the user/LSP update that state whenever it detects a change (e.g., a received payment, reorg, etc.). In the next commmit we will add/modify the corresponding API methods to do so.
1 parent 80e8a05 commit 38b0372

File tree

5 files changed

+54
-90
lines changed

5 files changed

+54
-90
lines changed

lightning-liquidity/src/lsps1/event.rs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,26 +165,6 @@ pub enum LSPS1ServiceEvent {
165165
/// The order requested by the client.
166166
order: LSPS1OrderParams,
167167
},
168-
/// A request from client to check the status of the payment.
169-
///
170-
/// An event to poll for checking payment status either onchain or lightning.
171-
///
172-
/// You must call [`LSPS1ServiceHandler::update_order_status`] to update the client
173-
/// regarding the status of the payment and order.
174-
///
175-
/// **Note: ** This event will *not* be persisted across restarts.
176-
///
177-
/// [`LSPS1ServiceHandler::update_order_status`]: crate::lsps1::service::LSPS1ServiceHandler::update_order_status
178-
CheckPaymentConfirmation {
179-
/// An identifier that must be passed to [`LSPS1ServiceHandler::update_order_status`].
180-
///
181-
/// [`LSPS1ServiceHandler::update_order_status`]: crate::lsps1::service::LSPS1ServiceHandler::update_order_status
182-
request_id: LSPSRequestId,
183-
/// The node id of the client making the information request.
184-
counterparty_node_id: PublicKey,
185-
/// The order id of order with pending payment.
186-
order_id: LSPS1OrderId,
187-
},
188168
/// If error is encountered, refund the amount if paid by the client.
189169
///
190170
/// **Note: ** This event will *not* be persisted across restarts.

lightning-liquidity/src/lsps1/msgs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub(crate) const LSPS1_GET_ORDER_METHOD_NAME: &str = "lsps1.get_order";
3232
pub(crate) const _LSPS1_CREATE_ORDER_REQUEST_INVALID_PARAMS_ERROR_CODE: i32 = -32602;
3333
#[cfg(lsps1_service)]
3434
pub(crate) const LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE: i32 = 100;
35+
#[cfg(lsps1_service)]
36+
pub(crate) const LSPS1_GET_ORDER_REQUEST_ORDER_NOT_FOUND_ERROR_CODE: i32 = 101;
3537

3638
/// The identifier of an order.
3739
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize, Hash)]

lightning-liquidity/src/lsps1/peer_state.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,27 @@ impl PeerState {
4343
channel_order
4444
}
4545

46+
pub(super) fn get_order<'a>(
47+
&'a self, order_id: &LSPS1OrderId,
48+
) -> Result<&'a ChannelOrder, PeerStateError> {
49+
let order = self
50+
.outbound_channels_by_order_id
51+
.get(order_id)
52+
.ok_or(PeerStateError::UnknownOrderId)?;
53+
Ok(order)
54+
}
55+
4656
pub(super) fn update_order<'a>(
4757
&'a mut self, order_id: &LSPS1OrderId, order_state: LSPS1OrderState,
4858
channel_details: Option<LSPS1ChannelInfo>,
49-
) -> Result<&'a ChannelOrder, PeerStateError> {
59+
) -> Result<(), PeerStateError> {
5060
let order = self
5161
.outbound_channels_by_order_id
5262
.get_mut(order_id)
5363
.ok_or(PeerStateError::UnknownOrderId)?;
5464
order.order_state = order_state;
5565
order.channel_details = channel_details;
56-
Ok(order)
66+
Ok(())
5767
}
5868

5969
pub(super) fn register_request(

lightning-liquidity/src/lsps1/service.rs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use super::msgs::{
1919
LSPS1GetOrderRequest, LSPS1Message, LSPS1Options, LSPS1OrderId, LSPS1OrderParams,
2020
LSPS1OrderState, LSPS1PaymentInfo, LSPS1Request, LSPS1Response,
2121
LSPS1_CREATE_ORDER_REQUEST_ORDER_MISMATCH_ERROR_CODE,
22+
LSPS1_GET_ORDER_REQUEST_ORDER_NOT_FOUND_ERROR_CODE,
2223
};
2324
use super::peer_state::PeerState;
2425
use crate::message_queue::MessageQueue;
@@ -245,78 +246,75 @@ where
245246
&self, request_id: LSPSRequestId, counterparty_node_id: &PublicKey,
246247
params: LSPS1GetOrderRequest,
247248
) -> Result<(), LightningError> {
248-
let event_queue_notifier = self.pending_events.notifier();
249+
let mut message_queue_notifier = self.pending_messages.notifier();
249250
let outer_state_lock = self.per_peer_state.read().unwrap();
250251
match outer_state_lock.get(counterparty_node_id) {
251252
Some(inner_state_lock) => {
252-
let mut peer_state_lock = inner_state_lock.lock().unwrap();
253-
254-
let request = LSPS1Request::GetOrder(params.clone());
255-
peer_state_lock.register_request(request_id.clone(), request).map_err(|e| {
253+
let peer_state_lock = inner_state_lock.lock().unwrap();
254+
255+
let order = peer_state_lock.get_order(&params.order_id).map_err(|e| {
256+
let response = LSPS1Response::GetOrderError(LSPSResponseError {
257+
code: LSPS1_GET_ORDER_REQUEST_ORDER_NOT_FOUND_ERROR_CODE,
258+
message: format!("Order with the requested order_id has not been found."),
259+
data: None,
260+
});
261+
let msg = LSPS1Message::Response(request_id.clone(), response).into();
262+
message_queue_notifier.enqueue(counterparty_node_id, msg);
256263
let err = format!("Failed to handle request due to: {}", e);
257264
let action = ErrorAction::IgnoreAndLog(Level::Error);
258265
LightningError { err, action }
259266
})?;
260267

261-
event_queue_notifier.enqueue(LSPS1ServiceEvent::CheckPaymentConfirmation {
262-
request_id,
263-
counterparty_node_id: *counterparty_node_id,
268+
let response = LSPS1Response::GetOrder(LSPS1CreateOrderResponse {
264269
order_id: params.order_id,
270+
order: order.order_params.clone(),
271+
order_state: order.order_state.clone(),
272+
created_at: order.created_at.clone(),
273+
payment: order.payment_details.clone(),
274+
channel: order.channel_details.clone(),
265275
});
276+
let msg = LSPS1Message::Response(request_id, response).into();
277+
message_queue_notifier.enqueue(&counterparty_node_id, msg);
278+
Ok(())
266279
},
267280
None => {
268-
return Err(LightningError {
269-
err: format!("Received error response for a create order request from an unknown counterparty ({:?})", counterparty_node_id),
270-
action: ErrorAction::IgnoreAndLog(Level::Info),
281+
let response = LSPS1Response::GetOrderError(LSPSResponseError {
282+
code: LSPS1_GET_ORDER_REQUEST_ORDER_NOT_FOUND_ERROR_CODE,
283+
message: format!("Order with the requested order_id has not been found."),
284+
data: None,
271285
});
286+
let msg = LSPS1Message::Response(request_id, response).into();
287+
message_queue_notifier.enqueue(counterparty_node_id, msg);
288+
Err(LightningError {
289+
err: format!(
290+
"Received get_order request from an unknown counterparty ({:?})",
291+
counterparty_node_id
292+
),
293+
action: ErrorAction::IgnoreAndLog(Level::Info),
294+
})
272295
},
273296
}
274-
275-
Ok(())
276297
}
277298

278299
/// Used by LSP to give details to client regarding the status of channel opening.
279-
/// Called to respond to client's `GetOrder` request.
280300
///
281301
/// The LSP continously polls for checking payment confirmation on-chain or Lightning
282302
/// and then responds to client request.
283-
///
284-
/// Should be called in response to receiving a [`LSPS1ServiceEvent::CheckPaymentConfirmation`] event.
285-
///
286-
/// [`LSPS1ServiceEvent::CheckPaymentConfirmation`]: crate::lsps1::event::LSPS1ServiceEvent::CheckPaymentConfirmation
287303
pub fn update_order_status(
288-
&self, request_id: LSPSRequestId, counterparty_node_id: PublicKey, order_id: LSPS1OrderId,
304+
&self, counterparty_node_id: PublicKey, order_id: LSPS1OrderId,
289305
order_state: LSPS1OrderState, channel_details: Option<LSPS1ChannelInfo>,
290306
) -> Result<(), APIError> {
291-
let mut message_queue_notifier = self.pending_messages.notifier();
292-
293307
let outer_state_lock = self.per_peer_state.read().unwrap();
294308

295309
match outer_state_lock.get(&counterparty_node_id) {
296310
Some(inner_state_lock) => {
297311
let mut peer_state_lock = inner_state_lock.lock().unwrap();
298-
let order = peer_state_lock
299-
.update_order(&order_id, order_state, channel_details)
300-
.map_err(|e| APIError::APIMisuseError {
301-
err: format!("Failed to update order: {:?}", e),
302-
})?;
303-
304-
peer_state_lock.remove_request(&request_id).map_err(|e| {
305-
debug_assert!(false, "Failed to send response due to: {}", e);
306-
let err = format!("Failed to send response due to: {}", e);
307-
APIError::APIMisuseError { err }
308-
})?;
312+
peer_state_lock.update_order(&order_id, order_state, channel_details).map_err(
313+
|e| APIError::APIMisuseError {
314+
err: format!("Failed to update order: {:?}", e),
315+
},
316+
)?;
309317

310-
let response = LSPS1Response::GetOrder(LSPS1CreateOrderResponse {
311-
order_id,
312-
order: order.order_params.clone(),
313-
order_state: order.order_state.clone(),
314-
created_at: order.created_at.clone(),
315-
payment: order.payment_details.clone(),
316-
channel: order.channel_details.clone(),
317-
});
318-
let msg = LSPS1Message::Response(request_id, response).into();
319-
message_queue_notifier.enqueue(&counterparty_node_id, msg);
320318
Ok(())
321319
},
322320
None => Err(APIError::APIMisuseError {

lightning-liquidity/tests/lsps1_integration_tests.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use lightning_liquidity::events::LiquidityEvent;
1010
use lightning_liquidity::lsps1::client::LSPS1ClientConfig;
1111
use lightning_liquidity::lsps1::event::LSPS1ClientEvent;
1212
use lightning_liquidity::lsps1::event::LSPS1ServiceEvent;
13-
use lightning_liquidity::lsps1::msgs::LSPS1OrderState;
1413
use lightning_liquidity::lsps1::msgs::{
1514
LSPS1OnchainPaymentInfo, LSPS1Options, LSPS1OrderParams, LSPS1PaymentInfo,
1615
};
@@ -214,31 +213,6 @@ fn lsps1_happy_path() {
214213
.handle_custom_message(check_order_status, client_node_id)
215214
.unwrap();
216215

217-
let _check_payment_confirmation_event = service_node.liquidity_manager.next_event().unwrap();
218-
219-
if let LiquidityEvent::LSPS1Service(LSPS1ServiceEvent::CheckPaymentConfirmation {
220-
request_id,
221-
counterparty_node_id,
222-
order_id,
223-
}) = _check_payment_confirmation_event
224-
{
225-
assert_eq!(request_id, check_order_status_id);
226-
assert_eq!(counterparty_node_id, client_node_id);
227-
assert_eq!(order_id, expected_order_id.clone());
228-
} else {
229-
panic!("Unexpected event");
230-
}
231-
232-
let _ = service_handler
233-
.update_order_status(
234-
check_order_status_id.clone(),
235-
client_node_id,
236-
expected_order_id.clone(),
237-
LSPS1OrderState::Created,
238-
None,
239-
)
240-
.unwrap();
241-
242216
let order_status_response = get_lsps_message!(service_node, client_node_id);
243217

244218
client_node

0 commit comments

Comments
 (0)