From 1c6ce8e78913ed1dbd0e4328a6df953b6de60f50 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 18 Jun 2024 17:24:21 +0000 Subject: [PATCH] Add skipping variants to `impl_writeable_tlv_based_enum_upgradable` In some cases, we have variants of an enum serialized using `impl_writeable_tlv_based_enum_upgradable` which we don't want to write/read. Here we add support for such variants by writing them using the (odd) type 255 without any contents and using `MaybeReadable` to decline to read them. --- lightning/src/util/ser_macros.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index 255fd3ebc..a45c50292 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -1001,7 +1001,7 @@ macro_rules! _impl_writeable_tlv_based_enum_common { impl $crate::util::ser::Writeable for $st { fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { match self { - $($st::$variant_name { $(ref $field),* } => { + $($st::$variant_name { $(ref $field, )* .. } => { let id: u8 = $variant_id; id.write(writer)?; $crate::write_tlv_fields!(writer, { @@ -1099,10 +1099,12 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable { {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} ),* $(,)* $(; - $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*)*) => { + $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*)? + $(unread_variants: $($unread_variant: ident),*)?) => { $crate::_impl_writeable_tlv_based_enum_common!($st, - $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*; - $($(($tuple_variant_id, $tuple_variant_name)),*)*); + $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),* + $(, $((255, $unread_variant) => {}),*)? + ; $($(($tuple_variant_id, $tuple_variant_name)),*)?); impl $crate::util::ser::MaybeReadable for $st { fn read(reader: &mut R) -> Result, $crate::ln::msgs::DecodeError> { @@ -1126,7 +1128,9 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable { $($($tuple_variant_id => { Ok(Some($st::$tuple_variant_name(Readable::read(reader)?))) }),*)* - _ if id % 2 == 1 => { + // Note that we explicitly match 255 here to reserve it for use in + // `unread_variants`. + 255|_ if id % 2 == 1 => { // Assume that a $variant_id was written, not a $tuple_variant_id, and read // the length prefix and discard the correct number of bytes. let tlv_len: $crate::util::ser::BigSize = $crate::util::ser::Readable::read(reader)?; -- 2.39.5