Skip to content

Commit 96d80a7

Browse files
committed
Send funding_locked immediately for inbound channels with 0conf
1 parent f1581b8 commit 96d80a7

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
@@ -2042,7 +2042,7 @@ impl<Signer: Sign> Channel<Signer> {
20422042
&self.get_counterparty_pubkeys().funding_pubkey
20432043
}
20442044

2045-
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 {
2045+
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 {
20462046
if self.is_outbound() {
20472047
return Err(ChannelError::Close("Received funding_created for an outbound channel?".to_owned()));
20482048
}
@@ -2117,7 +2117,7 @@ impl<Signer: Sign> Channel<Signer> {
21172117
Ok((msgs::FundingSigned {
21182118
channel_id: self.channel_id,
21192119
signature
2120-
}, channel_monitor))
2120+
}, channel_monitor, self.check_get_funding_locked(0)))
21212121
}
21222122

21232123
/// Handles a funding_signed message from the remote end.
@@ -6605,7 +6605,7 @@ mod tests {
66056605
}]};
66066606
let funding_outpoint = OutPoint{ txid: tx.txid(), index: 0 };
66076607
let funding_created_msg = node_a_chan.get_outbound_funding_created(tx.clone(), funding_outpoint, &&logger).unwrap();
6608-
let (funding_signed_msg, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
6608+
let (funding_signed_msg, _, _) = node_b_chan.funding_created(&funding_created_msg, best_block, &&logger).unwrap();
66096609

66106610
// Node B --> Node A: funding signed
66116611
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
@@ -2529,6 +2529,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25292529
action: msgs::ErrorAction::IgnoreError
25302530
});
25312531
}
2532+
if chan.get_short_channel_id().is_none() {
2533+
return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError});
2534+
}
25322535
log_trace!(self.logger, "Attempting to generate broadcast channel update for channel {}", log_bytes!(chan.channel_id()));
25332536
self.get_channel_update_for_unicast(chan)
25342537
}
@@ -2540,7 +2543,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25402543
/// May be called with channel_state already locked!
25412544
fn get_channel_update_for_unicast(&self, chan: &Channel<Signer>) -> Result<msgs::ChannelUpdate, LightningError> {
25422545
log_trace!(self.logger, "Attempting to generate channel update for channel {}", log_bytes!(chan.channel_id()));
2543-
let short_channel_id = match chan.get_short_channel_id() {
2546+
let short_channel_id = match chan.get_short_channel_id().or(chan.latest_inbound_scid_alias()) {
25442547
None => return Err(LightningError{err: "Channel not yet established".to_owned(), action: msgs::ErrorAction::IgnoreError}),
25452548
Some(id) => id,
25462549
};
@@ -4489,7 +4492,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
44894492
}
44904493

44914494
fn internal_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<(), MsgHandleErrInternal> {
4492-
let ((funding_msg, monitor), mut chan) = {
4495+
let ((funding_msg, monitor, mut funding_locked), mut chan) = {
44934496
let best_block = *self.best_block.read().unwrap();
44944497
let mut channel_lock = self.channel_state.lock().unwrap();
44954498
let channel_state = &mut *channel_lock;
@@ -4524,7 +4527,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
45244527
// hasn't persisted to disk yet - we can't lose money on a transaction that we haven't
45254528
// accepted payment from yet. We do, however, need to wait to send our funding_locked
45264529
// until we have persisted our monitor.
4527-
chan.monitor_update_failed(false, false, false, Vec::new(), Vec::new(), Vec::new());
4530+
chan.monitor_update_failed(false, false, funding_locked.is_some(), Vec::new(), Vec::new(), Vec::new());
4531+
funding_locked = None; // Don't send the funding_locked now
45284532
},
45294533
}
45304534
}
@@ -4539,6 +4543,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
45394543
node_id: counterparty_node_id.clone(),
45404544
msg: funding_msg,
45414545
});
4546+
if let Some(msg) = funding_locked {
4547+
send_funding_locked!(channel_state.short_to_id, channel_state.pending_msg_events, chan, msg);
4548+
}
45424549
e.insert(chan);
45434550
}
45444551
}

lightning/src/ln/priv_short_conf_tests.rs

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

551554
let chanmon_cfgs = create_chanmon_cfgs(2);
552555
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
@@ -579,11 +582,38 @@ fn test_simple_0_conf_channel() {
579582

580583
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created);
581584
check_added_monitors!(nodes[1], 1);
582-
let funding_signed = get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id());
583-
584-
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &funding_signed);
585-
check_added_monitors!(nodes[0], 1);
585+
let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events();
586+
assert_eq!(bs_signed_locked.len(), 2);
587+
let as_funding_locked;
588+
match &bs_signed_locked[0] {
589+
MessageSendEvent::SendFundingSigned { node_id, msg } => {
590+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
591+
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &msg);
592+
check_added_monitors!(nodes[0], 1);
593+
594+
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1);
595+
assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0)[0], tx);
596+
597+
as_funding_locked = get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id());
598+
}
599+
_ => panic!("Unexpected event"),
600+
}
601+
match &bs_signed_locked[1] {
602+
MessageSendEvent::SendFundingLocked { node_id, msg } => {
603+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
604+
nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &msg);
605+
}
606+
_ => panic!("Unexpected event"),
607+
}
586608

587-
let as_funding_locked = get_event_msg!(nodes[0], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id());
588609
nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &as_funding_locked);
610+
611+
let as_channel_update = get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id());
612+
let bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id());
613+
614+
nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_channel_update);
615+
nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &as_channel_update);
616+
617+
assert_eq!(nodes[0].node.list_usable_channels().len(), 1);
618+
assert_eq!(nodes[1].node.list_usable_channels().len(), 1);
589619
}

0 commit comments

Comments
 (0)