@@ -3469,6 +3469,149 @@ mod tests {
3469
3469
assert_eq ! ( channel_state. short_to_id. len( ) , 0 ) ;
3470
3470
}
3471
3471
3472
+ fn reconnect_nodes ( node_a : & Node , node_b : & Node , pre_all_htlcs : bool , pending_htlc_claims : ( usize , usize ) , pending_htlc_fails : ( usize , usize ) ) {
3473
+ let reestablish_1 = node_a. node . peer_connected ( & node_b. node . get_our_node_id ( ) ) ;
3474
+ let reestablish_2 = node_b. node . peer_connected ( & node_a. node . get_our_node_id ( ) ) ;
3475
+
3476
+ let mut resp_1 = Vec :: new ( ) ;
3477
+ for msg in reestablish_1 {
3478
+ resp_1. push ( node_b. node . handle_channel_reestablish ( & node_a. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
3479
+ }
3480
+ {
3481
+ let mut added_monitors = node_b. chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
3482
+ if pending_htlc_claims. 0 != 0 || pending_htlc_fails. 0 != 0 {
3483
+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
3484
+ } else {
3485
+ assert ! ( added_monitors. is_empty( ) ) ;
3486
+ }
3487
+ added_monitors. clear ( ) ;
3488
+ }
3489
+
3490
+ let mut resp_2 = Vec :: new ( ) ;
3491
+ for msg in reestablish_2 {
3492
+ resp_2. push ( node_a. node . handle_channel_reestablish ( & node_b. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
3493
+ }
3494
+ {
3495
+ let mut added_monitors = node_a. chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
3496
+ if pending_htlc_claims. 1 != 0 || pending_htlc_fails. 1 != 0 {
3497
+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
3498
+ } else {
3499
+ assert ! ( added_monitors. is_empty( ) ) ;
3500
+ }
3501
+ added_monitors. clear ( ) ;
3502
+ }
3503
+
3504
+ // We dont yet support both needing updates, as that would require a different commitment dance:
3505
+ assert ! ( ( pending_htlc_claims. 0 == 0 && pending_htlc_fails. 0 == 0 ) || ( pending_htlc_claims. 1 == 0 && pending_htlc_fails. 1 == 0 ) ) ;
3506
+
3507
+ for chan_msgs in resp_1. drain ( ..) {
3508
+ if pre_all_htlcs {
3509
+ let announcement_sigs_opt = node_a. node . handle_funding_locked ( & node_b. node . get_our_node_id ( ) , & chan_msgs. 0 . unwrap ( ) ) . unwrap ( ) ;
3510
+ //TODO
3511
+ } else {
3512
+ assert ! ( chan_msgs. 0 . is_none( ) ) ;
3513
+ }
3514
+ assert ! ( chan_msgs. 1 . is_none( ) ) ;
3515
+ if pending_htlc_claims. 0 != 0 || pending_htlc_fails. 0 != 0 {
3516
+ let commitment_update = chan_msgs. 2 . unwrap ( ) ;
3517
+ assert ! ( commitment_update. update_add_htlcs. is_empty( ) ) ; // We can't relay while disconnected
3518
+ assert_eq ! ( commitment_update. update_fulfill_htlcs. len( ) , pending_htlc_claims. 0 ) ;
3519
+ assert_eq ! ( commitment_update. update_fail_htlcs. len( ) , pending_htlc_fails. 0 ) ;
3520
+ assert ! ( commitment_update. update_fail_malformed_htlcs. is_empty( ) ) ;
3521
+ for update_fulfill in commitment_update. update_fulfill_htlcs {
3522
+ node_a. node . handle_update_fulfill_htlc ( & node_b. node . get_our_node_id ( ) , & update_fulfill) . unwrap ( ) ;
3523
+ }
3524
+ for update_fail in commitment_update. update_fail_htlcs {
3525
+ node_a. node . handle_update_fail_htlc ( & node_b. node . get_our_node_id ( ) , & update_fail) . unwrap ( ) ;
3526
+ }
3527
+
3528
+ commitment_signed_dance ! ( node_a, node_b, commitment_update. commitment_signed, false ) ;
3529
+ } else {
3530
+ assert ! ( chan_msgs. 2 . is_none( ) ) ;
3531
+ }
3532
+ }
3533
+
3534
+ for chan_msgs in resp_2. drain ( ..) {
3535
+ if pre_all_htlcs {
3536
+ let announcement_sigs_opt = node_b. node . handle_funding_locked ( & node_a. node . get_our_node_id ( ) , & chan_msgs. 0 . unwrap ( ) ) . unwrap ( ) ;
3537
+ //TODO
3538
+ } else {
3539
+ assert ! ( chan_msgs. 0 . is_none( ) ) ;
3540
+ }
3541
+ assert ! ( chan_msgs. 1 . is_none( ) ) ;
3542
+ if pending_htlc_claims. 1 != 0 || pending_htlc_fails. 1 != 0 {
3543
+ let commitment_update = chan_msgs. 2 . unwrap ( ) ;
3544
+ assert ! ( commitment_update. update_add_htlcs. is_empty( ) ) ; // We can't relay while disconnected
3545
+ assert_eq ! ( commitment_update. update_fulfill_htlcs. len( ) , pending_htlc_claims. 0 ) ;
3546
+ assert_eq ! ( commitment_update. update_fail_htlcs. len( ) , pending_htlc_fails. 0 ) ;
3547
+ assert ! ( commitment_update. update_fail_malformed_htlcs. is_empty( ) ) ;
3548
+ for update_fulfill in commitment_update. update_fulfill_htlcs {
3549
+ node_b. node . handle_update_fulfill_htlc ( & node_a. node . get_our_node_id ( ) , & update_fulfill) . unwrap ( ) ;
3550
+ }
3551
+ for update_fail in commitment_update. update_fail_htlcs {
3552
+ node_b. node . handle_update_fail_htlc ( & node_a. node . get_our_node_id ( ) , & update_fail) . unwrap ( ) ;
3553
+ }
3554
+
3555
+ commitment_signed_dance ! ( node_b, node_a, commitment_update. commitment_signed, false ) ;
3556
+ } else {
3557
+ assert ! ( chan_msgs. 2 . is_none( ) ) ;
3558
+ }
3559
+ }
3560
+ }
3561
+
3562
+ #[ test]
3563
+ fn test_simple_peer_disconnect ( ) {
3564
+ // Test that we can reconnect when there are no lost messages
3565
+ let nodes = create_network ( 3 ) ;
3566
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
3567
+ create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
3568
+
3569
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
3570
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
3571
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , true , ( 0 , 0 ) , ( 0 , 0 ) ) ;
3572
+
3573
+ let payment_preimage_1 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 0 ;
3574
+ let payment_hash_2 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 1 ;
3575
+ fail_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , payment_hash_2) ;
3576
+ claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , payment_preimage_1) ;
3577
+
3578
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
3579
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
3580
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , false , ( 0 , 0 ) , ( 0 , 0 ) ) ;
3581
+
3582
+ let payment_preimage_3 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 0 ;
3583
+ let payment_preimage_4 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 0 ;
3584
+ let payment_hash_5 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 1 ;
3585
+ let payment_hash_6 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 1 ;
3586
+
3587
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
3588
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
3589
+
3590
+ claim_payment_along_route ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , true , payment_preimage_3) ;
3591
+ fail_payment_along_route ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , true , payment_hash_5) ;
3592
+
3593
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , false , ( 1 , 0 ) , ( 1 , 0 ) ) ;
3594
+ {
3595
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
3596
+ assert_eq ! ( events. len( ) , 2 ) ;
3597
+ match events[ 0 ] {
3598
+ Event :: PaymentSent { payment_preimage } => {
3599
+ assert_eq ! ( payment_preimage, payment_preimage_3) ;
3600
+ } ,
3601
+ _ => panic ! ( "Unexpected event" ) ,
3602
+ }
3603
+ match events[ 1 ] {
3604
+ Event :: PaymentFailed { payment_hash } => {
3605
+ assert_eq ! ( payment_hash, payment_hash_5) ;
3606
+ } ,
3607
+ _ => panic ! ( "Unexpected event" ) ,
3608
+ }
3609
+ }
3610
+
3611
+ claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , payment_preimage_4) ;
3612
+ fail_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , payment_hash_6) ;
3613
+ }
3614
+
3472
3615
#[ test]
3473
3616
fn test_invalid_channel_announcement ( ) {
3474
3617
//Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs
0 commit comments