@@ -362,7 +362,13 @@ impl OutboundJITChannelState {
362
362
let mut payment_queue_lock = payment_queue. lock ( ) . unwrap ( ) ;
363
363
let payment_forwarded =
364
364
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) ;
366
372
Ok ( ( payment_forwarded, Some ( forward_htlcs) ) )
367
373
} ,
368
374
OutboundJITChannelState :: PaymentForwarded { channel_id } => {
@@ -898,6 +904,67 @@ where
898
904
Ok ( ( ) )
899
905
}
900
906
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
+
901
968
/// Forward [`Event::ChannelReady`] event parameters into this function.
902
969
///
903
970
/// Will forward the intercepted HTLC if it matches a channel
0 commit comments