X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fser_macros.rs;h=e8bd3326babfa3dc74f457e90ac84441c7c73aba;hb=eab9b3452657cef788eb54e3f46eda4fb06f9197;hp=e988ad7eeab940d0e6979904c63dcd88087f023e;hpb=45490d537edb422c945e8bf20fec21b1878863de;p=rust-lightning diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index e988ad7e..e8bd3326 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -115,6 +115,9 @@ macro_rules! check_tlv_order { ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, vec_type) => {{ // no-op }}; + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ignorable) => {{ + // no-op + }}; } macro_rules! check_missing_tlv { @@ -138,6 +141,9 @@ macro_rules! check_missing_tlv { ($last_seen_type: expr, $type: expr, $field: ident, option) => {{ // no-op }}; + ($last_seen_type: expr, $type: expr, $field: ident, ignorable) => {{ + // no-op + }}; } macro_rules! decode_tlv { @@ -148,17 +154,22 @@ macro_rules! decode_tlv { $field = ser::Readable::read(&mut $reader)?; }}; ($reader: expr, $field: ident, vec_type) => {{ - $field = Some(ser::Readable::read(&mut $reader)?); + let f: ::util::ser::VecReadWrapper<_> = ser::Readable::read(&mut $reader)?; + $field = Some(f.0); }}; ($reader: expr, $field: ident, option) => {{ $field = Some(ser::Readable::read(&mut $reader)?); }}; + ($reader: expr, $field: ident, ignorable) => {{ + $field = ser::MaybeReadable::read(&mut $reader)?; + }}; } macro_rules! decode_tlv_stream { ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { { use ln::msgs::DecodeError; let mut last_seen_type: Option = None; + let mut stream_ref = $stream; 'tlv_read: loop { use util::ser; @@ -168,7 +179,7 @@ macro_rules! decode_tlv_stream { // determine whether we should break or return ShortRead if we get an // UnexpectedEof. This should in every case be largely cosmetic, but its nice to // pass the TLV test vectors exactly, which requre this distinction. - let mut tracking_reader = ser::ReadTrackingReader::new($stream); + let mut tracking_reader = ser::ReadTrackingReader::new(&mut stream_ref); match ser::Readable::read(&mut tracking_reader) { Err(DecodeError::ShortRead) => { if !tracking_reader.have_read { @@ -196,8 +207,8 @@ macro_rules! decode_tlv_stream { last_seen_type = Some(typ.0); // Finally, read the length and value itself: - let length: ser::BigSize = ser::Readable::read($stream)?; - let mut s = ser::FixedLengthReader::new($stream, length.0); + let length: ser::BigSize = ser::Readable::read(&mut stream_ref)?; + let mut s = ser::FixedLengthReader::new(&mut stream_ref, length.0); match typ.0 { $($type => { decode_tlv!(s, $field, $fieldty); @@ -389,7 +400,7 @@ macro_rules! init_tlv_based_struct_field { $field.0.unwrap() }; ($field: ident, vec_type) => { - $field.unwrap().0 + $field.unwrap() }; } @@ -401,11 +412,11 @@ macro_rules! init_tlv_field_var { let mut $field = ::util::ser::OptionDeserWrapper(None); }; ($field: ident, vec_type) => { - let mut $field = Some(::util::ser::VecReadWrapper(Vec::new())); + let mut $field = Some(Vec::new()); }; ($field: ident, option) => { let mut $field = None; - } + }; } /// Implements Readable/Writeable for a struct storing it as a set of TLVs @@ -458,17 +469,7 @@ macro_rules! impl_writeable_tlv_based { } } -/// Implement Readable and Writeable for an enum, with struct variants stored as TLVs and tuple -/// variants stored directly. -/// The format is, for example -/// impl_writeable_tlv_based_enum!(EnumName, -/// (0, StructVariantA) => {(0, required_variant_field, required), (1, optional_variant_field, option)}, -/// (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, vec_type)}; -/// (2, TupleVariantA), (3, TupleVariantB), -/// ); -/// The type is written as a single byte, followed by any variant data. -/// Attempts to read an unknown type byte result in DecodeError::UnknownRequiredFeature. -macro_rules! impl_writeable_tlv_based_enum { +macro_rules! _impl_writeable_tlv_based_enum_common { ($st: ident, $(($variant_id: expr, $variant_name: ident) => {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} ),* $(,)*; @@ -492,6 +493,72 @@ macro_rules! impl_writeable_tlv_based_enum { Ok(()) } } + } +} + +/// Implement MaybeReadable and Writeable for an enum, with struct variants stored as TLVs and +/// tuple variants stored directly. +/// +/// This is largely identical to `impl_writeable_tlv_based_enum`, except that odd variants will +/// return `Ok(None)` instead of `Err(UnknownRequiredFeature)`. It should generally be preferred +/// when `MaybeReadable` is practical instead of just `Readable` as it provides an upgrade path for +/// new variants to be added which are simply ignored by existing clients. +macro_rules! impl_writeable_tlv_based_enum_upgradable { + ($st: ident, $(($variant_id: expr, $variant_name: ident) => + {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} + ),* $(,)*) => { + _impl_writeable_tlv_based_enum_common!($st, + $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*; ); + + impl ::util::ser::MaybeReadable for $st { + fn read(reader: &mut R) -> Result, ::ln::msgs::DecodeError> { + let id: u8 = ::util::ser::Readable::read(reader)?; + match id { + $($variant_id => { + // Because read_tlv_fields creates a labeled loop, we cannot call it twice + // in the same function body. Instead, we define a closure and call it. + let f = || { + $( + init_tlv_field_var!($field, $fieldty); + )* + read_tlv_fields!(reader, { + $(($type, $field, $fieldty)),* + }); + Ok(Some($st::$variant_name { + $( + $field: init_tlv_based_struct_field!($field, $fieldty) + ),* + })) + }; + f() + }),* + _ if id % 2 == 1 => Ok(None), + _ => Err(DecodeError::UnknownRequiredFeature), + } + } + } + + } +} + +/// Implement Readable and Writeable for an enum, with struct variants stored as TLVs and tuple +/// variants stored directly. +/// The format is, for example +/// impl_writeable_tlv_based_enum!(EnumName, +/// (0, StructVariantA) => {(0, required_variant_field, required), (1, optional_variant_field, option)}, +/// (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, vec_type)}; +/// (2, TupleVariantA), (3, TupleVariantB), +/// ); +/// The type is written as a single byte, followed by any variant data. +/// Attempts to read an unknown type byte result in DecodeError::UnknownRequiredFeature. +macro_rules! impl_writeable_tlv_based_enum { + ($st: ident, $(($variant_id: expr, $variant_name: ident) => + {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} + ),* $(,)*; + $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*) => { + _impl_writeable_tlv_based_enum_common!($st, + $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*; + $(($tuple_variant_id, $tuple_variant_name)),*); impl ::util::ser::Readable for $st { fn read(reader: &mut R) -> Result {