Skip to content

Commit ca130b9

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 1b85d21 commit ca130b9

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

+101
Original file line numberDiff line numberDiff line change
@@ -3429,3 +3429,104 @@ fn test_durable_preimages_on_closed_channel() {
34293429
do_test_durable_preimages_on_closed_channel(false, false, true);
34303430
do_test_durable_preimages_on_closed_channel(false, false, false);
34313431
}
3432+
3433+
fn do_test_reload_mon_update_completion_actions(close_during_reload: bool) {
3434+
// Test that if a `ChannelMonitorUpdate` completes but a `ChannelManager` isn't serialized
3435+
// before restart we run the monitor update completion action on startup.
3436+
let chanmon_cfgs = create_chanmon_cfgs(3);
3437+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
3438+
3439+
let persister;
3440+
let new_chain_monitor;
3441+
let nodes_1_deserialized;
3442+
3443+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
3444+
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
3445+
3446+
let chan_id_ab = create_announced_chan_between_nodes(&nodes, 0, 1).2;
3447+
let chan_id_bc = create_announced_chan_between_nodes(&nodes, 1, 2).2;
3448+
3449+
// Route a payment from A, through B, to C, then claim it on C. Once we pass B the
3450+
// `update_fulfill_htlc` we have a monitor update for both of B's channels. We complete the
3451+
// commitment signed dance on the B<->C channel but leave the A<->B monitor update pending,
3452+
// then reload B. At that point, the final monitor update on the B<->C channel is still pending
3453+
// because it can't fly until the preimage is persisted on the A<->B monitor.
3454+
let (payment_preimage, payment_hash, _) = route_payment(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000);
3455+
3456+
nodes[2].node.claim_funds(payment_preimage);
3457+
check_added_monitors(&nodes[2], 1);
3458+
expect_payment_claimed!(nodes[2], payment_hash, 1_000_000);
3459+
3460+
chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress);
3461+
let cs_updates = get_htlc_update_msgs(&nodes[2], &nodes[1].node.get_our_node_id());
3462+
nodes[1].node.handle_update_fulfill_htlc(&nodes[2].node.get_our_node_id(), &cs_updates.update_fulfill_htlcs[0]);
3463+
3464+
// B generates a new monitor update for the A <-> B channel, but doesn't send the new messages
3465+
// for it since the monitor update is marked in-progress.
3466+
check_added_monitors(&nodes[1], 1);
3467+
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
3468+
3469+
// Now step the Commitment Signed Dance between B and C and check that after the final RAA B
3470+
// doesn't let the preimage-removing monitor update fly.
3471+
nodes[1].node.handle_commitment_signed(&nodes[2].node.get_our_node_id(), &cs_updates.commitment_signed);
3472+
check_added_monitors(&nodes[1], 1);
3473+
let (bs_raa, bs_cs) = get_revoke_commit_msgs!(nodes[1], nodes[2].node.get_our_node_id());
3474+
3475+
nodes[2].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa);
3476+
check_added_monitors(&nodes[2], 1);
3477+
nodes[2].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_cs);
3478+
check_added_monitors(&nodes[2], 1);
3479+
3480+
let cs_final_raa = get_event_msg!(nodes[2], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id());
3481+
nodes[1].node.handle_revoke_and_ack(&nodes[2].node.get_our_node_id(), &cs_final_raa);
3482+
check_added_monitors(&nodes[1], 0);
3483+
3484+
// Finally, reload node B and check that after we call `process_pending_events` once we realize
3485+
// we've completed the A<->B preimage-including monitor update and so can release the B<->C
3486+
// preimage-removing monitor update.
3487+
let mon_ab = get_monitor!(nodes[1], chan_id_ab).encode();
3488+
let mon_bc = get_monitor!(nodes[1], chan_id_bc).encode();
3489+
let manager_b = nodes[1].node.encode();
3490+
reload_node!(nodes[1], &manager_b, &[&mon_ab, &mon_bc], persister, new_chain_monitor, nodes_1_deserialized);
3491+
3492+
if close_during_reload {
3493+
// Test that we still free the B<->C channel if the A<->B channel closed while we reloaded
3494+
// (as learned about during the on-reload block connection).
3495+
nodes[0].node.force_close_broadcasting_latest_txn(&chan_id_ab, &nodes[1].node.get_our_node_id()).unwrap();
3496+
check_added_monitors!(nodes[0], 1);
3497+
check_closed_broadcast!(nodes[0], true);
3498+
check_closed_event(&nodes[0], 1, ClosureReason::HolderForceClosed, false, &[nodes[1].node.get_our_node_id()], 100_000);
3499+
let as_closing_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
3500+
mine_transaction_without_consistency_checks(&nodes[1], &as_closing_tx[0]);
3501+
}
3502+
3503+
let bc_update_id = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id_bc).unwrap().2;
3504+
let mut events = nodes[1].node.get_and_clear_pending_events();
3505+
assert_eq!(events.len(), if close_during_reload { 2 } else { 1 });
3506+
expect_payment_forwarded(events.pop().unwrap(), &nodes[1], &nodes[0], &nodes[2], Some(1000), close_during_reload, false);
3507+
if close_during_reload {
3508+
match events[0] {
3509+
Event::ChannelClosed { .. } => {},
3510+
_ => panic!(),
3511+
}
3512+
check_closed_broadcast!(nodes[1], true);
3513+
}
3514+
3515+
// Once we run event processing the monitor should free, check that it was indeed the B<->C
3516+
// channel which was updated.
3517+
check_added_monitors(&nodes[1], if close_during_reload { 2 } else { 1 });
3518+
let post_ev_bc_update_id = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&chan_id_bc).unwrap().2;
3519+
assert!(bc_update_id != post_ev_bc_update_id);
3520+
3521+
// Finally, check that there's nothing left to do on B<->C reconnect and the channel operates
3522+
// fine.
3523+
nodes[2].node.peer_disconnected(&nodes[1].node.get_our_node_id());
3524+
reconnect_nodes(ReconnectArgs::new(&nodes[1], &nodes[2]));
3525+
send_payment(&nodes[1], &[&nodes[2]], 100_000);
3526+
}
3527+
3528+
#[test]
3529+
fn test_reload_mon_update_completion_actions() {
3530+
do_test_reload_mon_update_completion_actions(true);
3531+
do_test_reload_mon_update_completion_actions(false);
3532+
}

0 commit comments

Comments
 (0)