Skip to content

Commit ea01992

Browse files
fixup: Prevent abandoning JIT channel after channel creation or payment forwarding
- Added a state check in channel_open_abandoned to ensure a channel cannot be abandoned after creation or payment forwarding, returning an error if attempted. - Refactored state handling to use matches! macro for clarity. - Addressed minor nits: fixed indentation and import ordering.
1 parent 5fe0943 commit ea01992

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

lightning-liquidity/src/lsps2/service.rs

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use lightning::ln::msgs::{ErrorAction, LightningError};
3838
use lightning::ln::types::ChannelId;
3939
use lightning::util::errors::APIError;
4040
use lightning::util::logger::Level;
41-
4241
use lightning_types::payment::PaymentHash;
4342

4443
use bitcoin::secp256k1::PublicKey;
@@ -1029,6 +1028,21 @@ where
10291028
err: format!("Could not find a channel with user_channel_id {}", user_channel_id),
10301029
})?;
10311030

1031+
if let Some(jit_channel) =
1032+
peer_state.outbound_channels_by_intercept_scid.get(&intercept_scid)
1033+
{
1034+
if !matches!(
1035+
jit_channel.state,
1036+
OutboundJITChannelState::PendingInitialPayment { .. }
1037+
| OutboundJITChannelState::PendingChannelOpen { .. }
1038+
) {
1039+
return Err(APIError::APIMisuseError {
1040+
err: "Cannot abandon channel open after channel creation or payment forwarding"
1041+
.to_string(),
1042+
});
1043+
}
1044+
}
1045+
10321046
peer_state.outbound_channels_by_intercept_scid.remove(&intercept_scid);
10331047

10341048
peer_state.intercept_scid_by_channel_id.retain(|_, &mut scid| scid != intercept_scid);
@@ -1064,41 +1078,45 @@ where
10641078
})?;
10651079

10661080
let jit_channel = peer_state
1067-
.outbound_channels_by_intercept_scid
1068-
.get_mut(&intercept_scid)
1069-
.ok_or_else(|| APIError::APIMisuseError {
1081+
.outbound_channels_by_intercept_scid
1082+
.get_mut(&intercept_scid)
1083+
.ok_or_else(|| APIError::APIMisuseError {
10701084
err: format!(
10711085
"Failed to map the stored intercept_scid {} for the provided user_channel_id {} to a channel.",
10721086
intercept_scid, user_channel_id,
10731087
),
10741088
})?;
10751089

1076-
jit_channel.state = match &jit_channel.state {
1077-
OutboundJITChannelState::PendingChannelOpen { payment_queue, .. } => {
1078-
let mut queue = payment_queue.lock().unwrap();
1079-
let payment_hashes: Vec<_> = queue
1080-
.clear()
1081-
.into_iter()
1082-
.map(|htlc| htlc.payment_hash)
1083-
.collect::<HashSet<_>>()
1084-
.into_iter()
1085-
.collect();
1086-
1087-
for payment_hash in payment_hashes {
1088-
self.channel_manager.get_cm().fail_htlc_backwards_with_reason(
1089-
&payment_hash,
1090-
FailureCode::TemporaryNodeFailure,
1091-
);
1092-
}
1093-
OutboundJITChannelState::PendingInitialPayment {
1094-
payment_queue: payment_queue.clone(),
1095-
}
1096-
},
1097-
_ => {
1098-
return Err(APIError::APIMisuseError {
1099-
err: "Channel is not in the PendingChannelOpen state.".to_string(),
1100-
})
1101-
},
1090+
if !matches!(jit_channel.state, OutboundJITChannelState::PendingChannelOpen { .. }) {
1091+
return Err(APIError::APIMisuseError {
1092+
err: "Channel is not in the PendingChannelOpen state.".to_string(),
1093+
});
1094+
}
1095+
1096+
let payment_queue_arc =
1097+
if let OutboundJITChannelState::PendingChannelOpen { payment_queue, .. } =
1098+
&jit_channel.state
1099+
{
1100+
Arc::clone(payment_queue)
1101+
} else {
1102+
unreachable!()
1103+
};
1104+
let mut queue = payment_queue_arc.lock().unwrap();
1105+
let payment_hashes: Vec<_> = queue
1106+
.clear()
1107+
.into_iter()
1108+
.map(|htlc| htlc.payment_hash)
1109+
.collect::<HashSet<_>>()
1110+
.into_iter()
1111+
.collect();
1112+
for payment_hash in payment_hashes {
1113+
self.channel_manager
1114+
.get_cm()
1115+
.fail_htlc_backwards_with_reason(&payment_hash, FailureCode::TemporaryNodeFailure);
1116+
}
1117+
1118+
jit_channel.state = OutboundJITChannelState::PendingInitialPayment {
1119+
payment_queue: Arc::clone(&payment_queue_arc),
11021120
};
11031121

11041122
Ok(())

0 commit comments

Comments
 (0)