Skip to content

Commit 1f5b1ab

Browse files
committed
Optimize checking of onion route size
1 parent 33ee39e commit 1f5b1ab

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

lightning/src/ln/msgs.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,34 @@ mod fuzzy_internal_msgs {
887887
// 12 bytes of 0-padding for Legacy format
888888
}
889889

890+
impl OnionHopData {
891+
pub fn encoding_length(&self) -> usize {
892+
fn high_zero_bytes_dropped_length(data: u64) -> usize {
893+
8 - ((data.leading_zeros()/8) as usize)
894+
}
895+
match self.format {
896+
OnionHopDataFormat::Legacy { .. } => 1 + 8 + 8 + 4 + 12, // 33
897+
OnionHopDataFormat::NonFinalNode { .. } => {
898+
1 +
899+
1 + 1 + high_zero_bytes_dropped_length(self.amt_to_forward) +
900+
1 + 1 + high_zero_bytes_dropped_length(self.outgoing_cltv_value as u64) +
901+
1 + 1 + 8 // short_channel_id
902+
}
903+
OnionHopDataFormat::FinalNode { ref payment_data } => {
904+
1 +
905+
1 + 1 + high_zero_bytes_dropped_length(self.amt_to_forward) +
906+
1 + 1 + high_zero_bytes_dropped_length(self.outgoing_cltv_value as u64) +
907+
match payment_data {
908+
None => 0,
909+
Some(payment_data) =>
910+
1 + 1 + high_zero_bytes_dropped_length(payment_data.total_msat) +
911+
32 // payment_secret
912+
}
913+
}
914+
}
915+
}
916+
}
917+
890918
pub struct DecodedOnionErrorPacket {
891919
pub(crate) hmac: [u8; 32],
892920
pub(crate) failuremsg: Vec<u8>,
@@ -2477,6 +2505,7 @@ mod tests {
24772505
let encoded_value = msg.encode();
24782506
let target_value = hex::decode("00deadbeef1bad1dea0badf00d01020304ffffffff000000000000000000000000").unwrap();
24792507
assert_eq!(encoded_value, target_value);
2508+
assert_eq!(encoded_value.len(), msg.encoding_length())
24802509
}
24812510

24822511
#[test]
@@ -2497,6 +2526,7 @@ mod tests {
24972526
} else { panic!(); }
24982527
assert_eq!(msg.amt_to_forward, 0x0badf00d01020304);
24992528
assert_eq!(msg.outgoing_cltv_value, 0xffffffff);
2529+
assert_eq!(encoded_value.len(), msg.encoding_length())
25002530
}
25012531

25022532
#[test]
@@ -2515,6 +2545,7 @@ mod tests {
25152545
if let OnionHopDataFormat::FinalNode { payment_data: None } = msg.format { } else { panic!(); }
25162546
assert_eq!(msg.amt_to_forward, 0x0badf00d01020304);
25172547
assert_eq!(msg.outgoing_cltv_value, 0xffffffff);
2548+
assert_eq!(encoded_value.len(), msg.encoding_length())
25182549
}
25192550

25202551
#[test]
@@ -2544,6 +2575,7 @@ mod tests {
25442575
} else { panic!(); }
25452576
assert_eq!(msg.amt_to_forward, 0x0badf00d01020304);
25462577
assert_eq!(msg.outgoing_cltv_value, 0xffffffff);
2578+
assert_eq!(encoded_value.len(), msg.encoding_length())
25472579
}
25482580

25492581
#[test]

lightning/src/ln/onion_utils.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,9 @@ fn shift_arr_right(arr: &mut [u8; ONION_DATA_LEN], amt: usize) {
183183
pub(super) fn route_size_insane(payloads: &Vec<msgs::OnionHopData>) -> bool {
184184
let mut len = 0;
185185
for payload in payloads.iter() {
186-
let mut payload_len = Writer::new();
187-
payload.write(&mut payload_len).expect("Failed to calculate length");
188-
assert!(payload_len.len() + 32 < ONION_DATA_LEN);
189-
len += payload_len.len() + 32;
186+
let encoding_length = payload.encoding_length();
187+
assert!(encoding_length + 32 < ONION_DATA_LEN);
188+
len += encoding_length + 32;
190189
if len > ONION_DATA_LEN {
191190
return true;
192191
}

0 commit comments

Comments
 (0)