From: Jeffrey Czyz Date: Wed, 28 Sep 2022 16:01:35 +0000 (-0500) Subject: f - explicit encodings X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=3b5bb9636cfb2c148b61411986bcc18c67a61078;p=rust-lightning f - explicit encodings --- diff --git a/lightning/src/offers/offer.rs b/lightning/src/offers/offer.rs index de40623c5..a5a0e6f5c 100644 --- a/lightning/src/offers/offer.rs +++ b/lightning/src/offers/offer.rs @@ -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), - (4, metadata: Vec), + (2, chains: (Vec, WithoutLength)), + (4, metadata: (Vec, 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), - (18, issuer: String), - (20, quantity_min: u64), - (22, quantity_max: u64), + (14, absolute_expiry: (u64, HighZeroBytesDroppedBigSize)), + (16, paths: (Vec, WithoutLength)), + (18, issuer: (String, WithoutLength)), + (20, quantity_min: (u64, HighZeroBytesDroppedBigSize)), + (22, quantity_max: (u64, HighZeroBytesDroppedBigSize)), (24, node_id: PublicKey), }); diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 2f16f145e..f67998410 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -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, )* } pub(crate) struct $nameref<'a> { $( - pub(crate) $field: Option)?)>, + pub(crate) $field: Option, )* } impl<'a> ::util::ser::Writeable for $nameref<'a> { fn write(&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 + (($type:ty, $wrapper:ident)) => { + $wrapper<$type> }; - (u32) => { - ::util::ser::HighZeroBytesDroppedBigSize - }; - (u64) => { - ::util::ser::HighZeroBytesDroppedBigSize - }; - (char) => { - char - }; - (String) => { - ::util::ser::WithoutLength - }; - (Vec<$type:ty>) => { - ::util::ser::WithoutLength> - }; - ($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 - }; - (u32) => { - ::util::ser::HighZeroBytesDroppedBigSize - }; - (u64) => { - ::util::ser::HighZeroBytesDroppedBigSize - }; (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 }; }