From 2135718151a2d51c061a45157a6670818dfd1f37 Mon Sep 17 00:00:00 2001 From: Chris Waterson Date: Wed, 6 Sep 2023 13:08:50 -0700 Subject: [PATCH] Add basic async signer tests This adds a new `async_signer_tests` module and populates it with some simple checks for asynchronous handling of `funding_created` and `funding_signed`. --- lightning/src/ln/async_signer_tests.rs | 132 ++++++++++++++++++++++ lightning/src/ln/channel.rs | 1 - lightning/src/ln/functional_test_utils.rs | 7 +- lightning/src/ln/mod.rs | 3 + 4 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 lightning/src/ln/async_signer_tests.rs diff --git a/lightning/src/ln/async_signer_tests.rs b/lightning/src/ln/async_signer_tests.rs new file mode 100644 index 00000000000..d69e67286f3 --- /dev/null +++ b/lightning/src/ln/async_signer_tests.rs @@ -0,0 +1,132 @@ +// This file is Copyright its original authors, visible in version control +// history. +// +// This file is licensed under the Apache License, Version 2.0 or the MIT license +// , at your option. +// You may not use this file except in accordance with one or both of these +// licenses. + +//! Tests for asynchronous signing. These tests verify that the channel state machine behaves +//! properly with a signer implementation that asynchronously derives signatures. + +use crate::events::{MessageSendEvent, MessageSendEventsProvider}; +use crate::ln::msgs::ChannelMessageHandler; + +use crate::ln::functional_test_utils::*; + +#[test] +fn test_async_commitment_signature_for_funding_created() { + // Simulate acquiring the signature for `funding_created` asynchronously. + 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); + + 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); + + // nodes[0] <-- accept_channel --- nodes[1] + nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())); + + // nodes[0] --- funding_created --> nodes[1] + // + // But! Let's make node[0]'s signer be unavailable: we should *not* broadcast a funding_created + // message... + let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42); + nodes[0].set_channel_signer_available(&nodes[1].node.get_our_node_id(), &temporary_channel_id, false); + nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap(); + check_added_monitors(&nodes[0], 0); + + { + let events = nodes[0].node.get_and_clear_pending_msg_events(); + let n = events.len(); + assert_eq!(n, 0, "expected no events generated from nodes[0], found {n}"); + } + + // Now re-enable the signer and simulate a retry. The temporary_channel_id won't work anymore so + // we have to dig out the real channel ID. + 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] + }; + + 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))); + + let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id()); + nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg); + check_added_monitors(&nodes[1], 1); + expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id()); + + // nodes[0] <-- funding_signed --- nodes[1] + let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()); + nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg); + check_added_monitors(&nodes[0], 1); + expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id()); +} + +#[test] +fn test_async_commitment_signature_for_funding_signed() { + // Simulate acquiring the signature for `funding_signed` asynchronously. + 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); + + 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); + + // nodes[0] <-- accept_channel --- nodes[1] + nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())); + + // nodes[0] --- funding_created --> nodes[1] + let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42); + nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap(); + check_added_monitors(&nodes[0], 0); + + let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id()); + + // Now let's make node[1]'s signer be unavailable while handling the `funding_created`. It should + // *not* broadcast a `funding_signed`... + nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &temporary_channel_id, false); + nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg); + check_added_monitors(&nodes[1], 1); + + { + let events = nodes[1].node.get_and_clear_pending_msg_events(); + let n = events.len(); + assert_eq!(n, 0, "expected no events generated from nodes[1], found {n}"); + } + + // Now re-enable the signer and simulate a retry. The temporary_channel_id won't work anymore so + // we have to dig out the real channel ID. + let chan_id = { + let per_peer_state = nodes[1].node.per_peer_state.read().unwrap(); + let chan_lock = per_peer_state.get(&nodes[0].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] + }; + 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] + let funding_signed_msg = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()); + nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed_msg); + check_added_monitors(&nodes[0], 1); + expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id()); +} diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 99845c9ab4d..b891b34ee9a 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -2056,7 +2056,6 @@ impl ChannelContext where SP::Target: SignerProvider { } } } - } // Internal utility functions for channels diff --git a/lightning/src/ln/functional_test_utils.rs b/lightning/src/ln/functional_test_utils.rs index 87d183f3a1d..9c10553c6bd 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -2084,12 +2084,13 @@ macro_rules! expect_channel_shutdown_state { } #[cfg(any(test, ldk_bench, feature = "_test_utils"))] -pub fn expect_channel_pending_event<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, expected_counterparty_node_id: &PublicKey) { +pub fn expect_channel_pending_event<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, expected_counterparty_node_id: &PublicKey) -> ChannelId { let events = node.node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); - match events[0] { - crate::events::Event::ChannelPending { ref counterparty_node_id, .. } => { + match &events[0] { + crate::events::Event::ChannelPending { channel_id, counterparty_node_id, .. } => { assert_eq!(*expected_counterparty_node_id, *counterparty_node_id); + *channel_id }, _ => panic!("Unexpected event"), } diff --git a/lightning/src/ln/mod.rs b/lightning/src/ln/mod.rs index bacb9e86c79..a82696e1776 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -70,6 +70,9 @@ mod monitor_tests; #[cfg(test)] #[allow(unused_mut)] mod shutdown_tests; +#[cfg(test)] +#[allow(unused_mut)] +mod async_signer_tests; pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;