@@ -3639,6 +3639,149 @@ mod tests {
3639
3639
assert_eq ! ( channel_state. short_to_id. len( ) , 0 ) ;
3640
3640
}
3641
3641
3642
+ fn reconnect_nodes ( node_a : & Node , node_b : & Node , pre_all_htlcs : bool , pending_htlc_claims : ( usize , usize ) , pending_htlc_fails : ( usize , usize ) ) {
3643
+ let reestablish_1 = node_a. node . peer_connected ( & node_b. node . get_our_node_id ( ) ) ;
3644
+ let reestablish_2 = node_b. node . peer_connected ( & node_a. node . get_our_node_id ( ) ) ;
3645
+
3646
+ let mut resp_1 = Vec :: new ( ) ;
3647
+ for msg in reestablish_1 {
3648
+ resp_1. push ( node_b. node . handle_channel_reestablish ( & node_a. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
3649
+ }
3650
+ {
3651
+ let mut added_monitors = node_b. chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
3652
+ if pending_htlc_claims. 0 != 0 || pending_htlc_fails. 0 != 0 {
3653
+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
3654
+ } else {
3655
+ assert ! ( added_monitors. is_empty( ) ) ;
3656
+ }
3657
+ added_monitors. clear ( ) ;
3658
+ }
3659
+
3660
+ let mut resp_2 = Vec :: new ( ) ;
3661
+ for msg in reestablish_2 {
3662
+ resp_2. push ( node_a. node . handle_channel_reestablish ( & node_b. node . get_our_node_id ( ) , & msg) . unwrap ( ) ) ;
3663
+ }
3664
+ {
3665
+ let mut added_monitors = node_a. chan_monitor . added_monitors . lock ( ) . unwrap ( ) ;
3666
+ if pending_htlc_claims. 1 != 0 || pending_htlc_fails. 1 != 0 {
3667
+ assert_eq ! ( added_monitors. len( ) , 1 ) ;
3668
+ } else {
3669
+ assert ! ( added_monitors. is_empty( ) ) ;
3670
+ }
3671
+ added_monitors. clear ( ) ;
3672
+ }
3673
+
3674
+ // We dont yet support both needing updates, as that would require a different commitment dance:
3675
+ assert ! ( ( pending_htlc_claims. 0 == 0 && pending_htlc_fails. 0 == 0 ) || ( pending_htlc_claims. 1 == 0 && pending_htlc_fails. 1 == 0 ) ) ;
3676
+
3677
+ for chan_msgs in resp_1. drain ( ..) {
3678
+ if pre_all_htlcs {
3679
+ let announcement_sigs_opt = node_a. node . handle_funding_locked ( & node_b. node . get_our_node_id ( ) , & chan_msgs. 0 . unwrap ( ) ) . unwrap ( ) ;
3680
+ //TODO
3681
+ } else {
3682
+ assert ! ( chan_msgs. 0 . is_none( ) ) ;
3683
+ }
3684
+ assert ! ( chan_msgs. 1 . is_none( ) ) ;
3685
+ if pending_htlc_claims. 0 != 0 || pending_htlc_fails. 0 != 0 {
3686
+ let commitment_update = chan_msgs. 2 . unwrap ( ) ;
3687
+ assert ! ( commitment_update. update_add_htlcs. is_empty( ) ) ; // We can't relay while disconnected
3688
+ assert_eq ! ( commitment_update. update_fulfill_htlcs. len( ) , pending_htlc_claims. 0 ) ;
3689
+ assert_eq ! ( commitment_update. update_fail_htlcs. len( ) , pending_htlc_fails. 0 ) ;
3690
+ assert ! ( commitment_update. update_fail_malformed_htlcs. is_empty( ) ) ;
3691
+ for update_fulfill in commitment_update. update_fulfill_htlcs {
3692
+ node_a. node . handle_update_fulfill_htlc ( & node_b. node . get_our_node_id ( ) , & update_fulfill) . unwrap ( ) ;
3693
+ }
3694
+ for update_fail in commitment_update. update_fail_htlcs {
3695
+ node_a. node . handle_update_fail_htlc ( & node_b. node . get_our_node_id ( ) , & update_fail) . unwrap ( ) ;
3696
+ }
3697
+
3698
+ commitment_signed_dance ! ( node_a, node_b, commitment_update. commitment_signed, false ) ;
3699
+ } else {
3700
+ assert ! ( chan_msgs. 2 . is_none( ) ) ;
3701
+ }
3702
+ }
3703
+
3704
+ for chan_msgs in resp_2. drain ( ..) {
3705
+ if pre_all_htlcs {
3706
+ let announcement_sigs_opt = node_b. node . handle_funding_locked ( & node_a. node . get_our_node_id ( ) , & chan_msgs. 0 . unwrap ( ) ) . unwrap ( ) ;
3707
+ //TODO
3708
+ } else {
3709
+ assert ! ( chan_msgs. 0 . is_none( ) ) ;
3710
+ }
3711
+ assert ! ( chan_msgs. 1 . is_none( ) ) ;
3712
+ if pending_htlc_claims. 1 != 0 || pending_htlc_fails. 1 != 0 {
3713
+ let commitment_update = chan_msgs. 2 . unwrap ( ) ;
3714
+ assert ! ( commitment_update. update_add_htlcs. is_empty( ) ) ; // We can't relay while disconnected
3715
+ assert_eq ! ( commitment_update. update_fulfill_htlcs. len( ) , pending_htlc_claims. 0 ) ;
3716
+ assert_eq ! ( commitment_update. update_fail_htlcs. len( ) , pending_htlc_fails. 0 ) ;
3717
+ assert ! ( commitment_update. update_fail_malformed_htlcs. is_empty( ) ) ;
3718
+ for update_fulfill in commitment_update. update_fulfill_htlcs {
3719
+ node_b. node . handle_update_fulfill_htlc ( & node_a. node . get_our_node_id ( ) , & update_fulfill) . unwrap ( ) ;
3720
+ }
3721
+ for update_fail in commitment_update. update_fail_htlcs {
3722
+ node_b. node . handle_update_fail_htlc ( & node_a. node . get_our_node_id ( ) , & update_fail) . unwrap ( ) ;
3723
+ }
3724
+
3725
+ commitment_signed_dance ! ( node_b, node_a, commitment_update. commitment_signed, false ) ;
3726
+ } else {
3727
+ assert ! ( chan_msgs. 2 . is_none( ) ) ;
3728
+ }
3729
+ }
3730
+ }
3731
+
3732
+ #[ test]
3733
+ fn test_simple_peer_disconnect ( ) {
3734
+ // Test that we can reconnect when there are no lost messages
3735
+ let nodes = create_network ( 3 ) ;
3736
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
3737
+ create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
3738
+
3739
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
3740
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
3741
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , true , ( 0 , 0 ) , ( 0 , 0 ) ) ;
3742
+
3743
+ let payment_preimage_1 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 0 ;
3744
+ let payment_hash_2 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 1 ;
3745
+ fail_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , payment_hash_2) ;
3746
+ claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , payment_preimage_1) ;
3747
+
3748
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
3749
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
3750
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , false , ( 0 , 0 ) , ( 0 , 0 ) ) ;
3751
+
3752
+ let payment_preimage_3 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 0 ;
3753
+ let payment_preimage_4 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 0 ;
3754
+ let payment_hash_5 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 1 ;
3755
+ let payment_hash_6 = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 1000000 ) . 1 ;
3756
+
3757
+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
3758
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
3759
+
3760
+ claim_payment_along_route ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , true , payment_preimage_3) ;
3761
+ fail_payment_along_route ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , true , payment_hash_5) ;
3762
+
3763
+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , false , ( 1 , 0 ) , ( 1 , 0 ) ) ;
3764
+ {
3765
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
3766
+ assert_eq ! ( events. len( ) , 2 ) ;
3767
+ match events[ 0 ] {
3768
+ Event :: PaymentSent { payment_preimage } => {
3769
+ assert_eq ! ( payment_preimage, payment_preimage_3) ;
3770
+ } ,
3771
+ _ => panic ! ( "Unexpected event" ) ,
3772
+ }
3773
+ match events[ 1 ] {
3774
+ Event :: PaymentFailed { payment_hash } => {
3775
+ assert_eq ! ( payment_hash, payment_hash_5) ;
3776
+ } ,
3777
+ _ => panic ! ( "Unexpected event" ) ,
3778
+ }
3779
+ }
3780
+
3781
+ claim_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , payment_preimage_4) ;
3782
+ fail_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) , payment_hash_6) ;
3783
+ }
3784
+
3642
3785
#[ test]
3643
3786
fn test_invalid_channel_announcement ( ) {
3644
3787
//Test BOLT 7 channel_announcement msg requirement for final node, gather data to build customed channel_announcement msgs
0 commit comments