X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fser_macros.rs;h=d6a03a88fbe827efc72f6fcd96aa1e928aaa2c17;hb=refs%2Fheads%2F2023-05-no-background-event-dup-persist;hp=3f4b7153a7fe7ae2e30bb0e344b19aaf28c3dec4;hpb=0e88538b783a753a93f17419f5fabbe645fe1fba;p=rust-lightning diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 3f4b7153..d6a03a88 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -39,6 +39,11 @@ macro_rules! _encode_tlv { field.write($stream)?; } }; + ($stream: expr, $type: expr, $field: expr, optional_vec) => { + if !$field.is_empty() { + $crate::_encode_tlv!($stream, $type, $field, vec_type); + } + }; ($stream: expr, $type: expr, $field: expr, upgradable_required) => { $crate::_encode_tlv!($stream, $type, $field, required); }; @@ -51,6 +56,10 @@ macro_rules! _encode_tlv { ($stream: expr, $type: expr, $field: expr, (option, encoding: $fieldty: ty)) => { $crate::_encode_tlv!($stream, $type, $field, option); }; + ($stream: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => { + // Just a read-mapped type + $crate::_encode_tlv!($stream, $type, $field, option); + }; } /// Panics if the last seen TLV type is not numerically less than the TLV type currently being checked. @@ -161,6 +170,14 @@ macro_rules! _get_varint_length_prefixed_tlv_length { $len.0 += field_len; } }; + ($len: expr, $type: expr, $field: expr, optional_vec) => { + if !$field.is_empty() { + $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, vec_type); + } + }; + ($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => { + $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option); + }; ($len: expr, $type: expr, $field: expr, upgradable_required) => { $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required); }; @@ -210,15 +227,21 @@ macro_rules! _check_decoded_tlv_order { return Err(DecodeError::InvalidValue); } }}; + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{ + $crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required); + }}; ($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, upgradable_required) => {{ + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, optional_vec) => {{ // no-op }}; + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, upgradable_required) => {{ + _check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required) + }}; ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, upgradable_option) => {{ // no-op }}; @@ -252,15 +275,21 @@ macro_rules! _check_missing_tlv { return Err(DecodeError::InvalidValue); } }}; + ($last_seen_type: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{ + $crate::_check_missing_tlv!($last_seen_type, $type, $field, required); + }}; ($last_seen_type: expr, $type: expr, $field: ident, vec_type) => {{ // no-op }}; ($last_seen_type: expr, $type: expr, $field: ident, option) => {{ // no-op }}; - ($last_seen_type: expr, $type: expr, $field: ident, upgradable_required) => {{ + ($last_seen_type: expr, $type: expr, $field: ident, optional_vec) => {{ // no-op }}; + ($last_seen_type: expr, $type: expr, $field: ident, upgradable_required) => {{ + _check_missing_tlv!($last_seen_type, $type, $field, required) + }}; ($last_seen_type: expr, $type: expr, $field: ident, upgradable_option) => {{ // no-op }}; @@ -285,6 +314,9 @@ macro_rules! _decode_tlv { ($reader: expr, $field: ident, required) => {{ $field = $crate::util::ser::Readable::read(&mut $reader)?; }}; + ($reader: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{ + $field = $trait::read(&mut $reader $(, $read_arg)*)?; + }}; ($reader: expr, $field: ident, vec_type) => {{ let f: $crate::util::ser::WithoutLength> = $crate::util::ser::Readable::read(&mut $reader)?; $field = Some(f.0); @@ -292,9 +324,22 @@ macro_rules! _decode_tlv { ($reader: expr, $field: ident, option) => {{ $field = Some($crate::util::ser::Readable::read(&mut $reader)?); }}; + ($reader: expr, $field: ident, optional_vec) => {{ + $crate::_decode_tlv!($reader, $field, vec_type); + }}; + // `upgradable_required` indicates we're reading a required TLV that may have been upgraded + // without backwards compat. We'll error if the field is missing, and return `Ok(None)` if the + // field is present but we can no longer understand it. + // Note that this variant can only be used within a `MaybeReadable` read. ($reader: expr, $field: ident, upgradable_required) => {{ - $field = $crate::util::ser::MaybeReadable::read(&mut $reader)?; + $field = match $crate::util::ser::MaybeReadable::read(&mut $reader)? { + Some(res) => res, + _ => return Ok(None) + }; }}; + // `upgradable_option` indicates we're reading an Option-al TLV that may have been upgraded + // without backwards compat. $field will be None if the TLV is missing or if the field is present + // but we can no longer understand it. ($reader: expr, $field: ident, upgradable_option) => {{ $field = $crate::util::ser::MaybeReadable::read(&mut $reader)?; }}; @@ -393,7 +438,6 @@ macro_rules! decode_tlv_stream_with_custom_tlv_decode { macro_rules! _decode_tlv_stream_range { ($stream: expr, $range: expr, $rewind: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} $(, $decode_custom_tlv: expr)?) => { { - use core::ops::RangeBounds; use $crate::ln::msgs::DecodeError; let mut last_seen_type: Option = None; let mut stream_ref = $stream; @@ -416,7 +460,7 @@ macro_rules! _decode_tlv_stream_range { } }, Err(e) => return Err(e), - Ok(t) => if $range.contains(&t.0) { t } else { + Ok(t) => if core::ops::RangeBounds::contains(&$range, &t.0) { t } else { drop(tracking_reader); // Assumes the type id is minimally encoded, which is enforced on read. @@ -510,7 +554,7 @@ macro_rules! impl_writeable_msg { impl $crate::util::ser::Writeable for $st { fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { $( self.$field.write(w)?; )* - $crate::encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*}); + $crate::encode_tlv_stream!(w, {$(($type, self.$tlvfield.as_ref(), $fieldty)),*}); Ok(()) } } @@ -635,8 +679,11 @@ macro_rules! _init_tlv_based_struct_field { ($field: ident, option) => { $field }; + ($field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => { + $crate::_init_tlv_based_struct_field!($field, option) + }; ($field: ident, upgradable_required) => { - if $field.is_none() { return Ok(None); } else { $field.unwrap() } + $field.0.unwrap() }; ($field: ident, upgradable_option) => { $field @@ -647,6 +694,9 @@ macro_rules! _init_tlv_based_struct_field { ($field: ident, vec_type) => { $field.unwrap() }; + ($field: ident, optional_vec) => { + $field.unwrap() + }; } /// Initializes the variable we are going to read the TLV into. @@ -656,13 +706,16 @@ macro_rules! _init_tlv_based_struct_field { #[macro_export] macro_rules! _init_tlv_field_var { ($field: ident, (default_value, $default: expr)) => { - let mut $field = $crate::util::ser::OptionDeserWrapper(None); + let mut $field = $crate::util::ser::RequiredWrapper(None); }; ($field: ident, (static_value, $value: expr)) => { let $field; }; ($field: ident, required) => { - let mut $field = $crate::util::ser::OptionDeserWrapper(None); + let mut $field = $crate::util::ser::RequiredWrapper(None); + }; + ($field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => { + $crate::_init_tlv_field_var!($field, required); }; ($field: ident, vec_type) => { let mut $field = Some(Vec::new()); @@ -670,8 +723,17 @@ macro_rules! _init_tlv_field_var { ($field: ident, option) => { let mut $field = None; }; + ($field: ident, optional_vec) => { + let mut $field = Some(Vec::new()); + }; + ($field: ident, (option, encoding: ($fieldty: ty, $encoding: ident))) => { + $crate::_init_tlv_field_var!($field, option); + }; + ($field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => { + $crate::_init_tlv_field_var!($field, option); + }; ($field: ident, upgradable_required) => { - let mut $field = None; + let mut $field = $crate::util::ser::UpgradableRequired(None); }; ($field: ident, upgradable_option) => { let mut $field = None; @@ -699,7 +761,8 @@ macro_rules! _init_and_read_tlv_fields { /// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`]. /// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present. /// If `$fieldty` is `option`, then `$field` is optional field. -/// If `$fieldty` is `vec_type`, then `$field` is a [`Vec`], which needs to have its individual elements serialized. +/// If `$fieldty` is `optional_vec`, then `$field` is a [`Vec`], which needs to have its individual elements serialized. +/// Note that for `optional_vec` no bytes are written if the vec is empty /// /// For example, /// ``` @@ -715,7 +778,7 @@ macro_rules! _init_and_read_tlv_fields { /// (0, tlv_integer, required), /// (1, tlv_default_integer, (default_value, 7)), /// (2, tlv_optional_integer, option), -/// (3, tlv_vec_type_integer, vec_type), +/// (3, tlv_vec_type_integer, optional_vec), /// }); /// ``` /// @@ -840,6 +903,8 @@ macro_rules! tlv_record_ref_type { ($type:ty) => { &'a $type }; } +#[doc(hidden)] +#[macro_export] macro_rules! _impl_writeable_tlv_based_enum_common { ($st: ident, $(($variant_id: expr, $variant_name: ident) => {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} @@ -851,7 +916,7 @@ macro_rules! _impl_writeable_tlv_based_enum_common { $($st::$variant_name { $(ref $field),* } => { let id: u8 = $variant_id; id.write(writer)?; - write_tlv_fields!(writer, { + $crate::write_tlv_fields!(writer, { $(($type, *$field, $fieldty)),* }); }),* @@ -873,7 +938,7 @@ macro_rules! _impl_writeable_tlv_based_enum_common { /// ```ignore /// 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)}; +/// (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, optional_vec)}; /// (2, TupleVariantA), (3, TupleVariantB), /// ); /// ``` @@ -889,7 +954,7 @@ macro_rules! impl_writeable_tlv_based_enum { {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} ),* $(,)*; $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*) => { - _impl_writeable_tlv_based_enum_common!($st, + $crate::_impl_writeable_tlv_based_enum_common!($st, $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*; $(($tuple_variant_id, $tuple_variant_name)),*); @@ -901,12 +966,12 @@ macro_rules! impl_writeable_tlv_based_enum { // 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_and_read_tlv_fields!(reader, { + $crate::_init_and_read_tlv_fields!(reader, { $(($type, $field, $fieldty)),* }); Ok($st::$variant_name { $( - $field: _init_tlv_based_struct_field!($field, $fieldty) + $field: $crate::_init_tlv_based_struct_field!($field, $fieldty) ),* }) }; @@ -943,7 +1008,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable { ),* $(,)* $(; $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*)*) => { - _impl_writeable_tlv_based_enum_common!($st, + $crate::_impl_writeable_tlv_based_enum_common!($st, $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*; $($(($tuple_variant_id, $tuple_variant_name)),*)*); @@ -955,12 +1020,12 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable { // 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_and_read_tlv_fields!(reader, { + $crate::_init_and_read_tlv_fields!(reader, { $(($type, $field, $fieldty)),* }); Ok(Some($st::$variant_name { $( - $field: _init_tlv_based_struct_field!($field, $fieldty) + $field: $crate::_init_tlv_based_struct_field!($field, $fieldty) ),* })) }; @@ -1050,6 +1115,47 @@ mod tests { (0xdeadbeef1badbeef, 0x1bad1dea, Some(0x01020304))); } + #[derive(Debug, PartialEq)] + struct TestUpgradable { + a: u32, + b: u32, + c: Option, + } + + fn upgradable_tlv_reader(s: &[u8]) -> Result, DecodeError> { + let mut s = Cursor::new(s); + let mut a = 0; + let mut b = 0; + let mut c: Option = None; + decode_tlv_stream!(&mut s, {(2, a, upgradable_required), (3, b, upgradable_required), (4, c, upgradable_option)}); + Ok(Some(TestUpgradable { a, b, c, })) + } + + #[test] + fn upgradable_tlv_simple_good_cases() { + assert_eq!(upgradable_tlv_reader(&::hex::decode( + concat!("0204deadbeef", "03041bad1dea", "0404deadbeef") + ).unwrap()[..]).unwrap(), + Some(TestUpgradable { a: 0xdeadbeef, b: 0x1bad1dea, c: Some(0xdeadbeef) })); + + assert_eq!(upgradable_tlv_reader(&::hex::decode( + concat!("0204deadbeef", "03041bad1dea") + ).unwrap()[..]).unwrap(), + Some(TestUpgradable { a: 0xdeadbeef, b: 0x1bad1dea, c: None})); + } + + #[test] + fn missing_required_upgradable() { + if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&::hex::decode( + concat!("0100", "0204deadbeef") + ).unwrap()[..]) { + } else { panic!(); } + if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&::hex::decode( + concat!("0100", "03041bad1dea") + ).unwrap()[..]) { + } else { panic!(); } + } + // BOLT TLV test cases fn tlv_reader_n1(s: &[u8]) -> Result<(Option>, Option, Option<(PublicKey, u64, u64)>, Option), DecodeError> { let mut s = Cursor::new(s);