f67998410c9f5058bf0307507027782e2593039a
[rust-lightning] / lightning / src / util / ser_macros.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 macro_rules! encode_tlv {
11         ($stream: expr, $type: expr, $field: expr, (default_value, $default: expr)) => {
12                 encode_tlv!($stream, $type, $field, required)
13         };
14         ($stream: expr, $type: expr, $field: expr, required) => {
15                 BigSize($type).write($stream)?;
16                 BigSize($field.serialized_length() as u64).write($stream)?;
17                 $field.write($stream)?;
18         };
19         ($stream: expr, $type: expr, $field: expr, vec_type) => {
20                 encode_tlv!($stream, $type, ::util::ser::VecWriteWrapper(&$field), required);
21         };
22         ($stream: expr, $optional_type: expr, $optional_field: expr, option) => {
23                 if let Some(ref field) = $optional_field {
24                         BigSize($optional_type).write($stream)?;
25                         BigSize(field.serialized_length() as u64).write($stream)?;
26                         field.write($stream)?;
27                 }
28         };
29         ($stream: expr, $type: expr, $field: expr, (tlv_record, $fieldty:tt)) => {
30                 if let Some(field) = $field {
31                         let field: encoded_tlv_record_ref_type!($fieldty) = From::from(field);
32                         BigSize($type).write($stream)?;
33                         BigSize(field.serialized_length() as u64).write($stream)?;
34                         field.write($stream)?;
35                 }
36         };
37 }
38
39 macro_rules! encode_tlv_stream {
40         ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { {
41                 #[allow(unused_imports)]
42                 use {
43                         ln::msgs::DecodeError,
44                         util::ser,
45                         util::ser::BigSize,
46                 };
47
48                 $(
49                         encode_tlv!($stream, $type, $field, $fieldty);
50                 )*
51
52                 #[allow(unused_mut, unused_variables, unused_assignments)]
53                 #[cfg(debug_assertions)]
54                 {
55                         let mut last_seen: Option<u64> = None;
56                         $(
57                                 if let Some(t) = last_seen {
58                                         debug_assert!(t <= $type);
59                                 }
60                                 last_seen = Some($type);
61                         )*
62                 }
63         } }
64 }
65
66 macro_rules! get_varint_length_prefixed_tlv_length {
67         ($len: expr, $type: expr, $field: expr, (default_value, $default: expr)) => {
68                 get_varint_length_prefixed_tlv_length!($len, $type, $field, required)
69         };
70         ($len: expr, $type: expr, $field: expr, required) => {
71                 BigSize($type).write(&mut $len).expect("No in-memory data may fail to serialize");
72                 let field_len = $field.serialized_length();
73                 BigSize(field_len as u64).write(&mut $len).expect("No in-memory data may fail to serialize");
74                 $len.0 += field_len;
75         };
76         ($len: expr, $type: expr, $field: expr, vec_type) => {
77                 get_varint_length_prefixed_tlv_length!($len, $type, ::util::ser::VecWriteWrapper(&$field), required);
78         };
79         ($len: expr, $optional_type: expr, $optional_field: expr, option) => {
80                 if let Some(ref field) = $optional_field {
81                         BigSize($optional_type).write(&mut $len).expect("No in-memory data may fail to serialize");
82                         let field_len = field.serialized_length();
83                         BigSize(field_len as u64).write(&mut $len).expect("No in-memory data may fail to serialize");
84                         $len.0 += field_len;
85                 }
86         };
87 }
88
89 macro_rules! encode_varint_length_prefixed_tlv {
90         ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}) => { {
91                 use util::ser::BigSize;
92                 let len = {
93                         #[allow(unused_mut)]
94                         let mut len = ::util::ser::LengthCalculatingWriter(0);
95                         $(
96                                 get_varint_length_prefixed_tlv_length!(len, $type, $field, $fieldty);
97                         )*
98                         len.0
99                 };
100                 BigSize(len as u64).write($stream)?;
101                 encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* });
102         } }
103 }
104
105 macro_rules! check_tlv_order {
106         ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{
107                 #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true
108                 let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type;
109                 if invalid_order {
110                         $field = $default.into();
111                 }
112         }};
113         ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required) => {{
114                 #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true
115                 let invalid_order = ($last_seen_type.is_none() || $last_seen_type.unwrap() < $type) && $typ.0 > $type;
116                 if invalid_order {
117                         return Err(DecodeError::InvalidValue);
118                 }
119         }};
120         ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{
121                 // no-op
122         }};
123         ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, vec_type) => {{
124                 // no-op
125         }};
126         ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, ignorable) => {{
127                 // no-op
128         }};
129         ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
130                 // no-op
131         }};
132         ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (tlv_record, $fieldty:tt)) => {{
133                 // no-op
134         }};
135 }
136
137 macro_rules! check_missing_tlv {
138         ($last_seen_type: expr, $type: expr, $field: ident, (default_value, $default: expr)) => {{
139                 #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true
140                 let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type;
141                 if missing_req_type {
142                         $field = $default.into();
143                 }
144         }};
145         ($last_seen_type: expr, $type: expr, $field: ident, required) => {{
146                 #[allow(unused_comparisons)] // Note that $type may be 0 making the second comparison always true
147                 let missing_req_type = $last_seen_type.is_none() || $last_seen_type.unwrap() < $type;
148                 if missing_req_type {
149                         return Err(DecodeError::InvalidValue);
150                 }
151         }};
152         ($last_seen_type: expr, $type: expr, $field: ident, vec_type) => {{
153                 // no-op
154         }};
155         ($last_seen_type: expr, $type: expr, $field: ident, option) => {{
156                 // no-op
157         }};
158         ($last_seen_type: expr, $type: expr, $field: ident, ignorable) => {{
159                 // no-op
160         }};
161         ($last_seen_type: expr, $type: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
162                 // no-op
163         }};
164         ($last_seen_type: expr, $type: expr, $field: ident, (tlv_record, $fieldty:tt)) => {{
165                 // no-op
166         }};
167 }
168
169 macro_rules! decode_tlv {
170         ($reader: expr, $field: ident, (default_value, $default: expr)) => {{
171                 decode_tlv!($reader, $field, required)
172         }};
173         ($reader: expr, $field: ident, required) => {{
174                 $field = ser::Readable::read(&mut $reader)?;
175         }};
176         ($reader: expr, $field: ident, vec_type) => {{
177                 let f: ::util::ser::VecReadWrapper<_> = ser::Readable::read(&mut $reader)?;
178                 $field = Some(f.0);
179         }};
180         ($reader: expr, $field: ident, option) => {{
181                 $field = Some(ser::Readable::read(&mut $reader)?);
182         }};
183         ($reader: expr, $field: ident, ignorable) => {{
184                 $field = ser::MaybeReadable::read(&mut $reader)?;
185         }};
186         ($reader: expr, $field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {{
187                 $field = Some($trait::read(&mut $reader $(, $read_arg)*)?);
188         }};
189         ($reader: expr, $field: ident, (tlv_record, $fieldty:tt)) => {{
190                 $field = {
191                         let field: encoded_tlv_record_type!($fieldty) = ser::Readable::read(&mut $reader)?;
192                         Some(field.into())
193                 };
194         }};
195 }
196
197 macro_rules! decode_tlv_stream {
198         ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { {
199                 use ln::msgs::DecodeError;
200                 let mut last_seen_type: Option<u64> = None;
201                 let mut stream_ref = $stream;
202                 'tlv_read: loop {
203                         use util::ser;
204
205                         // First decode the type of this TLV:
206                         let typ: ser::BigSize = {
207                                 // We track whether any bytes were read during the consensus_decode call to
208                                 // determine whether we should break or return ShortRead if we get an
209                                 // UnexpectedEof. This should in every case be largely cosmetic, but its nice to
210                                 // pass the TLV test vectors exactly, which requre this distinction.
211                                 let mut tracking_reader = ser::ReadTrackingReader::new(&mut stream_ref);
212                                 match ser::Readable::read(&mut tracking_reader) {
213                                         Err(DecodeError::ShortRead) => {
214                                                 if !tracking_reader.have_read {
215                                                         break 'tlv_read;
216                                                 } else {
217                                                         return Err(DecodeError::ShortRead);
218                                                 }
219                                         },
220                                         Err(e) => return Err(e),
221                                         Ok(t) => t,
222                                 }
223                         };
224
225                         // Types must be unique and monotonically increasing:
226                         match last_seen_type {
227                                 Some(t) if typ.0 <= t => {
228                                         return Err(DecodeError::InvalidValue);
229                                 },
230                                 _ => {},
231                         }
232                         // As we read types, make sure we hit every required type:
233                         $({
234                                 check_tlv_order!(last_seen_type, typ, $type, $field, $fieldty);
235                         })*
236                         last_seen_type = Some(typ.0);
237
238                         // Finally, read the length and value itself:
239                         let length: ser::BigSize = ser::Readable::read(&mut stream_ref)?;
240                         let mut s = ser::FixedLengthReader::new(&mut stream_ref, length.0);
241                         match typ.0 {
242                                 $($type => {
243                                         decode_tlv!(s, $field, $fieldty);
244                                         if s.bytes_remain() {
245                                                 s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
246                                                 return Err(DecodeError::InvalidValue);
247                                         }
248                                 },)*
249                                 x if x % 2 == 0 => {
250                                         return Err(DecodeError::UnknownRequiredFeature);
251                                 },
252                                 _ => {},
253                         }
254                         s.eat_remaining()?;
255                 }
256                 // Make sure we got to each required type after we've read every TLV:
257                 $({
258                         check_missing_tlv!(last_seen_type, $type, $field, $fieldty);
259                 })*
260         } }
261 }
262
263 macro_rules! impl_writeable_msg {
264         ($st:ident, {$($field:ident),* $(,)*}, {$(($type: expr, $tlvfield: ident, $fieldty: tt)),* $(,)*}) => {
265                 impl ::util::ser::Writeable for $st {
266                         fn write<W: ::util::ser::Writer>(&self, w: &mut W) -> Result<(), $crate::io::Error> {
267                                 $( self.$field.write(w)?; )*
268                                 encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*});
269                                 Ok(())
270                         }
271                 }
272                 impl ::util::ser::Readable for $st {
273                         fn read<R: $crate::io::Read>(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
274                                 $(let $field = ::util::ser::Readable::read(r)?;)*
275                                 $(init_tlv_field_var!($tlvfield, $fieldty);)*
276                                 decode_tlv_stream!(r, {$(($type, $tlvfield, $fieldty)),*});
277                                 Ok(Self {
278                                         $($field),*,
279                                         $($tlvfield),*
280                                 })
281                         }
282                 }
283         }
284 }
285
286 macro_rules! impl_writeable {
287         ($st:ident, {$($field:ident),*}) => {
288                 impl ::util::ser::Writeable for $st {
289                         fn write<W: ::util::ser::Writer>(&self, w: &mut W) -> Result<(), $crate::io::Error> {
290                                 $( self.$field.write(w)?; )*
291                                 Ok(())
292                         }
293
294                         #[inline]
295                         fn serialized_length(&self) -> usize {
296                                 let mut len_calc = 0;
297                                 $( len_calc += self.$field.serialized_length(); )*
298                                 return len_calc;
299                         }
300                 }
301
302                 impl ::util::ser::Readable for $st {
303                         fn read<R: $crate::io::Read>(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
304                                 Ok(Self {
305                                         $($field: ::util::ser::Readable::read(r)?),*
306                                 })
307                         }
308                 }
309         }
310 }
311
312 /// Write out two bytes to indicate the version of an object.
313 /// $this_version represents a unique version of a type. Incremented whenever the type's
314 ///               serialization format has changed or has a new interpretation. Used by a type's
315 ///               reader to determine how to interpret fields or if it can understand a serialized
316 ///               object.
317 /// $min_version_that_can_read_this is the minimum reader version which can understand this
318 ///                                 serialized object. Previous versions will simply err with a
319 ///                                 DecodeError::UnknownVersion.
320 ///
321 /// Updates to either $this_version or $min_version_that_can_read_this should be included in
322 /// release notes.
323 ///
324 /// Both version fields can be specific to this type of object.
325 macro_rules! write_ver_prefix {
326         ($stream: expr, $this_version: expr, $min_version_that_can_read_this: expr) => {
327                 $stream.write_all(&[$this_version; 1])?;
328                 $stream.write_all(&[$min_version_that_can_read_this; 1])?;
329         }
330 }
331
332 /// Writes out a suffix to an object which contains potentially backwards-compatible, optional
333 /// fields which old nodes can happily ignore.
334 ///
335 /// It is written out in TLV format and, as with all TLV fields, unknown even fields cause a
336 /// DecodeError::UnknownRequiredFeature error, with unknown odd fields ignored.
337 ///
338 /// This is the preferred method of adding new fields that old nodes can ignore and still function
339 /// correctly.
340 macro_rules! write_tlv_fields {
341         ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => {
342                 encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty)),*})
343         }
344 }
345
346 /// Reads a prefix added by write_ver_prefix!(), above. Takes the current version of the
347 /// serialization logic for this object. This is compared against the
348 /// $min_version_that_can_read_this added by write_ver_prefix!().
349 macro_rules! read_ver_prefix {
350         ($stream: expr, $this_version: expr) => { {
351                 let ver: u8 = Readable::read($stream)?;
352                 let min_ver: u8 = Readable::read($stream)?;
353                 if min_ver > $this_version {
354                         return Err(DecodeError::UnknownVersion);
355                 }
356                 ver
357         } }
358 }
359
360 /// Reads a suffix added by write_tlv_fields.
361 macro_rules! read_tlv_fields {
362         ($stream: expr, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => { {
363                 let tlv_len: ::util::ser::BigSize = ::util::ser::Readable::read($stream)?;
364                 let mut rd = ::util::ser::FixedLengthReader::new($stream, tlv_len.0);
365                 decode_tlv_stream!(&mut rd, {$(($type, $field, $fieldty)),*});
366                 rd.eat_remaining().map_err(|_| ::ln::msgs::DecodeError::ShortRead)?;
367         } }
368 }
369
370 macro_rules! init_tlv_based_struct_field {
371         ($field: ident, (default_value, $default: expr)) => {
372                 $field.0.unwrap()
373         };
374         ($field: ident, option) => {
375                 $field
376         };
377         ($field: ident, required) => {
378                 $field.0.unwrap()
379         };
380         ($field: ident, vec_type) => {
381                 $field.unwrap()
382         };
383 }
384
385 macro_rules! init_tlv_field_var {
386         ($field: ident, (default_value, $default: expr)) => {
387                 let mut $field = ::util::ser::OptionDeserWrapper(None);
388         };
389         ($field: ident, required) => {
390                 let mut $field = ::util::ser::OptionDeserWrapper(None);
391         };
392         ($field: ident, vec_type) => {
393                 let mut $field = Some(Vec::new());
394         };
395         ($field: ident, option) => {
396                 let mut $field = None;
397         };
398         ($field: ident, tlv_record) => {
399                 let mut $field = None;
400         };
401 }
402
403 /// Implements Readable/Writeable for a struct storing it as a set of TLVs
404 /// If $fieldty is `required`, then $field is a required field that is not an Option nor a Vec.
405 /// If $fieldty is `option`, then $field is optional field.
406 /// if $fieldty is `vec_type`, then $field is a Vec, which needs to have its individual elements
407 /// serialized.
408 macro_rules! impl_writeable_tlv_based {
409         ($st: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
410                 impl ::util::ser::Writeable for $st {
411                         fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
412                                 write_tlv_fields!(writer, {
413                                         $(($type, self.$field, $fieldty)),*
414                                 });
415                                 Ok(())
416                         }
417
418                         #[inline]
419                         fn serialized_length(&self) -> usize {
420                                 use util::ser::BigSize;
421                                 let len = {
422                                         #[allow(unused_mut)]
423                                         let mut len = ::util::ser::LengthCalculatingWriter(0);
424                                         $(
425                                                 get_varint_length_prefixed_tlv_length!(len, $type, self.$field, $fieldty);
426                                         )*
427                                         len.0
428                                 };
429                                 let mut len_calc = ::util::ser::LengthCalculatingWriter(0);
430                                 BigSize(len as u64).write(&mut len_calc).expect("No in-memory data may fail to serialize");
431                                 len + len_calc.0
432                         }
433                 }
434
435                 impl ::util::ser::Readable for $st {
436                         fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
437                                 $(
438                                         init_tlv_field_var!($field, $fieldty);
439                                 )*
440                                 read_tlv_fields!(reader, {
441                                         $(($type, $field, $fieldty)),*
442                                 });
443                                 Ok(Self {
444                                         $(
445                                                 $field: init_tlv_based_struct_field!($field, $fieldty)
446                                         ),*
447                                 })
448                         }
449                 }
450         }
451 }
452
453 /// Defines a struct for a TLV stream and a similar struct using references for non-primitive types,
454 /// implementing [`Readable`] for the former and [`Writeable`] for the latter. Useful as an
455 /// intermediary format when reading or writing a type encoded as a TLV stream. Note that each field
456 /// representing a TLV record has its type wrapped with an [`Option`].
457 ///
458 /// [`Readable`]: crate::util::ser::Readable
459 /// [`Writeable`]: crate::util::ser::Writeable
460 macro_rules! tlv_stream {
461         ($name:ident, $nameref:ident, {
462                 $(($type:expr, $field:ident : $fieldty:tt)),* $(,)*
463         }) => {
464                 #[derive(Debug)]
465                 struct $name {
466                         $(
467                                 $field: Option<tlv_record_type!($fieldty)>,
468                         )*
469                 }
470
471                 pub(crate) struct $nameref<'a> {
472                         $(
473                                 pub(crate) $field: Option<tlv_record_ref_type!($fieldty)>,
474                         )*
475                 }
476
477                 impl<'a> ::util::ser::Writeable for $nameref<'a> {
478                         fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
479                                 encode_tlv_stream!(writer, {
480                                         $(($type, self.$field, (tlv_record, $fieldty))),*
481                                 });
482                                 Ok(())
483                         }
484                 }
485
486                 impl ::util::ser::Readable for $name {
487                         fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
488                                 $(
489                                         init_tlv_field_var!($field, tlv_record);
490                                 )*
491                                 decode_tlv_stream!(reader, {
492                                         $(($type, $field, (tlv_record, $fieldty))),*
493                                 });
494
495                                 Ok(Self {
496                                         $(
497                                                 $field: $field
498                                         ),*
499                                 })
500                         }
501                 }
502         }
503 }
504
505 macro_rules! tlv_record_type {
506         (($type:ty, $wrapper:ident)) => {
507                 $type
508         };
509         ($type:ty) => {
510                 $type
511         };
512 }
513
514 macro_rules! tlv_record_ref_type {
515         (u8) => {
516                 u8
517         };
518         (char) => {
519                 char
520         };
521         (($type:ty, HighZeroBytesDroppedBigSize)) => {
522                 $type
523         };
524         (($type:ty, $wrapper:ident)) => {
525                 &'a $type
526         };
527         ($type:ty) => {
528                 &'a $type
529         };
530 }
531
532 macro_rules! encoded_tlv_record_type {
533         (($type:ty, $wrapper:ident)) => {
534                 $wrapper<$type>
535         };
536         ($type:ty) => {
537                 $type
538         };
539 }
540
541 macro_rules! encoded_tlv_record_ref_type {
542         (u8) => {
543                 u8
544         };
545         (char) => {
546                 char
547         };
548         (($type:ty, HighZeroBytesDroppedBigSize)) => {
549                 HighZeroBytesDroppedBigSize<$type>
550         };
551         (($type:ty, $wrapper:ident)) => {
552                 $wrapper<&$type>
553         };
554         ($type:ty) => {
555                 &$type
556         };
557 }
558
559 macro_rules! _impl_writeable_tlv_based_enum_common {
560         ($st: ident, $(($variant_id: expr, $variant_name: ident) =>
561                 {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
562         ),* $(,)*;
563         $(($tuple_variant_id: expr, $tuple_variant_name: ident)),*  $(,)*) => {
564                 impl ::util::ser::Writeable for $st {
565                         fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), $crate::io::Error> {
566                                 match self {
567                                         $($st::$variant_name { $(ref $field),* } => {
568                                                 let id: u8 = $variant_id;
569                                                 id.write(writer)?;
570                                                 write_tlv_fields!(writer, {
571                                                         $(($type, $field, $fieldty)),*
572                                                 });
573                                         }),*
574                                         $($st::$tuple_variant_name (ref field) => {
575                                                 let id: u8 = $tuple_variant_id;
576                                                 id.write(writer)?;
577                                                 field.write(writer)?;
578                                         }),*
579                                 }
580                                 Ok(())
581                         }
582                 }
583         }
584 }
585
586 /// Implement MaybeReadable and Writeable for an enum, with struct variants stored as TLVs and
587 /// tuple variants stored directly.
588 ///
589 /// This is largely identical to `impl_writeable_tlv_based_enum`, except that odd variants will
590 /// return `Ok(None)` instead of `Err(UnknownRequiredFeature)`. It should generally be preferred
591 /// when `MaybeReadable` is practical instead of just `Readable` as it provides an upgrade path for
592 /// new variants to be added which are simply ignored by existing clients.
593 macro_rules! impl_writeable_tlv_based_enum_upgradable {
594         ($st: ident, $(($variant_id: expr, $variant_name: ident) =>
595                 {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
596         ),* $(,)*
597         $(;
598         $(($tuple_variant_id: expr, $tuple_variant_name: ident)),*  $(,)*)*) => {
599                 _impl_writeable_tlv_based_enum_common!($st,
600                         $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*;
601                         $($(($tuple_variant_id, $tuple_variant_name)),*)*);
602
603                 impl ::util::ser::MaybeReadable for $st {
604                         fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Option<Self>, ::ln::msgs::DecodeError> {
605                                 let id: u8 = ::util::ser::Readable::read(reader)?;
606                                 match id {
607                                         $($variant_id => {
608                                                 // Because read_tlv_fields creates a labeled loop, we cannot call it twice
609                                                 // in the same function body. Instead, we define a closure and call it.
610                                                 let f = || {
611                                                         $(
612                                                                 init_tlv_field_var!($field, $fieldty);
613                                                         )*
614                                                         read_tlv_fields!(reader, {
615                                                                 $(($type, $field, $fieldty)),*
616                                                         });
617                                                         Ok(Some($st::$variant_name {
618                                                                 $(
619                                                                         $field: init_tlv_based_struct_field!($field, $fieldty)
620                                                                 ),*
621                                                         }))
622                                                 };
623                                                 f()
624                                         }),*
625                                         $($($tuple_variant_id => {
626                                                 Ok(Some($st::$tuple_variant_name(Readable::read(reader)?)))
627                                         }),*)*
628                                         _ if id % 2 == 1 => Ok(None),
629                                         _ => Err(DecodeError::UnknownRequiredFeature),
630                                 }
631                         }
632                 }
633
634         }
635 }
636
637 /// Implement Readable and Writeable for an enum, with struct variants stored as TLVs and tuple
638 /// variants stored directly.
639 /// The format is, for example
640 /// impl_writeable_tlv_based_enum!(EnumName,
641 ///   (0, StructVariantA) => {(0, required_variant_field, required), (1, optional_variant_field, option)},
642 ///   (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, vec_type)};
643 ///   (2, TupleVariantA), (3, TupleVariantB),
644 /// );
645 /// The type is written as a single byte, followed by any variant data.
646 /// Attempts to read an unknown type byte result in DecodeError::UnknownRequiredFeature.
647 macro_rules! impl_writeable_tlv_based_enum {
648         ($st: ident, $(($variant_id: expr, $variant_name: ident) =>
649                 {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
650         ),* $(,)*;
651         $(($tuple_variant_id: expr, $tuple_variant_name: ident)),*  $(,)*) => {
652                 _impl_writeable_tlv_based_enum_common!($st,
653                         $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*;
654                         $(($tuple_variant_id, $tuple_variant_name)),*);
655
656                 impl ::util::ser::Readable for $st {
657                         fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
658                                 let id: u8 = ::util::ser::Readable::read(reader)?;
659                                 match id {
660                                         $($variant_id => {
661                                                 // Because read_tlv_fields creates a labeled loop, we cannot call it twice
662                                                 // in the same function body. Instead, we define a closure and call it.
663                                                 let f = || {
664                                                         $(
665                                                                 init_tlv_field_var!($field, $fieldty);
666                                                         )*
667                                                         read_tlv_fields!(reader, {
668                                                                 $(($type, $field, $fieldty)),*
669                                                         });
670                                                         Ok($st::$variant_name {
671                                                                 $(
672                                                                         $field: init_tlv_based_struct_field!($field, $fieldty)
673                                                                 ),*
674                                                         })
675                                                 };
676                                                 f()
677                                         }),*
678                                         $($tuple_variant_id => {
679                                                 Ok($st::$tuple_variant_name(Readable::read(reader)?))
680                                         }),*
681                                         _ => {
682                                                 Err(DecodeError::UnknownRequiredFeature)
683                                         },
684                                 }
685                         }
686                 }
687         }
688 }
689
690 #[cfg(test)]
691 mod tests {
692         use io::{self, Cursor};
693         use prelude::*;
694         use ln::msgs::DecodeError;
695         use util::ser::{Writeable, HighZeroBytesDroppedBigSize, VecWriter};
696         use bitcoin::secp256k1::PublicKey;
697
698         // The BOLT TLV test cases don't include any tests which use our "required-value" logic since
699         // the encoding layer in the BOLTs has no such concept, though it makes our macros easier to
700         // work with so they're baked into the decoder. Thus, we have a few additional tests below
701         fn tlv_reader(s: &[u8]) -> Result<(u64, u32, Option<u32>), DecodeError> {
702                 let mut s = Cursor::new(s);
703                 let mut a: u64 = 0;
704                 let mut b: u32 = 0;
705                 let mut c: Option<u32> = None;
706                 decode_tlv_stream!(&mut s, {(2, a, required), (3, b, required), (4, c, option)});
707                 Ok((a, b, c))
708         }
709
710         #[test]
711         fn tlv_v_short_read() {
712                 // We only expect a u32 for type 3 (which we are given), but the L says its 8 bytes.
713                 if let Err(DecodeError::ShortRead) = tlv_reader(&::hex::decode(
714                                 concat!("0100", "0208deadbeef1badbeef", "0308deadbeef")
715                                 ).unwrap()[..]) {
716                 } else { panic!(); }
717         }
718
719         #[test]
720         fn tlv_types_out_of_order() {
721                 if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
722                                 concat!("0100", "0304deadbeef", "0208deadbeef1badbeef")
723                                 ).unwrap()[..]) {
724                 } else { panic!(); }
725                 // ...even if its some field we don't understand
726                 if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
727                                 concat!("0208deadbeef1badbeef", "0100", "0304deadbeef")
728                                 ).unwrap()[..]) {
729                 } else { panic!(); }
730         }
731
732         #[test]
733         fn tlv_req_type_missing_or_extra() {
734                 // It's also bad if they included even fields we don't understand
735                 if let Err(DecodeError::UnknownRequiredFeature) = tlv_reader(&::hex::decode(
736                                 concat!("0100", "0208deadbeef1badbeef", "0304deadbeef", "0600")
737                                 ).unwrap()[..]) {
738                 } else { panic!(); }
739                 // ... or if they're missing fields we need
740                 if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
741                                 concat!("0100", "0208deadbeef1badbeef")
742                                 ).unwrap()[..]) {
743                 } else { panic!(); }
744                 // ... even if that field is even
745                 if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
746                                 concat!("0304deadbeef", "0500")
747                                 ).unwrap()[..]) {
748                 } else { panic!(); }
749         }
750
751         #[test]
752         fn tlv_simple_good_cases() {
753                 assert_eq!(tlv_reader(&::hex::decode(
754                                 concat!("0208deadbeef1badbeef", "03041bad1dea")
755                                 ).unwrap()[..]).unwrap(),
756                         (0xdeadbeef1badbeef, 0x1bad1dea, None));
757                 assert_eq!(tlv_reader(&::hex::decode(
758                                 concat!("0208deadbeef1badbeef", "03041bad1dea", "040401020304")
759                                 ).unwrap()[..]).unwrap(),
760                         (0xdeadbeef1badbeef, 0x1bad1dea, Some(0x01020304)));
761         }
762
763         // BOLT TLV test cases
764         fn tlv_reader_n1(s: &[u8]) -> Result<(Option<HighZeroBytesDroppedBigSize<u64>>, Option<u64>, Option<(PublicKey, u64, u64)>, Option<u16>), DecodeError> {
765                 let mut s = Cursor::new(s);
766                 let mut tlv1: Option<HighZeroBytesDroppedBigSize<u64>> = None;
767                 let mut tlv2: Option<u64> = None;
768                 let mut tlv3: Option<(PublicKey, u64, u64)> = None;
769                 let mut tlv4: Option<u16> = None;
770                 decode_tlv_stream!(&mut s, {(1, tlv1, option), (2, tlv2, option), (3, tlv3, option), (254, tlv4, option)});
771                 Ok((tlv1, tlv2, tlv3, tlv4))
772         }
773
774         #[test]
775         fn bolt_tlv_bogus_stream() {
776                 macro_rules! do_test {
777                         ($stream: expr, $reason: ident) => {
778                                 if let Err(DecodeError::$reason) = tlv_reader_n1(&::hex::decode($stream).unwrap()[..]) {
779                                 } else { panic!(); }
780                         }
781                 }
782
783                 // TLVs from the BOLT test cases which should not decode as either n1 or n2
784                 do_test!(concat!("fd01"), ShortRead);
785                 do_test!(concat!("fd0001", "00"), InvalidValue);
786                 do_test!(concat!("fd0101"), ShortRead);
787                 do_test!(concat!("0f", "fd"), ShortRead);
788                 do_test!(concat!("0f", "fd26"), ShortRead);
789                 do_test!(concat!("0f", "fd2602"), ShortRead);
790                 do_test!(concat!("0f", "fd0001", "00"), InvalidValue);
791                 do_test!(concat!("0f", "fd0201", "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), ShortRead);
792
793                 do_test!(concat!("12", "00"), UnknownRequiredFeature);
794                 do_test!(concat!("fd0102", "00"), UnknownRequiredFeature);
795                 do_test!(concat!("fe01000002", "00"), UnknownRequiredFeature);
796                 do_test!(concat!("ff0100000000000002", "00"), UnknownRequiredFeature);
797         }
798
799         #[test]
800         fn bolt_tlv_bogus_n1_stream() {
801                 macro_rules! do_test {
802                         ($stream: expr, $reason: ident) => {
803                                 if let Err(DecodeError::$reason) = tlv_reader_n1(&::hex::decode($stream).unwrap()[..]) {
804                                 } else { panic!(); }
805                         }
806                 }
807
808                 // TLVs from the BOLT test cases which should not decode as n1
809                 do_test!(concat!("01", "09", "ffffffffffffffffff"), InvalidValue);
810                 do_test!(concat!("01", "01", "00"), InvalidValue);
811                 do_test!(concat!("01", "02", "0001"), InvalidValue);
812                 do_test!(concat!("01", "03", "000100"), InvalidValue);
813                 do_test!(concat!("01", "04", "00010000"), InvalidValue);
814                 do_test!(concat!("01", "05", "0001000000"), InvalidValue);
815                 do_test!(concat!("01", "06", "000100000000"), InvalidValue);
816                 do_test!(concat!("01", "07", "00010000000000"), InvalidValue);
817                 do_test!(concat!("01", "08", "0001000000000000"), InvalidValue);
818                 do_test!(concat!("02", "07", "01010101010101"), ShortRead);
819                 do_test!(concat!("02", "09", "010101010101010101"), InvalidValue);
820                 do_test!(concat!("03", "21", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb"), ShortRead);
821                 do_test!(concat!("03", "29", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb0000000000000001"), ShortRead);
822                 do_test!(concat!("03", "30", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb000000000000000100000000000001"), ShortRead);
823                 do_test!(concat!("03", "31", "043da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb00000000000000010000000000000002"), InvalidValue);
824                 do_test!(concat!("03", "32", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb0000000000000001000000000000000001"), InvalidValue);
825                 do_test!(concat!("fd00fe", "00"), ShortRead);
826                 do_test!(concat!("fd00fe", "01", "01"), ShortRead);
827                 do_test!(concat!("fd00fe", "03", "010101"), InvalidValue);
828                 do_test!(concat!("00", "00"), UnknownRequiredFeature);
829
830                 do_test!(concat!("02", "08", "0000000000000226", "01", "01", "2a"), InvalidValue);
831                 do_test!(concat!("02", "08", "0000000000000231", "02", "08", "0000000000000451"), InvalidValue);
832                 do_test!(concat!("1f", "00", "0f", "01", "2a"), InvalidValue);
833                 do_test!(concat!("1f", "00", "1f", "01", "2a"), InvalidValue);
834
835                 // The last BOLT test modified to not require creating a new decoder for one trivial test.
836                 do_test!(concat!("ffffffffffffffffff", "00", "01", "00"), InvalidValue);
837         }
838
839         #[test]
840         fn bolt_tlv_valid_n1_stream() {
841                 macro_rules! do_test {
842                         ($stream: expr, $tlv1: expr, $tlv2: expr, $tlv3: expr, $tlv4: expr) => {
843                                 if let Ok((tlv1, tlv2, tlv3, tlv4)) = tlv_reader_n1(&::hex::decode($stream).unwrap()[..]) {
844                                         assert_eq!(tlv1.map(|v| v.0), $tlv1);
845                                         assert_eq!(tlv2, $tlv2);
846                                         assert_eq!(tlv3, $tlv3);
847                                         assert_eq!(tlv4, $tlv4);
848                                 } else { panic!(); }
849                         }
850                 }
851
852                 do_test!(concat!(""), None, None, None, None);
853                 do_test!(concat!("21", "00"), None, None, None, None);
854                 do_test!(concat!("fd0201", "00"), None, None, None, None);
855                 do_test!(concat!("fd00fd", "00"), None, None, None, None);
856                 do_test!(concat!("fd00ff", "00"), None, None, None, None);
857                 do_test!(concat!("fe02000001", "00"), None, None, None, None);
858                 do_test!(concat!("ff0200000000000001", "00"), None, None, None, None);
859
860                 do_test!(concat!("01", "00"), Some(0), None, None, None);
861                 do_test!(concat!("01", "01", "01"), Some(1), None, None, None);
862                 do_test!(concat!("01", "02", "0100"), Some(256), None, None, None);
863                 do_test!(concat!("01", "03", "010000"), Some(65536), None, None, None);
864                 do_test!(concat!("01", "04", "01000000"), Some(16777216), None, None, None);
865                 do_test!(concat!("01", "05", "0100000000"), Some(4294967296), None, None, None);
866                 do_test!(concat!("01", "06", "010000000000"), Some(1099511627776), None, None, None);
867                 do_test!(concat!("01", "07", "01000000000000"), Some(281474976710656), None, None, None);
868                 do_test!(concat!("01", "08", "0100000000000000"), Some(72057594037927936), None, None, None);
869                 do_test!(concat!("02", "08", "0000000000000226"), None, Some((0 << 30) | (0 << 5) | (550 << 0)), None, None);
870                 do_test!(concat!("03", "31", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb00000000000000010000000000000002"),
871                         None, None, Some((
872                                 PublicKey::from_slice(&::hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]).unwrap(), 1, 2)),
873                         None);
874                 do_test!(concat!("fd00fe", "02", "0226"), None, None, None, Some(550));
875         }
876
877         fn do_simple_test_tlv_write() -> Result<(), io::Error> {
878                 let mut stream = VecWriter(Vec::new());
879
880                 stream.0.clear();
881                 encode_varint_length_prefixed_tlv!(&mut stream, {(1, 1u8, required), (42, None::<u64>, option)});
882                 assert_eq!(stream.0, ::hex::decode("03010101").unwrap());
883
884                 stream.0.clear();
885                 encode_varint_length_prefixed_tlv!(&mut stream, {(1, Some(1u8), option)});
886                 assert_eq!(stream.0, ::hex::decode("03010101").unwrap());
887
888                 stream.0.clear();
889                 encode_varint_length_prefixed_tlv!(&mut stream, {(4, 0xabcdu16, required), (42, None::<u64>, option)});
890                 assert_eq!(stream.0, ::hex::decode("040402abcd").unwrap());
891
892                 stream.0.clear();
893                 encode_varint_length_prefixed_tlv!(&mut stream, {(42, None::<u64>, option), (0xff, 0xabcdu16, required)});
894                 assert_eq!(stream.0, ::hex::decode("06fd00ff02abcd").unwrap());
895
896                 stream.0.clear();
897                 encode_varint_length_prefixed_tlv!(&mut stream, {(0, 1u64, required), (42, None::<u64>, option), (0xff, HighZeroBytesDroppedBigSize(0u64), required)});
898                 assert_eq!(stream.0, ::hex::decode("0e00080000000000000001fd00ff00").unwrap());
899
900                 stream.0.clear();
901                 encode_varint_length_prefixed_tlv!(&mut stream, {(0, Some(1u64), option), (0xff, HighZeroBytesDroppedBigSize(0u64), required)});
902                 assert_eq!(stream.0, ::hex::decode("0e00080000000000000001fd00ff00").unwrap());
903
904                 Ok(())
905         }
906
907         #[test]
908         fn simple_test_tlv_write() {
909                 do_simple_test_tlv_write().unwrap();
910         }
911 }