Skip to content

Commit b3bda9e

Browse files
committed
Randomize user_channel_id for inbound channels
Previously, all inbound channels defaulted to a `user_channel_id` of 0, which didn't allow for them being discerned on that basis. Here, we simply randomize the identifier to fix this and enable the use of `user_channel_id` as a true identifier for channels (assuming an equally reasonable value is chosen for outbound channels and given upon `create_channel()`).
1 parent d6321e6 commit b3bda9e

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

lightning/src/ln/channelmanager.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1687,10 +1687,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
16871687
///
16881688
/// `user_channel_id` will be provided back as in
16891689
/// [`Event::FundingGenerationReady::user_channel_id`] to allow tracking of which events
1690-
/// correspond with which `create_channel` call. Note that the `user_channel_id` defaults to 0
1691-
/// for inbound channels, so you may wish to avoid using 0 for `user_channel_id` here.
1692-
/// `user_channel_id` has no meaning inside of LDK, it is simply copied to events and otherwise
1693-
/// ignored.
1690+
/// correspond with which `create_channel` call. Note that the `user_channel_id` defaults to a
1691+
/// randomized value for inbound channels. `user_channel_id` has no meaning inside of LDK, it
1692+
/// is simply copied to events and otherwise ignored.
16941693
///
16951694
/// Raises [`APIError::APIMisuseError`] when `channel_value_satoshis` > 2**24 or `push_msat` is
16961695
/// greater than `channel_value_satoshis * 1k` or `channel_value_satoshis < 1000`.
@@ -4457,9 +4456,13 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
44574456
return Err(MsgHandleErrInternal::send_err_msg_no_close("No inbound channels accepted".to_owned(), msg.temporary_channel_id.clone()));
44584457
}
44594458

4459+
let mut random_bytes = [0u8; 8];
4460+
random_bytes.copy_from_slice(&self.keys_manager.get_secure_random_bytes()[..8]);
4461+
let user_channel_id = u64::from_be_bytes(random_bytes);
4462+
44604463
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
44614464
let mut channel = match Channel::new_from_req(&self.fee_estimator, &self.keys_manager,
4462-
counterparty_node_id.clone(), &their_features, msg, 0, &self.default_configuration,
4465+
counterparty_node_id.clone(), &their_features, msg, user_channel_id, &self.default_configuration,
44634466
self.best_block.read().unwrap().height(), &self.logger, outbound_scid_alias)
44644467
{
44654468
Err(e) => {

0 commit comments

Comments
 (0)