Skip to content

Commit 68f1d8a

Browse files
committed
Add test for async open and accept channel
1 parent db9f2e6 commit 68f1d8a

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

lightning/src/ln/async_signer_tests.rs

+47
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,53 @@ use crate::ln::msgs::ChannelMessageHandler;
2222
use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
2323
use crate::util::test_channel_signer::SignerOp;
2424

25+
#[test]
26+
fn test_open_channel() {
27+
// Simulate acquiring the signature for `funding_created` asynchronously.
28+
let chanmon_cfgs = create_chanmon_cfgs(2);
29+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
30+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
31+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
32+
33+
// Open an outbound channel simulating an async signer.
34+
let channel_value_satoshis = 100000;
35+
let user_channel_id = 42;
36+
nodes[0].disable_next_channel_signer_op(SignerOp::GetPerCommitmentPoint);
37+
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();
38+
39+
{
40+
let msgs = nodes[0].node.get_and_clear_pending_msg_events();
41+
assert!(msgs.is_empty(), "Expected no message events; got {:?}", msgs);
42+
}
43+
44+
nodes[0].enable_channel_signer_op(&nodes[1].node.get_our_node_id(), &channel_id_0, SignerOp::GetPerCommitmentPoint);
45+
nodes[0].node.signer_unblocked(None);
46+
47+
// nodes[0] --- open_channel --> nodes[1]
48+
let mut open_chan_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
49+
50+
// Handle an inbound channel simulating an async signer.
51+
nodes[1].disable_next_channel_signer_op(SignerOp::GetPerCommitmentPoint);
52+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_chan_msg);
53+
54+
{
55+
let msgs = nodes[1].node.get_and_clear_pending_msg_events();
56+
assert!(msgs.is_empty(), "Expected no message events; got {:?}", msgs);
57+
}
58+
59+
let channel_id_1 = {
60+
let channels = nodes[1].node.list_channels();
61+
assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len());
62+
channels[0].channel_id
63+
};
64+
65+
nodes[1].enable_channel_signer_op(&nodes[0].node.get_our_node_id(), &channel_id_1, SignerOp::GetPerCommitmentPoint);
66+
nodes[1].node.signer_unblocked(None);
67+
68+
// nodes[0] <-- accept_channel --- nodes[1]
69+
get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
70+
}
71+
2572
#[test]
2673
fn test_async_commitment_signature_for_funding_created() {
2774
// Simulate acquiring the signature for `funding_created` asynchronously.

lightning/src/ln/functional_test_utils.rs

+5
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,11 @@ impl<'a, 'b, 'c> Node<'a, 'b, 'c> {
553553
entry.insert(signer_op);
554554
};
555555
}
556+
557+
#[cfg(test)]
558+
pub fn disable_next_channel_signer_op(&self, signer_op: SignerOp) {
559+
self.keys_manager.next_signer_disabled_ops.lock().unwrap().insert(signer_op);
560+
}
556561
}
557562

558563
/// If we need an unsafe pointer to a `Node` (ie to reference it in a thread

lightning/src/util/test_channel_signer.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ impl TestChannelSigner {
145145

146146
impl ChannelSigner for TestChannelSigner {
147147
fn get_per_commitment_point(&self, idx: u64, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<PublicKey, ()> {
148-
// TODO: implement a mask in EnforcementState to let you test signatures being
149-
// unavailable
148+
#[cfg(test)]
149+
if !self.is_signer_available(SignerOp::GetPerCommitmentPoint) {
150+
return Err(());
151+
}
150152
self.inner.get_per_commitment_point(idx, secp_ctx)
151153
}
152154

lightning/src/util/test_utils.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,7 @@ pub struct TestKeysInterface {
12171217
enforcement_states: Mutex<HashMap<[u8;32], Arc<Mutex<EnforcementState>>>>,
12181218
expectations: Mutex<Option<VecDeque<OnGetShutdownScriptpubkey>>>,
12191219
pub unavailable_signers_ops: Mutex<HashMap<[u8; 32], HashSet<SignerOp>>>,
1220+
pub next_signer_disabled_ops: Mutex<HashSet<SignerOp>>,
12201221
}
12211222

12221223
impl EntropySource for TestKeysInterface {
@@ -1282,6 +1283,10 @@ impl SignerProvider for TestKeysInterface {
12821283
signer.disable_op(op);
12831284
}
12841285
}
1286+
#[cfg(test)]
1287+
for op in self.next_signer_disabled_ops.lock().unwrap().drain() {
1288+
signer.disable_op(op);
1289+
}
12851290
signer
12861291
}
12871292

@@ -1321,6 +1326,7 @@ impl TestKeysInterface {
13211326
enforcement_states: Mutex::new(new_hash_map()),
13221327
expectations: Mutex::new(None),
13231328
unavailable_signers_ops: Mutex::new(new_hash_map()),
1329+
next_signer_disabled_ops: Mutex::new(new_hash_set()),
13241330
}
13251331
}
13261332

0 commit comments

Comments
 (0)