@@ -2746,3 +2746,219 @@ fn double_temp_error() {
27462746 commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , commitment_signed_b2, false ) ;
27472747 expect_payment_sent ! ( nodes[ 0 ] , payment_preimage_2) ;
27482748}
2749+
2750+ fn do_test_outbound_reload_without_init_mon ( use_0conf : bool ) {
2751+ // Test that if the monitor update generated in funding_signed is stored async and we restart
2752+ // with the latest ChannelManager but the ChannelMonitor persistence never completed we happily
2753+ // drop the channel and move on.
2754+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
2755+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
2756+
2757+ let persister: test_utils:: TestPersister ;
2758+ let new_chain_monitor: test_utils:: TestChainMonitor ;
2759+ let nodes_0_deserialized: ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ;
2760+
2761+ let mut chan_config = test_default_channel_config ( ) ;
2762+ chan_config. manually_accept_inbound_channels = true ;
2763+ chan_config. channel_handshake_limits . trust_own_funding_0conf = true ;
2764+
2765+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ Some ( chan_config) , Some ( chan_config) ] ) ;
2766+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
2767+
2768+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 43 , None ) . unwrap ( ) ;
2769+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
2770+
2771+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
2772+ assert_eq ! ( events. len( ) , 1 ) ;
2773+ match events[ 0 ] {
2774+ Event :: OpenChannelRequest { temporary_channel_id, .. } => {
2775+ if use_0conf {
2776+ nodes[ 1 ] . node . accept_inbound_channel_from_trusted_peer_0conf ( & temporary_channel_id, & nodes[ 0 ] . node . get_our_node_id ( ) , 0 ) . unwrap ( ) ;
2777+ } else {
2778+ nodes[ 1 ] . node . accept_inbound_channel ( & temporary_channel_id, & nodes[ 0 ] . node . get_our_node_id ( ) , 0 ) . unwrap ( ) ;
2779+ }
2780+ } ,
2781+ _ => panic ! ( "Unexpected event" ) ,
2782+ } ;
2783+
2784+ nodes[ 0 ] . node . handle_accept_channel ( & nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ) ;
2785+
2786+ let ( temporary_channel_id, funding_tx, ..) = create_funding_transaction ( & nodes[ 0 ] , & nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 43 ) ;
2787+
2788+ nodes[ 0 ] . node . funding_transaction_generated ( & temporary_channel_id, & nodes[ 1 ] . node . get_our_node_id ( ) , funding_tx. clone ( ) ) . unwrap ( ) ;
2789+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
2790+
2791+ let funding_created_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingCreated , nodes[ 1 ] . node. get_our_node_id( ) ) ;
2792+ nodes[ 1 ] . node . handle_funding_created ( & nodes[ 0 ] . node . get_our_node_id ( ) , & funding_created_msg) ;
2793+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
2794+
2795+ let bs_signed_locked = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
2796+ assert_eq ! ( bs_signed_locked. len( ) , if use_0conf { 2 } else { 1 } ) ;
2797+ match & bs_signed_locked[ 0 ] {
2798+ MessageSendEvent :: SendFundingSigned { msg, .. } => {
2799+ chanmon_cfgs[ 0 ] . persister . set_update_ret ( ChannelMonitorUpdateResult :: UpdateInProgress ) ;
2800+
2801+ nodes[ 0 ] . node . handle_funding_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & msg) ;
2802+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
2803+ }
2804+ _ => panic ! ( "Unexpected event" ) ,
2805+ }
2806+ if use_0conf {
2807+ match & bs_signed_locked[ 1 ] {
2808+ MessageSendEvent :: SendChannelReady { msg, .. } => {
2809+ nodes[ 0 ] . node . handle_channel_ready ( & nodes[ 1 ] . node . get_our_node_id ( ) , & msg) ;
2810+ }
2811+ _ => panic ! ( "Unexpected event" ) ,
2812+ }
2813+ }
2814+
2815+ assert ! ( nodes[ 0 ] . tx_broadcaster. txn_broadcasted. lock( ) . unwrap( ) . is_empty( ) ) ;
2816+ assert ! ( nodes[ 0 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
2817+ assert ! ( nodes[ 0 ] . node. get_and_clear_pending_events( ) . is_empty( ) ) ;
2818+
2819+ // nodes[0] is now waiting on the first ChannelMonitor persistence to complete in order to
2820+ // broadcast the funding transaction. If nodes[0] restarts at this point with the
2821+ // ChannelMonitor lost, we should simply discard the channel.
2822+
2823+ // The test framework checks that watched_txn/outputs match the monitor set, which they will
2824+ // not, so we have to clear them here.
2825+ nodes[ 0 ] . chain_source . watched_txn . lock ( ) . unwrap ( ) . clear ( ) ;
2826+ nodes[ 0 ] . chain_source . watched_outputs . lock ( ) . unwrap ( ) . clear ( ) ;
2827+
2828+ let nodes_0_serialized = nodes[ 0 ] . node . encode ( ) ;
2829+ persister = test_utils:: TestPersister :: new ( ) ;
2830+ let keys_manager = & chanmon_cfgs[ 0 ] . keys_manager ;
2831+ 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) ;
2832+ nodes[ 0 ] . chain_monitor = & new_chain_monitor;
2833+
2834+ let mut nodes_0_read = & nodes_0_serialized[ ..] ;
2835+ let config = UserConfig :: default ( ) ;
2836+ nodes_0_deserialized = {
2837+ <( BlockHash , ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ) >:: read ( & mut nodes_0_read, ChannelManagerReadArgs {
2838+ default_config : config,
2839+ keys_manager,
2840+ fee_estimator : node_cfgs[ 0 ] . fee_estimator ,
2841+ chain_monitor : nodes[ 0 ] . chain_monitor ,
2842+ tx_broadcaster : nodes[ 0 ] . tx_broadcaster . clone ( ) ,
2843+ logger : nodes[ 0 ] . logger ,
2844+ channel_monitors : HashMap :: new ( ) ,
2845+ } ) . unwrap ( ) . 1
2846+ } ;
2847+ nodes[ 0 ] . node = & nodes_0_deserialized;
2848+ assert ! ( nodes_0_read. is_empty( ) ) ;
2849+
2850+ check_closed_event ! ( nodes[ 0 ] , 1 , ClosureReason :: DisconnectedPeer ) ;
2851+ }
2852+
2853+ #[ test]
2854+ fn test_outbound_reload_without_init_mon ( ) {
2855+ do_test_outbound_reload_without_init_mon ( true ) ;
2856+ do_test_outbound_reload_without_init_mon ( false ) ;
2857+ }
2858+
2859+ fn do_test_inbound_reload_without_init_mon ( use_0conf : bool , lock_commitment : bool ) {
2860+ // Test that if the monitor update generated by funding_generated is stored async and we
2861+ // restart with the latest ChannelManager but the ChannelMonitor persistence never completed we
2862+ // happily drop the channel and move on.
2863+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
2864+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
2865+
2866+ let persister: test_utils:: TestPersister ;
2867+ let new_chain_monitor: test_utils:: TestChainMonitor ;
2868+ let nodes_1_deserialized: ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ;
2869+
2870+ let mut chan_config = test_default_channel_config ( ) ;
2871+ chan_config. manually_accept_inbound_channels = true ;
2872+ chan_config. channel_handshake_limits . trust_own_funding_0conf = true ;
2873+
2874+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ Some ( chan_config) , Some ( chan_config) ] ) ;
2875+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
2876+
2877+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 10001 , 43 , None ) . unwrap ( ) ;
2878+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ) ;
2879+
2880+ let events = nodes[ 1 ] . node . get_and_clear_pending_events ( ) ;
2881+ assert_eq ! ( events. len( ) , 1 ) ;
2882+ match events[ 0 ] {
2883+ Event :: OpenChannelRequest { temporary_channel_id, .. } => {
2884+ if use_0conf {
2885+ nodes[ 1 ] . node . accept_inbound_channel_from_trusted_peer_0conf ( & temporary_channel_id, & nodes[ 0 ] . node . get_our_node_id ( ) , 0 ) . unwrap ( ) ;
2886+ } else {
2887+ nodes[ 1 ] . node . accept_inbound_channel ( & temporary_channel_id, & nodes[ 0 ] . node . get_our_node_id ( ) , 0 ) . unwrap ( ) ;
2888+ }
2889+ } ,
2890+ _ => panic ! ( "Unexpected event" ) ,
2891+ } ;
2892+
2893+ nodes[ 0 ] . node . handle_accept_channel ( & nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ) ;
2894+
2895+ let ( temporary_channel_id, funding_tx, ..) = create_funding_transaction ( & nodes[ 0 ] , & nodes[ 1 ] . node . get_our_node_id ( ) , 100000 , 43 ) ;
2896+
2897+ nodes[ 0 ] . node . funding_transaction_generated ( & temporary_channel_id, & nodes[ 1 ] . node . get_our_node_id ( ) , funding_tx. clone ( ) ) . unwrap ( ) ;
2898+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
2899+
2900+ let funding_created_msg = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendFundingCreated , nodes[ 1 ] . node. get_our_node_id( ) ) ;
2901+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateResult :: UpdateInProgress ) ;
2902+ nodes[ 1 ] . node . handle_funding_created ( & nodes[ 0 ] . node . get_our_node_id ( ) , & funding_created_msg) ;
2903+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
2904+
2905+ // nodes[1] happily sends its funding_signed even though its awaiting the persistence of the
2906+ // initial ChannelMonitor, but it will decline to send its channel_ready even if the funding
2907+ // transaction is confirmed.
2908+ let funding_signed_msg = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendFundingSigned , nodes[ 0 ] . node. get_our_node_id( ) ) ;
2909+
2910+ nodes[ 0 ] . node . handle_funding_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & funding_signed_msg) ;
2911+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
2912+
2913+ let as_funding_tx = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
2914+ if lock_commitment {
2915+ confirm_transaction ( & nodes[ 0 ] , & as_funding_tx[ 0 ] ) ;
2916+ confirm_transaction ( & nodes[ 1 ] , & as_funding_tx[ 0 ] ) ;
2917+ }
2918+ if use_0conf || lock_commitment {
2919+ let as_ready = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendChannelReady , nodes[ 1 ] . node. get_our_node_id( ) ) ;
2920+ nodes[ 1 ] . node . handle_channel_ready ( & nodes[ 0 ] . node . get_our_node_id ( ) , & as_ready) ;
2921+ }
2922+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
2923+
2924+ // nodes[1] is now waiting on the first ChannelMonitor persistence to complete in order to
2925+ // move the channel to ready (or is waiting on the funding transaction to confirm). If nodes[1]
2926+ // restarts at this point with the ChannelMonitor lost, we should simply discard the channel.
2927+
2928+ // The test framework checks that watched_txn/outputs match the monitor set, which they will
2929+ // not, so we have to clear them here.
2930+ nodes[ 1 ] . chain_source . watched_txn . lock ( ) . unwrap ( ) . clear ( ) ;
2931+ nodes[ 1 ] . chain_source . watched_outputs . lock ( ) . unwrap ( ) . clear ( ) ;
2932+
2933+ let nodes_1_serialized = nodes[ 1 ] . node . encode ( ) ;
2934+ persister = test_utils:: TestPersister :: new ( ) ;
2935+ let keys_manager = & chanmon_cfgs[ 1 ] . keys_manager ;
2936+ new_chain_monitor = test_utils:: TestChainMonitor :: new ( Some ( nodes[ 1 ] . chain_source ) , nodes[ 1 ] . tx_broadcaster . clone ( ) , nodes[ 1 ] . logger , node_cfgs[ 1 ] . fee_estimator , & persister, keys_manager) ;
2937+ nodes[ 1 ] . chain_monitor = & new_chain_monitor;
2938+
2939+ let mut nodes_1_read = & nodes_1_serialized[ ..] ;
2940+ let config = UserConfig :: default ( ) ;
2941+ nodes_1_deserialized = {
2942+ <( BlockHash , ChannelManager < EnforcingSigner , & test_utils:: TestChainMonitor , & test_utils:: TestBroadcaster , & test_utils:: TestKeysInterface , & test_utils:: TestFeeEstimator , & test_utils:: TestLogger > ) >:: read ( & mut nodes_1_read, ChannelManagerReadArgs {
2943+ default_config : config,
2944+ keys_manager,
2945+ fee_estimator : node_cfgs[ 1 ] . fee_estimator ,
2946+ chain_monitor : nodes[ 1 ] . chain_monitor ,
2947+ tx_broadcaster : nodes[ 1 ] . tx_broadcaster . clone ( ) ,
2948+ logger : nodes[ 1 ] . logger ,
2949+ channel_monitors : HashMap :: new ( ) ,
2950+ } ) . unwrap ( ) . 1
2951+ } ;
2952+ nodes[ 1 ] . node = & nodes_1_deserialized;
2953+ assert ! ( nodes_1_read. is_empty( ) ) ;
2954+
2955+ check_closed_event ! ( nodes[ 1 ] , 1 , ClosureReason :: DisconnectedPeer ) ;
2956+ }
2957+
2958+ #[ test]
2959+ fn test_inbound_reload_without_init_mon ( ) {
2960+ do_test_inbound_reload_without_init_mon ( true , true ) ;
2961+ do_test_inbound_reload_without_init_mon ( true , false ) ;
2962+ do_test_inbound_reload_without_init_mon ( false , true ) ;
2963+ do_test_inbound_reload_without_init_mon ( false , false ) ;
2964+ }
0 commit comments