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