@@ -1823,6 +1823,132 @@ fn fake_network_test() {
1823
1823
close_channel ( & nodes[ 1 ] , & nodes[ 3 ] , & chan_5. 2 , chan_5. 3 , false ) ;
1824
1824
}
1825
1825
1826
+ #[ test]
1827
+ fn holding_cell_htlc_counting ( ) {
1828
+ // Tests that HTLCs in the holding cell count towards the pending HTLC limits on outbound HTLCs
1829
+ // to ensure we don't end up with HTLCs sitting around in our holding cell for several
1830
+ // commitment dance rounds.
1831
+ let mut nodes = create_network ( 3 ) ;
1832
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 ) ;
1833
+ let chan_2 = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) ;
1834
+
1835
+ let mut payments = Vec :: new ( ) ;
1836
+ for _ in 0 ..:: ln:: channel:: OUR_MAX_HTLCS {
1837
+ let route = nodes[ 1 ] . router . get_route ( & nodes[ 2 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 100000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1838
+ let ( payment_preimage, payment_hash) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1839
+ nodes[ 1 ] . node . send_payment ( route, payment_hash) . unwrap ( ) ;
1840
+ payments. push ( ( payment_preimage, payment_hash) ) ;
1841
+ }
1842
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1843
+
1844
+ let mut events = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1845
+ assert_eq ! ( events. len( ) , 1 ) ;
1846
+ let initial_payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1847
+ assert_eq ! ( initial_payment_event. node_id, nodes[ 2 ] . node. get_our_node_id( ) ) ;
1848
+
1849
+ // There is now one HTLC in an outbound commitment transaction and (OUR_MAX_HTLCS - 1) HTLCs in
1850
+ // the holding cell waiting on B's RAA to send. At this point we should not be able to add
1851
+ // another HTLC.
1852
+ let route = nodes[ 1 ] . router . get_route ( & nodes[ 2 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 100000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1853
+ let ( payment_preimage_1, payment_hash_1) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1854
+ if let APIError :: ChannelUnavailable { err } = nodes[ 1 ] . node . send_payment ( route, payment_hash_1) . unwrap_err ( ) {
1855
+ assert_eq ! ( err, "Cannot push more than their max accepted HTLCs" ) ;
1856
+ } else { panic ! ( "Unexpected event" ) ; }
1857
+
1858
+ // This should also be true if we try to forward a payment.
1859
+ let route = nodes[ 0 ] . router . get_route ( & nodes[ 2 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 100000 , TEST_FINAL_CLTV ) . unwrap ( ) ;
1860
+ let ( payment_preimage_2, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1861
+ nodes[ 0 ] . node . send_payment ( route, payment_hash_2) . unwrap ( ) ;
1862
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1863
+
1864
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1865
+ assert_eq ! ( events. len( ) , 1 ) ;
1866
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1867
+ assert_eq ! ( payment_event. node_id, nodes[ 1 ] . node. get_our_node_id( ) ) ;
1868
+
1869
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1870
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , payment_event. commitment_msg, false ) ;
1871
+ // We have to forward pending HTLCs twice - once tries to forward the payment forward (and
1872
+ // fails), the second will process the resulting failure.
1873
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1874
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1875
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1876
+
1877
+ let bs_fail_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1878
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_fail_updates. update_fail_htlcs [ 0 ] ) . unwrap ( ) ;
1879
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , bs_fail_updates. commitment_signed, false , true ) ;
1880
+
1881
+ let events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1882
+ assert_eq ! ( events. len( ) , 1 ) ;
1883
+ match events[ 0 ] {
1884
+ MessageSendEvent :: PaymentFailureNetworkUpdate { update : msgs:: HTLCFailChannelUpdate :: ChannelUpdateMessage { ref msg } } => {
1885
+ assert_eq ! ( msg. contents. short_channel_id, chan_2. 0 . contents. short_channel_id) ;
1886
+ } ,
1887
+ _ => panic ! ( "Unexpected event" ) ,
1888
+ }
1889
+
1890
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1891
+ assert_eq ! ( events. len( ) , 1 ) ;
1892
+ match events[ 0 ] {
1893
+ Event :: PaymentFailed { payment_hash, rejected_by_dest, .. } => {
1894
+ assert_eq ! ( payment_hash, payment_hash_2) ;
1895
+ assert ! ( !rejected_by_dest) ;
1896
+ } ,
1897
+ _ => panic ! ( "Unexpected event" ) ,
1898
+ }
1899
+
1900
+ // Now foward all the pending HTLCs and claim them back
1901
+ nodes[ 2 ] . node . handle_update_add_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & initial_payment_event. msgs [ 0 ] ) . unwrap ( ) ;
1902
+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & initial_payment_event. commitment_msg ) . unwrap ( ) ;
1903
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1904
+
1905
+ let ( bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1906
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & bs_revoke_and_ack) . unwrap ( ) ;
1907
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1908
+ let as_updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1909
+
1910
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & bs_commitment_signed) . unwrap ( ) ;
1911
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1912
+ let as_raa = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1913
+
1914
+ for ref update in as_updates. update_add_htlcs . iter ( ) {
1915
+ nodes[ 2 ] . node . handle_update_add_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , update) . unwrap ( ) ;
1916
+ }
1917
+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & as_updates. commitment_signed ) . unwrap ( ) ;
1918
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1919
+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & as_raa) . unwrap ( ) ;
1920
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1921
+ let ( bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1922
+
1923
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & bs_revoke_and_ack) . unwrap ( ) ;
1924
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1925
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & bs_commitment_signed) . unwrap ( ) ;
1926
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1927
+ let as_final_raa = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1928
+
1929
+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & as_final_raa) . unwrap ( ) ;
1930
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1931
+
1932
+ expect_pending_htlcs_forwardable ! ( nodes[ 2 ] ) ;
1933
+
1934
+ let events = nodes[ 2 ] . node . get_and_clear_pending_events ( ) ;
1935
+ assert_eq ! ( events. len( ) , payments. len( ) ) ;
1936
+ for ( ref event, & ( _, ref hash) ) in events. iter ( ) . zip ( payments. iter ( ) ) {
1937
+ match event {
1938
+ & Event :: PaymentReceived { ref payment_hash, .. } => {
1939
+ assert_eq ! ( * payment_hash, * hash) ;
1940
+ } ,
1941
+ _ => panic ! ( "Unexpected event" ) ,
1942
+ } ;
1943
+ }
1944
+
1945
+ for ( preimage, _) in payments. drain ( ..) {
1946
+ claim_payment ( & nodes[ 1 ] , & [ & nodes[ 2 ] ] , preimage) ;
1947
+ }
1948
+
1949
+ send_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1000000 ) ;
1950
+ }
1951
+
1826
1952
#[ test]
1827
1953
fn duplicate_htlc_test ( ) {
1828
1954
// Test that we accept duplicate payment_hash HTLCs across the network and that
0 commit comments