From 926c58810fea3898de46655489ddf8e5ab2c8ea8 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 | 126 ++++++++++++++++++++++ lightning/src/ln/channel.rs | 1 - lightning/src/ln/functional_test_utils.rs | 7 +- lightning/src/ln/mod.rs | 3 + 4 files changed, 133 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 000000000..60688740f --- /dev/null +++ b/lightning/src/ln/async_signer_tests.rs @@ -0,0 +1,126 @@ +// 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 channels = nodes[0].node.list_channels(); + 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))); + + 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 channels = nodes[0].node.list_channels(); + assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len()); + channels[0].channel_id + }; + 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 ceee6ca3b..4c8a99028 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -2156,7 +2156,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 b35c49321..b7d1e0ebd 100644 --- a/lightning/src/ln/functional_test_utils.rs +++ b/lightning/src/ln/functional_test_utils.rs @@ -2106,12 +2106,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 beefd2d46..6a9a10c80 100644 --- a/lightning/src/ln/mod.rs +++ b/lightning/src/ln/mod.rs @@ -73,6 +73,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; -- 2.39.5