@@ -3401,3 +3401,79 @@ fn test_durable_preimages_on_closed_channel() {
3401
3401
do_test_durable_preimages_on_closed_channel ( false , false , true ) ;
3402
3402
do_test_durable_preimages_on_closed_channel ( false , false , false ) ;
3403
3403
}
3404
+
3405
+ #[ test]
3406
+ fn test_reload_mon_update_completion_actions ( ) {
3407
+ // Test that if a `ChannelMonitorUpdate` completes but a `ChannelManager` isn't serialized
3408
+ // before restart we run the monitor update completion action on startup.
3409
+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
3410
+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
3411
+
3412
+ let persister;
3413
+ let new_chain_monitor;
3414
+ let nodes_1_deserialized;
3415
+
3416
+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
3417
+ let mut nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
3418
+
3419
+ let chan_id_ab = create_announced_chan_between_nodes ( & nodes, 0 , 1 ) . 2 ;
3420
+ let chan_id_bc = create_announced_chan_between_nodes ( & nodes, 1 , 2 ) . 2 ;
3421
+
3422
+ // Route a payment from A, through B, to C, then claim it on C. Once we pass B the
3423
+ // `update_fulfill_htlc` we have a monitor update for both of B's channels. We complete the
3424
+ // commitment signed dance on the B<->C channel but leave the A<->B monitor update pending,
3425
+ // then reload B. At that point, the final monitor update on the B<->C channel is still pending
3426
+ // because it can't fly until the preimage is persisted on the A<->B monitor.
3427
+ let ( payment_preimage, payment_hash, _) = route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , 1_000_000 ) ;
3428
+
3429
+ nodes[ 2 ] . node . claim_funds ( payment_preimage) ;
3430
+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3431
+ expect_payment_claimed ! ( nodes[ 2 ] , payment_hash, 1_000_000 ) ;
3432
+
3433
+ chanmon_cfgs[ 1 ] . persister . set_update_ret ( ChannelMonitorUpdateStatus :: InProgress ) ;
3434
+ let cs_updates = get_htlc_update_msgs ( & nodes[ 2 ] , & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3435
+ nodes[ 1 ] . node . handle_update_fulfill_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. update_fulfill_htlcs [ 0 ] ) ;
3436
+
3437
+ // B generates a new monitor update for the A <-> B channel, but doesn't send the new messages
3438
+ // for it since the monitor update is marked in-progress.
3439
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3440
+ assert ! ( nodes[ 1 ] . node. get_and_clear_pending_msg_events( ) . is_empty( ) ) ;
3441
+
3442
+ // Now step the Commitment Signed Dance between B and C and check that after the final RAA B
3443
+ // doesn't let the preimage-removing monitor update fly.
3444
+ nodes[ 1 ] . node . handle_commitment_signed ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_updates. commitment_signed ) ;
3445
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3446
+ let ( bs_raa, bs_cs) = get_revoke_commit_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
3447
+
3448
+ nodes[ 2 ] . node . handle_revoke_and_ack ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_raa) ;
3449
+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3450
+ nodes[ 2 ] . node . handle_commitment_signed ( & nodes[ 1 ] . node . get_our_node_id ( ) , & bs_cs) ;
3451
+ check_added_monitors ( & nodes[ 2 ] , 1 ) ;
3452
+
3453
+ let cs_final_raa = get_event_msg ! ( nodes[ 2 ] , MessageSendEvent :: SendRevokeAndACK , nodes[ 1 ] . node. get_our_node_id( ) ) ;
3454
+ nodes[ 1 ] . node . handle_revoke_and_ack ( & nodes[ 2 ] . node . get_our_node_id ( ) , & cs_final_raa) ;
3455
+ check_added_monitors ( & nodes[ 1 ] , 0 ) ;
3456
+
3457
+ // Finally, reload node B and check that after we call `process_events` once we realize we've
3458
+ // completed the A<->B preimage-including monitor update and so can release the B<->C
3459
+ // preimage-removing monitor update.
3460
+ let mon_ab = get_monitor ! ( nodes[ 1 ] , chan_id_ab) . encode ( ) ;
3461
+ let mon_bc = get_monitor ! ( nodes[ 1 ] , chan_id_bc) . encode ( ) ;
3462
+ let manager_b = nodes[ 1 ] . node . encode ( ) ;
3463
+ reload_node ! ( nodes[ 1 ] , & manager_b, & [ & mon_ab, & mon_bc] , persister, new_chain_monitor, nodes_1_deserialized) ;
3464
+
3465
+ let bc_update_id = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id_bc) . unwrap ( ) . 2 ;
3466
+ expect_payment_forwarded ! ( nodes[ 1 ] , nodes[ 0 ] , nodes[ 2 ] , Some ( 1000 ) , false , false ) ;
3467
+
3468
+ // Once we run event processing the monitor should free, check that it was indeed the B<->C
3469
+ // channel which was updated.
3470
+ check_added_monitors ( & nodes[ 1 ] , 1 ) ;
3471
+ let post_ev_bc_update_id = nodes[ 1 ] . chain_monitor . latest_monitor_update_id . lock ( ) . unwrap ( ) . get ( & chan_id_bc) . unwrap ( ) . 2 ;
3472
+ assert ! ( bc_update_id != post_ev_bc_update_id) ;
3473
+
3474
+ // Finally, check that there's nothing left to do on B<->C reconnect and the channel operates
3475
+ // fine.
3476
+ nodes[ 2 ] . node . peer_disconnected ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ;
3477
+ reconnect_nodes ( & nodes[ 1 ] , & nodes[ 2 ] , ( false , false ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( 0 , 0 ) , ( false , false ) ) ;
3478
+ send_payment ( & nodes[ 1 ] , & [ & nodes[ 2 ] ] , 100_000 ) ;
3479
+ }
0 commit comments