Skip to content

Commit 5502204

Browse files
committed
Send funding_locked immediately for inbound channels with 0conf
1 parent e7a17b7 commit 5502204

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

lightning/src/ln/channel.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ impl<Signer: Sign> Channel<Signer> {
20932093
&self.get_counterparty_pubkeys().funding_pubkey
20942094
}
20952095

2096-
pub fn funding_created<L: Deref>(&mut self, msg: &msgs::FundingCreated, best_block: BestBlock, logger: &L) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>), ChannelError> where L::Target: Logger {
2096+
pub fn funding_created<L: Deref>(&mut self, msg: &msgs::FundingCreated, best_block: BestBlock, logger: &L) -> Result<(msgs::FundingSigned, ChannelMonitor<Signer>, Option<msgs::FundingLocked>), ChannelError> where L::Target: Logger {
20972097
if self.is_outbound() {
20982098
return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
20992099
}
@@ -2168,7 +2168,7 @@ impl<Signer: Sign> Channel<Signer> {
21682168
Ok((msgs::FundingSigned {
21692169
channel_id: self.channel_id,
21702170
signature
2171-
}, channel_monitor))
2171+
}, channel_monitor, self.check_get_funding_locked(0)))
21722172
}
21732173

21742174
/// Handles a funding_signed message from the remote end.
@@ -6692,7 +6692,7 @@ mod tests {
66926692
}]};
66936693
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
66946694
let funding_created_msg = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap();
6695-
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
6695+
let (funding_signed_msg, _, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
66966696

66976697
// Node B --> Node A: funding signed
66986698
let _ = node_a_chan.funding_signed(&funding_signed_msg, best_block, &&logger);

lightning/src/ln/channelmanager.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22912291
action: msgs::ErrorAction::IgnoreError
22922292
});
22932293
}
2294+
if chan.get_short_channel_id().is_none() {
2295+
return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError});
2296+
}
22942297
log_trace!(self.logger, "Attempting to generate broadcast channel update for channel {}", log_bytes!(chan.channel_id()));
22952298
self.get_channel_update_for_unicast(chan)
22962299
}
@@ -2302,7 +2305,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23022305
/// May be called with channel_state already locked!
23032306
fn get_channel_update_for_unicast(&self, chan: &Channel<Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
23042307
log_trace!(self.logger, "Attempting to generate channel update for channel {}", log_bytes!(chan.channel_id()));
2305-
let short_channel_id = match chan.get_short_channel_id() {
2308+
let short_channel_id = match chan.get_short_channel_id().or(chan.latest_inbound_scid_alias()) {
23062309
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
23072310
Some(id) => id,
23082311
};
@@ -4260,7 +4263,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42604263
}
42614264

42624265
fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
4263-
let ((funding_msg, monitor), mut chan) = {
4266+
let ((funding_msg, monitor, mut funding_locked), mut chan) = {
42644267
let best_block = *self.best_block.read().unwrap();
42654268
let mut channel_lock = self.channel_state.lock().unwrap();
42664269
let channel_state = &mut *channel_lock;
@@ -4295,7 +4298,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42954298
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
42964299
// accepted payment from yet. We do, however, need to wait to send our funding_locked
42974300
// until we have persisted our monitor.
4298-
chan.monitor_update_failed(false, false, false, Vec::new(), Vec::new(), Vec::new());
4301+
chan.monitor_update_failed(false, false, funding_locked.is_some(), Vec::new(), Vec::new(), Vec::new());
4302+
funding_locked = None; // Don't send the funding_locked now
42994303
},
43004304
}
43014305
}
@@ -4310,6 +4314,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
43104314
node_id: counterparty_node_id.clone(),
43114315
msg: funding_msg,
43124316
});
4317+
if let Some(msg) = funding_locked {
4318+
send_funding_locked!(channel_state.short_to_id, channel_state.pending_msg_events, chan, msg);
4319+
}
43134320
e.insert(chan);
43144321
}
43154322
}

lightning/src/ln/priv_short_conf_tests.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,9 @@ fn test_simple_0conf_channel() {
570570
// If our peer tells us they will accept our channel with 0 confs, and we funded the channel,
571571
// we should trust the funding won't be double-spent (assuming `trust_own_funding_0conf` is
572572
// set)!
573+
// Further, if we `accept_inbound_channel_from_trusted_peer_0conf`, funding locked messages
574+
// should fly immediately and the channel should be available for use as soon as they are
575+
// received.
573576

574577
let chanmon_cfgs = create_chanmon_cfgs(2);
575578
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
@@ -602,11 +605,38 @@ fn test_simple_0conf_channel() {
602605

603606
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
604607
check_added_monitors!(nodes[1], 1);
605-
let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
606-
607-
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
608-
check_added_monitors!(nodes[0], 1);
608+
let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events();
609+
assert_eq!(bs_signed_locked.len(), 2);
610+
let as_funding_locked;
611+
match &bs_signed_locked[0] {
612+
MessageSendEvent::SendFundingSigned { node_id, msg } => {
613+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
614+
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &msg);
615+
check_added_monitors!(nodes[0], 1);
616+
617+
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
618+
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0)[0], tx);
619+
620+
as_funding_locked = get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id());
621+
}
622+
_ => panic!("Unexpected event"),
623+
}
624+
match &bs_signed_locked[1] {
625+
MessageSendEvent::SendFundingLocked { node_id, msg } => {
626+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
627+
nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &msg);
628+
}
629+
_ => panic!("Unexpected event"),
630+
}
609631

610-
let as_funding_locked = get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id());
611632
nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &as_funding_locked);
633+
634+
let as_channel_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
635+
let bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
636+
637+
nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_channel_update);
638+
nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &as_channel_update);
639+
640+
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
641+
assert_eq!(nodes[1].node.list_usable_channels().len(), 1);
612642
}

0 commit comments

Comments
 (0)