@@ -1138,8 +1138,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1138
1138
}
1139
1139
1140
1140
/// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
1141
- /// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
1142
- /// Ok(_) if debug assertions are turned on and preconditions are met.
1141
+ /// In such cases we debug_assert!(false) and return a ChannelError::Ignore. Thus, will always
1142
+ /// return Ok(_) if debug assertions are turned on or preconditions are met.
1143
+ ///
1144
+ /// Note that it is still possible to hit these assertions in case we find a preimage on-chain
1145
+ /// but then have a reorg which settles on an HTLC-failure on chain.
1143
1146
fn get_update_fulfill_htlc ( & mut self , htlc_id_arg : u64 , payment_preimage_arg : PaymentPreimage ) -> Result < ( Option < msgs:: UpdateFulfillHTLC > , Option < ChannelMonitorUpdate > ) , ChannelError > {
1144
1147
// Either ChannelFunded got set (which means it won't be unset) or there is no way any
1145
1148
// caller thought we could have something claimed (cause we wouldn't have accepted in an
@@ -1167,6 +1170,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1167
1170
} else {
1168
1171
log_warn ! ( self , "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}" , log_bytes!( htlc. payment_hash. 0 ) , log_bytes!( self . channel_id( ) ) ) ;
1169
1172
}
1173
+ debug_assert ! ( false , "Tried to fulfill an HTLC that was already fail/fulfilled" ) ;
1170
1174
return Ok ( ( None , None ) ) ;
1171
1175
} ,
1172
1176
_ => {
@@ -1200,6 +1204,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1200
1204
match pending_update {
1201
1205
& HTLCUpdateAwaitingACK :: ClaimHTLC { htlc_id, .. } => {
1202
1206
if htlc_id_arg == htlc_id {
1207
+ // Make sure we don't leave latest_monitor_update_id incremented here:
1208
+ self . latest_monitor_update_id -= 1 ;
1209
+ debug_assert ! ( false , "Tried to fulfill an HTLC that was already fulfilled" ) ;
1203
1210
return Ok ( ( None , None ) ) ;
1204
1211
}
1205
1212
} ,
@@ -1208,6 +1215,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1208
1215
log_warn ! ( self , "Have preimage and want to fulfill HTLC with pending failure against channel {}" , log_bytes!( self . channel_id( ) ) ) ;
1209
1216
// TODO: We may actually be able to switch to a fulfill here, though its
1210
1217
// rare enough it may not be worth the complexity burden.
1218
+ debug_assert ! ( false , "Tried to fulfill an HTLC that was already failed" ) ;
1211
1219
return Ok ( ( None , Some ( monitor_update) ) ) ;
1212
1220
}
1213
1221
} ,
@@ -1259,8 +1267,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1259
1267
}
1260
1268
1261
1269
/// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
1262
- /// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
1263
- /// Ok(_) if debug assertions are turned on and preconditions are met.
1270
+ /// In such cases we debug_assert!(false) and return a ChannelError::Ignore. Thus, will always
1271
+ /// return Ok(_) if debug assertions are turned on or preconditions are met.
1272
+ ///
1273
+ /// Note that it is still possible to hit these assertions in case we find a preimage on-chain
1274
+ /// but then have a reorg which settles on an HTLC-failure on chain.
1264
1275
pub fn get_update_fail_htlc ( & mut self , htlc_id_arg : u64 , err_packet : msgs:: OnionErrorPacket ) -> Result < Option < msgs:: UpdateFailHTLC > , ChannelError > {
1265
1276
if ( self . channel_state & ( ChannelState :: ChannelFunded as u32 ) ) != ( ChannelState :: ChannelFunded as u32 ) {
1266
1277
panic ! ( "Was asked to fail an HTLC when channel was not in an operational state" ) ;
@@ -1277,6 +1288,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1277
1288
match htlc. state {
1278
1289
InboundHTLCState :: Committed => { } ,
1279
1290
InboundHTLCState :: LocalRemoved ( _) => {
1291
+ debug_assert ! ( false , "Tried to fail an HTLC that was already fail/fulfilled" ) ;
1280
1292
return Ok ( None ) ;
1281
1293
} ,
1282
1294
_ => {
@@ -1297,11 +1309,13 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
1297
1309
match pending_update {
1298
1310
& HTLCUpdateAwaitingACK :: ClaimHTLC { htlc_id, .. } => {
1299
1311
if htlc_id_arg == htlc_id {
1312
+ debug_assert ! ( false , "Tried to fail an HTLC that was already fulfilled" ) ;
1300
1313
return Err ( ChannelError :: Ignore ( "Unable to find a pending HTLC which matched the given HTLC ID" ) ) ;
1301
1314
}
1302
1315
} ,
1303
1316
& HTLCUpdateAwaitingACK :: FailHTLC { htlc_id, .. } => {
1304
1317
if htlc_id_arg == htlc_id {
1318
+ debug_assert ! ( false , "Tried to fail an HTLC that was already failed" ) ;
1305
1319
return Err ( ChannelError :: Ignore ( "Unable to find a pending HTLC which matched the given HTLC ID" ) ) ;
1306
1320
}
1307
1321
} ,
@@ -3760,7 +3774,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3760
3774
/// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
3761
3775
/// Also returns the list of payment_hashes for channels which we can safely fail backwards
3762
3776
/// immediately (others we will have to allow to time out).
3763
- pub fn force_shutdown ( & mut self ) -> ( Vec < Transaction > , Vec < ( HTLCSource , PaymentHash ) > ) {
3777
+ pub fn force_shutdown ( & mut self , should_broadcast : bool ) -> ( Option < OutPoint > , ChannelMonitorUpdate , Vec < ( HTLCSource , PaymentHash ) > ) {
3764
3778
assert ! ( self . channel_state != ChannelState :: ShutdownComplete as u32 ) ;
3765
3779
3766
3780
// We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
@@ -3783,12 +3797,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3783
3797
3784
3798
self . channel_state = ChannelState :: ShutdownComplete as u32 ;
3785
3799
self . update_time_counter += 1 ;
3786
- if self . channel_monitor . is_some ( ) {
3787
- ( self . channel_monitor . as_mut ( ) . unwrap ( ) . get_latest_local_commitment_txn ( ) , dropped_outbound_htlcs)
3788
- } else {
3789
- // We aren't even signed funding yet, so can't broadcast anything
3790
- ( Vec :: new ( ) , dropped_outbound_htlcs)
3791
- }
3800
+ self . latest_monitor_update_id += 1 ;
3801
+ ( self . funding_txo . clone ( ) , ChannelMonitorUpdate {
3802
+ update_id : self . latest_monitor_update_id ,
3803
+ updates : vec ! [ ChannelMonitorUpdateStep :: ChannelForceClosed { should_broadcast } ] ,
3804
+ } , dropped_outbound_htlcs)
3792
3805
}
3793
3806
}
3794
3807
0 commit comments