Fix unknown handling in `impl_writeable_tlv_based_enum_upgradable`
[rust-lightning] / lightning / src / util / ser_macros.rs
index 5e55ab1d3b8182baf80f87e0e1a929d90380057c..5d6988ba1f193585c0d325c49fa679e52b4553c5 100644 (file)
 //! [`Readable`]: crate::util::ser::Readable
 //! [`Writeable`]: crate::util::ser::Writeable
 
+// There are quite a few TLV serialization "types" which behave differently. We currently only
+// publicly document the `optional` and `required` types, not supporting anything else publicly and
+// changing them at will.
+//
+// Some of the other types include:
+//  * (default_value, $default) - reads optionally, reading $default if no TLV is present
+//  * (static_value, $value) - ignores any TLVs, always using $value
+//  * required_vec - reads into a Vec without a length prefix, failing if no TLV is present.
+//  * optional_vec - reads into an Option<Vec> without a length prefix, continuing if no TLV is
+//                   present. Writes from a Vec directly, only if any elements are present. Note
+//                   that the struct deserialization macros return a Vec, not an Option.
+//  * upgradable_option - reads via MaybeReadable.
+//  * upgradable_required - reads via MaybeReadable, requiring a TLV be present but may return None
+//                          if MaybeReadable::read() returns None.
+
 /// Implements serialization for a single TLV record.
 /// This is exported for use by other exported macros, do not use directly.
 #[doc(hidden)]
@@ -29,7 +44,7 @@ macro_rules! _encode_tlv {
                BigSize($field.serialized_length() as u64).write($stream)?;
                $field.write($stream)?;
        };
-       ($stream: expr, $type: expr, $field: expr, vec_type) => {
+       ($stream: expr, $type: expr, $field: expr, required_vec) => {
                $crate::_encode_tlv!($stream, $type, $crate::util::ser::WithoutLength(&$field), required);
        };
        ($stream: expr, $optional_type: expr, $optional_field: expr, option) => {
@@ -39,6 +54,11 @@ macro_rules! _encode_tlv {
                        field.write($stream)?;
                }
        };
+       ($stream: expr, $type: expr, $field: expr, optional_vec) => {
+               if !$field.is_empty() {
+                       $crate::_encode_tlv!($stream, $type, $field, required_vec);
+               }
+       };
        ($stream: expr, $type: expr, $field: expr, upgradable_required) => {
                $crate::_encode_tlv!($stream, $type, $field, required);
        };
@@ -51,6 +71,10 @@ macro_rules! _encode_tlv {
        ($stream: expr, $type: expr, $field: expr, (option, encoding: $fieldty: ty)) => {
                $crate::_encode_tlv!($stream, $type, $field, option);
        };
+       ($stream: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
+               // Just a read-mapped type
+               $crate::_encode_tlv!($stream, $type, $field, option);
+       };
 }
 
 /// Panics if the last seen TLV type is not numerically less than the TLV type currently being checked.
@@ -108,7 +132,20 @@ macro_rules! _check_encoded_tlv_order {
 /// [`Writer`]: crate::util::ser::Writer
 #[macro_export]
 macro_rules! encode_tlv_stream {
+       ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => {
+               $crate::_encode_tlv_stream!($stream, {$(($type, $field, $fieldty)),*})
+       }
+}
+
+/// Implementation of [`encode_tlv_stream`].
+/// This is exported for use by other exported macros, do not use directly.
+#[doc(hidden)]
+#[macro_export]
+macro_rules! _encode_tlv_stream {
        ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}) => { {
+               $crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* }, &[])
+       } };
+       ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),* $(,)*}, $extra_tlvs: expr) => { {
                #[allow(unused_imports)]
                use $crate::{
                        ln::msgs::DecodeError,
@@ -120,6 +157,10 @@ macro_rules! encode_tlv_stream {
                $(
                        $crate::_encode_tlv!($stream, $type, $field, $fieldty);
                )*
+               for tlv in $extra_tlvs {
+                       let (typ, value): &(u64, Vec<u8>) = tlv;
+                       $crate::_encode_tlv!($stream, *typ, *value, required_vec);
+               }
 
                #[allow(unused_mut, unused_variables, unused_assignments)]
                #[cfg(debug_assertions)]
@@ -128,8 +169,12 @@ macro_rules! encode_tlv_stream {
                        $(
                                $crate::_check_encoded_tlv_order!(last_seen, $type, $fieldty);
                        )*
+                       for tlv in $extra_tlvs {
+                               let (typ, _): &(u64, Vec<u8>) = tlv;
+                               $crate::_check_encoded_tlv_order!(last_seen, *typ, required_vec);
+                       }
                }
-       } }
+       } };
 }
 
 /// Adds the length of the serialized field to a [`LengthCalculatingWriter`].
@@ -150,7 +195,7 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
                BigSize(field_len as u64).write(&mut $len).expect("No in-memory data may fail to serialize");
                $len.0 += field_len;
        };
-       ($len: expr, $type: expr, $field: expr, vec_type) => {
+       ($len: expr, $type: expr, $field: expr, required_vec) => {
                $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $crate::util::ser::WithoutLength(&$field), required);
        };
        ($len: expr, $optional_type: expr, $optional_field: expr, option) => {
@@ -161,6 +206,17 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
                        $len.0 += field_len;
                }
        };
+       ($len: expr, $type: expr, $field: expr, optional_vec) => {
+               if !$field.is_empty() {
+                       $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required_vec);
+               }
+       };
+       ($len: expr, $type: expr, $field: expr, (option: $trait: ident $(, $read_arg: expr)?)) => {
+               $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, option);
+       };
+       ($len: expr, $type: expr, $field: expr, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
+               $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field.map(|f| $encoding(f)), option);
+       };
        ($len: expr, $type: expr, $field: expr, upgradable_required) => {
                $crate::_get_varint_length_prefixed_tlv_length!($len, $type, $field, required);
        };
@@ -175,18 +231,27 @@ macro_rules! _get_varint_length_prefixed_tlv_length {
 #[macro_export]
 macro_rules! _encode_varint_length_prefixed_tlv {
        ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}) => { {
+               $crate::_encode_varint_length_prefixed_tlv!($stream, {$(($type, $field, $fieldty)),*}, &[])
+       } };
+       ($stream: expr, {$(($type: expr, $field: expr, $fieldty: tt)),*}, $extra_tlvs: expr) => { {
+               extern crate alloc;
                use $crate::util::ser::BigSize;
+               use alloc::vec::Vec;
                let len = {
                        #[allow(unused_mut)]
                        let mut len = $crate::util::ser::LengthCalculatingWriter(0);
                        $(
                                $crate::_get_varint_length_prefixed_tlv_length!(len, $type, $field, $fieldty);
                        )*
+                       for tlv in $extra_tlvs {
+                               let (typ, value): &(u64, Vec<u8>) = tlv;
+                               $crate::_get_varint_length_prefixed_tlv_length!(len, *typ, *value, required_vec);
+                       }
                        len.0
                };
                BigSize(len as u64).write($stream)?;
-               $crate::encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* });
-       } }
+               $crate::_encode_tlv_stream!($stream, { $(($type, $field, $fieldty)),* }, $extra_tlvs);
+       } };
 }
 
 /// Errors if there are missing required TLV types between the last seen type and the type currently being processed.
@@ -210,10 +275,16 @@ macro_rules! _check_decoded_tlv_order {
                        return Err(DecodeError::InvalidValue);
                }
        }};
+       ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
+               $crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
+       }};
        ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, option) => {{
                // no-op
        }};
-       ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, vec_type) => {{
+       ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, required_vec) => {{
+               $crate::_check_decoded_tlv_order!($last_seen_type, $typ, $type, $field, required);
+       }};
+       ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, optional_vec) => {{
                // no-op
        }};
        ($last_seen_type: expr, $typ: expr, $type: expr, $field: ident, upgradable_required) => {{
@@ -252,12 +323,18 @@ macro_rules! _check_missing_tlv {
                        return Err(DecodeError::InvalidValue);
                }
        }};
-       ($last_seen_type: expr, $type: expr, $field: ident, vec_type) => {{
-               // no-op
+       ($last_seen_type: expr, $type: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
+               $crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
+       }};
+       ($last_seen_type: expr, $type: expr, $field: ident, required_vec) => {{
+               $crate::_check_missing_tlv!($last_seen_type, $type, $field, required);
        }};
        ($last_seen_type: expr, $type: expr, $field: ident, option) => {{
                // no-op
        }};
+       ($last_seen_type: expr, $type: expr, $field: ident, optional_vec) => {{
+               // no-op
+       }};
        ($last_seen_type: expr, $type: expr, $field: ident, upgradable_required) => {{
                _check_missing_tlv!($last_seen_type, $type, $field, required)
        }};
@@ -285,13 +362,20 @@ macro_rules! _decode_tlv {
        ($reader: expr, $field: ident, required) => {{
                $field = $crate::util::ser::Readable::read(&mut $reader)?;
        }};
-       ($reader: expr, $field: ident, vec_type) => {{
+       ($reader: expr, $field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {{
+               $field = $trait::read(&mut $reader $(, $read_arg)*)?;
+       }};
+       ($reader: expr, $field: ident, required_vec) => {{
                let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
-               $field = Some(f.0);
+               $field = f.0;
        }};
        ($reader: expr, $field: ident, option) => {{
                $field = Some($crate::util::ser::Readable::read(&mut $reader)?);
        }};
+       ($reader: expr, $field: ident, optional_vec) => {{
+               let f: $crate::util::ser::WithoutLength<Vec<_>> = $crate::util::ser::Readable::read(&mut $reader)?;
+               $field = Some(f.0);
+       }};
        // `upgradable_required` indicates we're reading a required TLV that may have been upgraded
        // without backwards compat. We'll error if the field is missing, and return `Ok(None)` if the
        // field is present but we can no longer understand it.
@@ -519,7 +603,7 @@ macro_rules! impl_writeable_msg {
                impl $crate::util::ser::Writeable for $st {
                        fn write<W: $crate::util::ser::Writer>(&self, w: &mut W) -> Result<(), $crate::io::Error> {
                                $( self.$field.write(w)?; )*
-                               $crate::encode_tlv_stream!(w, {$(($type, self.$tlvfield, $fieldty)),*});
+                               $crate::encode_tlv_stream!(w, {$(($type, self.$tlvfield.as_ref(), $fieldty)),*});
                                Ok(())
                        }
                }
@@ -644,6 +728,9 @@ macro_rules! _init_tlv_based_struct_field {
        ($field: ident, option) => {
                $field
        };
+       ($field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {
+               $crate::_init_tlv_based_struct_field!($field, option)
+       };
        ($field: ident, upgradable_required) => {
                $field.0.unwrap()
        };
@@ -653,7 +740,10 @@ macro_rules! _init_tlv_based_struct_field {
        ($field: ident, required) => {
                $field.0.unwrap()
        };
-       ($field: ident, vec_type) => {
+       ($field: ident, required_vec) => {
+               $field
+       };
+       ($field: ident, optional_vec) => {
                $field.unwrap()
        };
 }
@@ -673,12 +763,24 @@ macro_rules! _init_tlv_field_var {
        ($field: ident, required) => {
                let mut $field = $crate::util::ser::RequiredWrapper(None);
        };
-       ($field: ident, vec_type) => {
-               let mut $field = Some(Vec::new());
+       ($field: ident, (required: $trait: ident $(, $read_arg: expr)?)) => {
+               $crate::_init_tlv_field_var!($field, required);
+       };
+       ($field: ident, required_vec) => {
+               let mut $field = Vec::new();
        };
        ($field: ident, option) => {
                let mut $field = None;
        };
+       ($field: ident, optional_vec) => {
+               let mut $field = Some(Vec::new());
+       };
+       ($field: ident, (option, encoding: ($fieldty: ty, $encoding: ident))) => {
+               $crate::_init_tlv_field_var!($field, option);
+       };
+       ($field: ident, (option: $trait: ident $(, $read_arg: expr)?)) => {
+               $crate::_init_tlv_field_var!($field, option);
+       };
        ($field: ident, upgradable_required) => {
                let mut $field = $crate::util::ser::UpgradableRequired(None);
        };
@@ -689,10 +791,13 @@ macro_rules! _init_tlv_field_var {
 
 /// Equivalent to running [`_init_tlv_field_var`] then [`read_tlv_fields`].
 ///
+/// If any unused values are read, their type MUST be specified or else `rustc` will read them as an
+/// `i64`.
+///
 /// This is exported for use by other exported macros, do not use directly.
 #[doc(hidden)]
 #[macro_export]
-macro_rules! _init_and_read_tlv_fields {
+macro_rules! _init_and_read_len_prefixed_tlv_fields {
        ($reader: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
                $(
                        $crate::_init_tlv_field_var!($field, $fieldty);
@@ -704,11 +809,28 @@ macro_rules! _init_and_read_tlv_fields {
        }
 }
 
+/// Equivalent to running [`_init_tlv_field_var`] then [`decode_tlv_stream`].
+///
+/// If any unused values are read, their type MUST be specified or else `rustc` will read them as an
+/// `i64`.
+macro_rules! _init_and_read_tlv_stream {
+       ($reader: ident, {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}) => {
+               $(
+                       $crate::_init_tlv_field_var!($field, $fieldty);
+               )*
+
+               $crate::decode_tlv_stream!($reader, {
+                       $(($type, $field, $fieldty)),*
+               });
+       }
+}
+
 /// Implements [`Readable`]/[`Writeable`] for a struct storing it as a set of TLVs
 /// If `$fieldty` is `required`, then `$field` is a required field that is not an [`Option`] nor a [`Vec`].
 /// If `$fieldty` is `(default_value, $default)`, then `$field` will be set to `$default` if not present.
 /// If `$fieldty` is `option`, then `$field` is optional field.
-/// If `$fieldty` is `vec_type`, then `$field` is a [`Vec`], which needs to have its individual elements serialized.
+/// If `$fieldty` is `optional_vec`, then `$field` is a [`Vec`], which needs to have its individual elements serialized.
+///    Note that for `optional_vec` no bytes are written if the vec is empty
 ///
 /// For example,
 /// ```
@@ -724,7 +846,7 @@ macro_rules! _init_and_read_tlv_fields {
 ///    (0, tlv_integer, required),
 ///    (1, tlv_default_integer, (default_value, 7)),
 ///    (2, tlv_optional_integer, option),
-///    (3, tlv_vec_type_integer, vec_type),
+///    (3, tlv_vec_type_integer, optional_vec),
 /// });
 /// ```
 ///
@@ -760,7 +882,7 @@ macro_rules! impl_writeable_tlv_based {
 
                impl $crate::util::ser::Readable for $st {
                        fn read<R: $crate::io::Read>(reader: &mut R) -> Result<Self, $crate::ln::msgs::DecodeError> {
-                               $crate::_init_and_read_tlv_fields!(reader, {
+                               $crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
                                        $(($type, $field, $fieldty)),*
                                });
                                Ok(Self {
@@ -795,7 +917,7 @@ macro_rules! tlv_stream {
 
                #[cfg_attr(test, derive(PartialEq))]
                #[derive(Debug)]
-               pub(super) struct $nameref<'a> {
+               pub(crate) struct $nameref<'a> {
                        $(
                                pub(super) $field: Option<tlv_record_ref_type!($fieldty)>,
                        )*
@@ -849,6 +971,8 @@ macro_rules! tlv_record_ref_type {
        ($type:ty) => { &'a $type };
 }
 
+#[doc(hidden)]
+#[macro_export]
 macro_rules! _impl_writeable_tlv_based_enum_common {
        ($st: ident, $(($variant_id: expr, $variant_name: ident) =>
                {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
@@ -860,7 +984,7 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
                                        $($st::$variant_name { $(ref $field),* } => {
                                                let id: u8 = $variant_id;
                                                id.write(writer)?;
-                                               write_tlv_fields!(writer, {
+                                               $crate::write_tlv_fields!(writer, {
                                                        $(($type, *$field, $fieldty)),*
                                                });
                                        }),*
@@ -882,7 +1006,7 @@ macro_rules! _impl_writeable_tlv_based_enum_common {
 /// ```ignore
 /// 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)};
+///   (1, StructVariantB) => {(0, variant_field_a, required), (1, variant_field_b, required), (2, variant_vec_field, optional_vec)};
 ///   (2, TupleVariantA), (3, TupleVariantB),
 /// );
 /// ```
@@ -898,7 +1022,7 @@ macro_rules! impl_writeable_tlv_based_enum {
                {$(($type: expr, $field: ident, $fieldty: tt)),* $(,)*}
        ),* $(,)*;
        $(($tuple_variant_id: expr, $tuple_variant_name: ident)),*  $(,)*) => {
-               _impl_writeable_tlv_based_enum_common!($st,
+               $crate::_impl_writeable_tlv_based_enum_common!($st,
                        $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*;
                        $(($tuple_variant_id, $tuple_variant_name)),*);
 
@@ -910,19 +1034,19 @@ macro_rules! impl_writeable_tlv_based_enum {
                                                // 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_and_read_tlv_fields!(reader, {
+                                                       $crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
                                                                $(($type, $field, $fieldty)),*
                                                        });
                                                        Ok($st::$variant_name {
                                                                $(
-                                                                       $field: _init_tlv_based_struct_field!($field, $fieldty)
+                                                                       $field: $crate::_init_tlv_based_struct_field!($field, $fieldty)
                                                                ),*
                                                        })
                                                };
                                                f()
                                        }),*
                                        $($tuple_variant_id => {
-                                               Ok($st::$tuple_variant_name(Readable::read(reader)?))
+                                               Ok($st::$tuple_variant_name($crate::util::ser::Readable::read(reader)?))
                                        }),*
                                        _ => {
                                                Err($crate::ln::msgs::DecodeError::UnknownRequiredFeature)
@@ -941,6 +1065,10 @@ macro_rules! impl_writeable_tlv_based_enum {
 /// 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.
 ///
+/// Note that only struct and unit variants (not tuple variants) will support downgrading, thus any
+/// new odd variants MUST be non-tuple (i.e. described using `$variant_id` and `$variant_name` not
+/// `$tuple_variant_id` and `$tuple_variant_name`).
+///
 /// [`MaybeReadable`]: crate::util::ser::MaybeReadable
 /// [`Writeable`]: crate::util::ser::Writeable
 /// [`DecodeError::UnknownRequiredFeature`]: crate::ln::msgs::DecodeError::UnknownRequiredFeature
@@ -952,7 +1080,7 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
        ),* $(,)*
        $(;
        $(($tuple_variant_id: expr, $tuple_variant_name: ident)),*  $(,)*)*) => {
-               _impl_writeable_tlv_based_enum_common!($st,
+               $crate::_impl_writeable_tlv_based_enum_common!($st,
                        $(($variant_id, $variant_name) => {$(($type, $field, $fieldty)),*}),*;
                        $($(($tuple_variant_id, $tuple_variant_name)),*)*);
 
@@ -964,12 +1092,12 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
                                                // 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_and_read_tlv_fields!(reader, {
+                                                       $crate::_init_and_read_len_prefixed_tlv_fields!(reader, {
                                                                $(($type, $field, $fieldty)),*
                                                        });
                                                        Ok(Some($st::$variant_name {
                                                                $(
-                                                                       $field: _init_tlv_based_struct_field!($field, $fieldty)
+                                                                       $field: $crate::_init_tlv_based_struct_field!($field, $fieldty)
                                                                ),*
                                                        }))
                                                };
@@ -978,7 +1106,14 @@ macro_rules! impl_writeable_tlv_based_enum_upgradable {
                                        $($($tuple_variant_id => {
                                                Ok(Some($st::$tuple_variant_name(Readable::read(reader)?)))
                                        }),*)*
-                                       _ if id % 2 == 1 => Ok(None),
+                                       _ 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)?;
+                                               let mut rd = $crate::util::ser::FixedLengthReader::new(reader, tlv_len.0);
+                                               rd.eat_remaining().map_err(|_| $crate::ln::msgs::DecodeError::ShortRead)?;
+                                               Ok(None)
+                                       },
                                        _ => Err($crate::ln::msgs::DecodeError::UnknownRequiredFeature),
                                }
                        }
@@ -992,6 +1127,7 @@ mod tests {
        use crate::prelude::*;
        use crate::ln::msgs::DecodeError;
        use crate::util::ser::{Writeable, HighZeroBytesDroppedBigSize, VecWriter};
+       use bitcoin::hashes::hex::FromHex;
        use bitcoin::secp256k1::PublicKey;
 
        // The BOLT TLV test cases don't include any tests which use our "required-value" logic since
@@ -1009,7 +1145,7 @@ mod tests {
        #[test]
        fn tlv_v_short_read() {
                // We only expect a u32 for type 3 (which we are given), but the L says its 8 bytes.
-               if let Err(DecodeError::ShortRead) = tlv_reader(&::hex::decode(
+               if let Err(DecodeError::ShortRead) = tlv_reader(&<Vec<u8>>::from_hex(
                                concat!("0100", "0208deadbeef1badbeef", "0308deadbeef")
                                ).unwrap()[..]) {
                } else { panic!(); }
@@ -1017,12 +1153,12 @@ mod tests {
 
        #[test]
        fn tlv_types_out_of_order() {
-               if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
+               if let Err(DecodeError::InvalidValue) = tlv_reader(&<Vec<u8>>::from_hex(
                                concat!("0100", "0304deadbeef", "0208deadbeef1badbeef")
                                ).unwrap()[..]) {
                } else { panic!(); }
                // ...even if its some field we don't understand
-               if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
+               if let Err(DecodeError::InvalidValue) = tlv_reader(&<Vec<u8>>::from_hex(
                                concat!("0208deadbeef1badbeef", "0100", "0304deadbeef")
                                ).unwrap()[..]) {
                } else { panic!(); }
@@ -1031,17 +1167,17 @@ mod tests {
        #[test]
        fn tlv_req_type_missing_or_extra() {
                // It's also bad if they included even fields we don't understand
-               if let Err(DecodeError::UnknownRequiredFeature) = tlv_reader(&::hex::decode(
+               if let Err(DecodeError::UnknownRequiredFeature) = tlv_reader(&<Vec<u8>>::from_hex(
                                concat!("0100", "0208deadbeef1badbeef", "0304deadbeef", "0600")
                                ).unwrap()[..]) {
                } else { panic!(); }
                // ... or if they're missing fields we need
-               if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
+               if let Err(DecodeError::InvalidValue) = tlv_reader(&<Vec<u8>>::from_hex(
                                concat!("0100", "0208deadbeef1badbeef")
                                ).unwrap()[..]) {
                } else { panic!(); }
                // ... even if that field is even
-               if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
+               if let Err(DecodeError::InvalidValue) = tlv_reader(&<Vec<u8>>::from_hex(
                                concat!("0304deadbeef", "0500")
                                ).unwrap()[..]) {
                } else { panic!(); }
@@ -1049,11 +1185,11 @@ mod tests {
 
        #[test]
        fn tlv_simple_good_cases() {
-               assert_eq!(tlv_reader(&::hex::decode(
+               assert_eq!(tlv_reader(&<Vec<u8>>::from_hex(
                                concat!("0208deadbeef1badbeef", "03041bad1dea")
                                ).unwrap()[..]).unwrap(),
                        (0xdeadbeef1badbeef, 0x1bad1dea, None));
-               assert_eq!(tlv_reader(&::hex::decode(
+               assert_eq!(tlv_reader(&<Vec<u8>>::from_hex(
                                concat!("0208deadbeef1badbeef", "03041bad1dea", "040401020304")
                                ).unwrap()[..]).unwrap(),
                        (0xdeadbeef1badbeef, 0x1bad1dea, Some(0x01020304)));
@@ -1077,12 +1213,12 @@ mod tests {
 
        #[test]
        fn upgradable_tlv_simple_good_cases() {
-               assert_eq!(upgradable_tlv_reader(&::hex::decode(
+               assert_eq!(upgradable_tlv_reader(&<Vec<u8>>::from_hex(
                        concat!("0204deadbeef", "03041bad1dea", "0404deadbeef")
                ).unwrap()[..]).unwrap(),
                Some(TestUpgradable { a: 0xdeadbeef, b: 0x1bad1dea, c: Some(0xdeadbeef) }));
 
-               assert_eq!(upgradable_tlv_reader(&::hex::decode(
+               assert_eq!(upgradable_tlv_reader(&<Vec<u8>>::from_hex(
                        concat!("0204deadbeef", "03041bad1dea")
                ).unwrap()[..]).unwrap(),
                Some(TestUpgradable { a: 0xdeadbeef, b: 0x1bad1dea, c: None}));
@@ -1090,11 +1226,11 @@ mod tests {
 
        #[test]
        fn missing_required_upgradable() {
-               if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&::hex::decode(
+               if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&<Vec<u8>>::from_hex(
                        concat!("0100", "0204deadbeef")
                        ).unwrap()[..]) {
                } else { panic!(); }
-               if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&::hex::decode(
+               if let Err(DecodeError::InvalidValue) = upgradable_tlv_reader(&<Vec<u8>>::from_hex(
                        concat!("0100", "03041bad1dea")
                ).unwrap()[..]) {
                } else { panic!(); }
@@ -1115,7 +1251,7 @@ mod tests {
        fn bolt_tlv_bogus_stream() {
                macro_rules! do_test {
                        ($stream: expr, $reason: ident) => {
-                               if let Err(DecodeError::$reason) = tlv_reader_n1(&::hex::decode($stream).unwrap()[..]) {
+                               if let Err(DecodeError::$reason) = tlv_reader_n1(&<Vec<u8>>::from_hex($stream).unwrap()[..]) {
                                } else { panic!(); }
                        }
                }
@@ -1140,7 +1276,7 @@ mod tests {
        fn bolt_tlv_bogus_n1_stream() {
                macro_rules! do_test {
                        ($stream: expr, $reason: ident) => {
-                               if let Err(DecodeError::$reason) = tlv_reader_n1(&::hex::decode($stream).unwrap()[..]) {
+                               if let Err(DecodeError::$reason) = tlv_reader_n1(&<Vec<u8>>::from_hex($stream).unwrap()[..]) {
                                } else { panic!(); }
                        }
                }
@@ -1180,7 +1316,7 @@ mod tests {
        fn bolt_tlv_valid_n1_stream() {
                macro_rules! do_test {
                        ($stream: expr, $tlv1: expr, $tlv2: expr, $tlv3: expr, $tlv4: expr) => {
-                               if let Ok((tlv1, tlv2, tlv3, tlv4)) = tlv_reader_n1(&::hex::decode($stream).unwrap()[..]) {
+                               if let Ok((tlv1, tlv2, tlv3, tlv4)) = tlv_reader_n1(&<Vec<u8>>::from_hex($stream).unwrap()[..]) {
                                        assert_eq!(tlv1.map(|v| v.0), $tlv1);
                                        assert_eq!(tlv2, $tlv2);
                                        assert_eq!(tlv3, $tlv3);
@@ -1209,7 +1345,7 @@ mod tests {
                do_test!(concat!("02", "08", "0000000000000226"), None, Some((0 << 30) | (0 << 5) | (550 << 0)), None, None);
                do_test!(concat!("03", "31", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb00000000000000010000000000000002"),
                        None, None, Some((
-                               PublicKey::from_slice(&::hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]).unwrap(), 1, 2)),
+                               PublicKey::from_slice(&<Vec<u8>>::from_hex("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]).unwrap(), 1, 2)),
                        None);
                do_test!(concat!("fd00fe", "02", "0226"), None, None, None, Some(550));
        }
@@ -1219,27 +1355,27 @@ mod tests {
 
                stream.0.clear();
                _encode_varint_length_prefixed_tlv!(&mut stream, {(1, 1u8, required), (42, None::<u64>, option)});
-               assert_eq!(stream.0, ::hex::decode("03010101").unwrap());
+               assert_eq!(stream.0, <Vec<u8>>::from_hex("03010101").unwrap());
 
                stream.0.clear();
                _encode_varint_length_prefixed_tlv!(&mut stream, {(1, Some(1u8), option)});
-               assert_eq!(stream.0, ::hex::decode("03010101").unwrap());
+               assert_eq!(stream.0, <Vec<u8>>::from_hex("03010101").unwrap());
 
                stream.0.clear();
                _encode_varint_length_prefixed_tlv!(&mut stream, {(4, 0xabcdu16, required), (42, None::<u64>, option)});
-               assert_eq!(stream.0, ::hex::decode("040402abcd").unwrap());
+               assert_eq!(stream.0, <Vec<u8>>::from_hex("040402abcd").unwrap());
 
                stream.0.clear();
                _encode_varint_length_prefixed_tlv!(&mut stream, {(42, None::<u64>, option), (0xff, 0xabcdu16, required)});
-               assert_eq!(stream.0, ::hex::decode("06fd00ff02abcd").unwrap());
+               assert_eq!(stream.0, <Vec<u8>>::from_hex("06fd00ff02abcd").unwrap());
 
                stream.0.clear();
                _encode_varint_length_prefixed_tlv!(&mut stream, {(0, 1u64, required), (42, None::<u64>, option), (0xff, HighZeroBytesDroppedBigSize(0u64), required)});
-               assert_eq!(stream.0, ::hex::decode("0e00080000000000000001fd00ff00").unwrap());
+               assert_eq!(stream.0, <Vec<u8>>::from_hex("0e00080000000000000001fd00ff00").unwrap());
 
                stream.0.clear();
                _encode_varint_length_prefixed_tlv!(&mut stream, {(0, Some(1u64), option), (0xff, HighZeroBytesDroppedBigSize(0u64), required)});
-               assert_eq!(stream.0, ::hex::decode("0e00080000000000000001fd00ff00").unwrap());
+               assert_eq!(stream.0, <Vec<u8>>::from_hex("0e00080000000000000001fd00ff00").unwrap());
 
                Ok(())
        }