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