@@ -26,7 +26,7 @@ use ln::channel::{Channel, ChannelError, ChannelKeys};
26
26
use ln:: channelmonitor:: { ManyChannelMonitor , CLTV_CLAIM_BUFFER , HTLC_FAIL_TIMEOUT_BLOCKS } ;
27
27
use ln:: router:: { Route , RouteHop } ;
28
28
use ln:: msgs;
29
- use ln:: msgs:: { HandleError , ChannelMessageHandler } ;
29
+ use ln:: msgs:: { ChannelMessageHandler , HandleError , RAACommitmentOrder } ;
30
30
use util:: { byte_utils, events, internal_traits, rng} ;
31
31
use util:: sha2:: Sha256 ;
32
32
use util:: ser:: { Readable , Writeable } ;
@@ -2168,17 +2168,17 @@ impl ChannelManager {
2168
2168
Ok ( ( ) )
2169
2169
}
2170
2170
2171
- fn internal_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > ) , MsgHandleErrInternal > {
2171
+ fn internal_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder ) , MsgHandleErrInternal > {
2172
2172
let ( res, chan_monitor) = {
2173
2173
let mut channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
2174
2174
match channel_state. by_id . get_mut ( & msg. channel_id ) {
2175
2175
Some ( chan) => {
2176
2176
if chan. get_their_node_id ( ) != * their_node_id {
2177
2177
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Got a message for a channel from the wrong node!" , msg. channel_id ) ) ;
2178
2178
}
2179
- let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor) = chan. channel_reestablish ( msg)
2179
+ let ( funding_locked, revoke_and_ack, commitment_update, channel_monitor, order ) = chan. channel_reestablish ( msg)
2180
2180
. map_err ( |e| MsgHandleErrInternal :: from_chan_maybe_close ( e, msg. channel_id ) ) ?;
2181
- ( Ok ( ( funding_locked, revoke_and_ack, commitment_update) ) , channel_monitor)
2181
+ ( Ok ( ( funding_locked, revoke_and_ack, commitment_update, order ) ) , channel_monitor)
2182
2182
} ,
2183
2183
None => return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Failed to find corresponding channel" , msg. channel_id ) )
2184
2184
}
@@ -2448,7 +2448,7 @@ impl ChannelMessageHandler for ChannelManager {
2448
2448
handle_error ! ( self , self . internal_announcement_signatures( their_node_id, msg) , their_node_id)
2449
2449
}
2450
2450
2451
- fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > ) , HandleError > {
2451
+ fn handle_channel_reestablish ( & self , their_node_id : & PublicKey , msg : & msgs:: ChannelReestablish ) -> Result < ( Option < msgs:: FundingLocked > , Option < msgs:: RevokeAndACK > , Option < msgs:: CommitmentUpdate > , RAACommitmentOrder ) , HandleError > {
2452
2452
handle_error ! ( self , self . internal_channel_reestablish( their_node_id, msg) , their_node_id)
2453
2453
}
2454
2454
@@ -4938,6 +4938,7 @@ mod tests {
4938
4938
assert ! ( chan_msgs. 0 . is_none( ) ) ;
4939
4939
}
4940
4940
if pending_raa. 0 {
4941
+ assert ! ( chan_msgs. 3 == msgs:: RAACommitmentOrder :: RevokeAndACKFirst ) ;
4941
4942
assert ! ( node_a. node. handle_revoke_and_ack( & node_b. node. get_our_node_id( ) , & chan_msgs. 1 . unwrap( ) ) . unwrap( ) . is_none( ) ) ;
4942
4943
check_added_monitors ! ( node_a, 1 ) ;
4943
4944
} else {
@@ -4985,6 +4986,7 @@ mod tests {
4985
4986
assert ! ( chan_msgs. 0 . is_none( ) ) ;
4986
4987
}
4987
4988
if pending_raa. 1 {
4989
+ assert ! ( chan_msgs. 3 == msgs:: RAACommitmentOrder :: RevokeAndACKFirst ) ;
4988
4990
assert ! ( node_b. node. handle_revoke_and_ack( & node_a. node. get_our_node_id( ) , & chan_msgs. 1 . unwrap( ) ) . unwrap( ) . is_none( ) ) ;
4989
4991
check_added_monitors ! ( node_b, 1 ) ;
4990
4992
} else {
@@ -5316,6 +5318,141 @@ mod tests {
5316
5318
claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage) ;
5317
5319
}
5318
5320
5321
+ #[ test]
5322
+ fn test_drop_messages_peer_disconnect_dual_htlc ( ) {
5323
+ // Test that we can handle reconnecting when both sides of a channel have pending
5324
+ // commitment_updates when we disconnect.
5325
+ let mut nodes = create_network ( 2 ) ;
5326
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
5327
+
5328
+ let ( payment_preimage_1, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , 1000000 ) ;
5329
+
5330
+ // Now try to send a second payment which will fail to send
5331
+ let route = nodes[ 0 ] . router . get_route ( & nodes[ 1 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
5332
+ let ( payment_preimage_2, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
5333
+
5334
+ nodes[ 0 ] . node . send_payment ( route. clone ( ) , payment_hash_2) . unwrap ( ) ;
5335
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
5336
+
5337
+ let events_1 = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
5338
+ assert_eq ! ( events_1. len( ) , 1 ) ;
5339
+ match events_1[ 0 ] {
5340
+ Event :: UpdateHTLCs { .. } => { } ,
5341
+ _ => panic ! ( "Unexpected event" ) ,
5342
+ }
5343
+
5344
+ assert ! ( nodes[ 1 ] . node. claim_funds( payment_preimage_1) ) ;
5345
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
5346
+
5347
+ let events_2 = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
5348
+ assert_eq ! ( events_2. len( ) , 1 ) ;
5349
+ match events_2[ 0 ] {
5350
+ Event :: UpdateHTLCs { ref node_id, updates : msgs:: CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
5351
+ assert_eq ! ( * node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
5352
+ assert ! ( update_add_htlcs. is_empty( ) ) ;
5353
+ assert_eq ! ( update_fulfill_htlcs. len( ) , 1 ) ;
5354
+ assert ! ( update_fail_htlcs. is_empty( ) ) ;
5355
+ assert ! ( update_fail_malformed_htlcs. is_empty( ) ) ;
5356
+ assert ! ( update_fee. is_none( ) ) ;
5357
+
5358
+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & update_fulfill_htlcs[ 0 ] ) . unwrap ( ) ;
5359
+ let events_3 = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
5360
+ assert_eq ! ( events_3. len( ) , 1 ) ;
5361
+ match events_3[ 0 ] {
5362
+ Event :: PaymentSent { ref payment_preimage } => {
5363
+ assert_eq ! ( * payment_preimage, payment_preimage_1) ;
5364
+ } ,
5365
+ _ => panic ! ( "Unexpected event" ) ,
5366
+ }
5367
+
5368
+ let ( _, commitment_update) = nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , commitment_signed) . unwrap ( ) ;
5369
+ assert ! ( commitment_update. is_none( ) ) ;
5370
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
5371
+ } ,
5372
+ _ => panic ! ( "Unexpected event" ) ,
5373
+ }
5374
+
5375
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
5376
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
5377
+
5378
+ let reestablish_1 = nodes[ 0 ] . node . peer_connected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
5379
+ assert_eq ! ( reestablish_1. len( ) , 1 ) ;
5380
+ let reestablish_2 = nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
5381
+ assert_eq ! ( reestablish_2. len( ) , 1 ) ;
5382
+
5383
+ let as_resp = nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) . unwrap ( ) ;
5384
+ let bs_resp = nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) . unwrap ( ) ;
5385
+
5386
+ assert ! ( as_resp. 0 . is_none( ) ) ;
5387
+ assert ! ( bs_resp. 0 . is_none( ) ) ;
5388
+
5389
+ assert ! ( bs_resp. 1 . is_none( ) ) ;
5390
+ assert ! ( bs_resp. 2 . is_none( ) ) ;
5391
+
5392
+ assert ! ( as_resp. 3 == msgs:: RAACommitmentOrder :: CommitmentFirst ) ;
5393
+
5394
+ assert_eq ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_add_htlcs. len( ) , 1 ) ;
5395
+ assert ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_fulfill_htlcs. is_empty( ) ) ;
5396
+ assert ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_fail_htlcs. is_empty( ) ) ;
5397
+ assert ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_fail_malformed_htlcs. is_empty( ) ) ;
5398
+ assert ! ( as_resp. 2 . as_ref( ) . unwrap( ) . update_fee. is_none( ) ) ;
5399
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_resp. 2 . as_ref ( ) . unwrap ( ) . update_add_htlcs [ 0 ] ) . unwrap ( ) ;
5400
+ let ( bs_revoke_and_ack, bs_commitment_signed) = nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_resp. 2 . as_ref ( ) . unwrap ( ) . commitment_signed ) . unwrap ( ) ;
5401
+ assert ! ( bs_commitment_signed. is_none( ) ) ;
5402
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
5403
+
5404
+ let bs_second_commitment_signed = nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , as_resp. 1 . as_ref ( ) . unwrap ( ) ) . unwrap ( ) . unwrap ( ) ;
5405
+ assert ! ( bs_second_commitment_signed. update_add_htlcs. is_empty( ) ) ;
5406
+ assert ! ( bs_second_commitment_signed. update_fulfill_htlcs. is_empty( ) ) ;
5407
+ assert ! ( bs_second_commitment_signed. update_fail_htlcs. is_empty( ) ) ;
5408
+ assert ! ( bs_second_commitment_signed. update_fail_malformed_htlcs. is_empty( ) ) ;
5409
+ assert ! ( bs_second_commitment_signed. update_fee. is_none( ) ) ;
5410
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
5411
+
5412
+ let as_commitment_signed = nodes[ 0 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_revoke_and_ack) . unwrap ( ) . unwrap ( ) ;
5413
+ assert ! ( as_commitment_signed. update_add_htlcs. is_empty( ) ) ;
5414
+ assert ! ( as_commitment_signed. update_fulfill_htlcs. is_empty( ) ) ;
5415
+ assert ! ( as_commitment_signed. update_fail_htlcs. is_empty( ) ) ;
5416
+ assert ! ( as_commitment_signed. update_fail_malformed_htlcs. is_empty( ) ) ;
5417
+ assert ! ( as_commitment_signed. update_fee. is_none( ) ) ;
5418
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
5419
+
5420
+ let ( as_revoke_and_ack, as_second_commitment_signed) = nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_second_commitment_signed. commitment_signed ) . unwrap ( ) ;
5421
+ assert ! ( as_second_commitment_signed. is_none( ) ) ;
5422
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
5423
+
5424
+ let ( bs_second_revoke_and_ack, bs_third_commitment_signed) = nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_commitment_signed. commitment_signed ) . unwrap ( ) ;
5425
+ assert ! ( bs_third_commitment_signed. is_none( ) ) ;
5426
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
5427
+
5428
+ assert ! ( nodes[ 1 ] . node. handle_revoke_and_ack( & nodes[ 0 ] . node. get_our_node_id( ) , & as_revoke_and_ack) . unwrap( ) . is_none( ) ) ;
5429
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
5430
+
5431
+ let events_4 = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
5432
+ assert_eq ! ( events_4. len( ) , 1 ) ;
5433
+ match events_4[ 0 ] {
5434
+ Event :: PendingHTLCsForwardable { .. } => { } ,
5435
+ _ => panic ! ( "Unexpected event" ) ,
5436
+ } ;
5437
+
5438
+ nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) . next_forward = Instant :: now ( ) ;
5439
+ nodes[ 1 ] . node . process_pending_htlc_forwards ( ) ;
5440
+
5441
+ let events_5 = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
5442
+ assert_eq ! ( events_5. len( ) , 1 ) ;
5443
+ match events_5[ 0 ] {
5444
+ Event :: PaymentReceived { ref payment_hash, amt : _ } => {
5445
+ assert_eq ! ( payment_hash_2, * payment_hash) ;
5446
+ } ,
5447
+ _ => panic ! ( "Unexpected event" ) ,
5448
+ }
5449
+
5450
+ assert ! ( nodes[ 0 ] . node. handle_revoke_and_ack( & nodes[ 1 ] . node. get_our_node_id( ) , & bs_second_revoke_and_ack) . unwrap( ) . is_none( ) ) ;
5451
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
5452
+
5453
+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_2) ;
5454
+ }
5455
+
5319
5456
#[ test]
5320
5457
fn test_invalid_channel_announcement ( ) {
5321
5458
//Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs
0 commit comments