]> git.bitcoin.ninja Git - rust-lightning/commitdiff
f - explicit encodings
authorJeffrey Czyz <jkczyz@gmail.com>
Wed, 28 Sep 2022 16:01:35 +0000 (11:01 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 28 Sep 2022 17:12:09 +0000 (12:12 -0500)
lightning/src/offers/offer.rs
lightning/src/util/ser_macros.rs

index de40623c5f39fc6bed785e2de5bd7793baef27fc..a5a0e6f5cd81478f68b4dcecfd5d0886fc2cc8df 100644 (file)
@@ -63,7 +63,7 @@ use io;
 use ln::features::OfferFeatures;
 use ln::msgs::MAX_VALUE_MSAT;
 use onion_message::BlindedPath;
-use util::ser::{Writeable, Writer};
+use util::ser::{HighZeroBytesDroppedBigSize, WithoutLength, Writeable, Writer};
 
 use prelude::*;
 
@@ -433,17 +433,17 @@ impl Amount {
 pub type CurrencyCode = [u8; 3];
 
 tlv_stream!(OfferTlvStream, OfferTlvStreamRef, {
-       (2, chains: Vec<ChainHash>),
-       (4, metadata: Vec<u8>),
+       (2, chains: (Vec<ChainHash>, WithoutLength)),
+       (4, metadata: (Vec<u8>, WithoutLength)),
        (6, currency: CurrencyCode),
-       (8, amount: u64),
-       (10, description: String),
+       (8, amount: (u64, HighZeroBytesDroppedBigSize)),
+       (10, description: (String, WithoutLength)),
        (12, features: OfferFeatures),
-       (14, absolute_expiry: u64),
-       (16, paths: Vec<BlindedPath>),
-       (18, issuer: String),
-       (20, quantity_min: u64),
-       (22, quantity_max: u64),
+       (14, absolute_expiry: (u64, HighZeroBytesDroppedBigSize)),
+       (16, paths: (Vec<BlindedPath>, WithoutLength)),
+       (18, issuer: (String, WithoutLength)),
+       (20, quantity_min: (u64, HighZeroBytesDroppedBigSize)),
+       (22, quantity_max: (u64, HighZeroBytesDroppedBigSize)),
        (24, node_id: PublicKey),
 });
 
index 2f16f145e6771f4c251efc72997fbce5d80d5f97..f67998410c9f5058bf0307507027782e2593039a 100644 (file)
@@ -26,9 +26,9 @@ macro_rules! encode_tlv {
                        field.write($stream)?;
                }
        };
-       ($stream: expr, $type: expr, $field: expr, (tlv_record, $fieldty:ident$(<$gen:ident>)?)) => {
+       ($stream: expr, $type: expr, $field: expr, (tlv_record, $fieldty:tt)) => {
                if let Some(field) = $field {
-                       let field: encoded_tlv_record_ref_type!($fieldty$(<$gen>)?) = From::from(field);
+                       let field: encoded_tlv_record_ref_type!($fieldty) = From::from(field);
                        BigSize($type).write($stream)?;
                        BigSize(field.serialized_length() as u64).write($stream)?;
                        field.write($stream)?;
@@ -129,7 +129,7 @@ macro_rules! check_tlv_order {
        ($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, $field: ident, (tlv_record, $fieldty:ident$(<$gen:ident>)?)) => {{
+       ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (tlv_record, $fieldty:tt)) => {{
                // no-op
        }};
 }
@@ -161,7 +161,7 @@ macro_rules! check_missing_tlv {
        ($last_seen_type: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
                // no-op
        }};
-       ($last_seen_type: expr, $type: expr, $field: ident, (tlv_record, $fieldty:ident$(<$gen:ident>)?)) => {{
+       ($last_seen_type: expr, $type: expr, $field: ident, (tlv_record, $fieldty:tt)) => {{
                // no-op
        }};
 }
@@ -186,10 +186,9 @@ macro_rules! decode_tlv {
        ($reader: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
                $field = Some($trait::read(&mut $reader $(, $read_arg)*)?);
        }};
-       ($reader: expr, $field: ident, (tlv_record, $fieldty:ident$(<$gen:ident>)?)) => {{
+       ($reader: expr, $field: ident, (tlv_record, $fieldty:tt)) => {{
                $field = {
-                       let field: encoded_tlv_record_type!($fieldty$(<$gen>)?) =
-                               ser::Readable::read(&mut $reader)?;
+                       let field: encoded_tlv_record_type!($fieldty) = ser::Readable::read(&mut $reader)?;
                        Some(field.into())
                };
        }};
@@ -460,25 +459,25 @@ macro_rules! impl_writeable_tlv_based {
 /// [`Writeable`]: crate::util::ser::Writeable
 macro_rules! tlv_stream {
        ($name:ident, $nameref:ident, {
-               $(($type:expr, $field:ident : $fieldty:ident$(<$gen:ident>)?)),* $(,)*
+               $(($type:expr, $field:ident : $fieldty:tt)),* $(,)*
        }) => {
                #[derive(Debug)]
                struct $name {
                        $(
-                               $field: Option<$fieldty$(<$gen>)?>,
+                               $field: Option<tlv_record_type!($fieldty)>,
                        )*
                }
 
                pub(crate) struct $nameref<'a> {
                        $(
-                               pub(crate) $field: Option<tlv_record_ref_type!($fieldty$(<$gen>)?)>,
+                               pub(crate) $field: Option<tlv_record_ref_type!($fieldty)>,
                        )*
                }
 
                impl<'a> ::util::ser::Writeable for $nameref<'a> {
                        fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
                                encode_tlv_stream!(writer, {
-                                       $(($type, self.$field, (tlv_record, $fieldty$(<$gen>)?))),*
+                                       $(($type, self.$field, (tlv_record, $fieldty))),*
                                });
                                Ok(())
                        }
@@ -490,7 +489,7 @@ macro_rules! tlv_stream {
                                        init_tlv_field_var!($field, tlv_record);
                                )*
                                decode_tlv_stream!(reader, {
-                                       $(($type, $field, (tlv_record, $fieldty$(<$gen>)?))),*
+                                       $(($type, $field, (tlv_record, $fieldty))),*
                                });
 
                                Ok(Self {
@@ -503,57 +502,39 @@ macro_rules! tlv_stream {
        }
 }
 
+macro_rules! tlv_record_type {
+       (($type:ty, $wrapper:ident)) => {
+               $type
+       };
+       ($type:ty) => {
+               $type
+       };
+}
+
 macro_rules! tlv_record_ref_type {
        (u8) => {
                u8
        };
-       (u16) => {
-               u16
-       };
-       (u32) => {
-               u32
-       };
-       (u64) => {
-               u64
-       };
        (char) => {
                char
        };
-       (String) => {
-               &'a crate::prelude::String
+       (($type:ty, HighZeroBytesDroppedBigSize)) => {
+               $type
        };
-       (Vec<$type:ty>) => {
-               &'a crate::prelude::Vec<$type>
+       (($type:ty, $wrapper:ident)) => {
+               &'a $type
        };
-       ($type:ident$(<$gen:ident>)?) => {
-               &'a $type$(<$gen>)?
+       ($type:ty) => {
+               &'a $type
        };
 }
 
 macro_rules! encoded_tlv_record_type {
-       (u8) => {
-               u8
-       };
-       (u16) => {
-               ::util::ser::HighZeroBytesDroppedBigSize<u16>
+       (($type:ty, $wrapper:ident)) => {
+               $wrapper<$type>
        };
-       (u32) => {
-               ::util::ser::HighZeroBytesDroppedBigSize<u32>
-       };
-       (u64) => {
-               ::util::ser::HighZeroBytesDroppedBigSize<u64>
-       };
-       (char) => {
-               char
-       };
-       (String) => {
-               ::util::ser::WithoutLength<crate::prelude::String>
-       };
-       (Vec<$type:ty>) => {
-               ::util::ser::WithoutLength<crate::prelude::Vec<$type>>
-       };
-       ($type:ident$(<$gen:ident>)?) => {
-               $type$(<$gen>)?
+       ($type:ty) => {
+               $type
        };
 }
 
@@ -561,26 +542,17 @@ macro_rules! encoded_tlv_record_ref_type {
        (u8) => {
                u8
        };
-       (u16) => {
-               ::util::ser::HighZeroBytesDroppedBigSize<u16>
-       };
-       (u32) => {
-               ::util::ser::HighZeroBytesDroppedBigSize<u32>
-       };
-       (u64) => {
-               ::util::ser::HighZeroBytesDroppedBigSize<u64>
-       };
        (char) => {
                char
        };
-       (String) => {
-               ::util::ser::WithoutLength<&crate::prelude::String>
+       (($type:ty, HighZeroBytesDroppedBigSize)) => {
+               HighZeroBytesDroppedBigSize<$type>
        };
-       (Vec<$type:ty>) => {
-               ::util::ser::WithoutLength<&crate::prelude::Vec<$type>>
+       (($type:ty, $wrapper:ident)) => {
+               $wrapper<&$type>
        };
-       ($type:ident$(<$gen:ident>)?) => {
-               &$type$(<$gen>)?
+       ($type:ty) => {
+               &$type
        };
 }