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