Skip to content

Commit e40ccd3

Browse files
committed
Fix inbound zero-conf
When we receive an inbound zero-conf channel, we need to defer sending the `channel_ready` message until *after* we've sent the `funding_signed` message. We won't be able to produce the `funding_signed` message until the signer has produced the counterparty commitment signature.
1 parent 006745a commit e40ccd3

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed

lightning/src/ln/async_signer_tests.rs

+104-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! Tests for asynchronous signing. These tests verify that the channel state machine behaves
1111
//! properly with a signer implementation that asynchronously derives signatures.
1212
13-
use crate::events::{MessageSendEvent, MessageSendEventsProvider};
13+
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider};
1414
use crate::ln::functional_test_utils::*;
1515
use crate::ln::msgs::ChannelMessageHandler;
1616
use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
@@ -183,6 +183,109 @@ fn test_async_commitment_signature_for_commitment_signed() {
183183
};
184184
}
185185

186+
#[test]
187+
fn test_async_commitment_signature_for_funding_signed_0conf() {
188+
// Simulate acquiring the signature for `funding_signed` asynchronously for a zero-conf channel.
189+
let mut manually_accept_config = test_default_channel_config();
190+
manually_accept_config.manually_accept_inbound_channels = true;
191+
192+
let chanmon_cfgs = create_chanmon_cfgs(2);
193+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
194+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_config)]);
195+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
196+
197+
// nodes[0] --- open_channel --> nodes[1]
198+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
199+
let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
200+
201+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
202+
203+
{
204+
let events = nodes[1].node.get_and_clear_pending_events();
205+
assert_eq!(events.len(), 1, "Expected one event, got {}", events.len());
206+
match &events[0] {
207+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
208+
nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(
209+
temporary_channel_id, &nodes[0].node.get_our_node_id(), 0)
210+
.expect("Unable to accept inbound zero-conf channel");
211+
},
212+
ev => panic!("Expected OpenChannelRequest, not {:?}", ev)
213+
}
214+
}
215+
216+
// nodes[0] <-- accept_channel --- nodes[1]
217+
let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
218+
assert_eq!(accept_channel.minimum_depth, 0, "Expected minimum depth of 0");
219+
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
220+
221+
// nodes[0] --- funding_created --> nodes[1]
222+
let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
223+
nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap();
224+
check_added_monitors(&nodes[0], 0);
225+
226+
let mut funding_created_msg = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
227+
228+
// Now let's make node[1]'s signer be unavailable while handling the `funding_created`. It should
229+
// *not* broadcast a `funding_signed`...
230+
nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &temporary_channel_id, false);
231+
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created_msg);
232+
check_added_monitors(&nodes[1], 1);
233+
234+
{
235+
let events = nodes[1].node.get_and_clear_pending_msg_events();
236+
let n = events.len();
237+
assert_eq!(n, 0, "expected no events generated from nodes[1], found {}", n);
238+
}
239+
240+
// Now re-enable the signer and simulate a retry. The temporary_channel_id won't work anymore so
241+
// we have to dig out the real channel ID.
242+
let chan_id = {
243+
let channels = nodes[0].node.list_channels();
244+
assert_eq!(channels.len(), 1, "expected one channel, not {}", channels.len());
245+
channels[0].channel_id
246+
};
247+
248+
// At this point, we basically expect the channel to open like a normal zero-conf channel.
249+
nodes[1].set_channel_signer_available(&nodes[0].node.get_our_node_id(), &chan_id, true);
250+
nodes[1].node.signer_unblocked(Some((nodes[0].node.get_our_node_id(), chan_id)));
251+
252+
let (funding_signed, channel_ready_1) = {
253+
let events = nodes[1].node.get_and_clear_pending_msg_events();
254+
assert_eq!(events.len(), 2);
255+
let funding_signed = match &events[0] {
256+
MessageSendEvent::SendFundingSigned { msg, .. } => msg.clone(),
257+
ev => panic!("Expected SendFundingSigned, not {:?}", ev)
258+
};
259+
let channel_ready = match &events[1] {
260+
MessageSendEvent::SendChannelReady { msg, .. } => msg.clone(),
261+
ev => panic!("Expected SendChannelReady, not {:?}", ev)
262+
};
263+
(funding_signed, channel_ready)
264+
};
265+
266+
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
267+
expect_channel_pending_event(&nodes[0], &nodes[1].node.get_our_node_id());
268+
expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
269+
check_added_monitors(&nodes[0], 1);
270+
271+
let channel_ready_0 = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id());
272+
273+
nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &channel_ready_1);
274+
expect_channel_ready_event(&nodes[0], &nodes[1].node.get_our_node_id());
275+
276+
nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &channel_ready_0);
277+
expect_channel_ready_event(&nodes[1], &nodes[0].node.get_our_node_id());
278+
279+
let channel_update_0 = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
280+
let channel_update_1 = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
281+
282+
nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &channel_update_1);
283+
nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &channel_update_0);
284+
285+
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
286+
assert_eq!(nodes[1].node.list_usable_channels().len(), 1);
287+
}
288+
186289
#[test]
187290
fn test_async_commitment_signature_for_peer_disconnect() {
188291
let chanmon_cfgs = create_chanmon_cfgs(2);

lightning/src/ln/channel.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ pub(super) struct SignerResumeUpdates {
538538
pub commitment_update: Option<msgs::CommitmentUpdate>,
539539
pub funding_signed: Option<msgs::FundingSigned>,
540540
pub funding_created: Option<msgs::FundingCreated>,
541+
pub channel_ready: Option<msgs::ChannelReady>,
541542
}
542543

543544
/// The return value of `channel_reestablish`
@@ -3973,13 +3974,17 @@ impl<SP: Deref> Channel<SP> where
39733974
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
39743975
self.context.get_funding_signed_msg(logger).1
39753976
} else { None };
3977+
let channel_ready = if funding_signed.is_some() {
3978+
self.check_get_channel_ready(0)
3979+
} else { None };
39763980
let funding_created = if self.context.signer_pending_funding && self.context.is_outbound() {
39773981
self.context.get_funding_created_msg(logger)
39783982
} else { None };
39793983
SignerResumeUpdates {
39803984
commitment_update,
39813985
funding_signed,
39823986
funding_created,
3987+
channel_ready,
39833988
}
39843989
}
39853990

@@ -6796,15 +6801,16 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
67966801
counterparty_initial_commitment_tx.to_broadcaster_value_sat(),
67976802
counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
67986803

6799-
log_info!(logger, "Generated funding_signed for peer for channel {}", &self.context.channel_id());
6804+
log_info!(logger, "{} funding_signed for peer for channel {}",
6805+
if funding_signed.is_some() { "Generated" } else { "Waiting for signature on" }, &self.context.channel_id());
68006806

68016807
// Promote the channel to a full-fledged one now that we have updated the state and have a
68026808
// `ChannelMonitor`.
68036809
let mut channel = Channel {
68046810
context: self.context,
68056811
};
68066812

6807-
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
6813+
let need_channel_ready = funding_signed.is_some() && channel.check_get_channel_ready(0).is_some();
68086814
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
68096815

68106816
Ok((channel, funding_signed, channel_monitor))

lightning/src/ln/channelmanager.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6996,9 +6996,9 @@ where
69966996
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
69976997

69986998
let unblock_chan = |phase: &mut ChannelPhase<SP>, pending_msg_events: &mut Vec<MessageSendEvent>| {
6999+
let node_id = phase.context().get_counterparty_node_id();
69997000
if let ChannelPhase::Funded(chan) = phase {
70007001
let msgs = chan.signer_maybe_unblocked(&self.logger);
7001-
let node_id = phase.context().get_counterparty_node_id();
70027002
if let Some(updates) = msgs.commitment_update {
70037003
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
70047004
node_id,
@@ -7017,6 +7017,9 @@ where
70177017
msg,
70187018
});
70197019
}
7020+
if let Some(msg) = msgs.channel_ready {
7021+
send_channel_ready!(self, pending_msg_events, chan, msg);
7022+
}
70207023
}
70217024
};
70227025

0 commit comments

Comments
 (0)