Merge pull request #2411 from valentinewallace/2023-07-blinded-onion-keys
[rust-lightning] / lightning / src / util / ser_macros.rs
index 742ea25714dafb4d291f249e30376366cfeebbaa..2626509f34e6a48cacab50013451a6e454a1a9fe 100644 (file)
 //! [`Readable`]: crate::util::ser::Readable
 //! [`Writeable`]: crate::util::ser::Writeable
 
+// There are quite a few TLV serialization "types" which behave differently. We currently only
+// publicly document the `optional` and `required` types, not supporting anything else publicly and
+// changing them at will.
+//
+// Some of the other types include:
+//  * (default_value, $default) - reads optionally, reading $default if no TLV is present
+//  * (static_value, $value) - ignores any TLVs, always using $value
+//  * required_vec - reads into a Vec without a length prefix, failing if no TLV is present.
+//  * optional_vec - reads into an Option<Vec> without a length prefix, continuing if no TLV is
+//                   present. Writes from a Vec directly, only if any elements are present. Note
+//                   that the struct deserialization macros return a Vec, not an Option.
+//  * upgradable_option - reads via MaybeReadable.
+//  * upgradable_required - reads via MaybeReadable, requiring a TLV be present but may return None
+//                          if MaybeReadable::read() returns None.
+
 /// Implements serialization for a single TLV record.
 /// This is exported for use by other exported macros, do not use directly.
 #[doc(hidden)]
@@ -29,7 +44,7 @@ macro_rules! _encode_tlv {
                BigSize($field.serialized_length() as u64).write($stream)?;
                $field.write($stream)?;
        };
-       ($stream: expr, $type: expr, $field: expr, vec_type) => {
+       ($stream: expr, $type: expr, $field: expr, required_vec) => {
                $crate::_encode_tlv!($stream, $type, $crate::util::ser::WithoutLength(&$field), required);
        };
        ($stream: expr, $optional_type: expr, $optional_field: expr, option) => {
@@ -41,7 +56,7 @@ macro_rules! _encode_tlv {
        };
        ($stream: expr, $type: expr, $field: expr, optional_vec) => {
                if !$field.is_empty() {
-                       $crate::_encode_tlv!($stream, $type, $field, vec_type);
+                       $crate::_encode_tlv!($stream, $type, $field, required_vec);
                }
        };
        ($stream: expr, $type: expr, $field: expr, upgradable_required) => {
@@ -117,7 +132,20 @@ macro_rules! _check_encoded_tlv_order {
 /// [`Writer`]: crate::util::ser::Writer
 #[macro_export]
 macro_rules! encode_tlv_stream {
+       ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => {
+               $crate::_encode_tlv_stream!($stream, {$(($type, $field, $fieldty)),*})
+       }
+}
+
+/// Implementation of [`encode_tlv_stream`].
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _encode_tlv_stream {
        ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { {
+               $crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* }, &[])
+       } };
+       ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}, $extra_tlvs: expr) => { {
                #[allow(unused_imports)]
                use $crate::{
                        ln::msgs::DecodeError,
@@ -129,6 +157,10 @@ macro_rules! encode_tlv_stream {
                $(
                        $crate::_encode_tlv!($stream, $type, $field, $fieldty);
                )*
+               for tlv in $extra_tlvs {
+                       let (typ, value): &(u64, Vec<u8>) = tlv;
+                       $crate::_encode_tlv!($stream, *typ, *value, required_vec);
+               }
 
                #[allow(unused_mut, unused_variables, unused_assignments)]
                #[cfg(debug_assertions)]
@@ -137,8 +169,12 @@ macro_rules! encode_tlv_stream {
                        $(
                                $crate::_check_encoded_tlv_order!(last_seen, $type, $fieldty);
                        )*
+                       for tlv in $extra_tlvs {
+                               let (typ, _): &(u64, Vec<u8>) = tlv;
+                               $crate::_check_encoded_tlv_order!(last_seen, *typ, required_vec);
+                       }
                }
-       } }
+       } };
 }
 
 /// Adds the length of the serialized field to a [`LengthCalculatingWriter`].
@@ -159,7 +195,7 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
                BigSize(field_len as u64).write(&mut $len).expect("No in-memory data may fail to serialize");
                $len.0 += field_len;
        };
-       ($len: expr, $type: expr, $field: expr, vec_type) => {
+       ($len: expr, $type: expr, $field: expr, required_vec) => {
                $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $crate::util::ser::WithoutLength(&$field), required);
        };
        ($len: expr, $optional_type: expr, $optional_field: expr, option) => {
@@ -172,7 +208,7 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
        };
        ($len: expr, $type: expr, $field: expr, optional_vec) => {
                if !$field.is_empty() {
-                       $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, vec_type);
+                       $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required_vec);
                }
        };
        ($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
@@ -195,18 +231,27 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
 #[macro_export]
 macro_rules! _encode_varint_length_prefixed_tlv {
        ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}) => { {
+               $crate::_encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty)),*}, &[])
+       } };
+       ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}, $extra_tlvs: expr) => { {
+               extern crate alloc;
                use $crate::util::ser::BigSize;
+               use alloc::vec::Vec;
                let len = {
                        #[allow(unused_mut)]
                        let mut len = $crate::util::ser::LengthCalculatingWriter(0);
                        $(
                                $crate::_get_varint_length_prefixed_tlv_length!(len, $type, $field, $fieldty);
                        )*
+                       for tlv in $extra_tlvs {
+                               let (typ, value): &(u64, Vec<u8>) = tlv;
+                               $crate::_get_varint_length_prefixed_tlv_length!(len, *typ, *value, required_vec);
+                       }
                        len.0
                };
                BigSize(len as u64).write($stream)?;
-               $crate::encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* });
-       } }
+               $crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* }, $extra_tlvs);
+       } };
 }
 
 /// Errors if there are missing required TLV types between the last seen type and the type currently being processed.
@@ -236,8 +281,8 @@ macro_rules! _check_decoded_tlv_order {
        ($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, required_vec) => {{
+               $crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
        }};
        ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, optional_vec) => {{
                // no-op
@@ -281,8 +326,8 @@ macro_rules! _check_missing_tlv {
        ($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, required_vec) => {{
+               $crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
        }};
        ($last_seen_type: expr, $type: expr, $field: ident, option) => {{
                // no-op
@@ -320,15 +365,16 @@ macro_rules! _decode_tlv {
        ($reader: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
                $field = $trait::read(&mut $reader $(, $read_arg)*)?;
        }};
-       ($reader: expr, $field: ident, vec_type) => {{
+       ($reader: expr, $field: ident, required_vec) => {{
                let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
-               $field = Some(f.0);
+               $field = f.0;
        }};
        ($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);
+               let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
+               $field = Some(f.0);
        }};
        // `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
@@ -694,8 +740,8 @@ macro_rules! _init_tlv_based_struct_field {
        ($field: ident, required) => {
                $field.0.unwrap()
        };
-       ($field: ident, vec_type) => {
-               $field.unwrap()
+       ($field: ident, required_vec) => {
+               $field
        };
        ($field: ident, optional_vec) => {
                $field.unwrap()
@@ -720,8 +766,8 @@ macro_rules! _init_tlv_field_var {
        ($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());
+       ($field: ident, required_vec) => {
+               let mut $field = Vec::new();
        };
        ($field: ident, option) => {
                let mut $field = None;