@@ -38,7 +38,6 @@ use lightning::ln::msgs::{ErrorAction, LightningError};
38
38
use lightning:: ln:: types:: ChannelId ;
39
39
use lightning:: util:: errors:: APIError ;
40
40
use lightning:: util:: logger:: Level ;
41
-
42
41
use lightning_types:: payment:: PaymentHash ;
43
42
44
43
use bitcoin:: secp256k1:: PublicKey ;
@@ -1029,6 +1028,21 @@ where
1029
1028
err : format ! ( "Could not find a channel with user_channel_id {}" , user_channel_id) ,
1030
1029
} ) ?;
1031
1030
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
+
1032
1046
peer_state. outbound_channels_by_intercept_scid . remove ( & intercept_scid) ;
1033
1047
1034
1048
peer_state. intercept_scid_by_channel_id . retain ( |_, & mut scid| scid != intercept_scid) ;
@@ -1064,41 +1078,45 @@ where
1064
1078
} ) ?;
1065
1079
1066
1080
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 {
1070
1084
err : format ! (
1071
1085
"Failed to map the stored intercept_scid {} for the provided user_channel_id {} to a channel." ,
1072
1086
intercept_scid, user_channel_id,
1073
1087
) ,
1074
1088
} ) ?;
1075
1089
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) ,
1102
1120
} ;
1103
1121
1104
1122
Ok ( ( ) )
0 commit comments