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