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