Skip to content

Commit 8eaea29

Browse files
fixup: address comments. changes:
- refactor highest_state_for_peer and has_active_requests for better readabiliy - refactor ordering logic OutboundJITChannelState
1 parent d9157eb commit 8eaea29

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

lightning-liquidity/src/lsps1/service.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,11 @@ where
176176

177177
pub(crate) fn has_active_requests(&self, counterparty_node_id: &PublicKey) -> bool {
178178
let outer_state_lock = self.per_peer_state.read().unwrap();
179-
if let Some(inner_state_lock) = outer_state_lock.get(counterparty_node_id) {
180-
let peer_state = inner_state_lock.lock().unwrap();
179+
outer_state_lock.get(counterparty_node_id).map_or(false, |inner| {
180+
let inner_state_lock = inner.lock().unwrap();
181181
!(peer_state.pending_requests.is_empty()
182182
&& peer_state.outbound_channels_by_order_id.is_empty())
183-
} else {
184-
false
185-
}
183+
})
186184
}
187185

188186
fn handle_get_info_request(

lightning-liquidity/src/lsps2/service.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,17 @@ struct ForwardPaymentAction(ChannelId, FeePayment);
107107
#[derive(Debug, PartialEq)]
108108
struct ForwardHTLCsAction(ChannelId, Vec<InterceptedHTLC>);
109109

110+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
111+
pub(crate) enum OutboundJITStage {
112+
PendingInitialPayment,
113+
PendingChannelOpen,
114+
PendingPaymentForward,
115+
PendingPayment,
116+
PaymentForwarded,
117+
}
118+
110119
/// The different states a requested JIT channel can be in.
111-
#[derive(Clone, Debug, PartialEq, Eq)]
120+
#[derive(Debug, PartialEq, Eq, Clone)]
112121
pub(crate) enum OutboundJITChannelState {
113122
/// The JIT channel SCID was created after a buy request, and we are awaiting an initial payment
114123
/// of sufficient size to open the channel.
@@ -135,13 +144,19 @@ pub(crate) enum OutboundJITChannelState {
135144
}
136145

137146
impl OutboundJITChannelState {
138-
fn ord_index(&self) -> u8 {
147+
pub(crate) fn stage(&self) -> OutboundJITStage {
139148
match self {
140-
OutboundJITChannelState::PendingInitialPayment { .. } => 0,
141-
OutboundJITChannelState::PendingChannelOpen { .. } => 1,
142-
OutboundJITChannelState::PendingPaymentForward { .. } => 2,
143-
OutboundJITChannelState::PendingPayment { .. } => 3,
144-
OutboundJITChannelState::PaymentForwarded { .. } => 4,
149+
OutboundJITChannelState::PendingInitialPayment { .. } => {
150+
OutboundJITStage::PendingInitialPayment
151+
},
152+
OutboundJITChannelState::PendingChannelOpen { .. } => {
153+
OutboundJITStage::PendingChannelOpen
154+
},
155+
OutboundJITChannelState::PendingPaymentForward { .. } => {
156+
OutboundJITStage::PendingPaymentForward
157+
},
158+
OutboundJITChannelState::PendingPayment { .. } => OutboundJITStage::PendingPayment,
159+
OutboundJITChannelState::PaymentForwarded { .. } => OutboundJITStage::PaymentForwarded,
145160
}
146161
}
147162
}
@@ -154,7 +169,7 @@ impl PartialOrd for OutboundJITChannelState {
154169

155170
impl Ord for OutboundJITChannelState {
156171
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
157-
self.ord_index().cmp(&other.ord_index())
172+
self.stage().cmp(&other.stage())
158173
}
159174
}
160175

@@ -600,12 +615,10 @@ where
600615
&self, counterparty_node_id: &PublicKey,
601616
) -> Option<OutboundJITChannelState> {
602617
let outer_state_lock = self.per_peer_state.read().unwrap();
603-
if let Some(inner_state_lock) = outer_state_lock.get(counterparty_node_id) {
604-
let peer_state = inner_state_lock.lock().unwrap();
618+
outer_state_lock.get(counterparty_node_id).and_then(|inner| {
619+
let peer_state = inner.lock().unwrap();
605620
peer_state.outbound_channels_by_intercept_scid.values().map(|c| c.state.clone()).max()
606-
} else {
607-
None
608-
}
621+
})
609622
}
610623

611624
/// Used by LSP to inform a client requesting a JIT Channel the token they used is invalid.

lightning-liquidity/src/lsps5/service.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::prelude::*;
2222
use crate::sync::{Arc, Mutex};
2323
use crate::utils::time::TimeProvider;
2424

25-
use crate::lsps2::service::OutboundJITChannelState;
25+
use crate::lsps2::service::{OutboundJITChannelState, OutboundJITStage};
2626
use bitcoin::secp256k1::PublicKey;
2727

2828
use lightning::ln::channelmanager::AChannelManager;
@@ -162,15 +162,7 @@ where
162162
) -> bool {
163163
self.client_has_open_channel(client_id)
164164
|| lsps1_has_activity
165-
|| lsps2_max_state.map_or(false, |s| {
166-
matches!(
167-
s,
168-
OutboundJITChannelState::PendingChannelOpen { .. }
169-
| OutboundJITChannelState::PendingPaymentForward { .. }
170-
| OutboundJITChannelState::PendingPayment { .. }
171-
| OutboundJITChannelState::PaymentForwarded { .. }
172-
)
173-
})
165+
|| lsps2_max_state.map_or(false, |s| s.stage() >= OutboundJITStage::PendingChannelOpen)
174166
}
175167

176168
fn check_prune_stale_webhooks(&self) {

0 commit comments

Comments
 (0)