@@ -18,11 +18,12 @@ use bitcoin::network::constants::Network;
1818use chain:: channelmonitor:: { ChannelMonitor , ChannelMonitorUpdateErr } ;
1919use chain:: transaction:: OutPoint ;
2020use chain:: Watch ;
21- use ln:: channelmanager:: { RAACommitmentOrder , PaymentPreimage , PaymentHash , PaymentSecret , PaymentSendFailure } ;
21+ use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , RAACommitmentOrder , PaymentPreimage , PaymentHash , PaymentSecret , PaymentSendFailure } ;
2222use ln:: features:: InitFeatures ;
2323use ln:: msgs;
2424use ln:: msgs:: { ChannelMessageHandler , ErrorAction , RoutingMessageHandler } ;
2525use routing:: router:: get_route;
26+ use util:: config:: UserConfig ;
2627use util:: enforcing_trait_impls:: EnforcingSigner ;
2728use util:: events:: { Event , EventsProvider , MessageSendEvent , MessageSendEventsProvider } ;
2829use util:: errors:: APIError ;
@@ -35,6 +36,8 @@ use ln::functional_test_utils::*;
3536
3637use util:: test_utils;
3738
39+ use std:: collections:: HashMap ;
40+
3841// If persister_fail is true, we have the persister return a PermanentFailure
3942// instead of the higher-level ChainMonitor.
4043fn do_test_simple_monitor_permanent_update_fail ( persister_fail : bool ) {
@@ -1972,3 +1975,199 @@ fn test_path_paused_mpp() {
19721975
19731976 claim_payment_along_route_with_secret ( & nodes[ 0 ] , & [ & [ & nodes[ 1 ] , & nodes[ 3 ] ] , & [ & nodes[ 2 ] , & nodes[ 3 ] ] ] , false , payment_preimage, Some ( payment_secret) , 200_000 ) ;
19741977}
1978+
1979+ fn do_channel_holding_cell_serialize ( disconnect : bool , reload_a : bool ) {
1980+ // Tests that, when we serialize a channel with AddHTLC entries in the holding cell, we
1981+ // properly free them on reconnect. We previously failed such HTLCs upon serialization, but
1982+ // that behavior was both somewhat unexpected and also broken (there was a debug assertion
1983+ // which failed in such a case).
1984+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1985+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1986+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1987+ let persister: test_utils:: TestPersister ;
1988+ let new_chain_monitor: test_utils:: TestChainMonitor ;
1989+ let nodes_0_deserialized: ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ;
1990+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1991+
1992+ let chan_id = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 15_000_000 , 7_000_000_000 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) . 2 ;
1993+ let ( payment_preimage_1, payment_hash_1) = get_payment_preimage_hash ! ( & nodes[ 0 ] ) ;
1994+ let ( payment_preimage_2, payment_hash_2) = get_payment_preimage_hash ! ( & nodes[ 0 ] ) ;
1995+
1996+ // Do a really complicated dance to get an HTLC into the holding cell, with MonitorUpdateFailed
1997+ // set but AwaitingRemoteRevoke unset. When this test was written, any attempts to send an HTLC
1998+ // while MonitorUpdateFailed is set are immediately failed-backwards. Thus, the only way to get
1999+ // an AddHTLC into the holding cell is to add it while AwaitingRemoteRevoke is set but
2000+ // MonitorUpdateFailed is unset, and then swap the flags.
2001+ //
2002+ // We do this by:
2003+ // a) routing a payment from node B to node A,
2004+ // b) sending a payment from node A to node B without delivering any of the generated messages,
2005+ // putting node A in AwaitingRemoteRevoke,
2006+ // c) sending a second payment from node A to node B, which is immediately placed in the
2007+ // holding cell,
2008+ // d) claiming the first payment from B, allowing us to fail the monitor update which occurs
2009+ // when we try to persist the payment preimage,
2010+ // e) delivering A's commitment_signed from (b) and the resulting B revoke_and_ack message,
2011+ // clearing AwaitingRemoteRevoke on node A.
2012+ //
2013+ // Note that because, at the end, MonitorUpdateFailed is still set, the HTLC generated in (c)
2014+ // will not be freed from the holding cell.
2015+ let ( payment_preimage_0, payment_hash_0) = route_payment ( & nodes[ 1 ] , & [ & nodes[ 0 ] ] , 100000 ) ;
2016+
2017+ let route = {
2018+ let net_graph_msg_handler = & nodes[ 0 ] . net_graph_msg_handler ;
2019+ 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 ( ) , 100000 , TEST_FINAL_CLTV , nodes[ 0 ] . logger ) . unwrap ( )
2020+ } ;
2021+
2022+ nodes[ 0 ] . node . send_payment ( & route, payment_hash_1, & None ) . unwrap ( ) ;
2023+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
2024+ let send = SendEvent :: from_node ( & nodes[ 0 ] ) ;
2025+ assert_eq ! ( send. msgs. len( ) , 1 ) ;
2026+
2027+ nodes[ 0 ] . node . send_payment ( & route, payment_hash_2, & None ) . unwrap ( ) ;
2028+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
2029+
2030+ * nodes[ 0 ] . chain_monitor . update_ret . lock ( ) . unwrap ( ) = Some ( Err ( ChannelMonitorUpdateErr :: TemporaryFailure ) ) ;
2031+ assert ! ( nodes[ 0 ] . node. claim_funds( payment_preimage_0, & None , 100000 ) ) ;
2032+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
2033+
2034+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & send. msgs [ 0 ] ) ;
2035+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & send. commitment_msg ) ;
2036+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
2037+
2038+ let ( raa, cs) = get_revoke_commit_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
2039+
2040+ nodes[ 0 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & raa) ;
2041+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
2042+
2043+ if disconnect {
2044+ // Optionally reload nodes[0] entirely through a serialization roundtrip, otherwise just
2045+ // disconnect the peers. Note that the fuzzer originally found this issue because
2046+ // deserializing a ChannelManager in this state causes an assertion failure.
2047+ if reload_a {
2048+ let nodes_0_serialized = nodes[ 0 ] . node . encode ( ) ;
2049+ let mut chan_0_monitor_serialized = test_utils:: TestVecWriter ( Vec :: new ( ) ) ;
2050+ nodes[ 0 ] . chain_monitor . chain_monitor . monitors . lock ( ) . unwrap ( ) . iter ( ) . next ( ) . unwrap ( ) . 1 . write ( & mut chan_0_monitor_serialized) . unwrap ( ) ;
2051+
2052+ persister = test_utils:: TestPersister :: new ( ) ;
2053+ let keys_manager = & chanmon_cfgs[ 0 ] . keys_manager ;
2054+ 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 , & persister, keys_manager) ;
2055+ nodes[ 0 ] . chain_monitor = & new_chain_monitor;
2056+ let mut chan_0_monitor_read = & chan_0_monitor_serialized. 0 [ ..] ;
2057+ let ( _, mut chan_0_monitor) = <( BlockHash , ChannelMonitor < EnforcingSigner > ) >:: read (
2058+ & mut chan_0_monitor_read, keys_manager) . unwrap ( ) ;
2059+ assert ! ( chan_0_monitor_read. is_empty( ) ) ;
2060+
2061+ let mut nodes_0_read = & nodes_0_serialized[ ..] ;
2062+ let config = UserConfig :: default ( ) ;
2063+ nodes_0_deserialized = {
2064+ let mut channel_monitors = HashMap :: new ( ) ;
2065+ channel_monitors. insert ( chan_0_monitor. get_funding_txo ( ) . 0 , & mut chan_0_monitor) ;
2066+ <( BlockHash , ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ) >:: read ( & mut nodes_0_read, ChannelManagerReadArgs {
2067+ default_config : config,
2068+ keys_manager,
2069+ fee_estimator : node_cfgs[ 0 ] . fee_estimator ,
2070+ chain_monitor : nodes[ 0 ] . chain_monitor ,
2071+ tx_broadcaster : nodes[ 0 ] . tx_broadcaster . clone ( ) ,
2072+ logger : nodes[ 0 ] . logger ,
2073+ channel_monitors,
2074+ } ) . unwrap ( ) . 1
2075+ } ;
2076+ nodes[ 0 ] . node = & nodes_0_deserialized;
2077+ assert ! ( nodes_0_read. is_empty( ) ) ;
2078+
2079+ nodes[ 0 ] . chain_monitor . watch_channel ( chan_0_monitor. get_funding_txo ( ) . 0 . clone ( ) , chan_0_monitor) . unwrap ( ) ;
2080+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
2081+ } else {
2082+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) , false ) ;
2083+ }
2084+ nodes[ 1 ] . node . peer_disconnected ( & nodes[ 0 ] . node . get_our_node_id ( ) , false ) ;
2085+
2086+ // Now reconnect the two
2087+ nodes[ 0 ] . node . peer_connected ( & nodes[ 1 ] . node . get_our_node_id ( ) , & msgs:: Init { features : InitFeatures :: empty ( ) } ) ;
2088+ let reestablish_1 = get_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
2089+ assert_eq ! ( reestablish_1. len( ) , 1 ) ;
2090+ nodes[ 1 ] . node . peer_connected ( & nodes[ 0 ] . node . get_our_node_id ( ) , & msgs:: Init { features : InitFeatures :: empty ( ) } ) ;
2091+ let reestablish_2 = get_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
2092+ assert_eq ! ( reestablish_2. len( ) , 1 ) ;
2093+
2094+ nodes[ 1 ] . node . handle_channel_reestablish ( & nodes[ 0 ] . node . get_our_node_id ( ) , & reestablish_1[ 0 ] ) ;
2095+ let resp_1 = handle_chan_reestablish_msgs ! ( nodes[ 1 ] , nodes[ 0 ] ) ;
2096+ check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
2097+
2098+ nodes[ 0 ] . node . handle_channel_reestablish ( & nodes[ 1 ] . node . get_our_node_id ( ) , & reestablish_2[ 0 ] ) ;
2099+ let resp_0 = handle_chan_reestablish_msgs ! ( nodes[ 0 ] , nodes[ 1 ] ) ;
2100+
2101+ assert ! ( resp_0. 0 . is_none( ) ) ;
2102+ assert ! ( resp_0. 1 . is_none( ) ) ;
2103+ assert ! ( resp_0. 2 . is_none( ) ) ;
2104+ assert ! ( resp_1. 0 . is_none( ) ) ;
2105+ assert ! ( resp_1. 1 . is_none( ) ) ;
2106+
2107+ // Check that the freshly-generated cs is equal to the original (which we will deliver in a
2108+ // moment).
2109+ if let Some ( pending_cs) = resp_1. 2 {
2110+ assert ! ( pending_cs. update_add_htlcs. is_empty( ) ) ;
2111+ assert ! ( pending_cs. update_fail_htlcs. is_empty( ) ) ;
2112+ assert ! ( pending_cs. update_fulfill_htlcs. is_empty( ) ) ;
2113+ assert_eq ! ( pending_cs. commitment_signed, cs) ;
2114+ } else { panic ! ( ) ; }
2115+
2116+ // There should be no monitor updates as we are still pending awaiting a failed one.
2117+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
2118+ check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
2119+ }
2120+
2121+ // If we finish updating the monitor, we should free the holding cell right away (this did
2122+ // not occur prior to #756).
2123+ * nodes[ 0 ] . chain_monitor . update_ret . lock ( ) . unwrap ( ) = None ;
2124+ let ( funding_txo, mon_id) = nodes[ 0 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id) . unwrap ( ) . clone ( ) ;
2125+ nodes[ 0 ] . node . channel_monitor_updated ( & funding_txo, mon_id) ;
2126+
2127+ // New outbound messages should be generated immediately.
2128+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
2129+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
2130+ assert_eq ! ( events. len( ) , 1 ) ;
2131+
2132+ // Deliver the pending in-flight CS
2133+ nodes[ 0 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & cs) ;
2134+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
2135+
2136+ let commitment_msg = match events. pop ( ) . unwrap ( ) {
2137+ MessageSendEvent :: UpdateHTLCs { node_id, updates } => {
2138+ assert ! ( updates. update_fail_htlcs. is_empty( ) ) ;
2139+ assert ! ( updates. update_fail_malformed_htlcs. is_empty( ) ) ;
2140+ assert ! ( updates. update_fee. is_none( ) ) ;
2141+ assert_eq ! ( updates. update_fulfill_htlcs. len( ) , 1 ) ;
2142+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & updates. update_fulfill_htlcs [ 0 ] ) ;
2143+ expect_payment_sent ! ( nodes[ 1 ] , payment_preimage_0) ;
2144+ assert_eq ! ( updates. update_add_htlcs. len( ) , 1 ) ;
2145+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & updates. update_add_htlcs [ 0 ] ) ;
2146+ updates. commitment_signed
2147+ } ,
2148+ _ => panic ! ( "Unexpected event type!" ) ,
2149+ } ;
2150+
2151+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 0 ] . node . get_our_node_id ( ) , & commitment_msg) ;
2152+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
2153+
2154+ let as_revoke_and_ack = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 1 ] . node. get_our_node_id( ) ) ;
2155+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_revoke_and_ack) ;
2156+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
2157+ expect_payment_received ! ( nodes[ 1 ] , payment_hash_1, 100000 ) ;
2158+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
2159+
2160+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , ( ) , false , true , false ) ;
2161+
2162+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
2163+ expect_payment_received ! ( nodes[ 1 ] , payment_hash_2, 100000 ) ;
2164+
2165+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_1, 100000 ) ;
2166+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , payment_preimage_2, 100000 ) ;
2167+ }
2168+ #[ test]
2169+ fn channel_holding_cell_serialize ( ) {
2170+ do_channel_holding_cell_serialize ( true , true ) ;
2171+ do_channel_holding_cell_serialize ( true , false ) ;
2172+ do_channel_holding_cell_serialize ( false , true ) ; // last arg doesn't matter
2173+ }
0 commit comments