@@ -3085,7 +3085,7 @@ fn test_blocked_chan_preimage_release() {
30853085 expect_payment_sent ( & nodes[ 2 ] , payment_preimage_2, None , true , true ) ;
30863086}
30873087
3088- fn do_test_inverted_mon_completion_order ( complete_bc_commitment_dance : bool ) {
3088+ fn do_test_inverted_mon_completion_order ( with_latest_manager : bool , complete_bc_commitment_dance : bool ) {
30893089 // When we forward a payment and receive an `update_fulfill_htlc` message from the downstream
30903090 // channel, we immediately claim the HTLC on the upstream channel, before even doing a
30913091 // `commitment_signed` dance on the downstream channel. This implies that our
@@ -3113,6 +3113,10 @@ fn do_test_inverted_mon_completion_order(complete_bc_commitment_dance: bool) {
31133113 let ( payment_preimage, payment_hash, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 100_000 ) ;
31143114
31153115 let mon_ab = get_monitor ! ( nodes[ 1 ] , chan_id_ab) . encode ( ) ;
3116+ let mut manager_b = Vec :: new ( ) ;
3117+ if !with_latest_manager {
3118+ manager_b = nodes[ 1 ] . node . encode ( ) ;
3119+ }
31163120
31173121 nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
31183122 check_added_monitors ( & nodes[ 2 ] , 1 ) ;
@@ -3149,58 +3153,222 @@ fn do_test_inverted_mon_completion_order(complete_bc_commitment_dance: bool) {
31493153 }
31503154
31513155 // Now reload node B
3152- let manager_b = nodes[ 1 ] . node . encode ( ) ;
3156+ if with_latest_manager {
3157+ manager_b = nodes[ 1 ] . node . encode ( ) ;
3158+ }
31533159
31543160 let mon_bc = get_monitor ! ( nodes[ 1 ] , chan_id_bc) . encode ( ) ;
31553161 reload_node ! ( nodes[ 1 ] , & manager_b, & [ & mon_ab, & mon_bc] , persister, new_chain_monitor, nodes_1_deserialized) ;
31563162
31573163 nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
31583164 nodes[ 2 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
31593165
3160- // If we used the latest ChannelManager to reload from, we should have both channels still
3161- // live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3162- // before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3163- // When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3164- // complete after reconnecting to our peers.
3165- persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3166- nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3167- check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3168- assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3166+ if with_latest_manager {
3167+ // If we used the latest ChannelManager to reload from, we should have both channels still
3168+ // live. The B <-> C channel's final RAA ChannelMonitorUpdate must still be blocked as
3169+ // before - the ChannelMonitorUpdate for the A <-> B channel hasn't completed.
3170+ // When we call `timer_tick_occurred` we will get that monitor update back, which we'll
3171+ // complete after reconnecting to our peers.
3172+ persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3173+ nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3174+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3175+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
31693176
3170- // Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3171- // the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3172- // expect to *not* receive the final RAA ChannelMonitorUpdate.
3173- if complete_bc_commitment_dance {
3174- reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3177+ // Now reconnect B to both A and C. If the B <-> C commitment signed dance wasn't run to
3178+ // the end go ahead and do that, though the -2 in `reconnect_nodes` indicates that we
3179+ // expect to *not* receive the final RAA ChannelMonitorUpdate.
3180+ if complete_bc_commitment_dance {
3181+ reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3182+ } else {
3183+ reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , -2 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , true ) ) ;
3184+ }
3185+
3186+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3187+
3188+ // (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3189+ // disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3190+ // process.
3191+ let ( outpoint, _, ab_update_id) = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id_ab) . unwrap ( ) . clone ( ) ;
3192+ nodes[ 1 ] . chain_monitor . chain_monitor . channel_monitor_updated ( outpoint, ab_update_id) . unwrap ( ) ;
3193+
3194+ // When we fetch B's HTLC update messages next (now that the ChannelMonitorUpdate has
3195+ // completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3196+ // channel.
31753197 } else {
3176- reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , -2 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , true ) ) ;
3177- }
3198+ // If the ChannelManager used in the reload was stale, check that the B <-> C channel was
3199+ // closed.
3200+ //
3201+ // Note that this will also process the ChannelMonitorUpdates which were queued up when we
3202+ // reloaded the ChannelManager. This will re-emit the A<->B preimage as well as the B<->C
3203+ // force-closure ChannelMonitorUpdate. Once the A<->B preimage update completes, the claim
3204+ // commitment update will be allowed to go out.
3205+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3206+ persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3207+ persister. set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3208+ check_closed_event ( & nodes[ 1 ] , 1 , ClosureReason :: OutdatedChannelManager , false ) ;
3209+ check_added_monitors ( & nodes[ 1 ] , 2 ) ;
3210+
3211+ nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3212+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3213+
3214+ // Don't bother to reconnect B to C - that channel has been closed. We don't need to
3215+ // exchange any messages here even though there's a pending commitment update because the
3216+ // ChannelMonitorUpdate hasn't yet completed.
3217+ reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
31783218
3179- reconnect_nodes ( & nodes[ 0 ] , & nodes[ 1 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3219+ let ( outpoint, _, ab_update_id) = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id_ab) . unwrap ( ) . clone ( ) ;
3220+ nodes[ 1 ] . chain_monitor . chain_monitor . channel_monitor_updated ( outpoint, ab_update_id) . unwrap ( ) ;
31803221
3181- // (Finally) complete the A <-> B ChannelMonitorUpdate, ensuring the preimage is durably on
3182- // disk in the proper ChannelMonitor, unblocking the B <-> C ChannelMonitor updating
3183- // process.
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 ( ) ;
3222+ // The ChannelMonitorUpdate which was completed prior to the reconnect only contained the
3223+ // preimage (as it was a replay of the original ChannelMonitorUpdate from before we
3224+ // restarted). When we go to fetch the commitment transaction updates we'll poll the
3225+ // ChannelMonitorUpdate completion, then generate (and complete) a new ChannelMonitorUpdate
3226+ // with the actual commitment transaction, which will allow us to fulfill the HTLC with
3227+ // node A.
3228+ }
31863229
3187- // When we fetch B's HTLC update messages here (now that the ChannelMonitorUpdate has
3188- // completed), it will also release the final RAA ChannelMonitorUpdate on the B <-> C
3189- // channel.
31903230 let bs_updates = get_htlc_update_msgs ( & nodes[ 1 ] , & nodes[ 0 ] . node . get_our_node_id ( ) ) ;
31913231 check_added_monitors ( & nodes[ 1 ] , 1 ) ;
31923232
31933233 nodes[ 0 ] . node . handle_update_fulfill_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_updates. update_fulfill_htlcs [ 0 ] ) ;
31943234 do_commitment_signed_dance ( & nodes[ 0 ] , & nodes[ 1 ] , & bs_updates. commitment_signed , false , false ) ;
31953235
3196- expect_payment_forwarded ! ( nodes[ 1 ] , & nodes[ 0 ] , & nodes[ 2 ] , Some ( 1_000 ) , false , false ) ;
3236+ expect_payment_forwarded ! ( nodes[ 1 ] , & nodes[ 0 ] , & nodes[ 2 ] , Some ( 1_000 ) , false , !with_latest_manager ) ;
31973237
31983238 // Finally, check that the payment was, ultimately, seen as sent by node A.
31993239 expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , true ) ;
32003240}
32013241
32023242#[ test]
32033243fn test_inverted_mon_completion_order ( ) {
3204- do_test_inverted_mon_completion_order ( true ) ;
3205- do_test_inverted_mon_completion_order ( false ) ;
3244+ do_test_inverted_mon_completion_order ( true , true ) ;
3245+ do_test_inverted_mon_completion_order ( true , false ) ;
3246+ do_test_inverted_mon_completion_order ( false , true ) ;
3247+ do_test_inverted_mon_completion_order ( false , false ) ;
3248+ }
3249+
3250+ fn do_test_durable_preimages_on_closed_channel ( close_chans_before_reload : bool , close_only_a : bool ) {
3251+ // Test that we can apply a `ChannelMonitorUpdate` with a payment preimage even if the channel
3252+ // is force-closed between when we generate the update on reload and when we go to handle the
3253+ // update or prior to generating the update at all.
3254+
3255+ if !close_chans_before_reload && close_only_a {
3256+ // If we're not closing, it makes no sense to "only close A"
3257+ panic ! ( ) ;
3258+ }
3259+
3260+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
3261+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
3262+
3263+ let persister;
3264+ let new_chain_monitor;
3265+ let nodes_1_deserialized;
3266+
3267+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
3268+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
3269+
3270+ let chan_id_ab = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 2 ;
3271+ let chan_id_bc = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) . 2 ;
3272+
3273+ // Route a payment from A, through B, to C, then claim it on C. Once we pass B the
3274+ // `update_fulfill_htlc` we have a monitor update for both of B's channels. We complete the one
3275+ // on the B<->C channel but leave the A<->B monitor update pending, then reload B.
3276+ let ( payment_preimage, payment_hash, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1_000_000 ) ;
3277+
3278+ let mon_ab = get_monitor ! ( nodes[ 1 ] , chan_id_ab) . encode ( ) ;
3279+
3280+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
3281+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3282+ expect_payment_claimed ! ( nodes[ 2 ] , payment_hash, 1_000_000 ) ;
3283+
3284+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3285+ let cs_updates = get_htlc_update_msgs ( & nodes[ 2 ] , & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3286+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. update_fulfill_htlcs [ 0 ] ) ;
3287+
3288+ // B generates a new monitor update for the A <-> B channel, but doesn't send the new messages
3289+ // for it since the monitor update is marked in-progress.
3290+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3291+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3292+
3293+ // Now step the Commitment Signed Dance between B and C forward a bit, ensuring we won't get
3294+ // the preimage when the nodes reconnect, at which point we have to ensure we get it from the
3295+ // ChannelMonitor.
3296+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. commitment_signed ) ;
3297+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3298+ let _ = get_revoke_commit_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
3299+
3300+ let mon_bc = get_monitor ! ( nodes[ 1 ] , chan_id_bc) . encode ( ) ;
3301+
3302+ if close_chans_before_reload {
3303+ if !close_only_a {
3304+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3305+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan_id_bc, & nodes[ 2 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
3306+ check_closed_broadcast ( & nodes[ 1 ] , 1 , true ) ;
3307+ check_closed_event ( & nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , false ) ;
3308+ }
3309+
3310+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3311+ nodes[ 1 ] . node . force_close_broadcasting_latest_txn ( & chan_id_ab, & nodes[ 0 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
3312+ check_closed_broadcast ( & nodes[ 1 ] , 1 , true ) ;
3313+ check_closed_event ( & nodes[ 1 ] , 1 , ClosureReason :: HolderForceClosed , false ) ;
3314+ }
3315+
3316+ // Now reload node B
3317+ let manager_b = nodes[ 1 ] . node . encode ( ) ;
3318+ reload_node ! ( nodes[ 1 ] , & manager_b, & [ & mon_ab, & mon_bc] , persister, new_chain_monitor, nodes_1_deserialized) ;
3319+
3320+ nodes[ 0 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3321+ nodes[ 2 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3322+
3323+ if close_chans_before_reload {
3324+ // If the channels were already closed, B will rebroadcast its closing transactions here.
3325+ let bs_close_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3326+ if close_only_a {
3327+ assert_eq ! ( bs_close_txn. len( ) , 2 ) ;
3328+ } else {
3329+ assert_eq ! ( bs_close_txn. len( ) , 3 ) ;
3330+ }
3331+ }
3332+
3333+ nodes[ 0 ] . node . force_close_broadcasting_latest_txn ( & chan_id_ab, & nodes[ 1 ] . node . get_our_node_id ( ) ) . unwrap ( ) ;
3334+ check_closed_event ( & nodes[ 0 ] , 1 , ClosureReason :: HolderForceClosed , false ) ;
3335+ let as_closing_tx = nodes[ 0 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3336+ assert_eq ! ( as_closing_tx. len( ) , 1 ) ;
3337+
3338+ // In order to give B A's closing transaction without processing background events first, use
3339+ // the _without_checks utility method. This is similar to connecting blocks during startup
3340+ // prior to the node being full initialized.
3341+ mine_transaction_without_checks ( & nodes[ 1 ] , & as_closing_tx[ 0 ] ) ;
3342+
3343+ // After a timer tick a payment preimage ChannelMonitorUpdate is applied to the A<->B
3344+ // ChannelMonitor (possible twice), even though the channel has since been closed.
3345+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3346+ nodes[ 1 ] . node . timer_tick_occurred ( ) ;
3347+ check_added_monitors ( & nodes[ 1 ] , if close_chans_before_reload { if !close_only_a { 4 } else { 3 } } else { 2 } ) ;
3348+
3349+ // Finally, check that B created a payment preimage transaction and close out the payment.
3350+ let bs_txn = nodes[ 1 ] . tx_broadcaster . txn_broadcasted . lock ( ) . unwrap ( ) . split_off ( 0 ) ;
3351+ assert_eq ! ( bs_txn. len( ) , if close_chans_before_reload && !close_only_a { 2 } else { 1 } ) ;
3352+ let bs_preimage_tx = & bs_txn[ 0 ] ;
3353+ check_spends ! ( bs_preimage_tx, as_closing_tx[ 0 ] ) ;
3354+
3355+ if !close_chans_before_reload {
3356+ check_closed_broadcast ( & nodes[ 1 ] , 1 , true ) ;
3357+ check_closed_event ( & nodes[ 1 ] , 1 , ClosureReason :: CommitmentTxConfirmed , false ) ;
3358+ } else {
3359+ // While we forwarded the payment a while ago, we don't want to process events too early or
3360+ // we'll run background tasks we wanted to test individually.
3361+ expect_payment_forwarded ! ( nodes[ 1 ] , nodes[ 0 ] , nodes[ 2 ] , None , false , !close_only_a) ;
3362+ }
3363+
3364+ mine_transactions ( & nodes[ 0 ] , & [ & as_closing_tx[ 0 ] , bs_preimage_tx] ) ;
3365+ check_closed_broadcast ( & nodes[ 0 ] , 1 , true ) ;
3366+ expect_payment_sent ( & nodes[ 0 ] , payment_preimage, None , true , true ) ;
3367+ }
3368+
3369+ #[ test]
3370+ fn test_durable_preimages_on_closed_channel ( ) {
3371+ do_test_durable_preimages_on_closed_channel ( true , true ) ;
3372+ do_test_durable_preimages_on_closed_channel ( true , false ) ;
3373+ do_test_durable_preimages_on_closed_channel ( false , false ) ;
32063374}
0 commit comments