Skip to content

Commit 71ea826

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 6ab8e62 commit 71ea826

File tree

3 files changed

+119
-4
lines changed

3 files changed

+119
-4
lines changed

lightning/src/ln/async_signer_tests.rs

+107-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};
@@ -189,6 +189,112 @@ fn test_async_commitment_signature_for_commitment_signed() {
189189
};
190190
}
191191

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

lightning/src/ln/channel.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ pub(super) struct SignerResumeUpdates {
520520
pub commitment_update: Option<msgs::CommitmentUpdate>,
521521
pub funding_signed: Option<msgs::FundingSigned>,
522522
pub funding_created: Option<msgs::FundingCreated>,
523+
pub channel_ready: Option<msgs::ChannelReady>,
523524
}
524525

525526
/// The return value of `channel_reestablish`
@@ -3892,13 +3893,17 @@ impl<SP: Deref> Channel<SP> where
38923893
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
38933894
self.context.get_funding_signed_msg(logger).1
38943895
} else { None };
3896+
let channel_ready = if funding_signed.is_some() {
3897+
self.check_get_channel_ready(0)
3898+
} else { None };
38953899
let funding_created = if self.context.signer_pending_funding && self.context.is_outbound() {
38963900
self.context.get_funding_created_msg(logger)
38973901
} else { None };
38983902
SignerResumeUpdates {
38993903
commitment_update,
39003904
funding_signed,
39013905
funding_created,
3906+
channel_ready,
39023907
}
39033908
}
39043909

@@ -6706,15 +6711,16 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
67066711
counterparty_initial_commitment_tx.to_broadcaster_value_sat(),
67076712
counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
67086713

6709-
log_info!(logger, "Generated funding_signed for peer for channel {}", &self.context.channel_id());
6714+
log_info!(logger, "{} funding_signed for peer for channel {}",
6715+
if funding_signed.is_some() { "Generated" } else { "Waiting for signature on" }, &self.context.channel_id());
67106716

67116717
// Promote the channel to a full-fledged one now that we have updated the state and have a
67126718
// `ChannelMonitor`.
67136719
let mut channel = Channel {
67146720
context: self.context,
67156721
};
67166722

6717-
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
6723+
let need_channel_ready = funding_signed.is_some() && channel.check_get_channel_ready(0).is_some();
67186724
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
67196725

67206726
Ok((channel, funding_signed, channel_monitor))

lightning/src/ln/channelmanager.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6800,9 +6800,9 @@ where
68006800
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
68016801

68026802
let unblock_chan = |phase: &mut ChannelPhase<SP>, pending_msg_events: &mut Vec<MessageSendEvent>| {
6803+
let node_id = phase.context().get_counterparty_node_id();
68036804
if let ChannelPhase::Funded(chan) = phase {
68046805
let msgs = chan.signer_maybe_unblocked(&self.logger);
6805-
let node_id = phase.context().get_counterparty_node_id();
68066806
if let Some(updates) = msgs.commitment_update {
68076807
pending_msg_events.push(events::MessageSendEvent::UpdateHTLCs {
68086808
node_id,
@@ -6821,6 +6821,9 @@ where
68216821
msg,
68226822
});
68236823
}
6824+
if let Some(msg) = msgs.channel_ready {
6825+
send_channel_ready!(self, pending_msg_events, chan, msg);
6826+
}
68246827
}
68256828
};
68266829

0 commit comments

Comments
 (0)