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