Skip to content

Commit

Permalink
Add test for async signing for commitment_signed
Browse files Browse the repository at this point in the history
This adds a test (and fixes the implementation!) to check that we correctly
resume processing after awaiting an asynchronous signature for a
`commitment_signed` event.
  • Loading branch information
waterson committed Sep 6, 2023
1 parent 2135718 commit 5ef9d7f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
65 changes: 62 additions & 3 deletions lightning/src/ln/async_signer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
//! 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::*;
use crate::ln::msgs::ChannelMessageHandler;
use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};

#[test]
fn test_async_commitment_signature_for_funding_created() {
Expand All @@ -24,7 +24,7 @@ fn test_async_commitment_signature_for_funding_created() {
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);
Expand Down Expand Up @@ -130,3 +130,62 @@ fn test_async_commitment_signature_for_funding_signed() {
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_commitment_signed() {
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);
create_announced_chan_between_nodes(&nodes, 0, 1);

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::<Vec<_>>();
let n = chan_ids.len();
assert_eq!(n, 1, "expected one channel, not {n}");
*chan_ids[0]
};

// Send a payment.
let src = &nodes[0];
let dst = &nodes[1];
let (route, our_payment_hash, _our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(src, dst, 8000000);
src.node.send_payment_with_route(&route, our_payment_hash,
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
check_added_monitors!(src, 1);

// Pass the payment along the route.
let payment_event = {
let mut events = src.node.get_and_clear_pending_msg_events();
assert_eq!(events.len(), 1);
SendEvent::from_event(events.remove(0))
};
assert_eq!(payment_event.node_id, dst.node.get_our_node_id());
assert_eq!(payment_event.msgs.len(), 1);

dst.node.handle_update_add_htlc(&src.node.get_our_node_id(), &payment_event.msgs[0]);

// Mark dst's signer as unavailable and handle src's commitment_signed: while dst won't yet have a
// `commitment_signed` of its own to offer, it should publish a `revoke_and_ack`.
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, false);
dst.node.handle_commitment_signed(&src.node.get_our_node_id(), &payment_event.commitment_msg);
check_added_monitors(dst, 1);

get_event_msg!(dst, MessageSendEvent::SendRevokeAndACK, src.node.get_our_node_id());

// Mark dst's signer as available and retry: we now expect to see dst's `commitment_signed`.
dst.set_channel_signer_available(&src.node.get_our_node_id(), &chan_id, true);
dst.node.signer_unblocked(Some((src.node.get_our_node_id(), chan_id)));

let events = dst.node.get_and_clear_pending_msg_events();
let n = events.len();
assert_eq!(n, 1, "expected one message, got {n}");
if let MessageSendEvent::UpdateHTLCs { ref node_id, ref updates } = events[0] {
assert_eq!(node_id, &src.node.get_our_node_id());
} else {
panic!("expected UpdateHTLCs message, not {:?}", events[0]);
};
}

2 changes: 1 addition & 1 deletion lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3863,7 +3863,7 @@ impl<SP: Deref> Channel<SP> where
/// blocked.
pub fn signer_maybe_unblocked<L: Deref>(&mut self, logger: &L) -> SignerResumeUpdates where L::Target: Logger {
let commitment_update = if self.context.signer_pending_commitment_update {
None
self.get_last_commitment_update_for_send(logger).ok()
} else { None };
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
self.context.get_funding_signed_msg(logger).1
Expand Down
3 changes: 2 additions & 1 deletion lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,8 @@ macro_rules! unwrap_send_err {
pub fn check_added_monitors<CM: AChannelManager, H: NodeHolder<CM=CM>>(node: &H, count: usize) {
if let Some(chain_monitor) = node.chain_monitor() {
let mut added_monitors = chain_monitor.added_monitors.lock().unwrap();
assert_eq!(added_monitors.len(), count);
let n = added_monitors.len();
assert_eq!(n, count, "expected {count} monitors to be added, not {n}");
added_monitors.clear();
}
}
Expand Down

0 comments on commit 5ef9d7f

Please sign in to comment.