X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fser_macros.rs;h=e8bd3326babfa3dc74f457e90ac84441c7c73aba;hb=eab9b3452657cef788eb54e3f46eda4fb06f9197;hp=b93115dcc95933bbc5a617bbf1dc5083f25cdd10;hpb=b9aa71f056a18d344c90f95dce151932d6d67cfa;p=rust-lightning diff --git a/lightning/src/util/ser_macros.rs b/lightning/src/util/ser_macros.rs index b93115dc..e8bd3326 100644 --- a/lightning/src/util/ser_macros.rs +++ b/lightning/src/util/ser_macros.rs @@ -8,6 +8,9 @@ // licenses. macro_rules! encode_tlv { + ($stream: expr, $type: expr, $field: expr, (default_value, $default: expr)) => { + encode_tlv!($stream, $type, $field, required) + }; ($stream: expr, $type: expr, $field: expr, required) => { BigSize($type).write($stream)?; BigSize($field.serialized_length() as u64).write($stream)?; @@ -26,7 +29,7 @@ macro_rules! encode_tlv { } macro_rules! encode_tlv_stream { - ($stream: expr, {$(($type: expr, $field: expr, $fieldty: ident)),*}) => { { + ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { { #[allow(unused_imports)] use { ln::msgs::DecodeError, @@ -53,6 +56,9 @@ macro_rules! encode_tlv_stream { } macro_rules! get_varint_length_prefixed_tlv_length { + ($len: expr, $type: expr, $field: expr, (default_value, $default: expr)) => { + get_varint_length_prefixed_tlv_length!($len, $type, $field, required) + }; ($len: expr, $type: expr, $field: expr, required) => { BigSize($type).write(&mut $len).expect("No in-memory data may fail to serialize"); let field_len = $field.serialized_length(); @@ -73,7 +79,7 @@ macro_rules! get_varint_length_prefixed_tlv_length { } macro_rules! encode_varint_length_prefixed_tlv { - ($stream: expr, {$(($type: expr, $field: expr, $fieldty: ident)),*}) => { { + ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}) => { { use util::ser::BigSize; let len = { #[allow(unused_mut)] @@ -89,53 +95,81 @@ macro_rules! encode_varint_length_prefixed_tlv { } macro_rules! check_tlv_order { - ($last_seen_type: expr, $typ: expr, $type: expr, required) => {{ + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{ + #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true + let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type; + if invalid_order { + $field = $default; + } + }}; + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required) => {{ #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type; if invalid_order { - Err(DecodeError::InvalidValue)? + return Err(DecodeError::InvalidValue); } }}; - ($last_seen_type: expr, $typ: expr, $type: expr, option) => {{ + ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{ // no-op }}; - ($last_seen_type: expr, $typ: expr, $type: expr, vec_type) => {{ + ($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 { - ($last_seen_type: expr, $type: expr, required) => {{ + ($last_seen_type: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{ + #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true + let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type; + if missing_req_type { + $field = $default; + } + }}; + ($last_seen_type: expr, $type: expr, $field: ident, required) => {{ #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type; if missing_req_type { - Err(DecodeError::InvalidValue)? + return Err(DecodeError::InvalidValue); } }}; - ($last_seen_type: expr, $type: expr, vec_type) => {{ + ($last_seen_type: expr, $type: expr, $field: ident, vec_type) => {{ + // no-op + }}; + ($last_seen_type: expr, $type: expr, $field: ident, option) => {{ // no-op }}; - ($last_seen_type: expr, $type: expr, option) => {{ + ($last_seen_type: expr, $type: expr, $field: ident, ignorable) => {{ // no-op }}; } macro_rules! decode_tlv { + ($reader: expr, $field: ident, (default_value, $default: expr)) => {{ + decode_tlv!($reader, $field, required) + }}; ($reader: expr, $field: ident, required) => {{ $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: ident)),* $(,)*}) => { { + ($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; @@ -145,16 +179,16 @@ 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 { - break 'tlv_read + break 'tlv_read; } else { - Err(DecodeError::ShortRead)? + return Err(DecodeError::ShortRead); } }, - Err(e) => Err(e)?, + Err(e) => return Err(e), Ok(t) => t, } }; @@ -162,29 +196,29 @@ macro_rules! decode_tlv_stream { // Types must be unique and monotonically increasing: match last_seen_type { Some(t) if typ.0 <= t => { - Err(DecodeError::InvalidValue)? + return Err(DecodeError::InvalidValue); }, _ => {}, } // As we read types, make sure we hit every required type: $({ - check_tlv_order!(last_seen_type, typ, $type, $fieldty); + check_tlv_order!(last_seen_type, typ, $type, $field, $fieldty); })* last_seen_type = Some(typ.0); // Finally, read the length and value itself: - let length: ser::BigSize = 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); if s.bytes_remain() { s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes - Err(DecodeError::InvalidValue)? + return Err(DecodeError::InvalidValue); } },)* x if x % 2 == 0 => { - Err(DecodeError::UnknownRequiredFeature)? + return Err(DecodeError::UnknownRequiredFeature); }, _ => {}, } @@ -192,7 +226,7 @@ macro_rules! decode_tlv_stream { } // Make sure we got to each required type after we've read every TLV: $({ - check_missing_tlv!(last_seen_type, $type, $fieldty); + check_missing_tlv!(last_seen_type, $type, $field, $fieldty); })* } } } @@ -200,7 +234,7 @@ macro_rules! decode_tlv_stream { macro_rules! impl_writeable { ($st:ident, $len: expr, {$($field:ident),*}) => { impl ::util::ser::Writeable for $st { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { if $len != 0 { w.size_hint($len); } @@ -235,7 +269,7 @@ macro_rules! impl_writeable { } impl ::util::ser::Readable for $st { - fn read(r: &mut R) -> Result { + fn read(r: &mut R) -> Result { Ok(Self { $($field: ::util::ser::Readable::read(r)?),* }) @@ -246,7 +280,7 @@ macro_rules! impl_writeable { macro_rules! impl_writeable_len_match { ($struct: ident, $cmp: tt, ($calc_len: expr), {$({$match: pat, $length: expr}),*}, {$($field:ident),*}) => { impl Writeable for $struct { - fn write(&self, w: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, w: &mut W) -> Result<(), $crate::io::Error> { let len = match *self { $($match => $length,)* }; @@ -282,7 +316,7 @@ macro_rules! impl_writeable_len_match { } impl ::util::ser::Readable for $struct { - fn read(r: &mut R) -> Result { + fn read(r: &mut R) -> Result { Ok(Self { $($field: Readable::read(r)?),* }) @@ -326,7 +360,7 @@ macro_rules! write_ver_prefix { /// This is the preferred method of adding new fields that old nodes can ignore and still function /// correctly. macro_rules! write_tlv_fields { - ($stream: expr, {$(($type: expr, $field: expr, $fieldty: ident)),* $(,)*}) => { + ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty)),*}); } } @@ -347,8 +381,8 @@ macro_rules! read_ver_prefix { /// Reads a suffix added by write_tlv_fields. macro_rules! read_tlv_fields { - ($stream: expr, {$(($type: expr, $field: ident, $fieldty: ident)),* $(,)*}) => { { - let tlv_len = ::util::ser::BigSize::read($stream)?; + ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { { + let tlv_len: ::util::ser::BigSize = ::util::ser::Readable::read($stream)?; let mut rd = ::util::ser::FixedLengthReader::new($stream, tlv_len.0); decode_tlv_stream!(&mut rd, {$(($type, $field, $fieldty)),*}); rd.eat_remaining().map_err(|_| ::ln::msgs::DecodeError::ShortRead)?; @@ -356,6 +390,9 @@ macro_rules! read_tlv_fields { } macro_rules! init_tlv_based_struct_field { + ($field: ident, (default_value, $default: expr)) => { + $field + }; ($field: ident, option) => { $field }; @@ -363,20 +400,23 @@ macro_rules! init_tlv_based_struct_field { $field.0.unwrap() }; ($field: ident, vec_type) => { - $field.unwrap().0 + $field.unwrap() }; } macro_rules! init_tlv_field_var { + ($field: ident, (default_value, $default: expr)) => { + let mut $field = $default; + }; ($field: ident, required) => { 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 @@ -385,9 +425,9 @@ macro_rules! init_tlv_field_var { /// if $fieldty is `vec_type`, then $field is a Vec, which needs to have its individual elements /// serialized. macro_rules! impl_writeable_tlv_based { - ($st: ident, {$(($type: expr, $field: ident, $fieldty: ident)),* $(,)*}) => { + ($st: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { impl ::util::ser::Writeable for $st { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { write_tlv_fields!(writer, { $(($type, self.$field, $fieldty)),* }); @@ -412,7 +452,7 @@ macro_rules! impl_writeable_tlv_based { } impl ::util::ser::Readable for $st { - fn read(reader: &mut R) -> Result { + fn read(reader: &mut R) -> Result { $( init_tlv_field_var!($field, $fieldty); )* @@ -429,23 +469,13 @@ 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: ident)),* $(,)*} + {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*} ),* $(,)*; $(($tuple_variant_id: expr, $tuple_variant_name: ident)),* $(,)*) => { impl ::util::ser::Writeable for $st { - fn write(&self, writer: &mut W) -> Result<(), ::std::io::Error> { + fn write(&self, writer: &mut W) -> Result<(), $crate::io::Error> { match self { $($st::$variant_name { $(ref $field),* } => { let id: u8 = $variant_id; @@ -463,9 +493,75 @@ 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 { + fn read(reader: &mut R) -> Result { let id: u8 = ::util::ser::Readable::read(reader)?; match id { $($variant_id => { @@ -490,7 +586,7 @@ macro_rules! impl_writeable_tlv_based_enum { Ok($st::$tuple_variant_name(Readable::read(reader)?)) }),* _ => { - Err(DecodeError::UnknownRequiredFeature)? + Err(DecodeError::UnknownRequiredFeature) }, } } @@ -500,10 +596,10 @@ macro_rules! impl_writeable_tlv_based_enum { #[cfg(test)] mod tests { + use io::{self, Cursor}; use prelude::*; - use std::io::Cursor; use ln::msgs::DecodeError; - use util::ser::{Readable, Writeable, HighZeroBytesDroppedVarInt, VecWriter}; + use util::ser::{Writeable, HighZeroBytesDroppedVarInt, VecWriter}; use bitcoin::secp256k1::PublicKey; // The BOLT TLV test cases don't include any tests which use our "required-value" logic since @@ -685,7 +781,7 @@ mod tests { do_test!(concat!("fd00fe", "02", "0226"), None, None, None, Some(550)); } - fn do_simple_test_tlv_write() -> Result<(), ::std::io::Error> { + fn do_simple_test_tlv_write() -> Result<(), io::Error> { let mut stream = VecWriter(Vec::new()); stream.0.clear();