Skip to content

Commit 45f9b61

Browse files
committed
Test monitor update completion actions on pre-startup completion
This adds a test for monitor update actions being completed on startup if a monitor update completed "while we were shut down" (or, really, the manager didn't get persisted after the update completed).
1 parent ac079bf commit 45f9b61

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

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

0 commit comments

Comments
 (0)