Skip to content

Commit 80699d9

Browse files
committed
f The K stands for complicated
1 parent d26e4b5 commit 80699d9

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

lightning/src/ln/channel.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -5988,9 +5988,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
59885988
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
59895989
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. We write
59905990
// 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);
5991+
let user_id_low = self.user_id as u64;
59945992
user_id_low.write(writer)?;
59955993

59965994
// Version 1 deserializers expected to read parts of the config object here. Version 2
@@ -6239,9 +6237,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
62396237
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
62406238
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. Therefore,
62416239
// 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));
6240+
let user_id_high_opt = Some((self.user_id >> 64) as u64);
62456241

62466242
write_tlv_fields!(writer, {
62476243
(0, self.announcement_sigs, option),
@@ -6585,12 +6581,11 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
65856581
// `user_id` used to be a single u64 value. In order to remain backwards
65866582
// compatible with versions prior to 0.0.113, the u128 is serialized as two
65876583
// 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);
6584+
let user_id = if let Some(user_id_high) = user_id_high_opt {
6585+
user_id_low as u128 + ((user_id_high as u128) << 64)
6586+
} else {
6587+
user_id_low as u128
6588+
};
65946589

65956590
Ok(Channel {
65966591
user_id,

lightning/src/util/events.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -787,12 +787,8 @@ impl Writeable for Event {
787787
// `user_channel_id` used to be a single u64 value. In order to remain backwards
788788
// compatible with versions prior to 0.0.113, the u128 is serialized as two
789789
// separate u64 values.
790-
let mut low_bytes = [0u8; 8];
791-
low_bytes.copy_from_slice(&user_channel_id.to_be_bytes()[8..16]);
792-
let user_channel_id_low = u64::from_be_bytes(low_bytes);
793-
let mut high_bytes = [0u8; 8];
794-
high_bytes.copy_from_slice(&user_channel_id.to_be_bytes()[0..8]);
795-
let user_channel_id_high = u64::from_be_bytes(high_bytes);
790+
let user_channel_id_low = *user_channel_id as u64;
791+
let user_channel_id_high = (*user_channel_id >> 64) as u64;
796792
write_tlv_fields!(writer, {
797793
(0, channel_id, required),
798794
(1, user_channel_id_low, required),
@@ -1021,13 +1017,11 @@ impl MaybeReadable for Event {
10211017
// backwards compatible with versions prior to 0.0.113, the u128 is serialized
10221018
// as two separate u64 values.
10231019
let user_channel_id = if let Some(user_channel_id_low) = user_channel_id_low_opt {
1024-
let mut user_channel_id_bytes = [0u8; 16];
10251020
if let Some(user_channel_id_high) = user_channel_id_high_opt {
1026-
user_channel_id_bytes[0..8].copy_from_slice(&user_channel_id_high.to_be_bytes());
1021+
user_channel_id_low as u128 + ((user_channel_id_high as u128) << 64)
1022+
} else {
1023+
user_channel_id_low as u128
10271024
}
1028-
1029-
user_channel_id_bytes[8..16].copy_from_slice(&user_channel_id_low.to_be_bytes());
1030-
u128::from_be_bytes(user_channel_id_bytes)
10311025
} else { 0u128 };
10321026
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: reason.unwrap() }))
10331027
};

0 commit comments

Comments
 (0)