@@ -3043,7 +3043,7 @@ fn test_blocked_chan_preimage_release() {
30433043 expect_payment_sent ( & nodes[ 2 ] , payment_preimage_2, None , true , true ) ;
30443044}
30453045
3046- fn do_test_inverted_mon_completion_order ( complete_bc_commitment_dance : bool ) {
3046+ fn do_test_inverted_mon_completion_order ( with_latest_manager : bool , complete_bc_commitment_dance : bool ) {
30473047 // When we forward a payment and receive an `update_fulfill_htlc` message from the downstream
30483048 // channel, we immediately claim the HTLC on the upstream channel, before even doing a
30493049 // `commitment_signed` dance on the downstream channel. This implies that our
@@ -3071,6 +3071,10 @@ fn do_test_inverted_mon_completion_order(complete_bc_commitment_dance: bool) {
30713071 let ( payment_preimage, payment_hash, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 100_000 ) ;
30723072
30733073 let mon_ab = get_monitor ! ( nodes[ 1 ] , chan_id_ab) . encode ( ) ;
3074+ let mut manager_b = Vec :: new ( ) ;
3075+ if !with_latest_manager {
3076+ manager_b = nodes[ 1 ] . node . encode ( ) ;
3077+ }
30743078
30753079 nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
30763080 check_added_monitors ( & nodes[ 2 ] , 1 ) ;
@@ -3107,58 +3111,227 @@ fn do_test_inverted_mon_completion_order(complete_bc_commitment_dance: bool) {
31073111 }
31083112
31093113 // Now reload node B
3110- let manager_b = nodes[ 1 ] . node . encode ( ) ;
3114+ if with_latest_manager {
3115+ manager_b = nodes[ 1 ] . node . encode ( ) ;
3116+ }
31113117
31123118 let mon_bc = get_monitor ! ( nodes[ 1 ] , chan_id_bc) . encode ( ) ;
31133119 reload_node ! ( nodes[ 1 ] , & manager_b, & [ & mon_ab, & mon_bc] , persister, new_chain_monitor, nodes_1_deserialized) ;
31143120
31153121 nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
31163122 nodes[ 2 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
31173123
3118- // If we used the latest ChannelManager to reload from, we should have both channels still
3119- // live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3120- // before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3121- // When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3122- // complete after reconnecting to our peers.
3123- persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3124- nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3125- check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3126- assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3124+ if with_latest_manager {
3125+ // If we used the latest ChannelManager to reload from, we should have both channels still
3126+ // live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3127+ // before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3128+ // When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3129+ // complete after reconnecting to our peers.
3130+ persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3131+ nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3132+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3133+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
31273134
3128- // Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3129- // the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3130- // expect to *not* receive the final RAA ChannelMonitorUpdate.
3131- if complete_bc_commitment_dance {
3132- reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3135+ // Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3136+ // the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3137+ // expect to *not* receive the final RAA ChannelMonitorUpdate.
3138+ if complete_bc_commitment_dance {
3139+ reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3140+ } else {
3141+ reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , -2 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , true ) ) ;
3142+ }
3143+
3144+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3145+
3146+ // (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3147+ // disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3148+ // process.
3149+ let ( outpoint, _, ab_update_id) = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id_ab) . unwrap ( ) . clone ( ) ;
3150+ nodes[ 1 ] . chain_monitor . chain_monitor . channel_monitor_updated ( outpoint, ab_update_id) . unwrap ( ) ;
3151+
3152+ // When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3153+ // completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3154+ // channel.
3155+ let bs_updates = get_htlc_update_msgs ( & nodes[ 1 ] , & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
3156+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3157+
3158+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fulfill_htlcs [ 0 ] ) ;
3159+ do_commitment_signed_dance ( & nodes[ 0 ] , & nodes[ 1 ] , & bs_updates. commitment_signed , false , false ) ;
3160+
3161+ expect_payment_forwarded ! ( nodes[ 1 ] , & nodes[ 0 ] , & nodes[ 2 ] , Some ( 1_000 ) , false , false ) ;
31333162 } else {
3134- reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , -2 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , true ) ) ;
3135- }
3163+ // If the ChannelManager used in the reload was stale, check that the B <-> C channel was
3164+ // closed.
3165+ //
3166+ // Note that this will also process the ChannelMonitorUpdates which were queued up when we
3167+ // reloaded the ChannelManager. This will re-emit the A<->B preimage as well as the B<->C
3168+ // force-closure ChannelMonitorUpdate. Once the A<->B preimage update completes, the claim
3169+ // commitment update will be allowed to go out.
3170+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3171+ persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3172+ persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3173+ check_closed_event ( & nodes[ 1 ] , 1 , ClosureReason :: OutdatedChannelManager , false ) ;
3174+ check_added_monitors ( & nodes[ 1 ] , 2 ) ;
31363175
3137- reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3176+ nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3177+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
31383178
3139- // (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3140- // disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3141- // process.
3142- let ( outpoint, _, ab_update_id) = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id_ab) . unwrap ( ) . clone ( ) ;
3143- nodes[ 1 ] . chain_monitor . chain_monitor . channel_monitor_updated ( outpoint, ab_update_id) . unwrap ( ) ;
3179+ // Don't bother to reconnect B to C - that channel has been closed. We don't need to
3180+ // exchange any messages here even though there's a pending commitment update because the
3181+ // ChannelMonitorUpdate hasn't yet completed.
3182+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
31443183
3145- // When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3146- // completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3147- // channel.
3148- let bs_updates = get_htlc_update_msgs ( & nodes[ 1 ] , & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
3149- check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3184+ let ( outpoint, _, ab_update_id) = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id_ab) . unwrap ( ) . clone ( ) ;
3185+ nodes[ 1 ] . chain_monitor . chain_monitor . channel_monitor_updated ( outpoint, ab_update_id) . unwrap ( ) ;
31503186
3151- nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fulfill_htlcs [ 0 ] ) ;
3152- do_commitment_signed_dance ( & nodes[ 0 ] , & nodes[ 1 ] , & bs_updates. commitment_signed , false , false ) ;
3187+ // The ChannelMonitorUpdate which was completed prior to the reconnect only contained the
3188+ // preimage (as it was a replay of the original ChannelMonitorUpdate from before we
3189+ // restarted). When we go to fetch the commitment transaction updates we'll poll the
3190+ // ChannelMonitorUpdate completion, then generate (and complete) a new ChannelMonitorUpdate
3191+ // with the actual commitment transaction, which will allow us to fulfill the HTLC with
3192+ // node A.
3193+ let bs_updates = get_htlc_update_msgs ( & nodes[ 1 ] , & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
3194+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
31533195
3154- expect_payment_forwarded ! ( nodes[ 1 ] , & nodes[ 0 ] , & nodes[ 2 ] , Some ( 1_000 ) , false , false ) ;
3196+ nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fulfill_htlcs [ 0 ] ) ;
3197+ do_commitment_signed_dance ( & nodes[ 0 ] , & nodes[ 1 ] , & bs_updates. commitment_signed , false , false ) ;
3198+ }
31553199
31563200 // Finally, check that the payment was, ultimately, seen as sent by node A.
31573201 expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , true ) ;
31583202}
31593203
31603204#[ test]
31613205fn test_inverted_mon_completion_order ( ) {
3162- do_test_inverted_mon_completion_order ( true ) ;
3163- do_test_inverted_mon_completion_order ( false ) ;
3206+ do_test_inverted_mon_completion_order ( true , true ) ;
3207+ do_test_inverted_mon_completion_order ( true , false ) ;
3208+ do_test_inverted_mon_completion_order ( false , true ) ;
3209+ do_test_inverted_mon_completion_order ( false , false ) ;
3210+ }
3211+
3212+ fn do_test_durable_preimages_on_closed_channel ( close_chans_before_reload : bool , close_only_a : bool ) {
3213+ // Test that we can apply a `ChannelMonitorUpdate` with a payment preimage even if the channel
3214+ // is force-closed between when we generate the update on reload and when we go to handle the
3215+ // update or prior to generating the update at all.
3216+
3217+ if !close_chans_before_reload && close_only_a {
3218+ // If we're not closing, it makes no sense to "only close A"
3219+ panic ! ( ) ;
3220+ }
3221+
3222+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
3223+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
3224+
3225+ let persister;
3226+ let new_chain_monitor;
3227+ let nodes_1_deserialized;
3228+
3229+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
3230+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
3231+
3232+ let chan_id_ab = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 2 ;
3233+ let chan_id_bc = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) . 2 ;
3234+
3235+ // Route a payment from A, through B, to C, then claim it on C. Once we pass B the
3236+ // `update_fulfill_htlc` we have a monitor update for both of B's channels. We complete the one
3237+ // on the B<->C channel but leave the A<->B monitor update pending, then reload B.
3238+ let ( payment_preimage, payment_hash, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1_000_000 ) ;
3239+
3240+ let mon_ab = get_monitor ! ( nodes[ 1 ] , chan_id_ab) . encode ( ) ;
3241+
3242+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
3243+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3244+ expect_payment_claimed ! ( nodes[ 2 ] , payment_hash, 1_000_000 ) ;
3245+
3246+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3247+ let cs_updates = get_htlc_update_msgs ( & nodes[ 2 ] , & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3248+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. update_fulfill_htlcs [ 0 ] ) ;
3249+
3250+ // B generates a new monitor update for the A <-> B channel, but doesn't send the new messages
3251+ // for it since the monitor update is marked in-progress.
3252+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3253+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3254+
3255+ // Now step the Commitment Signed Dance between B and C forward a bit, ensuring we won't get
3256+ // the preimage when the nodes reconnect, at which point we have to ensure we get it from the
3257+ // ChannelMonitor.
3258+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. commitment_signed ) ;
3259+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3260+ let _ = get_revoke_commit_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
3261+
3262+ let mon_bc = get_monitor ! ( nodes[ 1 ] , chan_id_bc) . encode ( ) ;
3263+
3264+ if close_chans_before_reload {
3265+ if !close_only_a {
3266+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3267+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan_id_bc, & nodes[ 2 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
3268+ check_closed_broadcast ( & nodes[ 1 ] , 1 , true ) ;
3269+ check_closed_event ( & nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , false ) ;
3270+ }
3271+
3272+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3273+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan_id_ab, & nodes[ 0 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
3274+ check_closed_broadcast ( & nodes[ 1 ] , 1 , true ) ;
3275+ check_closed_event ( & nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , false ) ;
3276+ }
3277+
3278+ // Now reload node B
3279+ let manager_b = nodes[ 1 ] . node . encode ( ) ;
3280+ reload_node ! ( nodes[ 1 ] , & manager_b, & [ & mon_ab, & mon_bc] , persister, new_chain_monitor, nodes_1_deserialized) ;
3281+
3282+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3283+ nodes[ 2 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3284+
3285+ if close_chans_before_reload {
3286+ // If the channels were already closed, B will rebroadcast its closing transactions here.
3287+ let bs_close_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3288+ if close_only_a {
3289+ assert_eq ! ( bs_close_txn. len( ) , 2 ) ;
3290+ } else {
3291+ assert_eq ! ( bs_close_txn. len( ) , 3 ) ;
3292+ }
3293+ }
3294+
3295+ nodes[ 0 ] . node . force_close_broadcasting_latest_txn ( & chan_id_ab, & nodes[ 1 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
3296+ check_closed_event ( & nodes[ 0 ] , 1 , ClosureReason :: HolderForceClosed , false ) ;
3297+ let as_closing_tx = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3298+ assert_eq ! ( as_closing_tx. len( ) , 1 ) ;
3299+
3300+ // In order to give B A's closing transaction without processing background events first, use
3301+ // the _without_checks utility method. This is similar to connecting blocks during startup
3302+ // prior to the node being full initialized.
3303+ mine_transaction_without_checks ( & nodes[ 1 ] , & as_closing_tx[ 0 ] ) ;
3304+
3305+ // After a timer tick a payment preimage ChannelMonitorUpdate is applied to the A<->B
3306+ // ChannelMonitor, even though the channel has since been closed.
3307+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3308+ nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3309+ check_added_monitors ( & nodes[ 1 ] , if close_chans_before_reload && !close_only_a { 3 } else { 2 } ) ;
3310+
3311+ // Finally, check that B created a payment preimage transaction and close out the payment.
3312+ let bs_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3313+ let bs_preimage_tx = if close_chans_before_reload && !close_only_a {
3314+ assert_eq ! ( bs_txn. len( ) , 2 ) ;
3315+ & bs_txn[ 1 ]
3316+ } else {
3317+ assert_eq ! ( bs_txn. len( ) , 1 ) ;
3318+ & bs_txn[ 0 ]
3319+ } ;
3320+ check_spends ! ( bs_preimage_tx, as_closing_tx[ 0 ] ) ;
3321+
3322+ if !close_chans_before_reload {
3323+ check_closed_broadcast ( & nodes[ 1 ] , 1 , true ) ;
3324+ check_closed_event ( & nodes[ 1 ] , 1 , ClosureReason :: CommitmentTxConfirmed , false ) ;
3325+ }
3326+
3327+ mine_transactions ( & nodes[ 0 ] , & [ & as_closing_tx[ 0 ] , bs_preimage_tx] ) ;
3328+ check_closed_broadcast ( & nodes[ 0 ] , 1 , true ) ;
3329+ expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , true ) ;
3330+ }
3331+
3332+ #[ test]
3333+ fn test_durable_preimages_on_closed_channel ( ) {
3334+ do_test_durable_preimages_on_closed_channel ( true , true ) ;
3335+ do_test_durable_preimages_on_closed_channel ( true , false ) ;
3336+ do_test_durable_preimages_on_closed_channel ( false , false ) ;
31643337}
0 commit comments