Skip to content

Commit d06e17b

Browse files
committed
f The K stands for complicated
1 parent 04f6c8a commit d06e17b

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
@@ -6005,9 +6005,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
60056005
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
60066006
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. We write
60076007
// the low bytes now and the optional high bytes later.
6008-
let mut low_bytes = [0u8; 8];
6009-
low_bytes.copy_from_slice(&self.user_id.to_be_bytes()[8..16]);
6010-
let user_id_low = u64::from_be_bytes(low_bytes);
6008+
let user_id_low = self.user_id as u64;
60116009
user_id_low.write(writer)?;
60126010

60136011
// Version 1 deserializers expected to read parts of the config object here. Version 2
@@ -6258,9 +6256,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
62586256
// `user_id` used to be a single u64 value. In order to remain backwards compatible with
62596257
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values. Therefore,
62606258
// we write the high bytes as an option here.
6261-
let mut high_bytes = [0u8; 8];
6262-
high_bytes.copy_from_slice(&self.user_id.to_be_bytes()[0..8]);
6263-
let user_id_high_opt = Some(u64::from_be_bytes(high_bytes));
6259+
let user_id_high_opt = Some((self.user_id >> 64) as u64);
62646260

62656261
write_tlv_fields!(writer, {
62666262
(0, self.announcement_sigs, option),
@@ -6607,12 +6603,11 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
66076603
// `user_id` used to be a single u64 value. In order to remain backwards
66086604
// compatible with versions prior to 0.0.113, the u128 is serialized as two
66096605
// separate u64 values.
6610-
let mut user_id_bytes = [0u8; 16];
6611-
user_id_bytes[8..16].copy_from_slice(&user_id_low.to_be_bytes());
6612-
if let Some(high_bytes) = user_id_high_opt {
6613-
user_id_bytes[0..8].copy_from_slice(&high_bytes.to_be_bytes());
6614-
}
6615-
let user_id = u128::from_be_bytes(user_id_bytes);
6606+
let user_id = if let Some(user_id_high) = user_id_high_opt {
6607+
user_id_low as u128 + ((user_id_high as u128) << 64)
6608+
} else {
6609+
user_id_low as u128
6610+
};
66166611

66176612
Ok(Channel {
66186613
user_id,

lightning/src/util/events.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -816,12 +816,8 @@ impl Writeable for Event {
816816
// `user_channel_id` used to be a single u64 value. In order to remain backwards
817817
// compatible with versions prior to 0.0.113, the u128 is serialized as two
818818
// separate u64 values.
819-
let mut low_bytes = [0u8; 8];
820-
low_bytes.copy_from_slice(&user_channel_id.to_be_bytes()[8..16]);
821-
let user_channel_id_low = u64::from_be_bytes(low_bytes);
822-
let mut high_bytes = [0u8; 8];
823-
high_bytes.copy_from_slice(&user_channel_id.to_be_bytes()[0..8]);
824-
let user_channel_id_high = u64::from_be_bytes(high_bytes);
819+
let user_channel_id_low = *user_channel_id as u64;
820+
let user_channel_id_high = (*user_channel_id >> 64) as u64;
825821
write_tlv_fields!(writer, {
826822
(0, channel_id, required),
827823
(1, user_channel_id_low, required),
@@ -1059,13 +1055,11 @@ impl MaybeReadable for Event {
10591055
// backwards compatible with versions prior to 0.0.113, the u128 is serialized
10601056
// as two separate u64 values.
10611057
let user_channel_id = if let Some(user_channel_id_low) = user_channel_id_low_opt {
1062-
let mut user_channel_id_bytes = [0u8; 16];
10631058
if let Some(user_channel_id_high) = user_channel_id_high_opt {
1064-
user_channel_id_bytes[0..8].copy_from_slice(&user_channel_id_high.to_be_bytes());
1059+
user_channel_id_low as u128 + ((user_channel_id_high as u128) << 64)
1060+
} else {
1061+
user_channel_id_low as u128
10651062
}
1066-
1067-
user_channel_id_bytes[8..16].copy_from_slice(&user_channel_id_low.to_be_bytes());
1068-
u128::from_be_bytes(user_channel_id_bytes)
10691063
} else { 0u128 };
10701064
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: reason.unwrap() }))
10711065
};

0 commit comments

Comments
 (0)