Skip to content

Commit 8b1c771

Browse files
committed
as_ref() before wrapping encoded types in writing option TLVs
We almost certainly don't want to be moving `option` TLVs during serialization, and while we had logic elsewhere to work around this previously its nice not to have to in the future.
1 parent 42e198c commit 8b1c771

4 files changed

Lines changed: 33 additions & 5 deletions

File tree

lightning/src/ln/features.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ macro_rules! impl_feature_write_without_length {
8181
}
8282
}
8383

84+
impl Writeable for WithoutLength<&&$features> {
85+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
86+
write_be(w, self.0.le_flags())
87+
}
88+
}
89+
8490
impl Readable for WithoutLength<$features> {
8591
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
8692
let v = io_extras::read_to_end(r)?;

lightning/src/ln/msgs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -763,10 +763,10 @@ pub struct UpdateAddHTLC {
763763

764764
struct AccountableBool<T>(T);
765765

766-
impl Writeable for AccountableBool<bool> {
766+
impl Writeable for AccountableBool<&bool> {
767767
#[inline]
768768
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
769-
let wire_value = if self.0 { 7u8 } else { 0u8 };
769+
let wire_value = if *self.0 { 7u8 } else { 0u8 };
770770
writer.write_all(&[wire_value])
771771
}
772772
}

lightning/src/util/ser.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,13 @@ macro_rules! impl_writeable_primitive {
610610
writer.write_all(&self.0.to_be_bytes()[(self.0.leading_zeros() / 8) as usize..$len])
611611
}
612612
}
613+
impl Writeable for HighZeroBytesDroppedBigSize<&$val_type> {
614+
#[inline]
615+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
616+
// Skip any full leading 0 bytes when writing (in BE):
617+
writer.write_all(&self.0.to_be_bytes()[(self.0.leading_zeros() / 8) as usize..$len])
618+
}
619+
}
613620
impl Readable for $val_type {
614621
#[inline]
615622
fn read<R: Read>(reader: &mut R) -> Result<$val_type, DecodeError> {
@@ -751,12 +758,20 @@ impl_array!(HMAC_LEN * HMAC_COUNT, u8);
751758
/// This is not exported to bindings users as manual TLV building is not currently supported in bindings
752759
pub struct WithoutLength<T>(pub T);
753760

761+
impl Writeable for WithoutLength<&&String> {
762+
#[inline]
763+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
764+
w.write_all(self.0.as_bytes())
765+
}
766+
}
767+
754768
impl Writeable for WithoutLength<&String> {
755769
#[inline]
756770
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
757771
w.write_all(self.0.as_bytes())
758772
}
759773
}
774+
760775
impl LengthReadable for WithoutLength<String> {
761776
#[inline]
762777
fn read_from_fixed_length_buffer<R: LengthLimitedRead>(r: &mut R) -> Result<Self, DecodeError> {
@@ -808,6 +823,14 @@ impl<T: Writeable> AsWriteableSlice for &Vec<T> {
808823
&self
809824
}
810825
}
826+
827+
impl<T: Writeable> AsWriteableSlice for &&Vec<T> {
828+
type Inner = T;
829+
fn as_slice(&self) -> &[T] {
830+
&self
831+
}
832+
}
833+
811834
impl<T: Writeable> AsWriteableSlice for &[T] {
812835
type Inner = T;
813836
fn as_slice(&self) -> &[T] {

lightning/src/util/ser_macros.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ macro_rules! _encode_tlv {
8181
$crate::_encode_tlv!($stream, $type, $field, option);
8282
};
8383
($stream: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident) $(, $self: ident)?)) => {
84-
$crate::_encode_tlv!($stream, $type, $field.map(|f| $encoding(f)), option);
84+
$crate::_encode_tlv!($stream, $type, $field.as_ref().map(|f| $encoding(f)), option);
8585
};
8686
($stream: expr, $type: expr, $field: expr, (option, encoding: $fieldty: ty) $(, $self: ident)?) => {
8787
$crate::_encode_tlv!($stream, $type, $field, option);
@@ -253,8 +253,7 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
253253
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
254254
};
255255
($len: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident)) $(, $self: ident)?) => {
256-
let field = $field.map(|f| $encoding(f));
257-
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, field, option);
256+
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field.as_ref().map(|f| $encoding(f)), option);
258257
};
259258
($len: expr, $type: expr, $field: expr, upgradable_required $(, $self: ident)?) => {
260259
$crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);

0 commit comments

Comments
 (0)