Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 494a03c

Browse files
ability to signal channel open failure
1 parent 30e80d4 commit 494a03c

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

src/lsps2/payment_queue.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ impl PaymentQueue {
4949
position.map(|position| self.payments.remove(position))
5050
}
5151

52-
pub(crate) fn clear(&mut self) -> Vec<InterceptedHTLC> {
53-
self.payments.drain(..).map(|(_k, v)| v).flatten().collect()
52+
pub(crate) fn clear(&mut self) -> Vec<(PaymentHash, Vec<InterceptedHTLC>)> {
53+
self.payments.drain(..).collect()
5454
}
5555
}
5656

@@ -109,11 +109,14 @@ mod tests {
109109
);
110110
assert_eq!(
111111
payment_queue.clear(),
112-
vec![InterceptedHTLC {
113-
intercept_id: InterceptId([1; 32]),
114-
expected_outbound_amount_msat: 300_000_000,
115-
payment_hash: PaymentHash([101; 32]),
116-
}]
112+
vec![(
113+
PaymentHash([101; 32]),
114+
vec![InterceptedHTLC {
115+
intercept_id: InterceptId([1; 32]),
116+
expected_outbound_amount_msat: 300_000_000,
117+
payment_hash: PaymentHash([101; 32]),
118+
}]
119+
)]
117120
);
118121
}
119122
}

src/lsps2/service.rs

+68-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,13 @@ impl OutboundJITChannelState {
362362
let mut payment_queue_lock = payment_queue.lock().unwrap();
363363
let payment_forwarded =
364364
OutboundJITChannelState::PaymentForwarded { channel_id: *channel_id };
365-
let forward_htlcs = ForwardHTLCsAction(*channel_id, payment_queue_lock.clear());
365+
let htlcs = payment_queue_lock
366+
.clear()
367+
.into_iter()
368+
.map(|(_, htlcs)| htlcs)
369+
.flatten()
370+
.collect();
371+
let forward_htlcs = ForwardHTLCsAction(*channel_id, htlcs);
366372
Ok((payment_forwarded, Some(forward_htlcs)))
367373
},
368374
OutboundJITChannelState::PaymentForwarded { channel_id } => {
@@ -898,6 +904,67 @@ where
898904
Ok(())
899905
}
900906

907+
/// Used by LSP to fail intercepted htlcs backwards when the channel open fails for any reason.
908+
///
909+
/// Should be called in response to receiving a [`LSPS2ServiceEvent::OpenChannel`] event.
910+
///
911+
/// The JIT channel state is reset such that the payer can attempt payment again.
912+
/// [`LSPS2ServiceEvent::OpenChannel`]: crate::lsps2::event::LSPS2ServiceEvent::OpenChannel
913+
pub fn channel_open_failed(
914+
&self, counterparty_node_id: &PublicKey, intercept_scid: u64,
915+
) -> Result<(), APIError> {
916+
let outer_state_lock = self.per_peer_state.read().unwrap();
917+
match outer_state_lock.get(counterparty_node_id) {
918+
Some(inner_state_lock) => {
919+
let mut peer_state = inner_state_lock.lock().unwrap();
920+
921+
if let Some(jit_channel) =
922+
peer_state.outbound_channels_by_intercept_scid.get_mut(&intercept_scid)
923+
{
924+
let new_state = if let OutboundJITChannelState::PendingChannelOpen {
925+
payment_queue,
926+
..
927+
} = &jit_channel.state
928+
{
929+
let mut queue = payment_queue.lock().unwrap();
930+
let payment_hashes = queue
931+
.clear()
932+
.into_iter()
933+
.map(|(payment_hash, _)| payment_hash)
934+
.collect::<Vec<_>>();
935+
for payment_hash in payment_hashes {
936+
self.channel_manager.get_cm().fail_htlc_backwards_with_reason(
937+
&payment_hash,
938+
FailureCode::TemporaryNodeFailure,
939+
);
940+
}
941+
OutboundJITChannelState::PendingInitialPayment {
942+
payment_queue: payment_queue.clone(),
943+
}
944+
} else {
945+
return Err(APIError::APIMisuseError {
946+
err: format!("Channel is not in the PendingChannelOpen state.",),
947+
});
948+
};
949+
jit_channel.state = new_state;
950+
} else {
951+
return Err(APIError::APIMisuseError {
952+
err: format!(
953+
"Could not find a channel with intercept_scid {}",
954+
intercept_scid
955+
),
956+
});
957+
}
958+
},
959+
None => {
960+
return Err(APIError::APIMisuseError {
961+
err: format!("No counterparty state for: {}", counterparty_node_id),
962+
});
963+
},
964+
}
965+
Ok(())
966+
}
967+
901968
/// Forward [`Event::ChannelReady`] event parameters into this function.
902969
///
903970
/// Will forward the intercepted HTLC if it matches a channel

0 commit comments

Comments
 (0)