]> git.bitcoin.ninja Git - rust-lightning/commitdiff
redo stuff some 2022-09-1719-tweaks
authorMatt Corallo <git@bluematt.me>
Thu, 29 Sep 2022 16:57:09 +0000 (16:57 +0000)
committerMatt Corallo <git@bluematt.me>
Thu, 29 Sep 2022 17:40:24 +0000 (17:40 +0000)
lightning/src/offers/offer.rs
lightning/src/util/ser_macros.rs

index b5efd6be5a80e42aedd293345c352cb42f99687c..1e52ee0f3c405f12554c6f67f5e108882d3535ac 100644 (file)
@@ -436,16 +436,16 @@ pub type CurrencyCode = [u8; 3];
 tlv_stream!(OfferTlvStream, OfferTlvStreamRef, {
        (2, chains: (Vec<ChainHash>, WithoutLength)),
        (4, metadata: (Vec<u8>, WithoutLength)),
-       (6, currency: CurrencyCode),
+       (6, currency: (CurrencyCode)),
        (8, amount: (u64, HighZeroBytesDroppedBigSize)),
        (10, description: (String, WithoutLength)),
-       (12, features: OfferFeatures),
+       (12, features: (OfferFeatures)),
        (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),
+       (24, node_id: (PublicKey)),
 });
 
 #[cfg(test)]
index f67998410c9f5058bf0307507027782e2593039a..764cf139b54ffe51c9fe1f6c71eedb47216b64ef 100644 (file)
@@ -26,14 +26,6 @@ macro_rules! encode_tlv {
                        field.write($stream)?;
                }
        };
-       ($stream: expr, $type: expr, $field: expr, (tlv_record, $fieldty:tt)) => {
-               if let Some(field) = $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)?;
-               }
-       };
 }
 
 macro_rules! encode_tlv_stream {
@@ -450,6 +442,22 @@ macro_rules! impl_writeable_tlv_based {
        }
 }
 
+macro_rules! _tlv_stream_impl_writeable {
+       ($nameref:ident, {
+               $(($type:expr, $field:ident : ($fieldty: ty $(, $serwrapper: ident)?))),* $(,)*
+       }) => {
+               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 $(.map(|a| $serwrapper(a)))?, option)),*
+                               });
+                               Ok(())
+                       }
+               }
+       }
+}
+
+
 /// 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
@@ -474,14 +482,7 @@ macro_rules! tlv_stream {
                        )*
                }
 
-               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))),*
-                               });
-                               Ok(())
-                       }
-               }
+               _tlv_stream_impl_writeable!($nameref, {$(($type, $field: $fieldty)),*});
 
                impl ::util::ser::Readable for $name {
                        fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
@@ -506,25 +507,26 @@ macro_rules! tlv_record_type {
        (($type:ty, $wrapper:ident)) => {
                $type
        };
-       ($type:ty) => {
+       (($type:ty)) => {
                $type
        };
 }
 
 macro_rules! tlv_record_ref_type {
-       (u8) => {
+       ((u8)) => {
                u8
        };
-       (char) => {
+       ((char)) => {
                char
        };
-       (($type:ty, HighZeroBytesDroppedBigSize)) => {
-               $type
-       };
+       ((u8, $wrapper: ident)) => { u8 };
+       ((u16, $wrapper: ident)) => { u16 };
+       ((u32, $wrapper: ident)) => { u32 };
+       ((u64, $wrapper: ident)) => { u64 };
        (($type:ty, $wrapper:ident)) => {
                &'a $type
        };
-       ($type:ty) => {
+       (($type:ty)) => {
                &'a $type
        };
 }
@@ -533,29 +535,11 @@ macro_rules! encoded_tlv_record_type {
        (($type:ty, $wrapper:ident)) => {
                $wrapper<$type>
        };
-       ($type:ty) => {
+       (($type:ty)) => {
                $type
        };
 }
 
-macro_rules! encoded_tlv_record_ref_type {
-       (u8) => {
-               u8
-       };
-       (char) => {
-               char
-       };
-       (($type:ty, HighZeroBytesDroppedBigSize)) => {
-               HighZeroBytesDroppedBigSize<$type>
-       };
-       (($type:ty, $wrapper:ident)) => {
-               $wrapper<&$type>
-       };
-       ($type:ty) => {
-               &$type
-       };
-}
-
 macro_rules! _impl_writeable_tlv_based_enum_common {
        ($st: ident, $(($variant_id: expr, $variant_name: ident) =>
                {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}