@@ -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
} ,
@@ -3444,10 +3458,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
3444
3458
my_current_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number + 1 ) )
3445
3459
} )
3446
3460
} else {
3447
- log_debug ! ( self , "We don't seen yet any revoked secret, if this channnel has already been updated it means we are fallen-behind, you should wait for other peer closing " ) ;
3461
+ log_info ! ( self , "Sending a data_loss_protect with no previous remote per_commitment_secret " ) ;
3448
3462
OptionalField :: Present ( DataLossProtect {
3449
3463
your_last_per_commitment_secret : [ 0 ; 32 ] ,
3450
- my_current_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number ) )
3464
+ my_current_per_commitment_point : PublicKey :: from_secret_key ( & self . secp_ctx , & self . build_local_commitment_secret ( self . cur_local_commitment_transaction_number + 1 ) )
3451
3465
} )
3452
3466
} ;
3453
3467
msgs:: ChannelReestablish {
@@ -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
@@ -4255,22 +4268,28 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for Channel<C
4255
4268
4256
4269
#[ cfg( test) ]
4257
4270
mod tests {
4271
+ use bitcoin:: BitcoinHash ;
4258
4272
use bitcoin:: util:: bip143;
4259
4273
use bitcoin:: consensus:: encode:: serialize;
4260
4274
use bitcoin:: blockdata:: script:: { Script , Builder } ;
4261
- use bitcoin:: blockdata:: transaction:: Transaction ;
4275
+ use bitcoin:: blockdata:: transaction:: { Transaction , TxOut } ;
4276
+ use bitcoin:: blockdata:: constants:: genesis_block;
4262
4277
use bitcoin:: blockdata:: opcodes;
4278
+ use bitcoin:: network:: constants:: Network ;
4263
4279
use bitcoin_hashes:: hex:: FromHex ;
4264
4280
use hex;
4265
4281
use ln:: channelmanager:: { HTLCSource , PaymentPreimage , PaymentHash } ;
4266
4282
use ln:: channel:: { Channel , ChannelKeys , InboundHTLCOutput , OutboundHTLCOutput , InboundHTLCState , OutboundHTLCState , HTLCOutputInCommitment , TxCreationKeys } ;
4267
4283
use ln:: channel:: MAX_FUNDING_SATOSHIS ;
4284
+ use ln:: features:: InitFeatures ;
4285
+ use ln:: msgs:: { OptionalField , DataLossProtect } ;
4268
4286
use ln:: chan_utils;
4269
4287
use ln:: chan_utils:: { LocalCommitmentTransaction , ChannelPublicKeys } ;
4270
4288
use chain:: chaininterface:: { FeeEstimator , ConfirmationTarget } ;
4271
4289
use chain:: keysinterface:: { InMemoryChannelKeys , KeysInterface } ;
4272
4290
use chain:: transaction:: OutPoint ;
4273
4291
use util:: config:: UserConfig ;
4292
+ use util:: enforcing_trait_impls:: EnforcingChannelKeys ;
4274
4293
use util:: test_utils;
4275
4294
use util:: logger:: Logger ;
4276
4295
use secp256k1:: { Secp256k1 , Message , Signature , All } ;
@@ -4280,6 +4299,7 @@ mod tests {
4280
4299
use bitcoin_hashes:: hash160:: Hash as Hash160 ;
4281
4300
use bitcoin_hashes:: Hash ;
4282
4301
use std:: sync:: Arc ;
4302
+ use rand:: { thread_rng, Rng } ;
4283
4303
4284
4304
struct TestFeeEstimator {
4285
4305
fee_est : u64
@@ -4327,6 +4347,70 @@ mod tests {
4327
4347
PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & hex:: decode ( hex) . unwrap ( ) [ ..] ) . unwrap ( ) )
4328
4348
}
4329
4349
4350
+ #[ test]
4351
+ fn channel_reestablish_no_updates ( ) {
4352
+ let feeest = TestFeeEstimator { fee_est : 15000 } ;
4353
+ let logger : Arc < Logger > = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
4354
+ let secp_ctx = Secp256k1 :: new ( ) ;
4355
+ let mut seed = [ 0 ; 32 ] ;
4356
+ let mut rng = thread_rng ( ) ;
4357
+ rng. fill_bytes ( & mut seed) ;
4358
+ let network = Network :: Testnet ;
4359
+ let keys_provider = test_utils:: TestKeysInterface :: new ( & seed, network, logger. clone ( ) as Arc < Logger > ) ;
4360
+
4361
+ // Go through the flow of opening a channel between two nodes.
4362
+
4363
+ // Create Node A's channel
4364
+ let node_a_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
4365
+ let config = UserConfig :: default ( ) ;
4366
+ let mut node_a_chan = Channel :: < EnforcingChannelKeys > :: new_outbound ( & & feeest, & & keys_provider, node_a_node_id, 10000000 , 100000 , 42 , Arc :: clone ( & logger) , & config) . unwrap ( ) ;
4367
+
4368
+ // Create Node B's channel by receiving Node A's open_channel message
4369
+ let open_channel_msg = node_a_chan. get_open_channel ( genesis_block ( network) . header . bitcoin_hash ( ) , & & feeest) ;
4370
+ let node_b_node_id = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 7 ; 32 ] ) . unwrap ( ) ) ;
4371
+ let mut node_b_chan = Channel :: < EnforcingChannelKeys > :: new_from_req ( & & feeest, & & keys_provider, node_b_node_id, InitFeatures :: supported ( ) , & open_channel_msg, 7 , logger, & config) . unwrap ( ) ;
4372
+
4373
+ // Node B --> Node A: accept channel
4374
+ let accept_channel_msg = node_b_chan. get_accept_channel ( ) ;
4375
+ node_a_chan. accept_channel ( & accept_channel_msg, & config, InitFeatures :: supported ( ) ) . unwrap ( ) ;
4376
+
4377
+ // Node A --> Node B: funding created
4378
+ let output_script = node_a_chan. get_funding_redeemscript ( ) ;
4379
+ let tx = Transaction { version : 1 , lock_time : 0 , input : Vec :: new ( ) , output : vec ! [ TxOut {
4380
+ value: 10000000 , script_pubkey: output_script. clone( ) ,
4381
+ } ] } ;
4382
+ let funding_outpoint = OutPoint :: new ( tx. txid ( ) , 0 ) ;
4383
+ let ( funding_created_msg, _) = node_a_chan. get_outbound_funding_created ( funding_outpoint) . unwrap ( ) ;
4384
+ let ( funding_signed_msg, _) = node_b_chan. funding_created ( & funding_created_msg) . unwrap ( ) ;
4385
+
4386
+ // Node B --> Node A: funding signed
4387
+ let _ = node_a_chan. funding_signed ( & funding_signed_msg) ;
4388
+
4389
+ // Now disconnect the two nodes and check that the commitment point in
4390
+ // Node B's channel_reestablish message is sane.
4391
+ node_b_chan. remove_uncommitted_htlcs_and_mark_paused ( ) ;
4392
+ let expected_commitment_point = PublicKey :: from_secret_key ( & secp_ctx, & node_b_chan. build_local_commitment_secret ( node_b_chan. cur_local_commitment_transaction_number + 1 ) ) ;
4393
+ let msg = node_b_chan. get_channel_reestablish ( ) ;
4394
+ match msg. data_loss_protect {
4395
+ OptionalField :: Present ( DataLossProtect { my_current_per_commitment_point, .. } ) => {
4396
+ assert_eq ! ( expected_commitment_point, my_current_per_commitment_point) ;
4397
+ } ,
4398
+ _ => panic ! ( )
4399
+ }
4400
+
4401
+ // Check that the commitment point in Node A's channel_reestablish message
4402
+ // is sane.
4403
+ node_a_chan. remove_uncommitted_htlcs_and_mark_paused ( ) ;
4404
+ let expected_commitment_point = PublicKey :: from_secret_key ( & secp_ctx, & node_a_chan. build_local_commitment_secret ( node_a_chan. cur_local_commitment_transaction_number + 1 ) ) ;
4405
+ let msg = node_a_chan. get_channel_reestablish ( ) ;
4406
+ match msg. data_loss_protect {
4407
+ OptionalField :: Present ( DataLossProtect { my_current_per_commitment_point, .. } ) => {
4408
+ assert_eq ! ( expected_commitment_point, my_current_per_commitment_point) ;
4409
+ } ,
4410
+ _ => panic ! ( )
4411
+ }
4412
+ }
4413
+
4330
4414
#[ test]
4331
4415
fn outbound_commitment_test ( ) {
4332
4416
// Test vectors from BOLT 3 Appendix C:
0 commit comments