Skip to content

Commit be5b553

Browse files
committed
Add API and signaling to accept incoming channels at 0conf
1 parent cd48991 commit be5b553

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

lightning/src/ln/channel.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ impl<Signer: Sign> Channel<Signer> {
13121312
counterparty_htlc_minimum_msat: msg.htlc_minimum_msat,
13131313
holder_htlc_minimum_msat: if config.own_channel_config.our_htlc_minimum_msat == 0 { 1 } else { config.own_channel_config.our_htlc_minimum_msat },
13141314
counterparty_max_accepted_htlcs: msg.max_accepted_htlcs,
1315-
minimum_depth: Some(config.own_channel_config.minimum_depth),
1315+
minimum_depth: Some(cmp::max(config.own_channel_config.minimum_depth, 1)),
13161316

13171317
counterparty_forwarding_info: None,
13181318

@@ -4863,6 +4863,12 @@ impl<Signer: Sign> Channel<Signer> {
48634863
self.inbound_awaiting_accept
48644864
}
48654865

4866+
/// Sets this channel to accepting 0conf, must be done before `get_accept_channel`
4867+
pub fn set_0conf(&mut self) {
4868+
assert!(self.inbound_awaiting_accept);
4869+
self.minimum_depth = Some(0);
4870+
}
4871+
48664872
/// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannel`] message which
48674873
/// should be sent back to the counterparty node.
48684874
///

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4120,18 +4120,43 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41204120
}
41214121
}
41224122

4123-
/// Called to accept a request to open a channel after [`Event::OpenChannelRequest`] has been
4124-
/// triggered.
4123+
/// Accepts a request to open a channel after a [`Event::OpenChannelRequest`].
41254124
///
41264125
/// The `temporary_channel_id` parameter indicates which inbound channel should be accepted.
41274126
///
4128-
/// For inbound channels, the `user_channel_id` parameter will be provided back in
4127+
/// The `user_channel_id` parameter will be provided back in
41294128
/// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
41304129
/// with which `accept_inbound_channel` call.
41314130
///
41324131
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
41334132
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
41344133
pub fn accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], user_channel_id: u64) -> Result<(), APIError> {
4134+
self.do_accept_inbound_channel(temporary_channel_id, false, user_channel_id)
4135+
}
4136+
4137+
/// Accepts a request to open a channel after a [`events::Event::OpenChannelRequest`], treating
4138+
/// it as confirmed immediately.
4139+
///
4140+
/// The `user_channel_id` parameter will be provided back in
4141+
/// [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
4142+
/// with which `accept_inbound_channel` call.
4143+
///
4144+
/// Unlike [`ChannelManager::accept_inbound_channel`], this method accepts the incoming channel
4145+
/// and (if the counterparty agrees), enables forwarding of payments immediately.
4146+
///
4147+
/// This fully trusts that the counterparty has honestly and correctly constructed the funding
4148+
/// transaction and blindly assumes that it will eventually confirm.
4149+
///
4150+
/// If it does not confirm before we decide to close the channel, or if the funding transaction
4151+
/// does not pay to the correct script the correct amount, *you will lose funds*.
4152+
///
4153+
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
4154+
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
4155+
pub fn accept_inbound_channel_from_trusted_peer_0conf(&self, temporary_channel_id: &[u8; 32], user_channel_id: u64) -> Result<(), APIError> {
4156+
self.do_accept_inbound_channel(temporary_channel_id, true, user_channel_id)
4157+
}
4158+
4159+
fn do_accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], accept_0conf: bool, user_channel_id: u64) -> Result<(), APIError> {
41354160
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
41364161

41374162
let mut channel_state_lock = self.channel_state.lock().unwrap();
@@ -4141,6 +4166,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41414166
if !channel.get().inbound_is_awaiting_accept() {
41424167
return Err(APIError::APIMisuseError { err: "The channel isn't currently awaiting to be accepted.".to_owned() });
41434168
}
4169+
if accept_0conf { channel.get_mut().set_0conf(); }
41444170
channel_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
41454171
node_id: channel.get().get_counterparty_node_id(),
41464172
msg: channel.get_mut().accept_inbound_channel(user_channel_id),

lightning/src/ln/priv_short_conf_tests.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,15 +565,27 @@ fn test_simple_0_conf_channel() {
565565

566566
let chanmon_cfgs = create_chanmon_cfgs(2);
567567
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
568-
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
568+
let mut chan_config = test_default_channel_config();
569+
chan_config.manually_accept_inbound_channels = true;
570+
571+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(chan_config)]);
569572
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
570573

571574
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
572575
let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
573576

574577
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
578+
let events = nodes[1].node.get_and_clear_pending_events();
579+
assert_eq!(events.len(), 1);
580+
match events[0] {
581+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
582+
nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, 0).unwrap();
583+
},
584+
_ => panic!("Unexpected event"),
585+
};
586+
575587
let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
576-
accept_channel.minimum_depth = 0;
588+
assert_eq!(accept_channel.minimum_depth, 0);
577589
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
578590

579591
let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], 100000, 42);

lightning/src/util/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ pub struct ChannelHandshakeConfig {
2222
/// Applied only for inbound channels (see ChannelHandshakeLimits::max_minimum_depth for the
2323
/// equivalent limit applied to outbound channels).
2424
///
25+
/// A lower-bound of 1 is applied, requiring all channels to have a confirmed commitment
26+
/// transaction before operation. If you wish to accept channels with zero confirmations, see
27+
/// [`UserConfig::manually_accept_inbound_channels`] and
28+
/// [`ChannelManager::accept_inbound_channel`].
29+
///
2530
/// Default value: 6.
31+
///
32+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
2633
pub minimum_depth: u32,
2734
/// Set to the number of blocks we require our counterparty to wait to claim their money (ie
2835
/// the number of blocks we have to punish our counterparty if they broadcast a revoked

0 commit comments

Comments
 (0)