Skip to content

Commit 1150480

Browse files
committed
Make user_channel_id a u128
We increase the `user_channel_id` type from `u64` to `u128`. In order to maintain backwards compatibility, we have to de-/serialize it as two separate `u64`s in `Event` as well as in the `Channel` itself.
1 parent b3bda9e commit 1150480

File tree

5 files changed

+81
-25
lines changed

5 files changed

+81
-25
lines changed

lightning/src/ln/channel.rs

+37-7
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ pub(super) struct Channel<Signer: Sign> {
509509

510510
inbound_handshake_limits_override: Option<ChannelHandshakeLimits>,
511511

512-
user_id: u64,
512+
user_id: u128,
513513

514514
channel_id: [u8; 32],
515515
channel_state: u32,
@@ -899,7 +899,7 @@ impl<Signer: Sign> Channel<Signer> {
899899
// Constructors:
900900
pub fn new_outbound<K: Deref, F: Deref>(
901901
fee_estimator: &LowerBoundedFeeEstimator<F>, keys_provider: &K, counterparty_node_id: PublicKey, their_features: &InitFeatures,
902-
channel_value_satoshis: u64, push_msat: u64, user_id: u64, config: &UserConfig, current_chain_height: u32,
902+
channel_value_satoshis: u64, push_msat: u64, user_id: u128, config: &UserConfig, current_chain_height: u32,
903903
outbound_scid_alias: u64
904904
) -> Result<Channel<Signer>, APIError>
905905
where K::Target: KeysInterface<Signer = Signer>,
@@ -1097,7 +1097,7 @@ impl<Signer: Sign> Channel<Signer> {
10971097
/// Assumes chain_hash has already been checked and corresponds with what we expect!
10981098
pub fn new_from_req<K: Deref, F: Deref, L: Deref>(
10991099
fee_estimator: &LowerBoundedFeeEstimator<F>, keys_provider: &K, counterparty_node_id: PublicKey, their_features: &InitFeatures,
1100-
msg: &msgs::OpenChannel, user_id: u64, config: &UserConfig, current_chain_height: u32, logger: &L,
1100+
msg: &msgs::OpenChannel, user_id: u128, config: &UserConfig, current_chain_height: u32, logger: &L,
11011101
outbound_scid_alias: u64
11021102
) -> Result<Channel<Signer>, ChannelError>
11031103
where K::Target: KeysInterface<Signer = Signer>,
@@ -4475,7 +4475,7 @@ impl<Signer: Sign> Channel<Signer> {
44754475

44764476
/// Gets the "user_id" value passed into the construction of this channel. It has no special
44774477
/// meaning and exists only to allow users to have a persistent identifier of a channel.
4478-
pub fn get_user_id(&self) -> u64 {
4478+
pub fn get_user_id(&self) -> u128 {
44794479
self.user_id
44804480
}
44814481

@@ -5156,7 +5156,7 @@ impl<Signer: Sign> Channel<Signer> {
51565156
/// should be sent back to the counterparty node.
51575157
///
51585158
/// [`msgs::AcceptChannel`]: crate::ln::msgs::AcceptChannel
5159-
pub fn accept_inbound_channel(&mut self, user_id: u64) -> msgs::AcceptChannel {
5159+
pub fn accept_inbound_channel(&mut self, user_id: u128) -> msgs::AcceptChannel {
51605160
if self.is_outbound() {
51615161
panic!("Tried to send accept_channel for an outbound channel?");
51625162
}
@@ -5985,7 +5985,13 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
59855985

59865986
write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
59875987

5988-
self.user_id.write(writer)?;
5988+
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
5989+
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. We write
5990+
// the low bytes now and the optional high bytes later.
5991+
let mut low_bytes = [0u8; 8];
5992+
low_bytes.copy_from_slice(&self.user_id.to_be_bytes()[8..16]);
5993+
let user_id_low = u64::from_be_bytes(low_bytes);
5994+
user_id_low.write(writer)?;
59895995

59905996
// Version 1 deserializers expected to read parts of the config object here. Version 2
59915997
// deserializers (0.0.99) now read config through TLVs, and as we now require them for
@@ -6230,6 +6236,13 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
62306236
if self.holder_max_htlc_value_in_flight_msat != Self::get_holder_max_htlc_value_in_flight_msat(self.channel_value_satoshis, &old_max_in_flight_percent_config)
62316237
{ Some(self.holder_max_htlc_value_in_flight_msat) } else { None };
62326238

6239+
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
6240+
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. Therefore,
6241+
// we write the high bytes as an option here.
6242+
let mut high_bytes = [0u8; 8];
6243+
high_bytes.copy_from_slice(&self.user_id.to_be_bytes()[0..8]);
6244+
let user_id_high_opt = Some(u64::from_be_bytes(high_bytes));
6245+
62336246
write_tlv_fields!(writer, {
62346247
(0, self.announcement_sigs, option),
62356248
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -6252,6 +6265,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
62526265
(17, self.announcement_sigs_state, required),
62536266
(19, self.latest_inbound_scid_alias, option),
62546267
(21, self.outbound_scid_alias, required),
6268+
(23, user_id_high_opt, option),
62556269
});
62566270

62576271
Ok(())
@@ -6265,7 +6279,10 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
62656279
let (keys_source, serialized_height) = args;
62666280
let ver = read_ver_prefix!(reader, SERIALIZATION_VERSION);
62676281

6268-
let user_id = Readable::read(reader)?;
6282+
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
6283+
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. We read
6284+
// the low bytes now and the high bytes later.
6285+
let user_id_low: u64 = Readable::read(reader)?;
62696286

62706287
let mut config = Some(LegacyChannelConfig::default());
62716288
if ver == 1 {
@@ -6510,6 +6527,8 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
65106527
let mut latest_inbound_scid_alias = None;
65116528
let mut outbound_scid_alias = None;
65126529

6530+
let mut user_id_high_opt: Option<u64> = None;
6531+
65136532
read_tlv_fields!(reader, {
65146533
(0, announcement_sigs, option),
65156534
(1, minimum_depth, option),
@@ -6526,6 +6545,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
65266545
(17, announcement_sigs_state, option),
65276546
(19, latest_inbound_scid_alias, option),
65286547
(21, outbound_scid_alias, option),
6548+
(23, user_id_high_opt, option),
65296549
});
65306550

65316551
if let Some(preimages) = preimages_opt {
@@ -6562,6 +6582,16 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
65626582
let mut secp_ctx = Secp256k1::new();
65636583
secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes());
65646584

6585+
// `user_id` used to be a single u64 value. In order to remain backwards
6586+
// compatible with versions prior to 0.0.113, the u128 is serialized as two
6587+
// separate u64 values.
6588+
let mut user_id_bytes = [0u8; 16];
6589+
user_id_bytes[8..16].copy_from_slice(&user_id_low.to_be_bytes());
6590+
if let Some(high_bytes) = user_id_high_opt {
6591+
user_id_bytes[0..8].copy_from_slice(&high_bytes.to_be_bytes());
6592+
}
6593+
let user_id = u128::from_be_bytes(user_id_bytes);
6594+
65656595
Ok(Channel {
65666596
user_id,
65676597

lightning/src/ln/channelmanager.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ type ShutdownResult = (Option<(OutPoint, ChannelMonitorUpdate)>, Vec<(HTLCSource
289289
290290
struct MsgHandleErrInternal {
291291
err: msgs::LightningError,
292-
chan_id: Option<([u8; 32], u64)>, // If Some a channel of ours has been closed
292+
chan_id: Option<([u8; 32], u128)>, // If Some a channel of ours has been closed
293293
shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
294294
}
295295
impl MsgHandleErrInternal {
@@ -325,7 +325,7 @@ impl MsgHandleErrInternal {
325325
Self { err, chan_id: None, shutdown_finish: None }
326326
}
327327
#[inline]
328-
fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u64, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
328+
fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u128, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
329329
Self {
330330
err: LightningError {
331331
err: err.clone(),
@@ -1076,7 +1076,7 @@ pub struct ChannelDetails {
10761076
/// [`outbound_capacity_msat`]: ChannelDetails::outbound_capacity_msat
10771077
pub unspendable_punishment_reserve: Option<u64>,
10781078
/// The `user_channel_id` passed in to create_channel, or 0 if the channel was inbound.
1079-
pub user_channel_id: u64,
1079+
pub user_channel_id: u128,
10801080
/// Our total balance. This is the amount we would get if we close the channel.
10811081
/// This value is not exact. Due to various in-flight changes and feerate changes, exactly this
10821082
/// amount is not likely to be recoverable on close.
@@ -1708,7 +1708,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
17081708
/// [`Event::FundingGenerationReady::user_channel_id`]: events::Event::FundingGenerationReady::user_channel_id
17091709
/// [`Event::FundingGenerationReady::temporary_channel_id`]: events::Event::FundingGenerationReady::temporary_channel_id
17101710
/// [`Event::ChannelClosed::channel_id`]: events::Event::ChannelClosed::channel_id
1711-
pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_channel_id: u64, override_config: Option<UserConfig>) -> Result<[u8; 32], APIError> {
1711+
pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, push_msat: u64, user_channel_id: u128, override_config: Option<UserConfig>) -> Result<[u8; 32], APIError> {
17121712
if channel_value_satoshis < 1000 {
17131713
return Err(APIError::APIMisuseError { err: format!("Channel value must be at least 1000 satoshis. It was {}", channel_value_satoshis) });
17141714
}
@@ -4382,7 +4382,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
43824382
///
43834383
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
43844384
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
4385-
pub fn accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, user_channel_id: u64) -> Result<(), APIError> {
4385+
pub fn accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, user_channel_id: u128) -> Result<(), APIError> {
43864386
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, false, user_channel_id)
43874387
}
43884388

@@ -4404,11 +4404,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
44044404
///
44054405
/// [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
44064406
/// [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
4407-
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> {
4407+
pub fn accept_inbound_channel_from_trusted_peer_0conf(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, user_channel_id: u128) -> Result<(), APIError> {
44084408
self.do_accept_inbound_channel(temporary_channel_id, counterparty_node_id, true, user_channel_id)
44094409
}
44104410

4411-
fn do_accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, accept_0conf: bool, user_channel_id: u64) -> Result<(), APIError> {
4411+
fn do_accept_inbound_channel(&self, temporary_channel_id: &[u8; 32], counterparty_node_id: &PublicKey, accept_0conf: bool, user_channel_id: u128) -> Result<(), APIError> {
44124412
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
44134413

44144414
let mut channel_state_lock = self.channel_state.lock().unwrap();
@@ -4456,9 +4456,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
44564456
return Err(MsgHandleErrInternal::send_err_msg_no_close("No inbound channels accepted".to_owned(), msg.temporary_channel_id.clone()));
44574457
}
44584458

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);
4459+
let mut random_bytes = [0u8; 16];
4460+
random_bytes.copy_from_slice(&self.keys_manager.get_secure_random_bytes()[..16]);
4461+
let user_channel_id = u128::from_be_bytes(random_bytes);
44624462

44634463
let outbound_scid_alias = self.create_and_insert_outbound_scid_alias();
44644464
let mut channel = match Channel::new_from_req(&self.fee_estimator, &self.keys_manager,

lightning/src/ln/functional_test_utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ macro_rules! check_added_monitors {
618618
}
619619
}
620620

621-
pub fn create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, expected_counterparty_node_id: &PublicKey, expected_chan_value: u64, expected_user_chan_id: u64) -> ([u8; 32], Transaction, OutPoint) {
621+
pub fn create_funding_transaction<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, expected_counterparty_node_id: &PublicKey, expected_chan_value: u64, expected_user_chan_id: u128) -> ([u8; 32], Transaction, OutPoint) {
622622
let chan_id = *node.network_chan_count.borrow();
623623

624624
let events = node.node.get_and_clear_pending_events();

lightning/src/util/events.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ pub enum Event {
317317
/// an inbound channel.
318318
///
319319
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
320-
user_channel_id: u64,
320+
user_channel_id: u128,
321321
},
322322
/// Indicates we've received (an offer of) money! Just gotta dig out that payment preimage and
323323
/// feed it to [`ChannelManager::claim_funds`] to get it....
@@ -607,7 +607,7 @@ pub enum Event {
607607
/// [`ChannelManager::create_channel`]: crate::ln::channelmanager::ChannelManager::create_channel
608608
/// [`ChannelManager::accept_inbound_channel`]: crate::ln::channelmanager::ChannelManager::accept_inbound_channel
609609
/// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
610-
user_channel_id: u64,
610+
user_channel_id: u128,
611611
/// The reason the channel was closed.
612612
reason: ClosureReason
613613
},
@@ -782,10 +782,20 @@ impl Writeable for Event {
782782
},
783783
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason } => {
784784
9u8.write(writer)?;
785+
// `user_channel_id` used to be a single u64 value. In order to remain backwards
786+
// compatible with versions prior to 0.0.113, the u128 is serialized as two
787+
// separate u64 values.
788+
let mut low_bytes = [0u8; 8];
789+
low_bytes.copy_from_slice(&user_channel_id.to_be_bytes()[8..16]);
790+
let user_channel_id_low = u64::from_be_bytes(low_bytes);
791+
let mut high_bytes = [0u8; 8];
792+
high_bytes.copy_from_slice(&user_channel_id.to_be_bytes()[0..8]);
793+
let user_channel_id_high = u64::from_be_bytes(high_bytes);
785794
write_tlv_fields!(writer, {
786795
(0, channel_id, required),
787-
(1, user_channel_id, required),
788-
(2, reason, required)
796+
(1, user_channel_id_low, required),
797+
(2, reason, required),
798+
(3, user_channel_id_high, required),
789799
});
790800
},
791801
&Event::DiscardFunding { ref channel_id, ref transaction } => {
@@ -995,14 +1005,29 @@ impl MaybeReadable for Event {
9951005
let f = || {
9961006
let mut channel_id = [0; 32];
9971007
let mut reason = None;
998-
let mut user_channel_id_opt = None;
1008+
let mut user_channel_id_low_opt: Option<u64> = None;
1009+
let mut user_channel_id_high_opt: Option<u64> = None;
9991010
read_tlv_fields!(reader, {
10001011
(0, channel_id, required),
1001-
(1, user_channel_id_opt, option),
1012+
(1, user_channel_id_low_opt, option),
10021013
(2, reason, ignorable),
1014+
(3, user_channel_id_high_opt, option),
10031015
});
10041016
if reason.is_none() { return Ok(None); }
1005-
let user_channel_id = if let Some(id) = user_channel_id_opt { id } else { 0 };
1017+
1018+
1019+
// `user_channel_id` used to be a single u64 value. In order to remain
1020+
// backwards compatible with versions prior to 0.0.113, the u128 is serialized
1021+
// as two separate u64 values.
1022+
let user_channel_id = if let Some(user_channel_id_low) = user_channel_id_low_opt {
1023+
let mut user_channel_id_bytes = [0u8; 16];
1024+
if let Some(user_channel_id_high) = user_channel_id_high_opt {
1025+
user_channel_id_bytes[0..8].copy_from_slice(&user_channel_id_high.to_be_bytes());
1026+
}
1027+
1028+
user_channel_id_bytes[8..16].copy_from_slice(&user_channel_id_low.to_be_bytes());
1029+
u128::from_be_bytes(user_channel_id_bytes)
1030+
} else { 0u128 };
10061031
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: reason.unwrap() }))
10071032
};
10081033
f()

lightning/src/util/ser.rs

+1
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ macro_rules! impl_writeable_primitive {
463463
}
464464
}
465465

466+
impl_writeable_primitive!(u128, 16);
466467
impl_writeable_primitive!(u64, 8);
467468
impl_writeable_primitive!(u32, 4);
468469
impl_writeable_primitive!(u16, 2);

0 commit comments

Comments
 (0)