X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fser_macros.rs;h=db7553d5ff8e3dea88b206bc462011e1c8d96f22;hb=94a07d9caee6d38d42954ac783c49afe1cf89697;hp=5d5171adbb4401c1dd1fb5faf3b1f8afab919db2;hpb=853007800ebb433f53b5b10e1a941cc65c04e829;p=rust-lightning diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 5d5171ad..db7553d5 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -8,13 +8,16 @@ // licenses. macro_rules! encode_tlv { + ($stream: expr, $type: expr, $field: expr, (default_value, $default: expr)) => { + encode_tlv!($stream, $type, $field, required) + }; ($stream: expr, $type: expr, $field: expr, required) => { BigSize($type).write($stream)?; BigSize($field.serialized_length() as u64).write($stream)?; $field.write($stream)?; }; ($stream: expr, $type: expr, $field: expr, vec_type) => { - encode_tlv!($stream, $type, ::util::ser::VecWriteWrapper(&$field), required); + encode_tlv!($stream, $type, $crate::util::ser::WithoutLength(&$field), required); }; ($stream: expr, $optional_type: expr, $optional_field: expr, option) => { if let Some(ref field) = $optional_field { @@ -23,12 +26,18 @@ macro_rules! encode_tlv { field.write($stream)?; } }; + ($stream: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => { + encode_tlv!($stream, $type, $field.map(|f| $encoding(f)), option); + }; + ($stream: expr, $type: expr, $field: expr, (option, encoding: $fieldty: ty)) => { + encode_tlv!($stream, $type, $field, option); + }; } macro_rules! encode_tlv_stream { - ($stream: expr, {$(($type: expr, $field: expr, $fieldty: ident)),*}) => { { + ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { { #[allow(unused_imports)] - use { + use $crate::{ ln::msgs::DecodeError, util::ser, util::ser::BigSize, @@ -53,6 +62,9 @@ macro_rules! encode_tlv_stream { } macro_rules! get_varint_length_prefixed_tlv_length { + ($len: expr, $type: expr, $field: expr, (default_value, $default: expr)) => { + get_varint_length_prefixed_tlv_length!($len, $type, $field, required) + }; ($len: expr, $type: expr, $field: expr, required) => { BigSize($type).write(&mut $len).expect("No in-memory data may fail to serialize"); let field_len = $field.serialized_length(); @@ -60,7 +72,7 @@ macro_rules! get_varint_length_prefixed_tlv_length { $len.0 += field_len; }; ($len: expr, $type: expr, $field: expr, vec_type) => { - get_varint_length_prefixed_tlv_length!($len, $type, ::util::ser::VecWriteWrapper(&$field), required); + get_varint_length_prefixed_tlv_length!($len, $type, $crate::util::ser::WithoutLength(&$field), required); }; ($len: expr, $optional_type: expr, $optional_field: expr, option) => { if let Some(ref field) = $optional_field { @@ -73,11 +85,11 @@ macro_rules! get_varint_length_prefixed_tlv_length { } macro_rules! encode_varint_length_prefixed_tlv { - ($stream: expr, {$(($type: expr, $field: expr, $fieldty: ident)),*}) => { { - use util::ser::BigSize; + ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}) => { { + use $crate::util::ser::BigSize; let len = { #[allow(unused_mut)] - let mut len = ::util::ser::LengthCalculatingWriter(0); + let mut len = $crate::util::ser::LengthCalculatingWriter(0); $( get_varint_length_prefixed_tlv_length!(len, $type, $field, $fieldty); )* @@ -89,55 +101,123 @@ macro_rules! encode_varint_length_prefixed_tlv { } macro_rules! check_tlv_order { - ($last_seen_type: expr, $typ: expr, $type: expr, required) => {{ + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{ + #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true + let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type; + if invalid_order { + $field = $default.into(); + } + }}; + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required) => {{ #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type; if invalid_order { return Err(DecodeError::InvalidValue); } }}; - ($last_seen_type: expr, $typ: expr, $type: expr, option) => {{ + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{ + // no-op + }}; + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, vec_type) => {{ + // no-op + }}; + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ignorable) => {{ + // no-op + }}; + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{ // no-op }}; - ($last_seen_type: expr, $typ: expr, $type: expr, vec_type) => {{ + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (option, encoding: $encoding: tt)) => {{ // no-op }}; } macro_rules! check_missing_tlv { - ($last_seen_type: expr, $type: expr, required) => {{ + ($last_seen_type: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{ + #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true + let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type; + if missing_req_type { + $field = $default.into(); + } + }}; + ($last_seen_type: expr, $type: expr, $field: ident, required) => {{ #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type; if missing_req_type { return Err(DecodeError::InvalidValue); } }}; - ($last_seen_type: expr, $type: expr, vec_type) => {{ + ($last_seen_type: expr, $type: expr, $field: ident, vec_type) => {{ // no-op }}; - ($last_seen_type: expr, $type: expr, option) => {{ + ($last_seen_type: expr, $type: expr, $field: ident, option) => {{ + // no-op + }}; + ($last_seen_type: expr, $type: expr, $field: ident, ignorable) => {{ + // no-op + }}; + ($last_seen_type: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{ + // no-op + }}; + ($last_seen_type: expr, $type: expr, $field: ident, (option, encoding: $encoding: tt)) => {{ // no-op }}; } macro_rules! decode_tlv { + ($reader: expr, $field: ident, (default_value, $default: expr)) => {{ + decode_tlv!($reader, $field, required) + }}; ($reader: expr, $field: ident, required) => {{ - $field = ser::Readable::read(&mut $reader)?; + $field = $crate::util::ser::Readable::read(&mut $reader)?; }}; ($reader: expr, $field: ident, vec_type) => {{ - $field = Some(ser::Readable::read(&mut $reader)?); + let f: $crate::util::ser::WithoutLength> = $crate::util::ser::Readable::read(&mut $reader)?; + $field = Some(f.0); }}; ($reader: expr, $field: ident, option) => {{ - $field = Some(ser::Readable::read(&mut $reader)?); + $field = Some($crate::util::ser::Readable::read(&mut $reader)?); + }}; + ($reader: expr, $field: ident, ignorable) => {{ + $field = $crate::util::ser::MaybeReadable::read(&mut $reader)?; + }}; + ($reader: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{ + $field = Some($trait::read(&mut $reader $(, $read_arg)*)?); + }}; + ($reader: expr, $field: ident, (option, encoding: ($fieldty: ty, $encoding: ident))) => {{ + $field = { + let field: $encoding<$fieldty> = ser::Readable::read(&mut $reader)?; + Some(field.0) + }; + }}; + ($reader: expr, $field: ident, (option, encoding: $fieldty: ty)) => {{ + decode_tlv!($reader, $field, option); }}; } +// `$decode_custom_tlv` is a closure that may be optionally provided to handle custom message types. +// If it is provided, it will be called with the custom type and the `FixedLengthReader` containing +// the message contents. It should return `Ok(true)` if the custom message is successfully parsed, +// `Ok(false)` if the message type is unknown, and `Err(DecodeError)` if parsing fails. macro_rules! decode_tlv_stream { - ($stream: expr, {$(($type: expr, $field: ident, $fieldty: ident)),* $(,)*}) => { { - use ln::msgs::DecodeError; + ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} + $(, $decode_custom_tlv: expr)?) => { { + let rewind = |_, _| { unreachable!() }; + use core::ops::RangeBounds; + decode_tlv_stream_range!( + $stream, .., rewind, {$(($type, $field, $fieldty)),*} $(, $decode_custom_tlv)? + ); + } } +} + +macro_rules! decode_tlv_stream_range { + ($stream: expr, $range: expr, $rewind: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} + $(, $decode_custom_tlv: expr)?) => { { + use $crate::ln::msgs::DecodeError; let mut last_seen_type: Option = None; + let mut stream_ref = $stream; 'tlv_read: loop { - use util::ser; + use $crate::util::ser; // First decode the type of this TLV: let typ: ser::BigSize = { @@ -145,8 +225,8 @@ macro_rules! decode_tlv_stream { // determine whether we should break or return ShortRead if we get an // UnexpectedEof. This should in every case be largely cosmetic, but its nice to // pass the TLV test vectors exactly, which requre this distinction. - let mut tracking_reader = ser::ReadTrackingReader::new($stream); - match ser::Readable::read(&mut tracking_reader) { + let mut tracking_reader = ser::ReadTrackingReader::new(&mut stream_ref); + match <$crate::util::ser::BigSize as $crate::util::ser::Readable>::read(&mut tracking_reader) { Err(DecodeError::ShortRead) => { if !tracking_reader.have_read { break 'tlv_read; @@ -155,7 +235,15 @@ macro_rules! decode_tlv_stream { } }, Err(e) => return Err(e), - Ok(t) => t, + Ok(t) => if $range.contains(&t.0) { t } else { + drop(tracking_reader); + + // Assumes the type id is minimally encoded, which is enforced on read. + use $crate::util::ser::Writeable; + let bytes_read = t.serialized_length(); + $rewind(stream_ref, bytes_read); + break 'tlv_read; + }, } }; @@ -168,13 +256,13 @@ macro_rules! decode_tlv_stream { } // As we read types, make sure we hit every required type: $({ - check_tlv_order!(last_seen_type, typ, $type, $fieldty); + check_tlv_order!(last_seen_type, typ, $type, $field, $fieldty); })* last_seen_type = Some(typ.0); // Finally, read the length and value itself: - let length: ser::BigSize = ser::Readable::read($stream)?; - let mut s = ser::FixedLengthReader::new($stream, length.0); + let length: ser::BigSize = $crate::util::ser::Readable::read(&mut stream_ref)?; + let mut s = ser::FixedLengthReader::new(&mut stream_ref, length.0); match typ.0 { $($type => { decode_tlv!(s, $field, $fieldty); @@ -183,117 +271,75 @@ macro_rules! decode_tlv_stream { return Err(DecodeError::InvalidValue); } },)* - x if x % 2 == 0 => { - return Err(DecodeError::UnknownRequiredFeature); - }, - _ => {}, + t => { + $( + if $decode_custom_tlv(t, &mut s)? { + // If a custom TLV was successfully read (i.e. decode_custom_tlv returns true), + // continue to the next TLV read. + s.eat_remaining()?; + continue 'tlv_read; + } + )? + if t % 2 == 0 { + return Err(DecodeError::UnknownRequiredFeature); + } + } } s.eat_remaining()?; } // Make sure we got to each required type after we've read every TLV: $({ - check_missing_tlv!(last_seen_type, $type, $fieldty); + check_missing_tlv!(last_seen_type, $type, $field, $fieldty); })* } } } -macro_rules! impl_writeable { - ($st:ident, $len: expr, {$($field:ident),*}) => { - impl ::util::ser::Writeable for $st { - fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { - if $len != 0 { - w.size_hint($len); - } - #[cfg(any(test, feature = "fuzztarget"))] - { - // In tests, assert that the hard-coded length matches the actual one - if $len != 0 { - let mut len_calc = ::util::ser::LengthCalculatingWriter(0); - $( self.$field.write(&mut len_calc).expect("No in-memory data may fail to serialize"); )* - assert_eq!(len_calc.0, $len); - assert_eq!(self.serialized_length(), $len); - } - } +macro_rules! impl_writeable_msg { + ($st:ident, {$($field:ident),* $(,)*}, {$(($type: expr, $tlvfield: ident, $fieldty: tt)),* $(,)*}) => { + impl $crate::util::ser::Writeable for $st { + fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { $( self.$field.write(w)?; )* + encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*}); Ok(()) } - - #[inline] - fn serialized_length(&self) -> usize { - if $len == 0 || cfg!(any(test, feature = "fuzztarget")) { - let mut len_calc = 0; - $( len_calc += self.$field.serialized_length(); )* - if $len != 0 { - // In tests, assert that the hard-coded length matches the actual one - assert_eq!(len_calc, $len); - } else { - return len_calc; - } - } - $len - } } - - impl ::util::ser::Readable for $st { - fn read(r: &mut R) -> Result { + impl $crate::util::ser::Readable for $st { + fn read(r: &mut R) -> Result { + $(let $field = $crate::util::ser::Readable::read(r)?;)* + $(init_tlv_field_var!($tlvfield, $fieldty);)* + decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*}); Ok(Self { - $($field: ::util::ser::Readable::read(r)?),* + $($field),*, + $($tlvfield),* }) } } } } -macro_rules! impl_writeable_len_match { - ($struct: ident, $cmp: tt, ($calc_len: expr), {$({$match: pat, $length: expr}),*}, {$($field:ident),*}) => { - impl Writeable for $struct { - fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { - let len = match *self { - $($match => $length,)* - }; - w.size_hint(len); - #[cfg(any(test, feature = "fuzztarget"))] - { - // In tests, assert that the hard-coded length matches the actual one - let mut len_calc = ::util::ser::LengthCalculatingWriter(0); - $( self.$field.write(&mut len_calc).expect("No in-memory data may fail to serialize"); )* - assert!(len_calc.0 $cmp len); - assert_eq!(len_calc.0, self.serialized_length()); - } + +macro_rules! impl_writeable { + ($st:ident, {$($field:ident),*}) => { + impl $crate::util::ser::Writeable for $st { + fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { $( self.$field.write(w)?; )* Ok(()) } #[inline] fn serialized_length(&self) -> usize { - if $calc_len || cfg!(any(test, feature = "fuzztarget")) { - let mut len_calc = 0; - $( len_calc += self.$field.serialized_length(); )* - if !$calc_len { - assert_eq!(len_calc, match *self { - $($match => $length,)* - }); - } - return len_calc - } - match *self { - $($match => $length,)* - } + let mut len_calc = 0; + $( len_calc += self.$field.serialized_length(); )* + return len_calc; } } - impl ::util::ser::Readable for $struct { - fn read(r: &mut R) -> Result { + impl $crate::util::ser::Readable for $st { + fn read(r: &mut R) -> Result { Ok(Self { - $($field: Readable::read(r)?),* + $($field: $crate::util::ser::Readable::read(r)?),* }) } } - }; - ($struct: ident, $cmp: tt, {$({$match: pat, $length: expr}),*}, {$($field:ident),*}) => { - impl_writeable_len_match!($struct, $cmp, (true), { $({ $match, $length }),* }, { $($field),* }); - }; - ($struct: ident, {$({$match: pat, $length: expr}),*}, {$($field:ident),*}) => { - impl_writeable_len_match!($struct, ==, (false), { $({ $match, $length }),* }, { $($field),* }); } } @@ -326,8 +372,8 @@ macro_rules! write_ver_prefix { /// This is the preferred method of adding new fields that old nodes can ignore and still function /// correctly. macro_rules! write_tlv_fields { - ($stream: expr, {$(($type: expr, $field: expr, $fieldty: ident)),* $(,)*}) => { - encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty)),*}); + ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { + encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty)),*}) } } @@ -347,15 +393,18 @@ macro_rules! read_ver_prefix { /// Reads a suffix added by write_tlv_fields. macro_rules! read_tlv_fields { - ($stream: expr, {$(($type: expr, $field: ident, $fieldty: ident)),* $(,)*}) => { { - let tlv_len = ::util::ser::BigSize::read($stream)?; - let mut rd = ::util::ser::FixedLengthReader::new($stream, tlv_len.0); + ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { { + let tlv_len: $crate::util::ser::BigSize = $crate::util::ser::Readable::read($stream)?; + let mut rd = $crate::util::ser::FixedLengthReader::new($stream, tlv_len.0); decode_tlv_stream!(&mut rd, {$(($type, $field, $fieldty)),*}); - rd.eat_remaining().map_err(|_| ::ln::msgs::DecodeError::ShortRead)?; + rd.eat_remaining().map_err(|_| $crate::ln::msgs::DecodeError::ShortRead)?; } } } macro_rules! init_tlv_based_struct_field { + ($field: ident, (default_value, $default: expr)) => { + $field.0.unwrap() + }; ($field: ident, option) => { $field }; @@ -363,20 +412,23 @@ macro_rules! init_tlv_based_struct_field { $field.0.unwrap() }; ($field: ident, vec_type) => { - $field.unwrap().0 + $field.unwrap() }; } macro_rules! init_tlv_field_var { + ($field: ident, (default_value, $default: expr)) => { + let mut $field = $crate::util::ser::OptionDeserWrapper(None); + }; ($field: ident, required) => { - let mut $field = ::util::ser::OptionDeserWrapper(None); + let mut $field = $crate::util::ser::OptionDeserWrapper(None); }; ($field: ident, vec_type) => { - let mut $field = Some(::util::ser::VecReadWrapper(Vec::new())); + let mut $field = Some(Vec::new()); }; ($field: ident, option) => { let mut $field = None; - } + }; } /// Implements Readable/Writeable for a struct storing it as a set of TLVs @@ -385,9 +437,9 @@ macro_rules! init_tlv_field_var { /// if $fieldty is `vec_type`, then $field is a Vec, which needs to have its individual elements /// serialized. macro_rules! impl_writeable_tlv_based { - ($st: ident, {$(($type: expr, $field: ident, $fieldty: ident)),* $(,)*}) => { - impl ::util::ser::Writeable for $st { - fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { + ($st: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { + impl $crate::util::ser::Writeable for $st { + fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { write_tlv_fields!(writer, { $(($type, self.$field, $fieldty)),* }); @@ -396,23 +448,23 @@ macro_rules! impl_writeable_tlv_based { #[inline] fn serialized_length(&self) -> usize { - use util::ser::BigSize; + use $crate::util::ser::BigSize; let len = { #[allow(unused_mut)] - let mut len = ::util::ser::LengthCalculatingWriter(0); + let mut len = $crate::util::ser::LengthCalculatingWriter(0); $( get_varint_length_prefixed_tlv_length!(len, $type, self.$field, $fieldty); )* len.0 }; - let mut len_calc = ::util::ser::LengthCalculatingWriter(0); + let mut len_calc = $crate::util::ser::LengthCalculatingWriter(0); BigSize(len as u64).write(&mut len_calc).expect("No in-memory data may fail to serialize"); len + len_calc.0 } } - impl ::util::ser::Readable for $st { - fn read(reader: &mut R) -> Result { + impl $crate::util::ser::Readable for $st { + fn read(reader: &mut R) -> Result { $( init_tlv_field_var!($field, $fieldty); )* @@ -429,29 +481,91 @@ macro_rules! impl_writeable_tlv_based { } } -/// Implement Readable and Writeable for an enum, with struct variants stored as TLVs and tuple -/// variants stored directly. -/// The format is, for example -/// impl_writeable_tlv_based_enum!(EnumName, -/// (0, StructVariantA) => {(0, required_variant_field, required), (1, optional_variant_field, option)}, -/// (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, vec_type)}; -/// (2, TupleVariantA), (3, TupleVariantB), -/// ); -/// The type is written as a single byte, followed by any variant data. -/// Attempts to read an unknown type byte result in DecodeError::UnknownRequiredFeature. -macro_rules! impl_writeable_tlv_based_enum { +/// Defines a struct for a TLV stream and a similar struct using references for non-primitive types, +/// implementing [`Readable`] for the former and [`Writeable`] for the latter. Useful as an +/// intermediary format when reading or writing a type encoded as a TLV stream. Note that each field +/// representing a TLV record has its type wrapped with an [`Option`]. A tuple consisting of a type +/// and a serialization wrapper may be given in place of a type when custom serialization is +/// required. +/// +/// [`Readable`]: crate::util::ser::Readable +/// [`Writeable`]: crate::util::ser::Writeable +macro_rules! tlv_stream { + ($name:ident, $nameref:ident, $range:expr, { + $(($type:expr, $field:ident : $fieldty:tt)),* $(,)* + }) => { + #[derive(Debug)] + pub(crate) struct $name { + $( + $field: Option, + )* + } + + pub(crate) struct $nameref<'a> { + $( + pub(crate) $field: Option, + )* + } + + impl<'a> $crate::util::ser::Writeable for $nameref<'a> { + fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { + encode_tlv_stream!(writer, { + $(($type, self.$field, (option, encoding: $fieldty))),* + }); + Ok(()) + } + } + + impl $crate::util::ser::SeekReadable for $name { + fn read(reader: &mut R) -> Result { + $( + init_tlv_field_var!($field, option); + )* + let rewind = |cursor: &mut R, offset: usize| { + cursor.seek($crate::io::SeekFrom::Current(-(offset as i64))).expect(""); + }; + decode_tlv_stream_range!(reader, $range, rewind, { + $(($type, $field, (option, encoding: $fieldty))),* + }); + + Ok(Self { + $( + $field: $field + ),* + }) + } + } + } +} + +macro_rules! tlv_record_type { + (($type:ty, $wrapper:ident)) => { $type }; + ($type:ty) => { $type }; +} + +macro_rules! tlv_record_ref_type { + (char) => { char }; + (u8) => { u8 }; + ((u16, $wrapper: ident)) => { u16 }; + ((u32, $wrapper: ident)) => { u32 }; + ((u64, $wrapper: ident)) => { u64 }; + (($type:ty, $wrapper:ident)) => { &'a $type }; + ($type:ty) => { &'a $type }; +} + +macro_rules! _impl_writeable_tlv_based_enum_common { ($st: ident, $(($variant_id: expr, $variant_name: ident) => - {$(($type: expr, $field: ident, $fieldty: ident)),* $(,)*} + {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} ),* $(,)*; $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*) => { - impl ::util::ser::Writeable for $st { - fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { + impl $crate::util::ser::Writeable for $st { + fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { match self { $($st::$variant_name { $(ref $field),* } => { let id: u8 = $variant_id; id.write(writer)?; write_tlv_fields!(writer, { - $(($type, $field, $fieldty)),* + $(($type, *$field, $fieldty)),* }); }),* $($st::$tuple_variant_name (ref field) => { @@ -463,10 +577,82 @@ macro_rules! impl_writeable_tlv_based_enum { Ok(()) } } + } +} + +/// Implement MaybeReadable and Writeable for an enum, with struct variants stored as TLVs and +/// tuple variants stored directly. +/// +/// This is largely identical to `impl_writeable_tlv_based_enum`, except that odd variants will +/// return `Ok(None)` instead of `Err(UnknownRequiredFeature)`. It should generally be preferred +/// when `MaybeReadable` is practical instead of just `Readable` as it provides an upgrade path for +/// new variants to be added which are simply ignored by existing clients. +macro_rules! impl_writeable_tlv_based_enum_upgradable { + ($st: ident, $(($variant_id: expr, $variant_name: ident) => + {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} + ),* $(,)* + $(; + $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*)*) => { + _impl_writeable_tlv_based_enum_common!($st, + $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*; + $($(($tuple_variant_id, $tuple_variant_name)),*)*); + + impl $crate::util::ser::MaybeReadable for $st { + fn read(reader: &mut R) -> Result, $crate::ln::msgs::DecodeError> { + let id: u8 = $crate::util::ser::Readable::read(reader)?; + match id { + $($variant_id => { + // Because read_tlv_fields creates a labeled loop, we cannot call it twice + // in the same function body. Instead, we define a closure and call it. + let f = || { + $( + init_tlv_field_var!($field, $fieldty); + )* + read_tlv_fields!(reader, { + $(($type, $field, $fieldty)),* + }); + Ok(Some($st::$variant_name { + $( + $field: init_tlv_based_struct_field!($field, $fieldty) + ),* + })) + }; + f() + }),* + $($($tuple_variant_id => { + Ok(Some($st::$tuple_variant_name(Readable::read(reader)?))) + }),*)* + _ if id % 2 == 1 => Ok(None), + _ => Err(DecodeError::UnknownRequiredFeature), + } + } + } + + } +} + +/// Implement Readable and Writeable for an enum, with struct variants stored as TLVs and tuple +/// variants stored directly. +/// The format is, for example +/// impl_writeable_tlv_based_enum!(EnumName, +/// (0, StructVariantA) => {(0, required_variant_field, required), (1, optional_variant_field, option)}, +/// (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, vec_type)}; +/// (2, TupleVariantA), (3, TupleVariantB), +/// ); +/// The type is written as a single byte, followed by any variant data. +/// Attempts to read an unknown type byte result in DecodeError::UnknownRequiredFeature. +macro_rules! impl_writeable_tlv_based_enum { + ($st: ident, $(($variant_id: expr, $variant_name: ident) => + {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} + ),* $(,)*; + $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*) => { + _impl_writeable_tlv_based_enum_common!($st, + $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*; + $(($tuple_variant_id, $tuple_variant_name)),*); - impl ::util::ser::Readable for $st { - fn read(reader: &mut R) -> Result { - let id: u8 = ::util::ser::Readable::read(reader)?; + impl $crate::util::ser::Readable for $st { + fn read(reader: &mut R) -> Result { + let id: u8 = $crate::util::ser::Readable::read(reader)?; match id { $($variant_id => { // Because read_tlv_fields creates a labeled loop, we cannot call it twice @@ -500,10 +686,10 @@ macro_rules! impl_writeable_tlv_based_enum { #[cfg(test)] mod tests { - use io::{self, Cursor}; - use prelude::*; - use ln::msgs::DecodeError; - use util::ser::{Writeable, HighZeroBytesDroppedVarInt, VecWriter}; + use crate::io::{self, Cursor}; + use crate::prelude::*; + use crate::ln::msgs::DecodeError; + use crate::util::ser::{Writeable, HighZeroBytesDroppedBigSize, VecWriter}; use bitcoin::secp256k1::PublicKey; // The BOLT TLV test cases don't include any tests which use our "required-value" logic since @@ -572,9 +758,9 @@ mod tests { } // BOLT TLV test cases - fn tlv_reader_n1(s: &[u8]) -> Result<(Option>, Option, Option<(PublicKey, u64, u64)>, Option), DecodeError> { + fn tlv_reader_n1(s: &[u8]) -> Result<(Option>, Option, Option<(PublicKey, u64, u64)>, Option), DecodeError> { let mut s = Cursor::new(s); - let mut tlv1: Option> = None; + let mut tlv1: Option> = None; let mut tlv2: Option = None; let mut tlv3: Option<(PublicKey, u64, u64)> = None; let mut tlv4: Option = None; @@ -705,11 +891,11 @@ mod tests { assert_eq!(stream.0, ::hex::decode("06fd00ff02abcd").unwrap()); stream.0.clear(); - encode_varint_length_prefixed_tlv!(&mut stream, {(0, 1u64, required), (42, None::, option), (0xff, HighZeroBytesDroppedVarInt(0u64), required)}); + encode_varint_length_prefixed_tlv!(&mut stream, {(0, 1u64, required), (42, None::, option), (0xff, HighZeroBytesDroppedBigSize(0u64), required)}); assert_eq!(stream.0, ::hex::decode("0e00080000000000000001fd00ff00").unwrap()); stream.0.clear(); - encode_varint_length_prefixed_tlv!(&mut stream, {(0, Some(1u64), option), (0xff, HighZeroBytesDroppedVarInt(0u64), required)}); + encode_varint_length_prefixed_tlv!(&mut stream, {(0, Some(1u64), option), (0xff, HighZeroBytesDroppedBigSize(0u64), required)}); assert_eq!(stream.0, ::hex::decode("0e00080000000000000001fd00ff00").unwrap()); Ok(())