Skip to content

Commit

Permalink
Add test for async open and accept channel
Browse files Browse the repository at this point in the history
  • Loading branch information
alecchendev committed Jun 11, 2024
1 parent ff5c999 commit 744f2ca
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
51 changes: 50 additions & 1 deletion lightning/src/ln/async_signer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -52,6 +52,55 @@ pub fn with_async_signer<DoFn, T>(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]);
Expand Down
3 changes: 3 additions & 0 deletions lightning/src/util/test_channel_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ impl ChannelSigner for TestChannelSigner {
fn get_per_commitment_point(&self, idx: u64, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<PublicKey, ()> {
// 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)
}

Expand Down

0 comments on commit 744f2ca

Please sign in to comment.