Skip to content

Commit e9c1787

Browse files
Rename HighZeroBytesDroppedVarInt to HighZeroBytesDroppedBigSize
As observed by @wpaulino, this struct encodes its bytes as big-endian, therefore it's a BigSize, not a VarInt.
1 parent e6cac08 commit e9c1787

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

lightning/src/ln/msgs.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use io_extras::read_to_end;
4242

4343
use util::events::MessageSendEventsProvider;
4444
use util::logger;
45-
use util::ser::{BigSize, LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedVarInt, Hostname};
45+
use util::ser::{BigSize, LengthReadable, Readable, ReadableArgs, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedBigSize, Hostname};
4646

4747
use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
4848

@@ -1375,14 +1375,14 @@ impl Writeable for OnionMessage {
13751375
impl Writeable for FinalOnionHopData {
13761376
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
13771377
self.payment_secret.0.write(w)?;
1378-
HighZeroBytesDroppedVarInt(self.total_msat).write(w)
1378+
HighZeroBytesDroppedBigSize(self.total_msat).write(w)
13791379
}
13801380
}
13811381

13821382
impl Readable for FinalOnionHopData {
13831383
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
13841384
let secret: [u8; 32] = Readable::read(r)?;
1385-
let amt: HighZeroBytesDroppedVarInt<u64> = Readable::read(r)?;
1385+
let amt: HighZeroBytesDroppedBigSize<u64> = Readable::read(r)?;
13861386
Ok(Self { payment_secret: PaymentSecret(secret), total_msat: amt.0 })
13871387
}
13881388
}
@@ -1399,15 +1399,15 @@ impl Writeable for OnionHopData {
13991399
},
14001400
OnionHopDataFormat::NonFinalNode { short_channel_id } => {
14011401
encode_varint_length_prefixed_tlv!(w, {
1402-
(2, HighZeroBytesDroppedVarInt(self.amt_to_forward), required),
1403-
(4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value), required),
1402+
(2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
1403+
(4, HighZeroBytesDroppedBigSize(self.outgoing_cltv_value), required),
14041404
(6, short_channel_id, required)
14051405
});
14061406
},
14071407
OnionHopDataFormat::FinalNode { ref payment_data, ref keysend_preimage } => {
14081408
encode_varint_length_prefixed_tlv!(w, {
1409-
(2, HighZeroBytesDroppedVarInt(self.amt_to_forward), required),
1410-
(4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value), required),
1409+
(2, HighZeroBytesDroppedBigSize(self.amt_to_forward), required),
1410+
(4, HighZeroBytesDroppedBigSize(self.outgoing_cltv_value), required),
14111411
(8, payment_data, option),
14121412
(5482373484, keysend_preimage, option)
14131413
});
@@ -1423,8 +1423,8 @@ impl Readable for OnionHopData {
14231423
const LEGACY_ONION_HOP_FLAG: u64 = 0;
14241424
let (format, amt, cltv_value) = if b.0 != LEGACY_ONION_HOP_FLAG {
14251425
let mut rd = FixedLengthReader::new(r, b.0);
1426-
let mut amt = HighZeroBytesDroppedVarInt(0u64);
1427-
let mut cltv_value = HighZeroBytesDroppedVarInt(0u32);
1426+
let mut amt = HighZeroBytesDroppedBigSize(0u64);
1427+
let mut cltv_value = HighZeroBytesDroppedBigSize(0u32);
14281428
let mut short_id: Option<u64> = None;
14291429
let mut payment_data: Option<FinalOnionHopData> = None;
14301430
let mut keysend_preimage: Option<PaymentPreimage> = None;
@@ -2835,7 +2835,7 @@ mod tests {
28352835
}
28362836
// see above test, needs to be a separate method for use of the serialization macros.
28372837
fn encode_big_payload() -> Result<Vec<u8>, std::io::Error> {
2838-
use util::ser::HighZeroBytesDroppedVarInt;
2838+
use util::ser::HighZeroBytesDroppedBigSize;
28392839
let payload = msgs::OnionHopData {
28402840
format: OnionHopDataFormat::NonFinalNode {
28412841
short_channel_id: 0xdeadbeef1bad1dea,
@@ -2848,8 +2848,8 @@ mod tests {
28482848
if let OnionHopDataFormat::NonFinalNode { short_channel_id } = payload.format {
28492849
encode_varint_length_prefixed_tlv!(&mut encoded_payload, {
28502850
(1, test_bytes, vec_type),
2851-
(2, HighZeroBytesDroppedVarInt(payload.amt_to_forward), required),
2852-
(4, HighZeroBytesDroppedVarInt(payload.outgoing_cltv_value), required),
2851+
(2, HighZeroBytesDroppedBigSize(payload.amt_to_forward), required),
2852+
(4, HighZeroBytesDroppedBigSize(payload.outgoing_cltv_value), required),
28532853
(6, short_channel_id, required)
28542854
});
28552855
}

lightning/src/util/ser.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ impl Readable for BigSize {
400400
/// variable-length integer which is simply truncated by skipping high zero bytes. This type
401401
/// encapsulates such integers implementing Readable/Writeable for them.
402402
#[cfg_attr(test, derive(PartialEq, Debug))]
403-
pub(crate) struct HighZeroBytesDroppedVarInt<T>(pub T);
403+
pub(crate) struct HighZeroBytesDroppedBigSize<T>(pub T);
404404

405405
macro_rules! impl_writeable_primitive {
406406
($val_type:ty, $len: expr) => {
@@ -410,7 +410,7 @@ macro_rules! impl_writeable_primitive {
410410
writer.write_all(&self.to_be_bytes())
411411
}
412412
}
413-
impl Writeable for HighZeroBytesDroppedVarInt<$val_type> {
413+
impl Writeable for HighZeroBytesDroppedBigSize<$val_type> {
414414
#[inline]
415415
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
416416
// Skip any full leading 0 bytes when writing (in BE):
@@ -425,9 +425,9 @@ macro_rules! impl_writeable_primitive {
425425
Ok(<$val_type>::from_be_bytes(buf))
426426
}
427427
}
428-
impl Readable for HighZeroBytesDroppedVarInt<$val_type> {
428+
impl Readable for HighZeroBytesDroppedBigSize<$val_type> {
429429
#[inline]
430-
fn read<R: Read>(reader: &mut R) -> Result<HighZeroBytesDroppedVarInt<$val_type>, DecodeError> {
430+
fn read<R: Read>(reader: &mut R) -> Result<HighZeroBytesDroppedBigSize<$val_type>, DecodeError> {
431431
// We need to accept short reads (read_len == 0) as "EOF" and handle them as simply
432432
// the high bytes being dropped. To do so, we start reading into the middle of buf
433433
// and then convert the appropriate number of bytes with extra high bytes out of
@@ -443,7 +443,7 @@ macro_rules! impl_writeable_primitive {
443443
let first_byte = $len - ($len - total_read_len);
444444
let mut bytes = [0; $len];
445445
bytes.copy_from_slice(&buf[first_byte..first_byte + $len]);
446-
Ok(HighZeroBytesDroppedVarInt(<$val_type>::from_be_bytes(bytes)))
446+
Ok(HighZeroBytesDroppedBigSize(<$val_type>::from_be_bytes(bytes)))
447447
} else {
448448
// If the encoding had extra zero bytes, return a failure even though we know
449449
// what they meant (as the TLV test vectors require this)

lightning/src/util/ser_macros.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ mod tests {
563563
use io::{self, Cursor};
564564
use prelude::*;
565565
use ln::msgs::DecodeError;
566-
use util::ser::{Writeable, HighZeroBytesDroppedVarInt, VecWriter};
566+
use util::ser::{Writeable, HighZeroBytesDroppedBigSize, VecWriter};
567567
use bitcoin::secp256k1::PublicKey;
568568

569569
// The BOLT TLV test cases don't include any tests which use our "required-value" logic since
@@ -632,9 +632,9 @@ mod tests {
632632
}
633633

634634
// BOLT TLV test cases
635-
fn tlv_reader_n1(s: &[u8]) -> Result<(Option<HighZeroBytesDroppedVarInt<u64>>, Option<u64>, Option<(PublicKey, u64, u64)>, Option<u16>), DecodeError> {
635+
fn tlv_reader_n1(s: &[u8]) -> Result<(Option<HighZeroBytesDroppedBigSize<u64>>, Option<u64>, Option<(PublicKey, u64, u64)>, Option<u16>), DecodeError> {
636636
let mut s = Cursor::new(s);
637-
let mut tlv1: Option<HighZeroBytesDroppedVarInt<u64>> = None;
637+
let mut tlv1: Option<HighZeroBytesDroppedBigSize<u64>> = None;
638638
let mut tlv2: Option<u64> = None;
639639
let mut tlv3: Option<(PublicKey, u64, u64)> = None;
640640
let mut tlv4: Option<u16> = None;
@@ -765,11 +765,11 @@ mod tests {
765765
assert_eq!(stream.0, ::hex::decode("06fd00ff02abcd").unwrap());
766766

767767
stream.0.clear();
768-
encode_varint_length_prefixed_tlv!(&mut stream, {(0, 1u64, required), (42, None::<u64>, option), (0xff, HighZeroBytesDroppedVarInt(0u64), required)});
768+
encode_varint_length_prefixed_tlv!(&mut stream, {(0, 1u64, required), (42, None::<u64>, option), (0xff, HighZeroBytesDroppedBigSize(0u64), required)});
769769
assert_eq!(stream.0, ::hex::decode("0e00080000000000000001fd00ff00").unwrap());
770770

771771
stream.0.clear();
772-
encode_varint_length_prefixed_tlv!(&mut stream, {(0, Some(1u64), option), (0xff, HighZeroBytesDroppedVarInt(0u64), required)});
772+
encode_varint_length_prefixed_tlv!(&mut stream, {(0, Some(1u64), option), (0xff, HighZeroBytesDroppedBigSize(0u64), required)});
773773
assert_eq!(stream.0, ::hex::decode("0e00080000000000000001fd00ff00").unwrap());
774774

775775
Ok(())

0 commit comments

Comments
 (0)