From dc6e0f44521b75d56988b895fb678cd11d31f98c Mon Sep 17 00:00:00 2001 From: Chris Waterson Date: Wed, 6 Sep 2023 16:10:42 -0700 Subject: [PATCH] Add test for peer disconnect and async signing This adds a test that disconnects and reconnects the peers with a commitment signature pending. --- lightning/src/ln/async_signer_tests.rs | 77 +++++++++++++++++++++-- lightning/src/ln/functional_test_utils.rs | 12 ++-- 2 files changed, 80 insertions(+), 9 deletions(-) diff --git a/lightning/src/ln/async_signer_tests.rs b/lightning/src/ln/async_signer_tests.rs index 5ec84b4f9..64a68e1e7 100644 --- a/lightning/src/ln/async_signer_tests.rs +++ b/lightning/src/ln/async_signer_tests.rs @@ -54,7 +54,7 @@ fn test_async_commitment_signature_for_funding_created() { assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len()); channels[0].channel_id }; - + nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &chan_id, true); nodes[0].node.signer_unblocked(Some((nodes[1].node.get_our_node_id(), chan_id))); @@ -79,7 +79,7 @@ fn test_async_commitment_signature_for_funding_signed() { let nodes = create_network(2, &node_cfgs, &node_chanmgrs); nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap(); - + // nodes[0] --- open_channel --> nodes[1] let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg); @@ -115,7 +115,7 @@ fn test_async_commitment_signature_for_funding_signed() { }; nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &chan_id, true); nodes[1].node.signer_unblocked(Some((nodes[0].node.get_our_node_id(), chan_id))); - + expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id()); // nodes[0] <-- funding_signed --- nodes[1] @@ -141,7 +141,7 @@ fn test_async_commitment_signature_for_commitment_signed() { assert_eq!(n, 1, "expected one channel, not {}", n); *chan_ids[0] }; - + // Send a payment. let src = &nodes[0]; let dst = &nodes[1]; @@ -168,7 +168,7 @@ fn test_async_commitment_signature_for_commitment_signed() { check_added_monitors(dst, 1); get_event_msg!(dst, MessageSendEvent::SendRevokeAndACK, src.node.get_our_node_id()); - + // Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`. dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, true); dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id))); @@ -183,3 +183,70 @@ fn test_async_commitment_signature_for_commitment_signed() { }; } +#[test] +fn test_async_commitment_signature_for_peer_disconnect() { + let chanmon_cfgs = create_chanmon_cfgs(2); + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); + create_announced_chan_between_nodes(&nodes, 0, 1); + + let chan_id = { + let per_peer_state = nodes[0].node.per_peer_state.read().unwrap(); + let chan_lock = per_peer_state.get(&nodes[1].node.get_our_node_id()).unwrap().lock().unwrap(); + let chan_ids = chan_lock.channel_by_id.keys().collect::>(); + let n = chan_ids.len(); + assert_eq!(n, 1, "expected one channel, not {}", n); + *chan_ids[0] + }; + + // Send a payment. + let src = &nodes[0]; + let dst = &nodes[1]; + let (route, our_payment_hash, _our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(src, dst, 8000000); + src.node.send_payment_with_route(&route, our_payment_hash, + RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap(); + check_added_monitors!(src, 1); + + // Pass the payment along the route. + let payment_event = { + let mut events = src.node.get_and_clear_pending_msg_events(); + assert_eq!(events.len(), 1); + SendEvent::from_event(events.remove(0)) + }; + assert_eq!(payment_event.node_id, dst.node.get_our_node_id()); + assert_eq!(payment_event.msgs.len(), 1); + + dst.node.handle_update_add_htlc(&src.node.get_our_node_id(), &payment_event.msgs[0]); + + // Mark dst's signer as unavailable and handle src's commitment_signed: while dst won't yet have a + // `commitment_signed` of its own to offer, it should publish a `revoke_and_ack`. + dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, false); + dst.node.handle_commitment_signed(&src.node.get_our_node_id(), &payment_event.commitment_msg); + check_added_monitors(dst, 1); + + get_event_msg!(dst, MessageSendEvent::SendRevokeAndACK, src.node.get_our_node_id()); + + // Now disconnect and reconnect the peers. + src.node.peer_disconnected(&dst.node.get_our_node_id()); + dst.node.peer_disconnected(&src.node.get_our_node_id()); + let mut reconnect_args = ReconnectArgs::new(&nodes[0], &nodes[1]); + reconnect_args.send_channel_ready = (false, false); + reconnect_args.pending_raa = (true, false); + reconnect_nodes(reconnect_args); + + // Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`. + dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, true); + dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id))); + + { + let events = dst.node.get_and_clear_pending_msg_events(); + let n = events.len(); + assert_eq!(n, 1, "expected one message, got {}", n); + if let MessageSendEvent::UpdateHTLCs { ref node_id, .. } = events[0] { + assert_eq!(node_id, &src.node.get_our_node_id()); + } else { + panic!("expected UpdateHTLCs message, not {:?}", events[0]); + }; + } +} diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 0e6b94ff5..04da6a81b 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -3164,24 +3164,28 @@ pub fn reconnect_nodes<'a, 'b, 'c, 'd>(args: ReconnectArgs<'a, 'b, 'c, 'd>) { // If a expects a channel_ready, it better not think it has received a revoke_and_ack // from b for reestablish in reestablish_1.iter() { - assert_eq!(reestablish.next_remote_commitment_number, 0); + let n = reestablish.next_remote_commitment_number; + assert_eq!(n, 0, "expected a->b next_remote_commitment_number to be 0, got {}", n); } } if send_channel_ready.1 { // If b expects a channel_ready, it better not think it has received a revoke_and_ack // from a for reestablish in reestablish_2.iter() { - assert_eq!(reestablish.next_remote_commitment_number, 0); + let n = reestablish.next_remote_commitment_number; + assert_eq!(n, 0, "expected b->a next_remote_commitment_number to be 0, got {}", n); } } if send_channel_ready.0 || send_channel_ready.1 { // If we expect any channel_ready's, both sides better have set // next_holder_commitment_number to 1 for reestablish in reestablish_1.iter() { - assert_eq!(reestablish.next_local_commitment_number, 1); + let n = reestablish.next_local_commitment_number; + assert_eq!(n, 1, "expected a->b next_local_commitment_number to be 1, got {}", n); } for reestablish in reestablish_2.iter() { - assert_eq!(reestablish.next_local_commitment_number, 1); + let n = reestablish.next_local_commitment_number; + assert_eq!(n, 1, "expected b->a next_local_commitment_number to be 1, got {}", n); } } -- 2.39.5