From 744f2cadc39c06fd7391caef842fe5c56e2cee89 Mon Sep 17 00:00:00 2001 From: Alec Chen Date: Mon, 10 Jun 2024 17:23:06 -0700 Subject: [PATCH] Add test for async open and accept channel --- lightning/src/ln/async_signer_tests.rs | 51 ++++++++++++++++++++++- lightning/src/util/test_channel_signer.rs | 3 ++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/lightning/src/ln/async_signer_tests.rs b/lightning/src/ln/async_signer_tests.rs index 321285df36e..dd73a0bbc0b 100644 --- a/lightning/src/ln/async_signer_tests.rs +++ b/lightning/src/ln/async_signer_tests.rs @@ -22,7 +22,7 @@ use crate::ln::ChannelId; use crate::ln::functional_test_utils::*; use crate::ln::msgs::ChannelMessageHandler; use crate::ln::channelmanager::{PaymentId, RecipientOnionFields}; -use crate::util::test_channel_signer::ops; +use crate::util::test_channel_signer::{EnforcementState, ops}; /// Helper to run operations with a simulated asynchronous signer. /// @@ -52,6 +52,55 @@ pub fn with_async_signer(node: &Node, peer_id: &PublicKey, channel_id: res } + +#[cfg(feature = "std")] +#[test] +fn test_open_channel() { + // 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); + + // Open an outbound channel simulating an async signer. + let channel_value_satoshis = 100000; + let user_channel_id = 42; + nodes[0].set_unborn_channel_signer_ops_unavailable(ops::GET_PER_COMMITMENT_POINT); + let channel_id_0 = nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, 10001, user_channel_id, None, None).unwrap(); + + { + let msgs = nodes[0].node.get_and_clear_pending_msg_events(); + assert!(msgs.is_empty(), "Expected no message events; got {:?}", msgs); + } + + nodes[0].set_channel_signer_ops_available(&nodes[1].node.get_our_node_id(), &channel_id_0, ops::GET_PER_COMMITMENT_POINT, true); + nodes[0].node.signer_unblocked(None); + + // 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()); + + // Handle an inbound channel simulating an async signer. + nodes[1].set_unborn_channel_signer_ops_unavailable(ops::GET_PER_COMMITMENT_POINT); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg); + + { + let msgs = nodes[1].node.get_and_clear_pending_msg_events(); + assert!(msgs.is_empty(), "Expected no message events; got {:?}", msgs); + } + + let channel_id_1 = { + let channels = nodes[1].node.list_channels(); + assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len()); + channels[0].channel_id + }; + + nodes[1].set_channel_signer_ops_available(&nodes[0].node.get_our_node_id(), &channel_id_1, ops::GET_PER_COMMITMENT_POINT, true); + nodes[1].node.signer_unblocked(None); + + // nodes[0] <-- accept_channel --- nodes[1] + get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()); +} + #[test] fn test_async_commitment_signature_for_funding_created() { do_test_async_funding_created(vec![ops::SIGN_COUNTERPARTY_COMMITMENT]); diff --git a/lightning/src/util/test_channel_signer.rs b/lightning/src/util/test_channel_signer.rs index 96e4f6dbb8e..cffb61eb805 100644 --- a/lightning/src/util/test_channel_signer.rs +++ b/lightning/src/util/test_channel_signer.rs @@ -179,6 +179,9 @@ impl ChannelSigner for TestChannelSigner { fn get_per_commitment_point(&self, idx: u64, secp_ctx: &Secp256k1) -> Result { // TODO: implement a mask in EnforcementState to let you test signatures being // unavailable + if !self.is_signer_available(ops::GET_PER_COMMITMENT_POINT) { + return Err(()); + } self.inner.get_per_commitment_point(idx, secp_ctx) }