Skip to content

Commit 6b942d6

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 8f8d33f commit 6b942d6

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
@@ -537,6 +537,7 @@ pub(super) struct SignerResumeUpdates {
537537
pub commitment_update: Option<msgs::CommitmentUpdate>,
538538
pub funding_signed: Option<msgs::FundingSigned>,
539539
pub funding_created: Option<msgs::FundingCreated>,
540+
pub channel_ready: Option<msgs::ChannelReady>,
540541
}
541542

542543
/// The return value of `channel_reestablish`
@@ -3971,13 +3972,17 @@ impl<SP: Deref> Channel<SP> where
39713972
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
39723973
self.context.get_funding_signed_msg(logger).1
39733974
} else { None };
3975+
let channel_ready = if funding_signed.is_some() {
3976+
self.check_get_channel_ready(0)
3977+
} else { None };
39743978
let funding_created = if self.context.signer_pending_funding && self.context.is_outbound() {
39753979
self.context.get_funding_created_msg(logger)
39763980
} else { None };
39773981
SignerResumeUpdates {
39783982
commitment_update,
39793983
funding_signed,
39803984
funding_created,
3985+
channel_ready,
39813986
}
39823987
}
39833988

@@ -6794,15 +6799,16 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
67946799
counterparty_initial_commitment_tx.to_broadcaster_value_sat(),
67956800
counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
67966801

6797-
log_info!(logger, "Generated funding_signed for peer for channel {}", &self.context.channel_id());
6802+
log_info!(logger, "{} funding_signed for peer for channel {}",
6803+
if funding_signed.is_some() { "Generated" } else { "Waiting for signature on" }, &self.context.channel_id());
67986804

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

6805-
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
6811+
let need_channel_ready = funding_signed.is_some() && channel.check_get_channel_ready(0).is_some();
68066812
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
68076813

68086814
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)