X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fser.rs;h=9682c09d95c9aa4e096c6213513b980f9a5ed98b;hb=fb670c8faae8c1e990496b869e62dfbde10a64f8;hp=85b991c61d43674970f588c93add1228722b219c;hpb=be8797e17ab5c56582340d1f5e57f92477ede69d;p=rust-lightning diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 85b991c6..9682c09d 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -199,8 +199,14 @@ pub trait Writeable { /// Writes `self` out to a `Vec`. fn encode(&self) -> Vec { - let mut msg = VecWriter(Vec::new()); + let len = self.serialized_length(); + let mut msg = VecWriter(Vec::with_capacity(len)); self.write(&mut msg).unwrap(); + // Note that objects with interior mutability may change size between when we called + // serialized_length and when we called write. That's okay, but shouldn't happen during + // testing as most of our tests are not threaded. + #[cfg(test)] + debug_assert_eq!(len, msg.0.len()); msg.0 } @@ -211,6 +217,7 @@ pub trait Writeable { 0u16.write(&mut msg).unwrap(); self.write(&mut msg).unwrap(); let len = msg.0.len(); + debug_assert_eq!(len - 2, self.serialized_length()); msg.0[..2].copy_from_slice(&(len as u16 - 2).to_be_bytes()); msg.0 } @@ -1289,7 +1296,7 @@ impl Readable for String { /// This serialization is used by [`BOLT 7`] hostnames. /// /// [`BOLT 7`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct Hostname(String); impl Hostname { /// Returns the length of the hostname. @@ -1382,7 +1389,7 @@ impl Readable for Duration { /// if the `Transaction`'s consensus-serialized length is <= u16::MAX. /// /// Use [`TransactionU16LenLimited::into_transaction`] to convert into the contained `Transaction`. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, Hash, PartialEq, Eq)] pub struct TransactionU16LenLimited(Transaction); impl TransactionU16LenLimited {