+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
macro_rules! encode_tlv {
($stream: expr, {$(($type: expr, $field: expr)),*}) => { {
+ #[allow(unused_imports)]
use util::ser::{BigSize, LengthCalculatingWriter};
$(
BigSize($type).write($stream)?;
macro_rules! encode_varint_length_prefixed_tlv {
($stream: expr, {$(($type: expr, $field: expr)),*}) => { {
use util::ser::{BigSize, LengthCalculatingWriter};
+ #[allow(unused_mut)]
let mut len = LengthCalculatingWriter(0);
{
$(
if $len != 0 {
w.size_hint($len);
}
+ #[cfg(any(test, feature = "fuzztarget"))]
+ {
+ // In tests, assert that the hard-coded length matches the actual one
+ if $len != 0 {
+ use util::ser::LengthCalculatingWriter;
+ let mut len_calc = LengthCalculatingWriter(0);
+ $( self.$field.write(&mut len_calc)?; )*
+ assert_eq!(len_calc.0, $len);
+ }
+ }
$( self.$field.write(w)?; )*
Ok(())
}
}
- impl<R: ::std::io::Read> ::util::ser::Readable<R> for $st {
- fn read(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
+ impl ::util::ser::Readable for $st {
+ fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
Ok(Self {
$($field: ::util::ser::Readable::read(r)?),*
})
}
}
macro_rules! impl_writeable_len_match {
- ($st:ident, {$({$m: pat, $l: expr}),*}, {$($field:ident),*}) => {
- impl Writeable for $st {
+ ($struct: ident, $cmp: tt, {$({$match: pat, $length: expr}),*}, {$($field:ident),*}) => {
+ impl Writeable for $struct {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
- w.size_hint(match *self {
- $($m => $l,)*
- });
+ let len = match *self {
+ $($match => $length,)*
+ };
+ w.size_hint(len);
+ #[cfg(any(test, feature = "fuzztarget"))]
+ {
+ // In tests, assert that the hard-coded length matches the actual one
+ use util::ser::LengthCalculatingWriter;
+ let mut len_calc = LengthCalculatingWriter(0);
+ $( self.$field.write(&mut len_calc)?; )*
+ assert!(len_calc.0 $cmp len);
+ }
$( self.$field.write(w)?; )*
Ok(())
}
}
- impl<R: ::std::io::Read> Readable<R> for $st {
- fn read(r: &mut R) -> Result<Self, DecodeError> {
+ impl ::util::ser::Readable for $struct {
+ fn read<R: ::std::io::Read>(r: &mut R) -> Result<Self, DecodeError> {
Ok(Self {
$($field: Readable::read(r)?),*
})
}
}
+ };
+ ($struct: ident, {$({$match: pat, $length: expr}),*}, {$($field:ident),*}) => {
+ impl_writeable_len_match!($struct, ==, { $({ $match, $length }),* }, { $($field),* });
+ }
+}
+
+/// Write out two bytes to indicate the version of an object.
+/// $this_version represents a unique version of a type. Incremented whenever the type's
+/// serialization format has changed or has a new interpretation. Used by a type's
+/// reader to determine how to interpret fields or if it can understand a serialized
+/// object.
+/// $min_version_that_can_read_this is the minimum reader version which can understand this
+/// serialized object. Previous versions will simply err with a
+/// DecodeError::UnknownVersion.
+///
+/// Updates to either $this_version or $min_version_that_can_read_this should be included in
+/// release notes.
+///
+/// Both version fields can be specific to this type of object.
+macro_rules! write_ver_prefix {
+ ($stream: expr, $this_version: expr, $min_version_that_can_read_this: expr) => {
+ $stream.write_all(&[$this_version; 1])?;
+ $stream.write_all(&[$min_version_that_can_read_this; 1])?;
+ }
+}
+
+/// Writes out a suffix to an object which contains potentially backwards-compatible, optional
+/// fields which old nodes can happily ignore.
+///
+/// It is written out in TLV format and, as with all TLV fields, unknown even fields cause a
+/// DecodeError::UnknownRequiredFeature error, with unknown odd fields ignored.
+///
+/// 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)),*}) => {
+ encode_varint_length_prefixed_tlv!($stream, {$(($type, $field)),*});
}
}
+/// Reads a prefix added by write_ver_prefix!(), above. Takes the current version of the
+/// serialization logic for this object. This is compared against the
+/// $min_version_that_can_read_this added by write_ver_prefix!().
+macro_rules! read_ver_prefix {
+ ($stream: expr, $this_version: expr) => { {
+ let ver: u8 = Readable::read($stream)?;
+ let min_ver: u8 = Readable::read($stream)?;
+ if min_ver > $this_version {
+ return Err(DecodeError::UnknownVersion);
+ }
+ ver
+ } }
+}
+
+/// Reads a suffix added by write_tlv_fields.
+macro_rules! read_tlv_fields {
+ ($stream: expr, {$(($reqtype: expr, $reqfield: ident)),*}, {$(($type: expr, $field: ident)),*}) => { {
+ let tlv_len = ::util::ser::BigSize::read($stream)?;
+ let mut rd = ::util::ser::FixedLengthReader::new($stream, tlv_len.0);
+ decode_tlv!(&mut rd, {$(($reqtype, $reqfield)),*}, {$(($type, $field)),*});
+ rd.eat_remaining().map_err(|_| DecodeError::ShortRead)?;
+ } }
+}
+
#[cfg(test)]
mod tests {
use std::io::{Cursor, Read};
use ln::msgs::DecodeError;
use util::ser::{Readable, Writeable, HighZeroBytesDroppedVarInt, VecWriter};
- use secp256k1::PublicKey;
+ use bitcoin::secp256k1::PublicKey;
// The BOLT TLV test cases don't include any tests which use our "required-value" logic since
// the encoding layer in the BOLTs has no such concept, though it makes our macros easier to
(0xdeadbeef1badbeef, 0x1bad1dea, Some(0x01020304)));
}
- impl<R: Read> Readable<R> for (PublicKey, u64, u64) {
+ impl Readable for (PublicKey, u64, u64) {
#[inline]
- fn read(reader: &mut R) -> Result<(PublicKey, u64, u64), DecodeError> {
+ fn read<R: Read>(reader: &mut R) -> Result<(PublicKey, u64, u64), DecodeError> {
Ok((Readable::read(reader)?, Readable::read(reader)?, Readable::read(reader)?))
}
}