]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add skipping variants to `impl_writeable_tlv_based_enum_upgradable`
authorMatt Corallo <git@bluematt.me>
Tue, 18 Jun 2024 17:24:21 +0000 (17:24 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 8 Jul 2024 19:06:59 +0000 (19:06 +0000)
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

index 255fd3ebc327899c98bc599a4a3213834179bb79..a45c502923003215a2996c5676beaf64c39774d1 100644 (file)
@@ -1001,7 +1001,7 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
                impl $crate::util::ser::Writeable for $st {
                        fn write<W: $crate::util::ser::Writer>(&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<R: $crate::io::Read>(reader: &mut R) -> Result<Option<Self>, $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)?;