Skip to content

Commit fb10c9c

Browse files
committed
Use PeerState::{register,remove}_request instead of map access
We introduce two new methods on `PeerState` to avoid direct access to the internal `pending_requests` map.
1 parent 0c7ad20 commit fb10c9c

File tree

2 files changed

+71
-16
lines changed

2 files changed

+71
-16
lines changed

lightning-liquidity/src/lsps1/peer_state.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ use super::msgs::{LSPS1OrderId, LSPS1OrderParams, LSPS1PaymentInfo, LSPS1Request
1414
use crate::lsps0::ser::{LSPSDateTime, LSPSRequestId};
1515
use crate::prelude::HashMap;
1616

17+
use core::fmt;
18+
1719
#[derive(Default)]
1820
pub(super) struct PeerState {
1921
outbound_channels_by_order_id: HashMap<LSPS1OrderId, OutboundCRChannel>,
20-
pub(super) pending_requests: HashMap<LSPSRequestId, LSPS1Request>,
22+
pending_requests: HashMap<LSPSRequestId, LSPS1Request>,
2123
}
2224

2325
impl PeerState {
@@ -33,11 +35,42 @@ impl PeerState {
3335
self.outbound_channels_by_order_id.get(order_id).map(|channel| &channel.order)
3436
}
3537

38+
pub(super) fn register_request(
39+
&mut self, request_id: LSPSRequestId, request: LSPS1Request,
40+
) -> Result<(), PeerStateError> {
41+
if self.pending_requests.contains_key(&request_id) {
42+
return Err(PeerStateError::DuplicateRequestId);
43+
}
44+
self.pending_requests.insert(request_id, request);
45+
Ok(())
46+
}
47+
48+
pub(super) fn remove_request(
49+
&mut self, request_id: &LSPSRequestId,
50+
) -> Result<LSPS1Request, PeerStateError> {
51+
self.pending_requests.remove(request_id).ok_or(PeerStateError::UnknownRequestId)
52+
}
53+
3654
pub(super) fn has_active_requests(&self) -> bool {
3755
!self.outbound_channels_by_order_id.is_empty()
3856
}
3957
}
4058

59+
#[derive(Debug, Copy, Clone)]
60+
pub(super) enum PeerStateError {
61+
UnknownRequestId,
62+
DuplicateRequestId,
63+
}
64+
65+
impl fmt::Display for PeerStateError {
66+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67+
match self {
68+
Self::UnknownRequestId => write!(f, "unknown request id"),
69+
Self::DuplicateRequestId => write!(f, "duplicate request id"),
70+
}
71+
}
72+
}
73+
4174
pub(super) struct ChannelOrder {
4275
pub(super) order_params: LSPS1OrderParams,
4376
pub(super) created_at: LSPSDateTime,

lightning-liquidity/src/lsps1/service.rs

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,12 @@ where
162162
.or_insert(Mutex::new(PeerState::default()));
163163
let mut peer_state_lock = inner_state_lock.lock().unwrap();
164164

165-
peer_state_lock
166-
.pending_requests
167-
.insert(request_id.clone(), LSPS1Request::CreateOrder(params.clone()));
165+
let request = LSPS1Request::CreateOrder(params.clone());
166+
peer_state_lock.register_request(request_id.clone(), request).map_err(|e| {
167+
let err = format!("Failed to handle request due to: {}", e);
168+
let action = ErrorAction::IgnoreAndLog(Level::Error);
169+
LightningError { err, action }
170+
})?;
168171
}
169172

170173
event_queue_notifier.enqueue(LSPS1ServiceEvent::RequestForPaymentDetails {
@@ -191,11 +194,15 @@ where
191194
match outer_state_lock.get(counterparty_node_id) {
192195
Some(inner_state_lock) => {
193196
let mut peer_state_lock = inner_state_lock.lock().unwrap();
194-
195-
match peer_state_lock.pending_requests.remove(&request_id) {
196-
Some(LSPS1Request::CreateOrder(params)) => {
197+
let request = peer_state_lock.remove_request(&request_id).map_err(|e| {
198+
debug_assert!(false, "Failed to send response due to: {}", e);
199+
let err = format!("Failed to send response due to: {}", e);
200+
APIError::APIMisuseError { err }
201+
})?;
202+
203+
match request {
204+
LSPS1Request::CreateOrder(params) => {
197205
let order_id = self.generate_order_id();
198-
199206
peer_state_lock.new_order(
200207
order_id.clone(),
201208
params.order.clone(),
@@ -206,6 +213,9 @@ where
206213
let response = LSPS1Response::CreateOrder(LSPS1CreateOrderResponse {
207214
order: params.order,
208215
order_id,
216+
217+
// TODO, we need to set this in the peer/channel state, and send the
218+
// set value here:
209219
order_state: LSPS1OrderState::Created,
210220
created_at,
211221
payment,
@@ -215,14 +225,22 @@ where
215225
message_queue_notifier.enqueue(counterparty_node_id, msg);
216226
Ok(())
217227
},
218-
219-
_ => Err(APIError::APIMisuseError {
220-
err: format!("No pending buy request for request_id: {:?}", request_id),
221-
}),
228+
t => {
229+
debug_assert!(
230+
false,
231+
"Failed to send response due to unexpected request type: {:?}",
232+
t
233+
);
234+
let err = format!(
235+
"Failed to send response due to unexpected request type: {:?}",
236+
t
237+
);
238+
return Err(APIError::APIMisuseError { err });
239+
},
222240
}
223241
},
224242
None => Err(APIError::APIMisuseError {
225-
err: format!("No state for the counterparty exists: {:?}", counterparty_node_id),
243+
err: format!("No state for the counterparty exists: {}", counterparty_node_id),
226244
}),
227245
}
228246
}
@@ -236,9 +254,13 @@ where
236254
match outer_state_lock.get(counterparty_node_id) {
237255
Some(inner_state_lock) => {
238256
let mut peer_state_lock = inner_state_lock.lock().unwrap();
239-
peer_state_lock
240-
.pending_requests
241-
.insert(request_id.clone(), LSPS1Request::GetOrder(params.clone()));
257+
258+
let request = LSPS1Request::GetOrder(params.clone());
259+
peer_state_lock.register_request(request_id.clone(), request).map_err(|e| {
260+
let err = format!("Failed to handle request due to: {}", e);
261+
let action = ErrorAction::IgnoreAndLog(Level::Error);
262+
LightningError { err, action }
263+
})?;
242264

243265
event_queue_notifier.enqueue(LSPS1ServiceEvent::CheckPaymentConfirmation {
244266
request_id,

0 commit comments

Comments
 (0)