@@ -36,7 +36,6 @@ use lightning::ln::channelmanager::{AChannelManager, FailureCode, InterceptId};
36
36
use lightning:: ln:: msgs:: { ErrorAction , LightningError } ;
37
37
use lightning:: ln:: types:: ChannelId ;
38
38
use lightning:: util:: errors:: APIError ;
39
- use lightning:: util:: hash_tables:: HashSet ;
40
39
use lightning:: util:: logger:: Level ;
41
40
42
41
use lightning_types:: payment:: PaymentHash ;
@@ -989,18 +988,18 @@ where
989
988
/// Abandons a pending JIT‐open flow for `user_channel_id`, removing all local state.
990
989
///
991
990
/// This removes the intercept SCID, any outbound channel state, and associated
992
- /// channel‐ID mappings for the specified `user_channel_id`, but only while the JIT
993
- /// channel is still in `PendingInitialPayment` or `PendingChannelOpen` .
991
+ /// channel‐ID mappings for the specified `user_channel_id`, but only while no payment
992
+ /// has been forwarded yet and no channel has been opened on-chain .
994
993
///
995
994
/// Returns an error if:
996
995
/// - there is no channel matching `user_channel_id`, or
997
- /// - the channel has already advanced past `PendingChannelOpen` (e.g. to
998
- /// `PendingPaymentForward` or beyond).
996
+ /// - a payment has already been forwarded or a channel has already been opened
999
997
///
1000
998
/// Note: this does *not* close or roll back any on‐chain channel which may already
1001
- /// have been opened. The caller must only invoke this before a channel is actually
1002
- /// confirmed (or else provide its own on‐chain cleanup) and is responsible for
1003
- /// managing any pending channel open attempts separately.
999
+ /// have been opened. The caller must call this before or instead of initiating the channel
1000
+ /// open, as it only affects the local LSPS2 state and doesn't affect any channels that
1001
+ /// might already exist on-chain. Any pending channel open attempts must be managed
1002
+ /// separately.
1004
1003
pub fn channel_open_abandoned (
1005
1004
& self , counterparty_node_id : & PublicKey , user_channel_id : u128 ,
1006
1005
) -> Result < ( ) , APIError > {
@@ -1013,28 +1012,37 @@ where
1013
1012
1014
1013
let intercept_scid = peer_state
1015
1014
. intercept_scid_by_user_channel_id
1016
- . remove ( & user_channel_id)
1015
+ . get ( & user_channel_id)
1016
+ . copied ( )
1017
1017
. ok_or_else ( || APIError :: APIMisuseError {
1018
1018
err : format ! ( "Could not find a channel with user_channel_id {}" , user_channel_id) ,
1019
1019
} ) ?;
1020
1020
1021
- if let Some ( jit_channel) =
1022
- peer_state. outbound_channels_by_intercept_scid . get ( & intercept_scid)
1023
- {
1024
- if !matches ! (
1025
- jit_channel. state,
1026
- OutboundJITChannelState :: PendingInitialPayment { .. }
1027
- | OutboundJITChannelState :: PendingChannelOpen { .. }
1028
- ) {
1029
- return Err ( APIError :: APIMisuseError {
1030
- err : "Cannot abandon channel open after channel creation or payment forwarding"
1031
- . to_string ( ) ,
1032
- } ) ;
1033
- }
1021
+ let jit_channel = peer_state
1022
+ . outbound_channels_by_intercept_scid
1023
+ . get ( & intercept_scid)
1024
+ . ok_or_else ( || APIError :: APIMisuseError {
1025
+ err : format ! (
1026
+ "Failed to map intercept_scid {} for user_channel_id {} to a channel." ,
1027
+ intercept_scid, user_channel_id,
1028
+ ) ,
1029
+ } ) ?;
1030
+
1031
+ let is_pending = matches ! (
1032
+ jit_channel. state,
1033
+ OutboundJITChannelState :: PendingInitialPayment { .. }
1034
+ | OutboundJITChannelState :: PendingChannelOpen { .. }
1035
+ ) ;
1036
+
1037
+ if !is_pending {
1038
+ return Err ( APIError :: APIMisuseError {
1039
+ err : "Cannot abandon channel open after channel creation or payment forwarding"
1040
+ . to_string ( ) ,
1041
+ } ) ;
1034
1042
}
1035
1043
1044
+ peer_state. intercept_scid_by_user_channel_id . remove ( & user_channel_id) ;
1036
1045
peer_state. outbound_channels_by_intercept_scid . remove ( & intercept_scid) ;
1037
-
1038
1046
peer_state. intercept_scid_by_channel_id . retain ( |_, & mut scid| scid != intercept_scid) ;
1039
1047
1040
1048
Ok ( ( ) )
@@ -1077,33 +1085,22 @@ where
1077
1085
) ,
1078
1086
} ) ?;
1079
1087
1080
- if !matches ! ( jit_channel. state, OutboundJITChannelState :: PendingChannelOpen { .. } ) {
1081
- return Err ( APIError :: APIMisuseError {
1082
- err : "Channel is not in the PendingChannelOpen state." . to_string ( ) ,
1083
- } ) ;
1084
- }
1085
-
1086
- let mut payment_queue = match & jit_channel. state {
1087
- OutboundJITChannelState :: PendingChannelOpen { payment_queue, .. } => {
1088
- payment_queue. clone ( )
1089
- } ,
1090
- _ => {
1088
+ let mut payment_queue =
1089
+ if let OutboundJITChannelState :: PendingChannelOpen { payment_queue, .. } =
1090
+ & mut jit_channel. state
1091
+ {
1092
+ core:: mem:: take ( payment_queue)
1093
+ } else {
1091
1094
return Err ( APIError :: APIMisuseError {
1092
1095
err : "Channel is not in the PendingChannelOpen state." . to_string ( ) ,
1093
1096
} ) ;
1094
- } ,
1095
- } ;
1096
- let payment_hashes: Vec < _ > = payment_queue
1097
- . clear ( )
1098
- . into_iter ( )
1099
- . map ( |htlc| htlc. payment_hash )
1100
- . collect :: < HashSet < _ > > ( )
1101
- . into_iter ( )
1102
- . collect ( ) ;
1103
- for payment_hash in payment_hashes {
1104
- self . channel_manager
1105
- . get_cm ( )
1106
- . fail_htlc_backwards_with_reason ( & payment_hash, FailureCode :: TemporaryNodeFailure ) ;
1097
+ } ;
1098
+
1099
+ for htlc in payment_queue. clear ( ) {
1100
+ self . channel_manager . get_cm ( ) . fail_htlc_backwards_with_reason (
1101
+ & htlc. payment_hash ,
1102
+ FailureCode :: TemporaryNodeFailure ,
1103
+ ) ;
1107
1104
}
1108
1105
1109
1106
jit_channel. state = OutboundJITChannelState :: PendingInitialPayment { payment_queue } ;
0 commit comments