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