Skip to content

Commit e7a17b7

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

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
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

@@ -4878,6 +4878,12 @@ impl<Signer: Sign> Channel<Signer> {
48784878
self.inbound_awaiting_accept
48794879
}
48804880

4881+
/// Sets this channel to accepting 0conf, must be done before `get_accept_channel`
4882+
pub fn set_0conf(&mut self) {
4883+
assert!(self.inbound_awaiting_accept);
4884+
self.minimum_depth = Some(0);
4885+
}
4886+
48814887
/// Marks an inbound channel as accepted and generates a [`msgs::AcceptChannel`] message which
48824888
/// should be sent back to the counterparty node.
48834889
///

lightning/src/ln/channelmanager.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4117,20 +4117,45 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41174117
}
41184118
}
41194119

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

41364161
let mut channel_state_lock = self.channel_state.lock().unwrap();
@@ -4143,6 +4168,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
41434168
if *counterparty_node_id != channel.get().get_counterparty_node_id() {
41444169
return Err(APIError::APIMisuseError { err: "The passed counterparty_node_id doesn't match the channel's counterparty node_id".to_owned() });
41454170
}
4171+
if accept_0conf { channel.get_mut().set_0conf(); }
41464172
channel_state.pending_msg_events.push(events::MessageSendEvent::SendAcceptChannel {
41474173
node_id: channel.get().get_counterparty_node_id(),
41484174
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
@@ -573,15 +573,27 @@ fn test_simple_0conf_channel() {
573573

574574
let chanmon_cfgs = create_chanmon_cfgs(2);
575575
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
576-
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
576+
let mut chan_config = test_default_channel_config();
577+
chan_config.manually_accept_inbound_channels = true;
578+
579+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(chan_config)]);
577580
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
578581

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

582585
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
586+
let events = nodes[1].node.get_and_clear_pending_events();
587+
assert_eq!(events.len(), 1);
588+
match events[0] {
589+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
590+
nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap();
591+
},
592+
_ => panic!("Unexpected event"),
593+
};
594+
583595
let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
584-
accept_channel.minimum_depth = 0;
596+
assert_eq!(accept_channel.minimum_depth, 0);
585597
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
586598

587599
let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);

lightning/src/util/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ 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_from_trusted_peer_0conf`].
29+
///
2530
/// Default value: 6.
31+
///
32+
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
33+
/// [`ChannelManager::accept_inbound_channel_from_trusted_peer_0conf`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel_from_trusted_peer_0conf
2634
pub minimum_depth: u32,
2735
/// Set to the number of blocks we require our counterparty to wait to claim their money (ie
2836
/// the number of blocks we have to punish our counterparty if they broadcast a revoked

0 commit comments

Comments
 (0)