Skip to content

Commit 93192e1

Browse files
committed
Send funding_locked immediately for inbound channels with 0conf
1 parent 2a7ba4e commit 93192e1

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
@@ -2089,7 +2089,7 @@ impl<Signer: Sign> Channel<Signer> {
20892089
&self.get_counterparty_pubkeys().funding_pubkey
20902090
}
20912091

2092-
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 {
2092+
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 {
20932093
if self.is_outbound() {
20942094
return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
20952095
}
@@ -2164,7 +2164,7 @@ impl<Signer: Sign> Channel<Signer> {
21642164
Ok((msgs::FundingSigned {
21652165
channel_id: self.channel_id,
21662166
signature
2167-
}, channel_monitor))
2167+
}, channel_monitor, self.check_get_funding_locked(0)))
21682168
}
21692169

21702170
/// Handles a funding_signed message from the remote end.
@@ -6677,7 +6677,7 @@ mod tests {
66776677
}]};
66786678
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
66796679
let funding_created_msg = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap();
6680-
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
6680+
let (funding_signed_msg, _, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
66816681

66826682
// Node B --> Node A: funding signed
66836683
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
@@ -2294,6 +2294,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22942294
action: msgs::ErrorAction::IgnoreError
22952295
});
22962296
}
2297+
if chan.get_short_channel_id().is_none() {
2298+
return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError});
2299+
}
22972300
log_trace!(self.logger, "Attempting to generate broadcast channel update for channel {}", log_bytes!(chan.channel_id()));
22982301
self.get_channel_update_for_unicast(chan)
22992302
}
@@ -2305,7 +2308,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
23052308
/// May be called with channel_state already locked!
23062309
fn get_channel_update_for_unicast(&self, chan: &Channel<Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
23072310
log_trace!(self.logger, "Attempting to generate channel update for channel {}", log_bytes!(chan.channel_id()));
2308-
let short_channel_id = match chan.get_short_channel_id() {
2311+
let short_channel_id = match chan.get_short_channel_id().or(chan.latest_inbound_scid_alias()) {
23092312
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
23102313
Some(id) => id,
23112314
};
@@ -4257,7 +4260,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42574260
}
42584261

42594262
fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
4260-
let ((funding_msg, monitor), mut chan) = {
4263+
let ((funding_msg, monitor, mut funding_locked), mut chan) = {
42614264
let best_block = *self.best_block.read().unwrap();
42624265
let mut channel_lock = self.channel_state.lock().unwrap();
42634266
let channel_state = &mut *channel_lock;
@@ -4292,7 +4295,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42924295
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
42934296
// accepted payment from yet. We do, however, need to wait to send our funding_locked
42944297
// until we have persisted our monitor.
4295-
chan.monitor_update_failed(false, false, false, Vec::new(), Vec::new(), Vec::new());
4298+
chan.monitor_update_failed(false, false, funding_locked.is_some(), Vec::new(), Vec::new(), Vec::new());
4299+
funding_locked = None; // Don't send the funding_locked now
42964300
},
42974301
}
42984302
}
@@ -4307,6 +4311,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
43074311
node_id: counterparty_node_id.clone(),
43084312
msg: funding_msg,
43094313
});
4314+
if let Some(msg) = funding_locked {
4315+
send_funding_locked!(channel_state.short_to_id, channel_state.pending_msg_events, chan, msg);
4316+
}
43104317
e.insert(chan);
43114318
}
43124319
}

lightning/src/ln/priv_short_conf_tests.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ fn test_simple_0_conf_channel() {
562562
// If our peer tells us they will accept our channel with 0 confs, and we funded the channel,
563563
// we should trust the funding won't be double-spent (assuming `trust_own_funding_0conf` is
564564
// set)!
565+
// Further, if we `accept_inbound_channel_from_trusted_peer_0conf`, funding locked messages
566+
// should fly immediately and the channel should be available for use as soon as they are
567+
// received.
565568

566569
let chanmon_cfgs = create_chanmon_cfgs(2);
567570
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
@@ -594,11 +597,38 @@ fn test_simple_0_conf_channel() {
594597

595598
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
596599
check_added_monitors!(nodes[1], 1);
597-
let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
598-
599-
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
600-
check_added_monitors!(nodes[0], 1);
600+
let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events();
601+
assert_eq!(bs_signed_locked.len(), 2);
602+
let as_funding_locked;
603+
match &bs_signed_locked[0] {
604+
MessageSendEvent::SendFundingSigned { node_id, msg } => {
605+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
606+
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &msg);
607+
check_added_monitors!(nodes[0], 1);
608+
609+
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
610+
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0)[0], tx);
611+
612+
as_funding_locked = get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id());
613+
}
614+
_ => panic!("Unexpected event"),
615+
}
616+
match &bs_signed_locked[1] {
617+
MessageSendEvent::SendFundingLocked { node_id, msg } => {
618+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
619+
nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &msg);
620+
}
621+
_ => panic!("Unexpected event"),
622+
}
601623

602-
let as_funding_locked = get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id());
603624
nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &as_funding_locked);
625+
626+
let as_channel_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
627+
let bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
628+
629+
nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_channel_update);
630+
nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &as_channel_update);
631+
632+
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
633+
assert_eq!(nodes[1].node.list_usable_channels().len(), 1);
604634
}

0 commit comments

Comments
 (0)