@@ -18,15 +18,16 @@ use bitcoin::network::constants::Network;
18
18
use chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr } ;
19
19
use chain:: transaction:: OutPoint ;
20
20
use chain:: Watch ;
21
- use ln:: channelmanager:: { RAACommitmentOrder , PaymentPreimage , PaymentHash , PaymentSecret , PaymentSendFailure } ;
21
+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , RAACommitmentOrder , PaymentPreimage , PaymentHash , PaymentSecret , PaymentSendFailure } ;
22
22
use ln:: features:: InitFeatures ;
23
23
use ln:: msgs;
24
24
use ln:: msgs:: { ChannelMessageHandler , ErrorAction , RoutingMessageHandler } ;
25
25
use routing:: router:: get_route;
26
+ use util:: config:: UserConfig ;
26
27
use util:: enforcing_trait_impls:: EnforcingChannelKeys ;
27
28
use util:: events:: { Event , EventsProvider , MessageSendEvent , MessageSendEventsProvider } ;
28
29
use util:: errors:: APIError ;
29
- use util:: ser:: Readable ;
30
+ use util:: ser:: { Readable , ReadableArgs , Writeable } ;
30
31
31
32
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
32
33
use bitcoin:: hashes:: Hash ;
@@ -35,6 +36,8 @@ use ln::functional_test_utils::*;
35
36
36
37
use util:: test_utils;
37
38
39
+ use std:: collections:: HashMap ;
40
+
38
41
// If persister_fail is true, we have the persister return a PermanentFailure
39
42
// instead of the higher-level ChainMonitor.
40
43
fn do_test_simple_monitor_permanent_update_fail ( persister_fail : bool ) {
@@ -1809,6 +1812,140 @@ fn monitor_update_claim_fail_no_response() {
1809
1812
claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_2, 1_000_000 ) ;
1810
1813
}
1811
1814
1815
+ #[ test]
1816
+ fn test_chan_reload_discard_outbound_holding ( ) {
1817
+ // Test that when we reload a ChannelManager from disk we discard (by failing backwards)
1818
+ // outbound HTLCs sitting in the holding cell. We currently assert that there are no holding
1819
+ // cell outbound HTLCs when we reconnect to a peer, so this would otherwise fail a
1820
+ // debug_assertion, but its also good hygiene - if we are sitting on an HTLC when we reload,
1821
+ // its reasonable to assume its been a while, and, short of having some criteria based on the
1822
+ // CLTV value, trying to forward it likely doesn't make sense.
1823
+ // chanmon_fail_consistency found the debug_assertion failure.
1824
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1825
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1826
+ let node_state_0;
1827
+ let new_chain_monitor;
1828
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1829
+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1830
+ create_announced_chan_between_nodes ( & nodes, 0 , 1 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) . 2 ;
1831
+ let logger = test_utils:: TestLogger :: new ( ) ;
1832
+
1833
+ // Start forwarding a payment, skipping the first RAA so A is in AwaitingRAA
1834
+ let ( payment_preimage_1, payment_hash_1) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1835
+ {
1836
+ let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler ;
1837
+ let route = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , & net_graph_msg_handler. network_graph . read ( ) . unwrap ( ) , & nodes[ 1 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV , & logger) . unwrap ( ) ;
1838
+ nodes[ 0 ] . node . send_payment ( & route, payment_hash_1, & None ) . unwrap ( ) ;
1839
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1840
+ }
1841
+
1842
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1843
+ assert_eq ! ( events. len( ) , 1 ) ;
1844
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1845
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
1846
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. commitment_msg ) ;
1847
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1848
+
1849
+ let ( bs_revoke_and_ack, bs_commitment_signed) = get_revoke_commit_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1850
+
1851
+ // Now forward a second payment, getting it stuck in A's outbound holding cell.
1852
+ let ( _, payment_hash_2) = get_payment_preimage_hash ! ( nodes[ 0 ] ) ;
1853
+ {
1854
+ let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler ;
1855
+ let route = get_route ( & nodes[ 0 ] . node . get_our_node_id ( ) , & net_graph_msg_handler. network_graph . read ( ) . unwrap ( ) , & nodes[ 1 ] . node . get_our_node_id ( ) , None , & Vec :: new ( ) , 1000000 , TEST_FINAL_CLTV , & logger) . unwrap ( ) ;
1856
+ nodes[ 0 ] . node . send_payment ( & route, payment_hash_2, & None ) . unwrap ( ) ;
1857
+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
1858
+ }
1859
+
1860
+ let node_state = nodes[ 0 ] . node . encode ( ) ;
1861
+ let mut chain_monitor_state = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
1862
+ let funding_outpoint = * nodes[ 0 ] . chain_monitor . chain_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . 0 ;
1863
+ nodes[ 0 ] . chain_monitor . chain_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . 1 . serialize_for_disk ( & mut chain_monitor_state) . unwrap ( ) ;
1864
+
1865
+ // Now if we pass the RAA back to A it should free the holding cell outbound HTLC.
1866
+ nodes[ 0 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_revoke_and_ack) ;
1867
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1868
+ events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1869
+ assert_eq ! ( events. len( ) , 1 ) ;
1870
+ let payment_event = SendEvent :: from_event ( events. pop ( ) . unwrap ( ) ) ;
1871
+ assert_eq ! ( payment_event. msgs. len( ) , 1 ) ;
1872
+
1873
+ // Reload A's ChannelManager/Monitor and make sure the reload generates a PaymentFailed for the
1874
+ // second payment.
1875
+ let mut chain_monitor = <( BlockHash , ChannelMonitor < EnforcingChannelKeys > ) >:: read ( & mut :: std:: io:: Cursor :: new ( chain_monitor_state. 0 ) ) . unwrap ( ) . 1 ;
1876
+ new_chain_monitor = test_utils:: TestChainMonitor :: new ( Some ( nodes[ 0 ] . chain_source ) , nodes[ 0 ] . tx_broadcaster . clone ( ) , & nodes[ 0 ] . logger , & node_cfgs[ 0 ] . fee_estimator , & chanmon_cfgs[ 0 ] . persister ) ;
1877
+ nodes[ 0 ] . chain_monitor = & new_chain_monitor;
1878
+ node_state_0 = {
1879
+ let mut channel_monitors = HashMap :: new ( ) ;
1880
+ channel_monitors. insert ( funding_outpoint, & mut chain_monitor) ;
1881
+ <( BlockHash , ChannelManager < EnforcingChannelKeys , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ) >:: read ( & mut :: std:: io:: Cursor :: new ( node_state) , ChannelManagerReadArgs {
1882
+ keys_manager : & nodes[ 0 ] . keys_manager ,
1883
+ fee_estimator : & node_cfgs[ 0 ] . fee_estimator ,
1884
+ chain_monitor : & nodes[ 0 ] . chain_monitor ,
1885
+ logger : & nodes[ 0 ] . logger ,
1886
+ tx_broadcaster : & nodes[ 0 ] . tx_broadcaster ,
1887
+ default_config : UserConfig :: default ( ) ,
1888
+ channel_monitors,
1889
+ } ) . unwrap ( ) . 1
1890
+ } ;
1891
+ nodes[ 0 ] . node = & node_state_0;
1892
+ assert ! ( nodes[ 0 ] . chain_monitor. watch_channel( funding_outpoint, chain_monitor) . is_ok( ) ) ;
1893
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1894
+
1895
+ let events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1896
+ assert_eq ! ( events. len( ) , 1 ) ;
1897
+ match events[ 0 ] {
1898
+ Event :: PaymentFailed { ref payment_hash, rejected_by_dest, .. } => {
1899
+ assert_eq ! ( * payment_hash, payment_hash_2) ;
1900
+ assert ! ( !rejected_by_dest) ;
1901
+ } ,
1902
+ _ => panic ! ( "Unexpected event" ) ,
1903
+ }
1904
+
1905
+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
1906
+
1907
+ nodes[ 0 ] . node . peer_connected ( & nodes[ 1 ] . node . get_our_node_id ( ) , & msgs:: Init { features : InitFeatures :: empty ( ) } ) ;
1908
+ nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) , & msgs:: Init { features : InitFeatures :: empty ( ) } ) ;
1909
+
1910
+ let node_0_reestablish = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1911
+ let node_1_reestablish = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendChannelReestablish , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1912
+
1913
+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & node_1_reestablish) ;
1914
+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & node_0_reestablish) ;
1915
+
1916
+ assert ! ( nodes[ 0 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
1917
+
1918
+ // Make sure nodes[1] rebroadcasts the undelivered messages:
1919
+ let node_1_msgs = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
1920
+ assert_eq ! ( node_1_msgs. len( ) , 2 ) ;
1921
+ match node_1_msgs[ 0 ] {
1922
+ MessageSendEvent :: SendRevokeAndACK { ref node_id, ref msg } => {
1923
+ assert_eq ! ( * node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
1924
+ assert ! ( * msg == bs_revoke_and_ack) ;
1925
+ } ,
1926
+ _ => panic ! ( ) ,
1927
+ }
1928
+ match node_1_msgs[ 1 ] {
1929
+ MessageSendEvent :: UpdateHTLCs { ref node_id, ref updates } => {
1930
+ assert_eq ! ( * node_id, nodes[ 0 ] . node. get_our_node_id( ) ) ;
1931
+ assert ! ( updates. commitment_signed == bs_commitment_signed) ;
1932
+ } ,
1933
+ _ => panic ! ( ) ,
1934
+ }
1935
+
1936
+ nodes[ 0 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_revoke_and_ack) ;
1937
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1938
+ nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_commitment_signed) ;
1939
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1940
+
1941
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
1942
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1943
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1944
+ expect_payment_received ! ( nodes[ 1 ] , payment_hash_1, 1_000_000 ) ;
1945
+
1946
+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_1, 1_000_000 ) ;
1947
+ }
1948
+
1812
1949
// confirm_a_first and restore_b_before_conf are wholly unrelated to earlier bools and
1813
1950
// restore_b_before_conf has no meaning if !confirm_a_first
1814
1951
fn do_during_funding_monitor_fail ( confirm_a_first : bool , restore_b_before_conf : bool ) {
0 commit comments