Skip to content

Commit 1c6ce8e

Browse files
committed
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.
1 parent 88e1b56 commit 1c6ce8e

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lightning/src/util/ser_macros.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
10011001
impl $crate::util::ser::Writeable for $st {
10021002
fn write<W: $crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
10031003
match self {
1004-
$($st::$variant_name { $(ref $field),* } => {
1004+
$($st::$variant_name { $(ref $field, )* .. } => {
10051005
let id: u8 = $variant_id;
10061006
id.write(writer)?;
10071007
$crate::write_tlv_fields!(writer, {
@@ -1099,10 +1099,12 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
10991099
{$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
11001100
),* $(,)*
11011101
$(;
1102-
$(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*)*) => {
1102+
$(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*)?
1103+
$(unread_variants: $($unread_variant: ident),*)?) => {
11031104
$crate::_impl_writeable_tlv_based_enum_common!($st,
1104-
$(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*;
1105-
$($(($tuple_variant_id, $tuple_variant_name)),*)*);
1105+
$(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*
1106+
$(, $((255, $unread_variant) => {}),*)?
1107+
; $($(($tuple_variant_id, $tuple_variant_name)),*)?);
11061108

11071109
impl $crate::util::ser::MaybeReadable for $st {
11081110
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 {
11261128
$($($tuple_variant_id => {
11271129
Ok(Some($st::$tuple_variant_name(Readable::read(reader)?)))
11281130
}),*)*
1129-
_ if id % 2 == 1 => {
1131+
// Note that we explicitly match 255 here to reserve it for use in
1132+
// `unread_variants`.
1133+
255|_ if id % 2 == 1 => {
11301134
// Assume that a $variant_id was written, not a $tuple_variant_id, and read
11311135
// the length prefix and discard the correct number of bytes.
11321136
let tlv_len: $crate::util::ser::BigSize = $crate::util::ser::Readable::read(reader)?;

0 commit comments

Comments
 (0)