Skip to content

Commit be82239

Browse files
committed
Return the temporary channel id in success from create_channel
This makes it more practical for users to track channels prior to funding, especially if the channel fails because the peer rejects it for a parameter mismatch.
1 parent 2144166 commit be82239

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

lightning/src/ln/channelmanager.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -1267,22 +1267,30 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
12671267

12681268
/// Creates a new outbound channel to the given remote node and with the given value.
12691269
///
1270-
/// user_id will be provided back as user_channel_id in FundingGenerationReady events to allow
1271-
/// tracking of which events correspond with which create_channel call. Note that the
1272-
/// user_channel_id defaults to 0 for inbound channels, so you may wish to avoid using 0 for
1273-
/// user_id here. user_id has no meaning inside of LDK, it is simply copied to events and
1274-
/// otherwise ignored.
1270+
/// `user_id` will be provided back as `user_channel_id` in [`Event::FundingGenerationReady`]
1271+
/// to allow tracking of which events correspond with which `create_channel` call. Note that
1272+
/// the `user_channel_id` defaults to 0 for inbound channels, so you may wish to avoid using 0
1273+
/// for `user_id` here. `user_id` has no meaning inside of LDK, it is simply copied to events
1274+
/// and otherwise ignored.
12751275
///
1276-
/// If successful, will generate a SendOpenChannel message event, so you should probably poll
1277-
/// PeerManager::process_events afterwards.
1278-
///
1279-
/// Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat is
1280-
/// greater than channel_value_satoshis * 1k or channel_value_satoshis is < 1000.
1276+
/// Raises [`APIError::APIMisuseError`] when `channel_value_satoshis` > 2**24 or `push_msat` is
1277+
/// greater than `channel_value_satoshis * 1k` or `channel_value_satoshis < 1000`.
12811278
///
12821279
/// Note that we do not check if you are currently connected to the given peer. If no
12831280
/// connection is available, the outbound `open_channel` message may fail to send, resulting in
1284-
/// the channel eventually being silently forgotten.
1285-
pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, override_config: Option<UserConfig>) -> Result<(), APIError> {
1281+
/// the channel eventually being silently forgotten (dropped on reload).
1282+
///
1283+
/// Returns the new Channel's temporary `channel_id`. This ID will appear as
1284+
/// [`Event::FundingGenerationReady::temporary_channel_id`] and in
1285+
/// [`ChannelDetails::channel_id`] until after
1286+
/// [`ChannelManager::funding_transaction_generated`] is called, swapping the Channel's ID for
1287+
/// one derived from the funding transaction's TXID. If the counterparty rejects the channel
1288+
/// immediately, this temporary ID will appear in [`Event::ChannelClosed::channel_id`].
1289+
///
1290+
/// [`Event::FundingGenerationReady`]: events::Event::FundingGenerationReady
1291+
/// [`Event::FundingGenerationReady::temporary_channel_id`]: events::Event::FundingGenerationReady::temporary_channel_id
1292+
/// [`Event::ChannelClosed::channel_id`]: events::Event::ChannelClosed::channel_id
1293+
pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_id: u64, override_config: Option<UserConfig>) -> Result<[u8; 32], APIError> {
12861294
if channel_value_satoshis < 1000 {
12871295
return Err(APIError::APIMisuseError { err: format!("Channel value must be at least 1000 satoshis. It was {}", channel_value_satoshis) });
12881296
}
@@ -1305,8 +1313,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13051313
// We want to make sure the lock is actually acquired by PersistenceNotifierGuard.
13061314
debug_assert!(&self.total_consistency_lock.try_write().is_err());
13071315

1316+
let temporary_channel_id = channel.channel_id();
13081317
let mut channel_state = self.channel_state.lock().unwrap();
1309-
match channel_state.by_id.entry(channel.channel_id()) {
1318+
match channel_state.by_id.entry(temporary_channel_id) {
13101319
hash_map::Entry::Occupied(_) => {
13111320
if cfg!(feature = "fuzztarget") {
13121321
return Err(APIError::APIMisuseError { err: "Fuzzy bad RNG".to_owned() });
@@ -1320,7 +1329,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
13201329
node_id: their_network_key,
13211330
msg: res,
13221331
});
1323-
Ok(())
1332+
Ok(temporary_channel_id)
13241333
}
13251334

13261335
fn list_channels_with_filter<Fn: FnMut(&(&[u8; 32], &Channel<Signer>)) -> bool>(&self, f: Fn) -> Vec<ChannelDetails> {

lightning/src/ln/functional_test_utils.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,12 @@ pub fn create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, expected_
512512
}
513513

514514
pub fn create_chan_between_nodes_with_value_init<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>, channel_value: u64, push_msat: u64, a_flags: InitFeatures, b_flags: InitFeatures) -> Transaction {
515-
node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42, None).unwrap();
515+
let create_chan_id = node_a.node.create_channel(node_b.node.get_our_node_id(), channel_value, push_msat, 42, None).unwrap();
516516
node_b.node.handle_open_channel(&node_a.node.get_our_node_id(), a_flags, &get_event_msg!(node_a, MessageSendEvent::SendOpenChannel, node_b.node.get_our_node_id()));
517517
node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), b_flags, &get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id()));
518518

519519
let (temporary_channel_id, tx, funding_output) = create_funding_transaction(node_a, channel_value, 42);
520+
assert_eq!(temporary_channel_id, create_chan_id);
520521

521522
node_a.node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
522523
check_added_monitors!(node_a, 0);

0 commit comments

Comments
 (0)